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 xml = etree.parse(gpx_file)
37 bbox = gpx.bound_box()
39 cx=(bbox[0][1]+bbox[1][1])/2
40 cy=(bbox[0][0]+bbox[1][0])/2
42 w=(bbox[1][1]-bbox[0][1])*1.2
43 h=(bbox[1][0]-bbox[0][0])*1.2
45 bounds = (cx-w/2,cy-h/2,cx+w/2,cy+h/2)
48 m = mapnik.Map(imgx,imgy)
49 mapnik.load_map(m,mapfile)
51 m.background = mapnik.Color('rgb(255,255,255)')
53 # ensure the target map projection is mercator
56 bbox = mapnik.Box2d(*bounds)
60 transform = mapnik.ProjTransform(longlat,merc)
61 merc_bbox = transform.forward(bbox)
62 m.zoom_to_box(merc_bbox)
64 style = mapnik.Style()
67 point_symbolizer = mapnik.MarkersSymbolizer()
68 point_symbolizer.allow_overlap = True
69 point_symbolizer.opacity = 0.8 # semi-transparent
70 rule.symbols.append(point_symbolizer)
72 style.rules.append(rule)
73 m.append_style('GPS_tracking_points', style)
75 layer = mapnik.Layer('GPS_tracking_points')
76 layer.datasource = mapnik.Ogr(file=gpx_file, layer='track_points')
77 layer.styles.append('GPS_tracking_points')
78 m.layers.append(layer)
80 im = mapnik.Image(imgx,imgy)
82 im.save(map_uri,'png')
83 sys.stdout.write('output image to %s!\n' % map_uri)
86 def render_all(db,mapfile,imgx,imgy):
89 from os.path import dirname
91 conn = sqlite3.connect(db)
93 updcur = conn.cursor()
95 cur.execute ("select id,filename from tracks where preview_name is null")
103 preview_name = dirname(filename) + '/' + str(id) + '.png'
105 print id,filename,preview_name
108 render_map(mapfile,preview_name.encode('utf8'),filename,imgx,imgy)
109 updcur.execute("update tracks set preview_name=? where id=?", (preview_name,id))
117 parser = OptionParser()
118 parser.add_option("-m", "--map", dest="mapfile",
119 help="use map file", metavar="MAP")
120 parser.add_option("-o", "--output", dest="outfile",
121 help="output image to file", metavar="OUT")
122 parser.add_option("-g", "--gpx", dest="gpxfile",
123 help="track to render", metavar="GPX")
124 parser.add_option("-x", "--x-size", dest="x",
125 help="image width", metavar="X")
126 parser.add_option("-y", "--y-size", dest="y",
127 help="image height", metavar="Y")
128 parser.add_option("-d", "--db", dest="db",
129 help="render all not process files in database", metavar="DB")
130 (options, args) = parser.parse_args()
133 mapfile = options.mapfile
135 mapfile = "/etc/mapnik-osm-carto-data/veloroad-transparent.xml"
138 map_uri = options.outfile
140 map_uri = "image.png"
143 imgx = int(options.x)
147 imgy = int(options.y)
152 render_all(options.db,mapfile,imgx,imgy)
155 gpx_file = options.gpxfile
156 render_map(mapfile,map_uri,gpx_file,imgx,imgy)
158 print "No input file"
161 if __name__ == "__main__":