-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_csv.py
More file actions
executable file
·275 lines (232 loc) · 8.1 KB
/
create_csv.py
File metadata and controls
executable file
·275 lines (232 loc) · 8.1 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/bin/python
#
# Author: B. Herfort, 2016
############################################
import os, sys
from math import ceil
import math
import urlparse
import urllib
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 tile_coords_and_zoom_to_quadKey(x, y, zoom):
quadKey = ''
for i in range(zoom, 0, -1):
digit = 0
mask = 1 << (i - 1)
if(x & mask) != 0:
digit += 1
if(y & mask) != 0:
digit += 2
quadKey += str(digit)
#print "\nThe quadkey is {}".format(quadKey)
return quadKey
def quadKey_to_URL(quadKey, api_key):
tile_url = ("http://t0.tiles.virtualearth.net/tiles/a{}.jpeg?"
"g=854&mkt=en-US&token={}".format(quadKey, api_key))
#print "\nThe tile URL is: {}".format(tile_url)
return tile_url
def main(infile, zoomlevel):
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()
# check if the input is a URL, if so, download it
parts = urlparse.urlsplit(infile)
if parts.scheme:
print('infile is a URL')
if not os.path.exists('tmp/'):
os.makedirs('tmp')
temp_infile = (os.getcwd()) + '/tmp/infile.kml'
print(temp_infile)
urllib.urlretrieve(infile, temp_infile)
print(temp_infile)
infile = temp_infile
else:
print "Infile is not a URL"
# 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 API key from local text file
try:
f = open('api_key.txt')
api_key = f.read()
except:
print ("Something is wrong with your API key."
"Do you even have an API key?")
# 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(zoomlevel)
# create output file
outputGridfn = infile_name + '_tiles.' + infile_extension
outfile = infile_name + '_tiles.csv'
l = 0
if os.path.exists(outfile):
os.remove(outfile)
fileobj_output = file(outfile,'w')
fileobj_output.write('wkt$TileX$TileY$TileZ$URL\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 TileX, TileY, TileZ
TileX_field = ogr.FieldDefn('TileX',ogr.OFTInteger)
outLayer.CreateField(TileX_field)
TileY_field = ogr.FieldDefn('TileY',ogr.OFTInteger)
outLayer.CreateField(TileY_field)
TileZ_field = ogr.FieldDefn('TileZ',ogr.OFTInteger)
outLayer.CreateField(TileZ_field)
URL_field = ogr.FieldDefn('URL' , ogr.OFTString)
# get upper left left tile coordinates
pixel = lat_long_zoom_to_pixel_coords(ymax, xmin, zoom)
tile = pixel_coords_to_tile_address(pixel.x, pixel.y)
TileX_left = tile.x
TileY_top = tile.y
# get lower right tile coordinates
pixel = lat_long_zoom_to_pixel_coords(ymin, xmax, zoom)
tile = pixel_coords_to_tile_address(pixel.x, pixel.y)
TileX_right = tile.x
TileY_bottom = tile.y
for TileY in range(TileY_top,TileY_bottom+1):
for TileX in range(TileX_left,TileX_right+1):
# Calculate lat, lon of upper left corner of tile
PixelX = TileX * 256
PixelY = TileY * 256
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 = (TileX+1) * 256
PixelY = (TileY+1) * 256
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()
quadKey = tile_coords_and_zoom_to_quadKey(
int(TileX),int(TileY),int(zoomlevel))
URL = quadKey_to_URL(quadKey, api_key)
fileobj_output.write('\"'+o_line+'\"$'+str(TileX)+'$'+str(TileY)+'$'+str(zoomlevel)+'$'+ URL)
outFeature = ogr.Feature(featureDefn)
outFeature.SetGeometry(poly)
if infile_extension == 'kml':
col_row_zoom = str(
TileX)+'_'+str(TileY)+'_'+str(int(zoom))
outFeature.SetField(
'name', col_row_zoom)
desc = str(TileX) + "_" + str(TileY) + "_" + str(int(zoom)) + "\nTile URL: " + URL
outFeature.SetField(
'description', desc)
else:
outFeature.SetField('TileX', TileX)
outFeature.SetField('TileY', TileY)
outFeature.SetField('TileZ', zoom)
outFeature.SetField('URL', URL)
outLayer.CreateFeature(outFeature)
outFeature.Destroy
# Close DataSources
outDataSource.Destroy()
print '############ END ######################################'
print '##'
print '## input file: '+infile
print '##'
print '## zoomlevel: '+str(zoomlevel)
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 18
#
if len( sys.argv ) != 3:
print "[ ERROR ] you must supply 2 arguments: (input-shapefile-name.kml or URL pointing to a KML, SHP, or GeoJSON polygon) (zoomlevel)"
sys.exit( 1 )
main( sys.argv[1], sys.argv[2] )