Example #1
0
 def forward(self,geom):
     if self.to_prj.geographic and self.from_prj.geographic:
         return geom
     elif not self.to_prj.geographic and self.from_prj.geographic:
         return forward_(geom,self.to_prj)
     elif self.to_prj.geographic and not self.from_prj.geographic:
         return inverse_(geom,self.to_prj)
     elif not self.to_prj.geographic and not self.from_prj.geographic:
         if self.to_prj.params() == self.from_prj.params():
             return geom
         else:
             raise AttributeError(TRANSFORM_ERROR)
Example #2
0
 def backward(self, geom):
     if self.to_prj.geographic and self.from_prj.geographic:
         return geom
     elif not self.to_prj.geographic and self.from_prj.geographic:
         return mapnik.inverse_(geom, self.to_prj)
     elif self.to_prj.geographic and not self.from_prj.geographic:
         return mapnik.forward_(geom, self.to_prj)
     elif not self.to_prj.geographic and not self.from_prj.geographic:
         if self.to_prj.params() == self.from_prj.params():
             return geom
         else:
             raise NotImplementedError(TRANSFORM_ERROR)
Example #3
0
 def forward(self,geom):
     if self.to_prj.geographic and self.from_prj.geographic:
         return geom
     elif not self.to_prj.geographic and self.from_prj.geographic:
         return mapnik.forward_(geom,self.to_prj)
     elif self.to_prj.geographic and not self.from_prj.geographic:
         return mapnik.inverse_(geom,self.to_prj)
     elif not self.to_prj.geographic and not self.from_prj.geographic:
         if self.to_prj.params() == self.from_prj.params():
             return geom
         else:
             raise NotImplementedError(TRANSFORM_ERROR)
Example #4
0
    def render_one_tile(self, zoom, x, y):

        bbox = [0, 0, 0, 0]
        bbox[3], bbox[0] = num2deg(x, y, zoom)
        bbox[1], bbox[2] = num2deg(x + 1, y + 1, zoom)
        bbox = mapnik.Box2d(*bbox)

        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_(bbox, prj)
        self.m.zoom_to_box(e)

        s = mapnik.Image(tile_size, tile_size)
        mapnik.render(self.m, s)

        view = s.view(0, 0, tile_size, tile_size)
        return view.tostring('png')
Example #5
0
    def render_tiles(self, zoom, bx, by, bw, bh):
        cherrypy.log('RENDER z=%d (%d,%d) (%d,%d)' % (zoom, bx, by, bw, bh))
        bbox = [0, 0, 0, 0]
        bbox[3], bbox[0] = num2deg(bx, by, zoom)
        bbox[1], bbox[2] = num2deg(bx + bw, by + bh, zoom)
        bbox = mapnik.Box2d(*bbox)

        self.m.resize(tile_size * bw, tile_size * bh)

        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_(bbox, prj)
        self.m.zoom_to_box(e)

        s = mapnik.Image(tile_size * bw, tile_size * bh)
        mapnik.render(self.m, s)

        for x in range(bx, bx + bw):
            for y in range(by, by + bh):
                ox = (x - bx) * tile_size
                oy = (y - by) * tile_size
                view = s.view(ox, oy, tile_size, tile_size)
                yield (x, y), view.tostring('png')
        cherrypy.log('RENDER end')
Example #6
0
m = mapnik.Map(pic_output_width, pic_output_height)

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

# Define projection
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")

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

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

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

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

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

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

# Lines (outlines of polygons and/or simple lines. Line-Color (RGB) line-thickness
Example #7
0
#!/usr/bin/env python

from mapnik import Coord, Envelope, Projection, forward_, inverse_
import sys

if (len(sys.argv) < 2):
    print sys.argv[0] + " <epsg:code> <Coord or Envelope>"
    sys.exit(1)
else:
    epsg = sys.argv[1]
    p = Projection('+init=%s' % epsg)
    coord = map(float,sys.argv[2].split(','))
    if len(coord) == 2:
        long_lat = Coord(*coord)
        print 'Forwarded:',
        print p.forward(long_lat)
        print
        print 'Inversed:',
        print p.inverse(long_lat)      
    elif len(coord) == 4:
        extent = Envelope(*coord)
        print 'Forwarded:',
        print forward_(extent,p)
        print
        print 'Inversed:',
        print inverse_(extent,p)
