7 from lxml import html,etree
8 from optparse import OptionParser
10 from pprint import pprint
15 # spherical mercator (most common target map projection of osm data imported with osm2pgsql)
16 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')
18 # long/lat in degrees, aka ESPG:4326 and "WGS 84"
19 longlat = mapnik.Projection('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')
20 # can also be constructed as:
21 #longlat = mapnik.Projection('+init=epsg:4326')
23 # ensure minimum mapnik version
24 if not hasattr(mapnik,'mapnik_version') and not mapnik.mapnik_version() >= 600:
25 raise SystemExit('This script requires Mapnik >=0.6.0)')
28 def render_map(mapfile,map_uri,gpx_file,imgx,imgy):
30 with open(gpx_file,'r') as f:
33 xml = etree.fromstring(data)
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 line_symbolizer = mapnik.LineSymbolizer()
69 line_symbolizer.stroke = mapnik.Color('rgb(0,0,127)')
70 line_symbolizer.stroke_width = 4.0
71 line_symbolizer.stroke_opacity = 0.5
73 rule.symbols.append(line_symbolizer)
75 style.rules.append(rule)
76 m.append_style('GPS_tracking_points', style)
78 layer = mapnik.Layer('GPS_tracking_points')
79 layer.datasource = mapnik.Ogr(file=gpx_file, layer='tracks')
80 layer.styles.append('GPS_tracking_points')
81 m.layers.append(layer)
83 im = mapnik.Image(imgx,imgy)
85 im.save(map_uri,'png')
86 sys.stdout.write('output image to %s!\n' % map_uri)
89 def render_all(db,mapfile,imgx,imgy):
92 from os.path import dirname
94 conn = sqlite3.connect(db)
95 conn.text_factory = str
97 updcur = conn.cursor()
99 cur.execute ("select id,filename from tracks where preview_name is null")
107 preview_name = dirname(filename) + '/' + str(id) + '.png'
109 print(id,filename,preview_name)
112 render_map(mapfile,preview_name.encode('utf8'),filename,imgx,imgy)
113 updcur.execute("update tracks set preview_name=? where id=?", (preview_name,id))
121 parser = OptionParser()
122 parser.add_option("-m", "--map", dest="mapfile",
123 help="use map file", metavar="MAP")
124 parser.add_option("-o", "--output", dest="outfile",
125 help="output image to file", metavar="OUT")
126 parser.add_option("-g", "--gpx", dest="gpxfile",
127 help="track to render", metavar="GPX")
128 parser.add_option("-x", "--x-size", dest="x",
129 help="image width", metavar="X")
130 parser.add_option("-y", "--y-size", dest="y",
131 help="image height", metavar="Y")
132 parser.add_option("-d", "--db", dest="db",
133 help="render all not process files in database", metavar="DB")
134 (options, args) = parser.parse_args()
137 mapfile = options.mapfile
139 mapfile = "/etc/mapnik-osm-carto-data/veloroad-imposm.xml"
142 map_uri = options.outfile
144 map_uri = "image.png"
147 imgx = int(options.x)
151 imgy = int(options.y)
156 render_all(options.db,mapfile,imgx,imgy)
159 gpx_file = options.gpxfile
160 render_map(mapfile,map_uri,gpx_file,imgx,imgy)
162 print("No input file")
163 raise IOError("No input file")
165 if __name__ == "__main__":