def __init__(self, content): """ With MapRenderer you can render an aktionskarten map in different file formats like pdf, svg or png. Internally it uses mapnik and cairo to archieve this. A valid style has to be defined through `MAPNIK_OSM_XML`. Normally this a carto derived `style.xml`. In this file your datasource is specified. This can be for instance a postgres+postgis database with imported osm data. :param content: dict of map content """ # Mapnik uses mercator as internal projection. Our data is encoded in # latlon. Therefor we need a transformer for coordindates from longlat # to mercator proj_merc = Projection('+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0\ +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m \ +nadgrids=@null +no_defs +over') proj_longlat = Projection('+proj=longlat +ellps=WGS84 +datum=WGS84 \ +no_defs') transformer = ProjTransform(proj_longlat, proj_merc) # our maps should be printable on a DIN A4 page with 150dpi self._map = Map(1754, 1240) bbox = transformer.forward(Box2d(*content['bbox'])) self._map.zoom_to_box(bbox) self._map.buffer_size = 5 start = timer() # add osm data (background) load_map(self._map, current_app.config['MAPNIK_OSM_XML']) mid = timer() self._add_grid(content['grid']) self._add_features(content['features']) self._add_legend(content['name'], content['place'], content['datetime'], content['attributes']) end = timer() print("Map.init - OSM: ", mid - start) print("Map.init - Map: ", end - mid) print("Map.init - Total: ", end - start)
# it to within the web mercator 'square world' if necessary. # # Example usage: latlon2merc.py -180 -90 180 90 import sys from mapnik import Box2d, Coord, Projection, ProjTransform latlon = Projection('+proj=latlong +datum=WGS84') merc = Projection( '+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs' ) max_4326 = 85.0511287798066 max_3785 = 20037508.3427892 transform = ProjTransform(latlon, merc) if len(sys.argv) == 3: ll = transform.forward(Coord(float(sys.argv[1]), float(sys.argv[2]))) if (ll.y > max_3785): print(' '.join([str(ll.x), str(max_3785)])) elif (ll.y < -max_3785): print(' '.join([str(ll.x), str(-max_3785)])) else: print(' '.join([str(ll.x), str(ll.y)])) elif len(sys.argv) == 5: minx, miny, maxx, maxy = (float(sys.argv[1]), float(sys.argv[2]), float(sys.argv[3]), float(sys.argv[4])) if (miny < -max_4326):
def transform(self, from_prj, to_prj): trans = ProjTransform(from_prj, to_prj) return trans.forward(self)
# note: requires mapnik SVN r822 or greater. # Example of reprojecting the extent for the netherlands in epsg:28992 to google mercator and then back to the original netherlands extent. import sys try: from mapnik import ProjTransform, Projection, Envelope except ImportError, E: sys.exit('Requires Mapnik SVN r822 or greater:\n%s' % E) # http://spatialreference.org/ref/epsg/28992/ # http://mapnik.dbsgeo.com/days/2009-01-25 amersfoort_extent = Envelope(13599.999985, 306799.999985, 277999.999985, 619299.999985) amersfoort_proj = Projection('+init=epsg:28992') merc_proj = Projection( '+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs' ) transform = ProjTransform(amersfoort_proj, merc_proj) merc_extent = transform.forward(amersfoort_extent) if transform.backward(merc_extent).__repr__() == amersfoort_extent.__repr__(): print 'Transformation successful!' print 'Original Dutch Extent: %s' % amersfoort_extent print 'Merc Extent: %s' % merc_extent else: print 'Reprojection failure...'