Skip to content

Instantly share code, notes, and snippets.

@lukasmartinelli
Created May 17, 2016 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukasmartinelli/f902b21f9a8e7a7406ea762b17f2a893 to your computer and use it in GitHub Desktop.
Save lukasmartinelli/f902b21f9a8e7a7406ea762b17f2a893 to your computer and use it in GitHub Desktop.
Scripts to deal with XYZ tile lists
import fileinput
import sys
def around_north_pole(y):
return y < 75
def around_south_pole(y):
return y > 184
if __name__ == '__main__':
for line in fileinput.input():
z, x, y = line.split('/')
#if not around_north_pole(int(y)) and not around_south_pole(int(y)):
# sys.stdout.write(line)
if around_north_pole(int(y)):
sys.stdout.write(line)
import json
import mercantile
import fileinput
def create_feature(x, y, z):
west, south, east, north = mercantile.bounds(x, y, z)
return {
"type": "Feature",
"properties": {
"x": x,
"y": y,
"z": z
},
"geometry": {
"type": "Polygon",
'coordinates': [[
[west, south],
[west, north],
[east, north],
[east, south],
[west, south]]]
}
}
def convert_line_to_tile(line):
tile_part = line.split('\t')
z, x, y = tile_part[0].split('/')
return {
'x': int(x),
'y': int(y),
'z': int(z)
}
def features():
for line in fileinput.input():
tile = convert_line_to_tile(line)
yield create_feature(tile['x'], tile['y'], tile['z'])
collection = {
"type": "FeatureCollection",
"features": list(features())
}
print(json.dumps(collection))
import json
import mercantile
import hashlib
import fileinput
def create_pyramid_job(x, y, z):
bounds = mercantile.bounds(x, y, z)
pyramid = {
'tile': {
'x': x,
'y': y,
'min_zoom': z,
'max_zoom': 14
},
'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):
tile_part = line.split('\t')
z, x, y = tile_part[0].split('/')
return {
'x': int(x),
'y': int(y),
'z': int(z)
}
for line in fileinput.input():
tile = convert_line_to_tile(line)
print(json.dumps(create_pyramid_job(tile['x'], tile['y'], tile['z'])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment