4 import mapnik2 as mapnik
10 from lxml import html,etree
11 from optparse import OptionParser
13 from pprint import pprint
18 # spherical mercator (most common target map projection of osm data imported with osm2pgsql)
19 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 +nadgrids=@null +no_defs +over')
21 # long/lat in degrees, aka ESPG:4326 and "WGS 84"
22 longlat = mapnik.Projection('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')
23 # can also be constructed as:
24 #longlat = mapnik.Projection('+init=epsg:4326')
26 # ensure minimum mapnik version
27 if not hasattr(mapnik,'mapnik_version') and not mapnik.mapnik_version() >= 600:
28 raise SystemExit('This script requires Mapnik >=0.6.0)')
31 def render_map(mapfile,map_uri,gpx_file,imgx,imgy):
33 with open(gpx_file,'r') as f:
36 xml = etree.fromstring(data)
40 bbox = gpx.bound_box()
42 cx=(bbox[0][1]+bbox[1][1])/2
43 cy=(bbox[0][0]+bbox[1][0])/2
45 w=(bbox[1][1]-bbox[0][1])*1.2
46 h=(bbox[1][0]-bbox[0][0])*1.2
48 bounds = (cx-w/2,cy-h/2,cx+w/2,cy+h/2)
51 m = mapnik.Map(imgx,imgy)
52 mapnik.load_map(m,mapfile)
54 m.background = mapnik.Color('rgb(255,255,255)')
56 # ensure the target map projection is mercator
59 bbox = mapnik.Box2d(*bounds)
63 transform = mapnik.ProjTransform(longlat,merc)
64 merc_bbox = transform.forward(bbox)
65 m.zoom_to_box(merc_bbox)
67 style = mapnik.Style()
70 line_symbolizer = mapnik.LineSymbolizer()
71 line_symbolizer.stroke = mapnik.Color('rgb(125, 125, 0)')
72 rule.symbols.append(line_symbolizer)
74 point_symbolizer = mapnik.MarkersSymbolizer()
75 point_symbolizer.allow_overlap = True
76 point_symbolizer.opacity = 0.8 # semi-transparent
77 rule.symbols.append(point_symbolizer)
79 style.rules.append(rule)
80 m.append_style('GPS_tracking_points', style)
82 layer = mapnik.Layer('GPS_tracking_points')
83 layer.datasource = mapnik.Ogr(file=gpx_file, layer='track_points')
84 layer.styles.append('GPS_tracking_points')
85 m.layers.append(layer)
87 im = mapnik.Image(imgx,imgy)
89 im.save(map_uri,'png')
90 sys.stdout.write('output image to %s!\n' % map_uri)
93 def render_all(db,mapfile,imgx,imgy):
96 from os.path import dirname
98 conn = sqlite3.connect(db)
100 updcur = conn.cursor()
102 cur.execute ("select id,filename from tracks where preview_name is null")
110 preview_name = dirname(filename) + '/' + str(id) + '.png'
112 print id,filename,preview_name
115 render_map(mapfile,preview_name.encode('utf8'),filename,imgx,imgy)
116 updcur.execute("update tracks set preview_name=? where id=?", (preview_name,id))
124 parser = OptionParser()
125 parser.add_option("-m", "--map", dest="mapfile",
126 help="use map file", metavar="MAP")
127 parser.add_option("-o", "--output", dest="outfile",
128 help="output image to file", metavar="OUT")
129 parser.add_option("-g", "--gpx", dest="gpxfile",
130 help="track to render", metavar="GPX")
131 parser.add_option("-x", "--x-size", dest="x",
132 help="image width", metavar="X")
133 parser.add_option("-y", "--y-size", dest="y",
134 help="image height", metavar="Y")
135 parser.add_option("-d", "--db", dest="db",
136 help="render all not process files in database", metavar="DB")
137 (options, args) = parser.parse_args()
140 mapfile = options.mapfile
142 mapfile = "/etc/mapnik-osm-carto-data/veloroad-transparent.xml"
145 map_uri = options.outfile
147 map_uri = "image.png"
150 imgx = int(options.x)
154 imgy = int(options.y)
159 render_all(options.db,mapfile,imgx,imgy)
162 gpx_file = options.gpxfile
163 render_map(mapfile,map_uri,gpx_file,imgx,imgy)
165 print "No input file"
168 if __name__ == "__main__":