15

I have the following coordinates

minx, maxx, miny ,maxy = 448262.080078, 450360.750122, 6262492.020081, 6262938.950073

I wish to create a square grid of size 1 m using python.

import math


minx,maxx,miny,maxy = 448262.080078, 450360.750122, 6262492.020081, 6262938.950073
size = 1

def set_bbox(minx, maxx, miny, maxy, distx, disty):
    nx = int(math.ceil(abs(maxx - minx)/distx))
    ny = int(math.ceil(abs(maxy - miny)/disty))
    new_maxx = minx + (nx*distx)
    new_miny = maxy - (ny*disty)
    return ((minx, new_maxx, new_miny, maxy),ny,nx)

# shift the bottom (right - down)
coord, ny, nx = set_bbox(minx,maxx,miny,maxy,size,size)
# left-up origin
origin = coord[0],coord[3]
# number of tiles
ncell = ny*nx
Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389
Gianni
  • 409
  • 2
  • 4
  • 8
  • Is this attached to any specific GIS platform or is the requirement to do this in pure python without any specified output format (eg. shapefile, textfile etc etc) –  Mar 11 '13 at 21:07
  • Thanks @Dan, i wish to perform in pure python and the output will be in shapefile format – Gianni Mar 11 '13 at 21:13
  • The ArcInfo level of license of ArcMap has the Fishnet tool but you haven't indicated how you intend to create the shapefile. –  Mar 11 '13 at 21:17
  • Sorry i don't use commercial Software. I prefer program in pure language Java, Python, C++. – Gianni Mar 11 '13 at 21:39
  • 1
    But you don't mind using a library such as GDAL/OGR (https://pypi.python.org/pypi/GDAL/) or pyshp (https://pypi.python.org/pypi/pyshp/)? – Snorfalorpagus Aug 09 '13 at 08:11
  • Is there any similar method using OGR C++ API libraries. I too need to divide a entier shapefile into 20 width and 20 height square boxes. – anonymous Dec 03 '19 at 09:30

4 Answers4

13

The following script will do the job with GDAL and Python:

import os, sys
import ogr
from math import ceil

def main(outputGridfn,xmin,xmax,ymin,ymax,gridHeight,gridWidth):

    # convert sys.argv to float
    xmin = float(xmin)
    xmax = float(xmax)
    ymin = float(ymin)
    ymax = float(ymax)
    gridWidth = float(gridWidth)
    gridHeight = float(gridHeight)

    # get rows
    rows = ceil((ymax-ymin)/gridHeight)
    # get columns
    cols = ceil((xmax-xmin)/gridWidth)

    # start grid cell envelope
    ringXleftOrigin = xmin
    ringXrightOrigin = xmin + gridWidth
    ringYtopOrigin = ymax
    ringYbottomOrigin = ymax-gridHeight

    # create output file
    outDriver = ogr.GetDriverByName('ESRI Shapefile')
    if os.path.exists(outputGridfn):
        os.remove(outputGridfn)
    outDataSource = outDriver.CreateDataSource(outputGridfn)
    outLayer = outDataSource.CreateLayer(outputGridfn,geom_type=ogr.wkbPolygon )
    featureDefn = outLayer.GetLayerDefn()

    # create grid cells
    countcols = 0
    while countcols < cols:
        countcols += 1

        # reset envelope for rows
        ringYtop = ringYtopOrigin
        ringYbottom =ringYbottomOrigin
        countrows = 0

        while countrows < rows:
            countrows += 1
            ring = ogr.Geometry(ogr.wkbLinearRing)
            ring.AddPoint(ringXleftOrigin, ringYtop)
            ring.AddPoint(ringXrightOrigin, ringYtop)
            ring.AddPoint(ringXrightOrigin, ringYbottom)
            ring.AddPoint(ringXleftOrigin, ringYbottom)
            ring.AddPoint(ringXleftOrigin, ringYtop)
            poly = ogr.Geometry(ogr.wkbPolygon)
            poly.AddGeometry(ring)

            # add new geom to layer
            outFeature = ogr.Feature(featureDefn)
            outFeature.SetGeometry(poly)
            outLayer.CreateFeature(outFeature)
            outFeature.Destroy

            # new envelope for next poly
            ringYtop = ringYtop - gridHeight
            ringYbottom = ringYbottom - gridHeight

        # new envelope for next poly
        ringXleftOrigin = ringXleftOrigin + gridWidth
        ringXrightOrigin = ringXrightOrigin + gridWidth

    # Close DataSources
    outDataSource.Destroy()


