2

I have used the code from Mapnik python tutorial. Now I want to use zoom_to_box to zoom to certain extent. I want to zoom to Box2d(-180.0, 0.0, 0.0, 90.0), however, after appied zoom_to_box, I saw the map envelope is Box2d(-180.0,-45.0,0.0,135.0), so what do I miss here?

The testing shapefile can be downloaded here: shapefile

Here below my code:

import mapnik
m = mapnik.Map(256,256)
m.background = mapnik.Color('steelblue')
s = mapnik.Style()
r = mapnik.Rule()
polygon_symbolizer = mapnik.PolygonSymbolizer(mapnik.Color('#f2eff9'))
r.symbols.append(polygon_symbolizer)
line_symbolizer = mapnik.LineSymbolizer(mapnik.Color('rgb(50%,50%,50%)'),0.1)
r.symbols.append(line_symbolizer)
s.rules.append(r)
m.append_style('My Style',s)
ds = mapnik.Shapefile(file='ne_110m_admin_0_countries.shp')
print ds.envelope()
layer = mapnik.Layer('world')
layer.datasource = ds
layer.styles.append('My Style')
m.layers.append(layer)
print m.envelope(), layer.envelope()
m.zoom_all()
print m.envelope(), m.scale()
bbox = mapnik.Box2d(-180.0, 0.0, 0.0, 90.0)  # 00
m.zoom_to_box(bbox)
print m.envelope(), m.scale()
mapnik.render_to_file(m,'world00.png', 'png#')
print "rendered image to 'world.png'"
  • can anybody help? – stevenhz Oct 13 '15 at 8:02
  • I came by your question when searching for a similar problem. Probably too late, but I found this page and thought it might be helpful to others: github.com/mapnik/mapnik/wiki/Aspect-Fix-Mode I set aspect_fix_mode to map.aspect_fix_mode = mapnik.aspect_fix_mode.RESPECT, and mapnik respected my bbox! – kaveh May 10 '16 at 16:34
2

kaveh is probably right. The zoom_to_box()method sets the extent and then call the fixAspectRatio()method. More details here: https://github.com/mapnik/mapnik/blob/7931295d3322b7cc57f820d7e2db6449d791159f/src/map.cpp#L581

Depending on the aspect_fix_mode that you have picked, it will readjust the extent. If you don't want it to modify your extent, you can set the aspect_fix_modeto RESPECTand the following code will be executed at the beginning of the fixAspectRatio()method:

if (aspectFixMode_ == RESPECT) return;

leaving your extent unchanged.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.