Created
September 19, 2013 20:48
-
-
Save flippmoke/6629633 to your computer and use it in GitHub Desktop.
Mapnik vector tile python wrappings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "vector_tile_datasource.hpp" | |
#include "vector_tile_compression.hpp" | |
#include "vector_tile_projection.hpp" | |
#include <stdio.h> | |
#include <mapnik/box2d.hpp> | |
#include <mapnik/layer.hpp> | |
#include <mapnik/map.hpp> | |
#include <Python.h> | |
#include <boost/python.hpp> | |
struct Tile_Struct { | |
mapnik::vector::tile t; | |
int x; | |
int y; | |
int z; | |
mapnik::box2d<double> bbox; | |
}; | |
Tile_Struct to_tile(std::string buffer, int x, int y, int z) { | |
Tile_Struct ts; | |
ts.x = x; | |
ts.y = y; | |
ts.z = z; | |
ts.t.ParseFromString(buffer); | |
double minx,miny,maxx,maxy; | |
mapnik::vector::spherical_mercator merc(4096); | |
merc.xyz(ts.x,ts.y,ts.z,minx,miny,maxx,maxy); | |
ts.bbox.init(minx,miny,maxx,maxy); | |
return ts; | |
} | |
void to_map(mapnik::Map& map, | |
Tile_Struct& ts) | |
{ | |
for (int i = 0; i < ts.t.layers_size(); ++i) | |
{ | |
mapnik::vector::tile_layer const& layer = ts.t.layers(i); | |
boost::shared_ptr<mapnik::vector::tile_datasource> ds = boost::make_shared< | |
mapnik::vector::tile_datasource>( | |
layer,ts.x,ts.y,ts.z,map.width()); | |
ds->set_envelope(ts.bbox); | |
mapnik::layer lyr(layer.name()); | |
lyr.set_datasource(ds); | |
lyr.add_style("style"); | |
map.addLayer(lyr); | |
} | |
} | |
using namespace boost::python; | |
tuple xyz_bbox(int x, int y, int z) | |
{ | |
double minx,miny,maxx,maxy; | |
mapnik::vector::spherical_mercator merc(4096); | |
merc.xyz(x,y,z,minx,miny,maxx,maxy); | |
return make_tuple(minx, miny, maxx, maxy); | |
} | |
BOOST_PYTHON_MODULE(MVT) | |
{ | |
class_<Tile_Struct>("tile", no_init); | |
def("to_tile", &to_tile); | |
def("xyz_bbox", &xyz_bbox); | |
def("to_map", &to_map, | |
with_custodian_and_ward<1, 2>()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for linking me this the other day in #mapnik.
I added 'make_tile' function for making tiles from xml mapfile:
https://gist.github.com/demiurg/6642052