Example #8
0
                % epsg.params())
        mapnik_map.srs = epsg.params()

    if var.has_key('t'):
        output_message('Pausing for %s seconds...' % var['t'])
        pause_for(var['t'])

    #TODO: refactor, test zoom function
    if var.has_key('e'):
        try:
            bbox = [float(x) for x in var['e'].split(",")]
            bbox = mapnik.Envelope(*bbox)
            p = mapnik.Projection("%s" % mapnik_map.srs)
            if not p.geographic:
                output_message('Initialized projection: %s' % p.params())
                bbox = mapnik.forward_(bbox, p)
                output_message('BBOX in projected coordinates is: %s' % bbox)
        except Exception, E:
            output_error("Problem setting geographic bounding box", E)
        mapnik_map.zoom_to_box(bbox)
    elif var.has_key('r'):
        try:
            bbox = [float(x) for x in var['r'].split(",")]
            bbox = mapnik.Envelope(*bbox)
            output_message('BBOX is: %s' % bbox)
        except Exception, E:
            output_error("Problem setting projected bounding box", E)
        mapnik_map.zoom_to_box(bbox)
    else:
        try:
            mapnik_map.zoom_all()
Example #9
0
      output_message("Mapnik projection successfully initiated with epsg code: '%s'" % epsg.params())
    mapnik_map.srs = epsg.params()

  if var.has_key('t'):
    output_message('Pausing for %s seconds...' % var['t'])
    pause_for(var['t'])

  #TODO: refactor, test zoom function
  if var.has_key('e'):
    try:
      bbox = [float(x) for x in var['e'].split(",")]
      bbox = mapnik.Envelope(*bbox)
      p = mapnik.Projection("%s" % mapnik_map.srs)
      if not p.geographic:
        output_message('Initialized projection: %s' % p.params())
        bbox = mapnik.forward_(bbox, p)
        output_message('BBOX in projected coordinates is: %s' % bbox)
    except Exception, E:
       output_error("Problem setting geographic bounding box", E)
    mapnik_map.zoom_to_box(bbox)
  elif var.has_key('r'):
    try:
      bbox = [float(x) for x in var['r'].split(",")]
      bbox = mapnik.Envelope(*bbox)
      output_message('BBOX is: %s' % bbox)
    except Exception, E:
       output_error("Problem setting projected bounding box", E)
    mapnik_map.zoom_to_box(bbox)
  else:
    try:    
      mapnik_map.zoom_all()
Example #10
0
 def forward(self,obj):
     return forward_(self,obj)
Example #11
0
 def forward(self,obj):
     return mapnik.forward_(self,obj)
Example #12
0
elif not form.has_key("format"):
  # No format specified
  output_error("No format specified")
else:
  # 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");

  # Get the bounds of the area to render
  bbox = [float(x) for x in form.getvalue("bbox").split(",")]

  if bbox[0] >= bbox[2] or bbox[1] >= bbox[3]:
    # Bogus bounding box
    output_error("Invalid bounding box")
  else:
    # Project the bounds to the map projection
    bbox = mapnik.forward_(mapnik.Box2d(*bbox), prj)

    # Get the style to use
    style = form.getvalue("style", "default")

    # Calculate the size of the final rendered image
    scale = float(form.getvalue("scale"))
    width = int(bbox.width() / scale / 0.00028)
    height = int(bbox.height() / scale / 0.00028)

    # Limit the size of map we are prepared to produce
    if width * height > 4000000:
      # Map is too large (limit is approximately A2 size)
      output_error("Map too large")
    else:
      # Create map
Example #13
0
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)
Example #14
0
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)
Example #15
0
 def forward(self, obj):
     return mapnik.forward_(self, obj)
Example #16
0
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)
Example #17
0
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)
Example #18
0
#!/usr/bin/env python

from mapnik import Coord, Envelope, Projection, forward_, inverse_
import sys

if (len(sys.argv) < 2):
    print sys.argv[0] + " <epsg:code> <Coord or Envelope>"
    sys.exit(1)
else:
    epsg = sys.argv[1]
    p = Projection('+init=%s' % epsg)
    coord = map(float, sys.argv[2].split(','))
    if len(coord) == 2:
        long_lat = Coord(*coord)
        print 'Forwarded:',
        print p.forward(long_lat)
        print
        print 'Inversed:',
        print p.inverse(long_lat)
    elif len(coord) == 4:
        extent = Envelope(*coord)
        print 'Forwarded:',
        print forward_(extent, p)
        print
        print 'Inversed:',
        print inverse_(extent, p)