Code Monkey home page Code Monkey logo

tilepie's Introduction

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

tilepie's People

Contributors

drewbo avatar kamicut avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

tilepie's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.