Skip to content

kamicut/tilepie

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

16 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

tilepie πŸ•

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.

done: () -> void

A function called at the end of all jobs.

Installing

You can install tilepie from PyPi ✨

pip install tilepie

Example

from tilepie import tilereduce
import mapbox_vector_tile

total_count = 0

## Define a mapper function that operates on each tile
def mapper(x, y, z, data):
  if data is None:
          return 0

  tile = mapbox_vector_tile.decode(data)

  count = 0
  if (tile['osm']['features']):
    count = len(tile['osm']['features'])

  return count

## Define a callback when each tile finishes
def on_tile_done(count):
  global total_count
  total_count += count

## Define a function that runs at the end of all jobs
def on_end():
  global total_count
  print total_count

## Log errors
def on_error(e):
  print(e)

# Call tilereduce
# This is using lebanon.mbtiles from the QA Tiles
tilereduce(
  {
    '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
)

Acknowledgements & previous work

License

MIT Β© Marc Farra unless otherwise specified