Example #1
0
# DB has to have the same name as cmd user
mapnik2.load_map(m, path_to_osm_xml)

# Define projection
prj = mapnik2.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"
)

# Map bounds. Bound values come from SQL-query
if hasattr(mapnik2, 'Box2d'):
    bbox = mapnik2.Box2d(xmin, ymin, xmax, ymax)
else:
    bbox = mapnik2.Envelope(xmin, ymin, xmax, ymax)

# Project bounds to map projection
e = mapnik2.forward_(bbox, prj)

# Zoom map to bounding box
m.zoom_to_box(e)

###
### START Layer 1
###

# style object to hold rules
s = mapnik2.Style()
s2 = mapnik2.Style()

# rule object to hold symbolizers
r = mapnik2.Rule()
r2 = mapnik2.Rule()
def zoom2size(bbox, zoom):
    prj = 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")
    e = mapnik.forward_(mapnik.Box2d(*bbox), prj)
    wm = e.maxx - e.minx;
    hm = e.maxy - e.miny;
    
    if zoom == 1:
        scale = 279541132.014
    
    elif zoom == 2:
        scale = 139770566.007
    
    elif zoom == 3:
        scale = 69885283.0036
    
    elif zoom == 4:
        scale = 34942641.5018
    
    elif zoom == 5:
        scale = 17471320.7509
    
    elif zoom == 6:
        scale = 8735660.37545
    
    elif zoom == 7:
        scale = 4367830.18772
    
    elif zoom == 8:
        scale = 2183915.09386
    
    elif zoom == 9:
        scale = 1091957.54693
    
    elif zoom == 10:
        scale = 545978.773466
    
    elif zoom == 11:
        scale = 272989.386733
    
    elif zoom == 12:
        scale = 136494.693366
    
    elif zoom == 13:
        scale = 68247.3466832
    
    elif zoom == 14:
        scale = 34123.6733416
    
    elif zoom == 15:
        scale = 17061.8366708
    
    elif zoom == 16:
        scale = 8530.9183354
    
    elif zoom == 17:
        scale = 4265.4591677
    
    elif zoom == 18:
        scale = 2132.72958385
    
    # map_width_in_pixels = map_width_in_metres / scale_denominator / standardized_pixel_size
    # see http://www.britishideas.com/2009/09/22/map-scales-and-printing-with-mapnik/
    
    wp = int(wm / scale / 0.00028)
    hp = int(hm / scale / 0.00028)
    
    return (wp, hp)
m = mapnik2.Map(pic_output_width, pic_output_height)

# Load osm-xml-stylesheet for rendering the views
mapnik2.load_map(m, path_to_osm_xml)

# Define projection
prj = mapnik2.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")

# Map bounds. Bound values come from SQL-query
if hasattr(mapnik2, 'Box2d'):
    bbox = mapnik2.Box2d(xmin,ymin,xmax,ymax)
else:
    bbox = mapnik2.Envelope(xmin,ymin,xmax,ymax)

# Project bounds to map projection
e = mapnik2.forward_(bbox, prj)

# Zoom map to bounding box
m.zoom_to_box(e)

###
### START Layer 1
###

# style object to hold rules
s = mapnik2.Style() 

# rule object to hold symbolizers
r = mapnik2.Rule() 

# Lines (outlines of polygons and/or simple lines. Line-Color (RGB) line-thickness
def render(options):
    # create view
    if(options.view):
        columns = options.viewcolumns.split(',')
        if(options.extracolumns):
            columns += options.extracolumns.split(',')
        
        create_views(options.dsn, options.dbprefix, options.viewprefix, options.viewhstore, columns, options.date)
    
    # create map
    m = mapnik.Map(options.size[0], options.size[1])
    
    # load style
    mapnik.load_map(m, options.style)
    
    # create projection object
    prj = 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")
    
    #c0 = prj.forward(Coord(bbox[0],bbox[1]))
    #c1 = prj.forward(Coord(bbox[2],bbox[3]))
    #e = Box2d(c0.x,c0.y,c1.x,c1.y)
    
    # map bounds
    if hasattr(mapnik, 'Box2d'):
        bbox = mapnik.Box2d(*options.bbox)
    else:
        bbox = mapnik.Envelope(*options.bbox)
    
    # project bounds to map projection
    e = mapnik.forward_(bbox, prj)
    
    # zoom map to bounding box
    m.zoom_to_box(e)
    
    options.file = options.file + "." + options.type
    if(options.type in ("png", "jpeg")):
        s = mapnik.Image(options.size[0], options.size[1])
    
    elif cairo_exists and type == "svg":
        s = cairo.SVGSurface(options.file, options.size[0], options.size[1])
    
    elif cairo_exists and type == "pdf":
        s = cairo.PDFSurface(options.file, options.size[0], options.size[1])
    
    elif cairo_exists and type == "ps":
        s = cairo.PSSurface(options.file, options.size[0], options.size[1])
    
    else:
        print "invalid image type"
        print
        parser.print_help()
        sys.exit(1)
    
    mapnik.render(m, s)
    
    if isinstance(s, mapnik.Image):
        view = s.view(0, 0, options.size[0], options.size[1])
        view.save(options.file, options.type)
    
    elif isinstance(s, cairo.Surface):
        s.finish()
    
    if(options.view):
        drop_views(options.dsn, options.viewprefix)