-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_custom_grid.py
236 lines (188 loc) · 6.66 KB
/
create_custom_grid.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/bin/python
# -*- coding: UTF-8 -*-
# Author: B. Herfort, 2016
###########################################
import os, sys
from math import ceil
import math
class Point:
def __init__(self, x=0.0, y=0.0):
self.x = x
self.y = y
class Tile:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def lat_long_zoom_to_pixel_coords(lat, lon, zoom):
p = Point()
sinLat = math.sin(lat * math.pi/180.0)
x = ((lon + 180) / 360) * 256 * math.pow(2,zoom)
y = (0.5 - math.log((1 + sinLat) / (1 - sinLat)) /
(4 * math.pi)) * 256 * math.pow(2,zoom)
p.x = int(math.floor(x))
p.y = int(math.floor(y))
#print "\nThe pixel coordinates are x = {} and y = {}".format(p.x, p.y)
return p
def pixel_coords_to_tile_address(x,y):
t = Tile()
t.x = int(math.floor(x / 256))
t.y = int(math.floor(y / 256))
#print"\nThe tile coordinates are x = {} and y = {}".format(t.x, t.y)
return t
def main(infile, width, height, zoom):
try:
from osgeo import ogr, osr
print 'Import of ogr and osr from osgeo worked. Hurray!\n'
except:
print '############ ERROR ######################################'
print '## Import of ogr from osgeo failed\n\n'
print '#########################################################'
sys.exit()
# Get filename and extension
try:
infile_name = infile.split('.')[0]
infile_extension = infile.split('.')[-1]
except:
print "check input file"
sys.exit()
# Get the driver --> supported formats: Shapefiles, GeoJSON, kml
if infile_extension == 'shp':
driver = ogr.GetDriverByName('ESRI Shapefile')
elif infile_extension == 'geojson':
driver = ogr.GetDriverByName('GeoJSON')
elif infile_extension == 'kml':
driver = ogr.GetDriverByName('KML')
else:
print 'Check input file format for '+infile
print 'Supported formats .shp .geojson .kml'
sys.exit()
# open the data source
datasource = driver.Open(infile, 0)
try:
# Get the data layer
layer = datasource.GetLayer()
except:
print '############ ERROR ######################################'
print '##'
print '## Check input file!'
print '## '+infile
print '##'
print '#########################################################'
sys.exit()
# Get layer definition
layer_defn = layer.GetLayerDefn()
# Get layer extent
extent = layer.GetExtent()
xmin = extent[0]
xmax = extent[1]
ymin = extent[2]
ymax = extent[3]
# get feature geometry of all features of the input file
geomcol = ogr.Geometry(ogr.wkbGeometryCollection)
for feature in layer:
geomcol.AddGeometry(feature.GetGeometryRef())
# get Zoomlevel
zoom = float(zoom)
# create output file
outputGridfn = infile_name + '_grid.' + infile_extension
outfile = infile_name + '_grid.csv'
l = 0
if os.path.exists(outfile):
os.remove(outfile)
fileobj_output = file(outfile,'w')
fileobj_output.write('id;wkt;width;height;zoom\n')
outDriver = driver
if os.path.exists(outputGridfn):
os.remove(outputGridfn)
outDataSource = outDriver.CreateDataSource(outputGridfn)
outLayer = outDataSource.CreateLayer(outputGridfn,geom_type=ogr.wkbPolygon )
featureDefn = outLayer.GetLayerDefn()
# create fields for width, height, zoom
width_field = ogr.FieldDefn('width',ogr.OFTInteger)
outLayer.CreateField(width_field)
height_field = ogr.FieldDefn('height',ogr.OFTInteger)
outLayer.CreateField(height_field)
zoom_field = ogr.FieldDefn('zoom',ogr.OFTInteger)
outLayer.CreateField(zoom_field)
# get upper left pixel coordinates
pixel = lat_long_zoom_to_pixel_coords(ymax, xmin, zoom)
pixel_left = pixel.x
pixel_top = pixel.y
# get lower right pixel coordinates
pixel = lat_long_zoom_to_pixel_coords(ymin, xmax, zoom)
pixel_right = pixel.x
pixel_bottom = pixel.y
aoi_width = pixel_right - pixel_left
aoi_height = pixel_bottom - pixel_top
x_grids = int((pixel_right - pixel_left)/float(width))+1
y_grids = int((pixel_bottom - pixel_top)/float(height))+1
for i in range(0,y_grids):
for j in range(0,x_grids):
# Calculate lat, lon of upper left corner of tile
PixelX = pixel_left + (j*int(width))
PixelY = pixel_top + (i*int(height))
MapSize = 256*math.pow(2,zoom)
x = (PixelX / MapSize) - 0.5
y = 0.5 - (PixelY / MapSize)
lon_left = 360 * x
lat_top = 90 - 360 * math.atan(math.exp(-y * 2 * math.pi)) / math.pi
# Calculate lat, lon of lower right corner of tile
PixelX = pixel_left + ((j+1)*int(width))
PixelY = pixel_top + ((i+1)*int(height))
MapSize = 256*math.pow(2,zoom)
x = (PixelX / MapSize) - 0.5
y = 0.5 - (PixelY / MapSize)
lon_right = 360 * x
lat_bottom = 90 - 360 * math.atan(math.exp(-y * 2 * math.pi)) / math.pi
# Create Geometry
ring = ogr.Geometry(ogr.wkbLinearRing)
ring.AddPoint(lon_left, lat_top)
ring.AddPoint(lon_right, lat_top)
ring.AddPoint(lon_right, lat_bottom)
ring.AddPoint(lon_left, lat_bottom)
ring.AddPoint(lon_left, lat_top)
poly = ogr.Geometry(ogr.wkbPolygon)
poly.AddGeometry(ring)
# add new geom to layer
intersect = geomcol.Intersect(poly)
if intersect == True:
l = l+1
o_line = poly.ExportToWkt()
fileobj_output.write(str(l-1)+';'+o_line+';'+str(width)+';'+str(height)+';'+str(int(zoom))+'\n')
outFeature = ogr.Feature(featureDefn)
outFeature.SetGeometry(poly)
if infile_extension == 'kml':
width_height_zoom = str(width)+'_'+str(height)+'_'+str(int(zoom))
outFeature.SetField('name', width_height_zoom)
outFeature.SetField('description', 'width_height_zoom')
else:
outFeature.SetField('width', width)
outFeature.SetField('height', height)
outFeature.SetField('zoom', zoom)
outLayer.CreateFeature(outFeature)
outFeature.Destroy
# Close DataSources
outDataSource.Destroy()
print '############ END ######################################'
print '##'
print '## input file: '+infile
print '##'
print '## zoom: '+str(zoom)
print '## width: '+width
print '## height: '+height
print '##'
print '## output files:'
print '## '+outputGridfn
print '## '+outfile
print '##'
print '## B. Herfort, GIScience Research Group'
print '##'
print '#######################################################'
if __name__ == "__main__":
#
# example run : $ python tiles_grid.py polygon.shp 360 480 18
#
if len( sys.argv ) != 5:
print "[ ERROR ] you must supply 4 arguments: input-shapefile-name.shp width height zoom"
sys.exit( 1 )
main( sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4],)