Hi George,
What you are looking for is:
m.aspect_fix_mode = mapnik.GROW_CANVAS
Basically, that directs mapnik internally, when m.zoom_to_box() is called, to
adjust the canvas size to match the bbox aspect ratio, rather than the other
way around.
I have an "annotated" version of generate_image.py (see line 60) that I've been
meaning to commit to osm svn to illustrate this issue, but I've not done that
yet.
So, instead i've attached it to this email.
Hope it helps. Let me know if you have further questions.
Dane
#!/usr/bin/python
import mapnik
import sys, os
# Set up projections
# spherical mercator (most common target map projection of osm data imported with osm2pgsql)
merc = mapnik.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 +nadgri...@null +no_defs +over')
# long/lat in degrees, aka ESPG:4326 and "WGS 84"
longlat = mapnik.Projection('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')
# can also be constructed as:
#longlat = mapnik.Projection('+init=epsg:4326')
# ensure minimum mapnik version
if not hasattr(mapnik,'mapnik_version') and not mapnik.mapnik_version() >= 600:
raise SystemExit('This script requires Mapnik >=0.6.0)')
if __name__ == "__main__":
try:
mapfile = os.environ['MAPNIK_MAP_FILE']
except KeyError:
mapfile = "osm.xml"
map_uri = "image.png"
#---------------------------------------------------
# Change this to the bounding box you want
#
bounds = (-6.5, 49.5, 2.1, 59)
#---------------------------------------------------
z = 10
imgx = 500 * z
imgy = 1000 * z
m = mapnik.Map(imgx,imgy)
mapnik.load_map(m,mapfile)
# ensure the target map projection is mercator
m.srs = merc.params()
if hasattr(mapnik,'Box2d'):
bbox = mapnik.Box2d(*bounds)
else:
bbox = mapnik.Envelope(*bounds)
# Our bounds above are in long/lat, but our map
# is in spherical mercator, so we need to transform
# the bounding box to mercator to properly position
# the Map when we call `zoom_to_box()`
transform = mapnik.ProjTransform(longlat,merc)
merc_bbox = transform.forward(bbox)
# Mapnik internally will fix the aspect ratio of the bounding box
# to match the aspect ratio of the target image width and height
# This behavior is controlled by setting the `m.aspect_fix_mode`
# and defaults to GROW_BBOX, but you can also change it to alter
# the target image size by setting aspect_fix_mode to GROW_CANVAS
#m.aspect_fix_mode = mapnik.GROW_CANVAS
# Note: aspect_fix_mode is only available in Mapnik >= 0.6.0
m.zoom_to_box(merc_bbox)
# render the map to an image
im = mapnik.Image(imgx,imgy)
mapnik.render(m, im)
im.save(map_uri,'png')
sys.stdout.write('output image to %s!\n' % map_uri)
# Note: instead of creating an image, rendering to it, and then
# saving, we can also do this in one step like:
# mapnik.render_to_file(m, map_uri,'png')
# And in Mapnik >= 0.7.0 you can also use `render_to_file()` to output
# to Cairo supported formats if you have Mapnik built with Cairo support
# For example, to render to pdf or svg do:
# mapnik.render_to_file(m, "image.pdf")
mapnik.render_to_file(m, "image.svg")
On Nov 17, 2010, at 4:40 PM, George Ionescu wrote:
> Hello mapnik users,
> Hello Artem,
>
> I'm using mapnik 0.7.1 through python-2.6 bindings on Windows 2008 to
> render osm data.
>
> The use case is pretty simple:
>
> m = mapnik.Map(512,256,"+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 +nadgri...@null +no_defs")
> mapnik.load_map(m,'./osm/osm.xml')
> bbox = mapnik.Envelope(mapnik.Coord(2906441.56,5532817.86),
> mapnik.Coord(2907664.56,5533429.35))
> m.zoom_to_box(bbox)
> im = mapnik.Image(m.width,m.height)
> map_output = 'map.png'
> mapnik.render(m,im)
> im.save(map_output)
>
> The problem I'm facing is that if I modify Image's size, e.g.
> im = mapnik.Image(m.width*2,m.height*2)
>
> the generated image has a modified bounding box, which is something I
> do not want.
>
> So, my question is this: how can I make sure that the geographical
> coordinates remain the same (e.g. specified through map.zoom_to_box),
> no matter the resolution of the output image it is rendered into?
>
> Thank you for any pointers,
> George.
> _______________________________________________
> Mapnik-users mailing list
> [email protected]
> https://lists.berlios.de/mailman/listinfo/mapnik-users
_______________________________________________
Mapnik-users mailing list
[email protected]
https://lists.berlios.de/mailman/listinfo/mapnik-users