You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Simple .mbtiles processor for python. Built with the QA Tiles in mind.
Based on the map/reduce/end structure in @mapbox/tilereduce. Tiles are read from the mbtiles container and passed to a worker pool as asynchronous jobs that can run concurrently.
Docs
The main function is tilereduce(options, map_function=map_function, callback=callback, error_callback=error_callback, done=done):
options
A dictionary with the following keys:
source: a path to an mbtiles
bbox: a bounding box limiting the tiles to read
zoom: the zoom level to read from
args: an optional dictionary which is passed to each worker
map_function: (x, y, z, data) -> any
A function that run on each tile asynchronously.
x, y and z specify the tile coordinates, and data is the tile contents.
The mapfunc takes the tile data and should return a value.
callback: (any) -> void
A function called with the return value of map_function.
error_callback: (any) -> void
A function called with an exception instance if one occurs in the worker.
fromtilepieimporttilereduceimportmapbox_vector_tiletotal_count=0## Define a mapper function that operates on each tiledefmapper(x, y, z, data):
ifdataisNone:
return0tile=mapbox_vector_tile.decode(data)
count=0if (tile['osm']['features']):
count=len(tile['osm']['features'])
returncount## Define a callback when each tile finishesdefon_tile_done(count):
globaltotal_counttotal_count+=count## Define a function that runs at the end of all jobsdefon_end():
globaltotal_countprinttotal_count## Log errorsdefon_error(e):
print(e)
# Call tilereduce# This is using lebanon.mbtiles from the QA Tilestilereduce(
{
'zoom': 12,
'source': '~/data/lebanon.mbtiles',
'bbox': (35.1260526873, 33.0890400254, 36.6117501157, 34.6449140488)
},
map_function=mapper,
callback=on_tile_done,
error_callback=on_error,
done=on_end
)