New library ✨ tilepie šŸ•

17 Dec 2017

On a @drewbo suggestion I built tilepie and published it on Pypi. This is a simplified, non-feature complete python port of @mapbox/tilereduce that allows us to read and process vector tiles in parallel. With this tool, you could for example read the OSM QA tiles and quickly get a count of buildings for a country.

Porting this to python opens vector tile processing to use Numpy and Scipy. We will be using tilepie at Development Seed to improve our machine learning training sets, but I’d love to hear it being used in other places!

Simple 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 onTileDone(count):
  global total_count
  total_count += count
  
## Define a function that runs at the end of all jobs
def onEnd():
  global total_count
  print total_count
  
# 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)
  },
  mapper,
  onTileDone,
  onEnd
)

# Output is 86343

PRs welcome! Let me know if you use it or have suggestions to improve it.