added gen_map to have the source complete
[osm-map-evolution.git] / generate_image.py
blobd759e2e035dc4ee9ea0107d8c365f5525b018dde
1 #!/usr/bin/python
3 # Generates a single large PNG image for a UK bounding box
4 # Tweak the lat/lon bounding box (ll) and image dimensions
5 # to get an image of arbitrary size.
7 # To use this script you must first have installed mapnik
8 # and imported a planet file into a Postgres DB using
9 # osm2pgsql.
11 # Note that mapnik renders data differently depending on
12 # the size of image. More detail appears as the image size
13 # increases but note that the text is rendered at a constant
14 # pixel size so will appear smaller on a large image.
16 from mapnik import *
17 import sys, os
19 if __name__ == "__main__":
20 try:
21 mapfile = os.environ['MAPNIK_MAP_FILE']
22 except KeyError:
23 mapfile = "osm.xml"
24 map_uri = "image.png"
26 #---------------------------------------------------
27 # Change this to the bounding box you want
29 #ll = (-6.5, 49.5, 2.1, 59)
30 ll = (24.32615, 44.10178, 24.37145, 44.1322) # Caracal
31 ll = (24.30, 44.08, 24.38, 44.14) # Caracal
32 #---------------------------------------------------
34 z = 15
35 imgx = 500 * z
36 imgy = 1000 * z
38 m = Map(imgx,imgy)
39 load_map(m,mapfile)
40 prj = 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")
41 c0 = prj.forward(Coord(ll[0],ll[1]))
42 c1 = prj.forward(Coord(ll[2],ll[3]))
43 bbox = Envelope(c0.x,c0.y,c1.x,c1.y)
44 m.zoom_to_box(bbox)
45 im = Image(imgx,imgy)
46 render(m, im)
47 view = im.view(0,0,imgx,imgy) # x,y,width,height
48 view.save(map_uri,'png')