Created
August 2, 2016 10:43
-
-
Save lukasmartinelli/122b77f64b2809914d51bbffbffdc439 to your computer and use it in GitHub Desktop.
Turn tile text files into GeoJSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Usage example: | |
cat missing_tiles.txt | sed 's/MISSING//g' | python tiles_to_geojson.py > missing_tiles.geojson | |
""" | |
import json | |
import hashlib | |
import mercantile | |
import fileinput | |
def create_pyramid_job(x, y, min_zoom, max_zoom, bounds): | |
pyramid = { | |
'tile': { | |
'x': x, | |
'y': y, | |
'min_zoom': min_zoom, | |
'max_zoom': max_zoom | |
}, | |
'bounds': { | |
'west': bounds.west, | |
'south': bounds.south, | |
'east': bounds.east, | |
'north': bounds.north | |
} | |
} | |
def payload_id(): | |
hash_obj = json.dumps(pyramid, sort_keys=True).encode('utf-8') | |
return hashlib.sha1(hash_obj).hexdigest() | |
return { | |
'id': payload_id(), | |
'type': 'pyramid', | |
'pyramid': pyramid | |
} | |
def convert_line_to_tile(line): | |
z, x, y = line.split('/') | |
return {'x': int(x), 'y': int(y), 'z': int(z)} | |
if __name__ == '__main__': | |
features = [] | |
for line in fileinput.input(): | |
tile = convert_line_to_tile(line) | |
bounds = mercantile.bounds(tile['x'], tile['y'], tile['z']) | |
features.append({ | |
"type": "Feature", | |
"geometry": {"type": "Polygon", "coordinates": [[ | |
[bounds.west, bounds.north], | |
[bounds.east, bounds.north], | |
[bounds.east, bounds.south], | |
[bounds.west, bounds.south], | |
[bounds.west, bounds.north], | |
]]}, | |
"properties": tile, | |
}) | |
geojson = { | |
"type": "FeatureCollection", | |
"features": features | |
} | |
print(json.dumps(geojson)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment