-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfishnet.py
executable file
·108 lines (87 loc) · 3.29 KB
/
fishnet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python
import sys
import os
import argparse
import ogr
import osr
from math import ceil
import subprocess as subp
##https://pcjericks.github.io/py-gdalogr-cookbook/vector_layers.html#create-fishnet-grid
def getparser():
parser = argparse.ArgumentParser(description='Create a vecotr grid within a lat/lon bounding box')
parser.add_argument('out_fn', type=str, help='Output vector grid name)')
parser.add_argument('xmin', type=float, default=None, help='xmin')
parser.add_argument('xmax', type=float, default=None, help='xmax')
parser.add_argument('ymin', type=float, default=None, help='ymin')
parser.add_argument('ymax', type=float, default=None, help='ymax')
parser.add_argument('gridWidth', type=float, default=1, help='Width of vector grid cell')
parser.add_argument('gridHeight', type=float, default=1, help='Height of vector grid cell')
return parser
def main():
parser = getparser()
args = parser.parse_args()
out_fn = args.out_fn
# convert sys.argv to float
xmin = args.xmin
xmax = args.xmax
ymin = args.ymin
ymax = args.ymax
gridWidth = args.gridWidth
gridHeight = args.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(out_fn):
os.remove(out_fn)
outDataSource = outDriver.CreateDataSource(out_fn)
outLayer = outDataSource.CreateLayer(out_fn,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 = None
# new envelope for next poly
ringYtop = ringYtop - gridHeight
ringYbottom = ringYbottom - gridHeight
# new envelope for next poly
ringXleftOrigin = ringXleftOrigin + gridWidth
ringXrightOrigin = ringXrightOrigin + gridWidth
# Save and close DataSources
outDataSource = None
# make prj
# https://pcjericks.github.io/py-gdalogr-cookbook/projection.html
spatialRef = osr.SpatialReference()
spatialRef.ImportFromEPSG(4326)
spatialRef.MorphToESRI()
prj_file = open(out_fn.replace("shp","prj"), 'w')
prj_file.write(spatialRef.ExportToWkt())
prj_file.close()
if __name__ == '__main__':
main()