if __name__ == "__main__":

    #
    # example run : $ python grid.py <full-path><output-shapefile-name>.shp xmin xmax ymin ymax gridHeight gridWidth
    #

    if len( sys.argv ) != 8:
        print "[ ERROR ] you must supply seven arguments: output-shapefile-name.shp xmin xmax ymin ymax gridHeight gridWidth"
        sys.exit( 1 )

    main( sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5], sys.argv[6], sys.argv[7] )
ustroetz
  • 7,994
  • 10
  • 72
  • 118
9

This Python script uses the pyshp library, as suggested by user16044:

import shapefile as shp
import math

minx,maxx,miny,maxy = 448262.080078, 450360.750122, 6262492.020081, 6262938.950073
dx = 100
dy = 100

nx = int(math.ceil(abs(maxx - minx)/dx))
ny = int(math.ceil(abs(maxy - miny)/dy))

w = shp.Writer(shp.POLYGON)
w.autoBalance = 1
w.field("ID")
id=0

for i in range(ny):
    for j in range(nx):
        id+=1
        vertices = []
        parts = []
        vertices.append([min(minx+dx*j,maxx),max(maxy-dy*i,miny)])
        vertices.append([min(minx+dx*(j+1),maxx),max(maxy-dy*i,miny)])
        vertices.append([min(minx+dx*(j+1),maxx),max(maxy-dy*(i+1),miny)])
        vertices.append([min(minx+dx*j,maxx),max(maxy-dy*(i+1),miny)])
        parts.append(vertices)
        w.poly(parts)
        w.record(id)

w.save('polygon_grid')

Note: a square grid of size 1 m with such extent equals to a layer containing about 1 million of polygons and so the script performance decreases sensibly.

Antonio Falciano
  • 14,333
  • 2
  • 36
  • 66
3

This question is answered since time ago, but I add another solution using shapely and fiona libraries:

import fiona
from shapely.geometry import mapping, LineString, MultiLineString

file = 'input.shp'
with fiona.open(file, 'r') as ds_in:
    num_tiles = 5
    schema = {
    "geometry": "MultiLineString",
    "properties": {"id": "int"}
     }
minx, miny, maxx, maxy = ds_in.bounds
dx = (maxx - minx) / num_tiles
dy = (maxy - miny) / num_tiles

lines = []
for x in range(num_tiles + 1):
    lines.append(LineString([(minx + x * dx, miny), (minx + x * dx, maxy)]))
for y in range(num_tiles + 1):
    lines.append(LineString([(minx, miny + y * dy), (maxx, miny + y * dy)]))
grid = MultiLineString(lines)
out = 'gridtest.shp'
with fiona.open(out, 'w', driver=ds_in.driver, schema=schema, crs=ds_in.crs) as ds_dst:
    ds_dst.write({'geometry': mapping(grid), "properties": {"id": 0}})
ImanolUr
  • 1,102
  • 1
  • 10
  • 21
-2

The answer to Creating fishnet grid Shapefile in QGIS? shows a create grid option in the QGIS processing toolbox.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
user965586
  • 159
  • 6
  • 1
    OP stated that he/she prefer an example in pure Python rather than with a software – LaughU Aug 03 '17 at 05:51
  • Given the other answers import libraries, importing QGIS modules is a valid way forward to avoid a GUI. Like in this answer https://gis.stackexchange.com/questions/79916/how-to-generate-a-grid-programmatically-in-python-without-gui – user965586 Aug 03 '17 at 05:57
  • 1
    The other answers provide code so if yours did too then I think it would be better received. – PolyGeo Aug 03 '17 at 06:57