source: subversion/applications/rendering/mapnik/generate_image.py @ 26274

Last change on this file since 26274 was 26274, checked in by Dane Springmeyer, 8 years ago

fix the python shebang line so that the default python is used

  • Property svn:executable set to *
File size: 2.8 KB
Line 
1#!/usr/bin/env python
2
3import mapnik
4import sys, os
5
6# Set up projections
7# spherical mercator (most common target map projection of osm data imported with osm2pgsql)
8merc = 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 +nadgrids=@null +no_defs +over')
9
10# long/lat in degrees, aka ESPG:4326 and "WGS 84"
11longlat = mapnik.Projection('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')
12# can also be constructed as:
13#longlat = mapnik.Projection('+init=epsg:4326')
14
15# ensure minimum mapnik version
16if not hasattr(mapnik,'mapnik_version') and not mapnik.mapnik_version() >= 600:
17    raise SystemExit('This script requires Mapnik >=0.6.0)')
18
19if __name__ == "__main__":
20    try:
21        mapfile = os.environ['MAPNIK_MAP_FILE']
22    except KeyError:
23        mapfile = "osm.xml"
24   
25    map_uri = "image.png"
26
27    #---------------------------------------------------
28    #  Change this to the bounding box you want
29    #
30    bounds = (-6.5, 49.5, 2.1, 59)
31    #---------------------------------------------------
32
33    z = 10
34    imgx = 500 * z
35    imgy = 1000 * z
36
37    m = mapnik.Map(imgx,imgy)
38    mapnik.load_map(m,mapfile)
39   
40    # ensure the target map projection is mercator
41    m.srs = merc.params()
42
43    if hasattr(mapnik,'Box2d'):
44        bbox = mapnik.Box2d(*bounds)
45    else:
46        bbox = mapnik.Envelope(*bounds)
47
48    # Our bounds above are in long/lat, but our map
49    # is in spherical mercator, so we need to transform
50    # the bounding box to mercator to properly position
51    # the Map when we call `zoom_to_box()`
52    transform = mapnik.ProjTransform(longlat,merc)
53    merc_bbox = transform.forward(bbox)
54   
55    # Mapnik internally will fix the aspect ratio of the bounding box
56    # to match the aspect ratio of the target image width and height
57    # This behavior is controlled by setting the `m.aspect_fix_mode`
58    # and defaults to GROW_BBOX, but you can also change it to alter
59    # the target image size by setting aspect_fix_mode to GROW_CANVAS
60    #m.aspect_fix_mode = mapnik.GROW_CANVAS
61    # Note: aspect_fix_mode is only available in Mapnik >= 0.6.0
62    m.zoom_to_box(merc_bbox)
63   
64    # render the map to an image
65    im = mapnik.Image(imgx,imgy)
66    mapnik.render(m, im)
67    im.save(map_uri,'png')
68   
69    sys.stdout.write('output image to %s!\n' % map_uri)
70   
71    # Note: instead of creating an image, rendering to it, and then
72    # saving, we can also do this in one step like:
73    # mapnik.render_to_file(m, map_uri,'png')
74   
75    # And in Mapnik >= 0.7.0 you can also use `render_to_file()` to output
76    # to Cairo supported formats if you have Mapnik built with Cairo support
77    # For example, to render to pdf or svg do:
78    # mapnik.render_to_file(m, "image.pdf")
79    mapnik.render_to_file(m, "image.svg")
80   
81
Note: See TracBrowser for help on using the repository browser.