7 from lxml import html,etree
 
   8 from optparse import OptionParser
 
  10 from pprint import pprint
 
  12 from pyrungps.pygpx import GPX
 
  14 from pyproj import Transformer
 
  17 # spherical mercator (most common target map projection of osm data imported with osm2pgsql)
 
  18 #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')
 
  20 # long/lat in degrees, aka ESPG:4326 and "WGS 84" 
 
  21 #longlat = mapnik.Projection('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs')
 
  22 # can also be constructed as:
 
  23 #longlat = mapnik.Projection('+init=epsg:4326')
 
  25 # ensure minimum mapnik version
 
  26 if not hasattr(mapnik,'mapnik_version') and not mapnik.mapnik_version() >= 600:
 
  27     raise SystemExit('This script requires Mapnik >=0.6.0)')
 
  30 def render_map(mapfile,map_uri,gpx_file,imgx,imgy):
 
  32     with open(gpx_file,'r') as f:
 
  35     xml = etree.fromstring(data)
 
  39     bbox = gpx.bound_box()
 
  41     cx=(bbox[0][1]+bbox[1][1])/2
 
  42     cy=(bbox[0][0]+bbox[1][0])/2
 
  44     w=(bbox[1][1]-bbox[0][1])*1.2
 
  45     h=(bbox[1][0]-bbox[0][0])*1.2
 
  47     bounds = (cx-w/2,cy-h/2,cx+w/2,cy+h/2)
 
  50     m = mapnik.Map(imgx,imgy)
 
  51     mapnik.load_map(m,mapfile)
 
  53     m.background = mapnik.Color('rgb(255,255,255)')
 
  55     # ensure the target map projection is mercator
 
  56     #m.srs = merc.params()
 
  58     bbox = mapnik.Box2d(*bounds)
 
  67     print(minlat,maxlat,minlon,maxlon)
 
  69     transformer = Transformer.from_crs("EPSG:4326", "EPSG:3857", always_xy=True)
 
  70     minP=transformer.transform(minlon,minlat)
 
  71     maxP=transformer.transform(maxlon,maxlat)
 
  73     merc_bbox = mapnik.Box2d(minP[0],minP[1],maxP[0],maxP[1])
 
  74     m.zoom_to_box(merc_bbox)
 
  76     style = mapnik.Style()
 
  79     line_symbolizer = mapnik.LineSymbolizer()
 
  81     line_symbolizer.stroke = mapnik.Color('rgb(0,0,127)')
 
  82     line_symbolizer.stroke_width = 4.0
 
  83     line_symbolizer.stroke_opacity = 0.5
 
  85     rule.symbols.append(line_symbolizer)
 
  87     style.rules.append(rule)
 
  88     m.append_style('GPS_tracking_points', style)
 
  90     layer = mapnik.Layer('GPS_tracking_points')
 
  91     layer.datasource = mapnik.Ogr(file=gpx_file, layer='tracks')
 
  92     layer.styles.append('GPS_tracking_points')
 
  93     m.layers.append(layer)
 
  95     im = mapnik.Image(imgx,imgy)
 
  97     im.save(map_uri,'png')
 
  98     sys.stdout.write('output image to %s!\n' % map_uri)
 
 101 def render_all(db,mapfile,imgx,imgy):
 
 104     from os.path import dirname    
 
 106     conn = sqlite3.connect(db)
 
 107     conn.text_factory = str
 
 109     updcur = conn.cursor()
 
 111     cur.execute ("select id,filename from tracks where preview_name is null")
 
 119       preview_name = dirname(filename) + '/' + str(id) + '.png'
 
 121       print(id,filename,preview_name)
 
 124         render_map(mapfile,preview_name.encode('utf8'),filename,imgx,imgy)
 
 125         updcur.execute("update tracks set preview_name=? where id=?", (preview_name,id))
 
 133     parser = OptionParser()
 
 134     parser.add_option("-m", "--map", dest="mapfile",
 
 135       help="use map file", metavar="MAP")
 
 136     parser.add_option("-o", "--output", dest="outfile",
 
 137       help="output image to file", metavar="OUT")
 
 138     parser.add_option("-g", "--gpx", dest="gpxfile",
 
 139       help="track to render", metavar="GPX")
 
 140     parser.add_option("-x", "--x-size", dest="x",
 
 141       help="image width", metavar="X")
 
 142     parser.add_option("-y", "--y-size", dest="y",
 
 143       help="image height", metavar="Y")
 
 144     parser.add_option("-d", "--db", dest="db",
 
 145       help="render all not process files in database", metavar="DB")
 
 146     (options, args) = parser.parse_args()
 
 149       mapfile = options.mapfile
 
 151       mapfile = "/etc/mapnik-osm-carto-data/veloroad-imposm.xml"
 
 154       map_uri = options.outfile
 
 156       map_uri = "image.png"
 
 159       imgx = int(options.x)
 
 163       imgy = int(options.y)
 
 168       render_all(options.db,mapfile,imgx,imgy) 
 
 171         gpx_file = options.gpxfile
 
 172         render_map(mapfile,map_uri,gpx_file,imgx,imgy)
 
 174         print("No input file")
 
 175         raise IOError("No input file")
 
 177 if __name__ == "__main__":