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

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

use mapnik2 if present

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