Example #1
0
def renderLayer(name, z, x, y, ntiles, map, suffix = 'png', useCairo = False):
    """Renders the specified map tile (layer) as a mapnik.Image."""
    console.debugMessage(' Rendering layer: ' + name)
    env = getMercTileEnv(z, x, y, ntiles, True)
    tilesize = getTileSize(ntiles, True)
    map.zoom_to_box(env)
    if useCairo and USE_CAIRO:
        assert mapnik.has_cairo()
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, tilesize, tilesize)
        mapnik.render(map, surface)
        image = mapnik.Image.from_cairo(surface)
    else:            
        image = mapnik.Image(tilesize, tilesize)
        mapnik.render(map, image)
    return image
Example #2
0
def renderLayer(name, z, x, y, ntiles, map, suffix='png', useCairo=False):
    """Renders the specified map tile (layer) as a mapnik.Image."""
    console.debugMessage(' Rendering layer: ' + name)
    env = getMercTileEnv(z, x, y, ntiles, True)
    tilesize = getTileSize(ntiles, True)
    map.zoom_to_box(env)
    if useCairo and USE_CAIRO:
        assert mapnik.has_cairo()
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, tilesize, tilesize)
        mapnik.render(map, surface)
        image = mapnik.Image.from_cairo(surface)
    else:
        image = mapnik.Image(tilesize, tilesize)
        mapnik.render(map, image)
    return image
Example #3
0
def test_render_points():

    if not mapnik2.has_cairo():
        return

    # create and populate point datasource (WGS84 lat-lon coordinates)
    places_ds = mapnik2.PointDatasource()
    places_ds.add_point(142.48, -38.38, "Name", "Westernmost Point")  # westernmost
    places_ds.add_point(143.10, -38.60, "Name", "Southernmost Point")  # southernmost
    # create layer/rule/style
    s = mapnik2.Style()
    r = mapnik2.Rule()
    symb = mapnik2.PointSymbolizer()
    symb.allow_overlap = True
    r.symbols.append(symb)
    s.rules.append(r)
    lyr = mapnik2.Layer("Places", "+proj=latlon +datum=WGS84")
    lyr.datasource = places_ds
    lyr.styles.append("places_labels")
    # latlon bounding box corners
    ul_lonlat = mapnik2.Coord(142.30, -38.20)
    lr_lonlat = mapnik2.Coord(143.40, -38.80)
    # render for different projections
    projs = {
        "latlon": "+proj=latlon +datum=WGS84",
        "merc": "+proj=merc +datum=WGS84 +k=1.0 +units=m +over +no_defs",
        "google": "+proj=merc +ellps=sphere +R=6378137 +a=6378137 +units=m",
        "utm": "+proj=utm +zone=54 +datum=WGS84",
    }
    for projdescr in projs.iterkeys():
        m = mapnik2.Map(1000, 500, projs[projdescr])
        m.append_style("places_labels", s)
        m.layers.append(lyr)
        p = mapnik2.Projection(projs[projdescr])
        m.zoom_to_box(p.forward(mapnik2.Box2d(ul_lonlat, lr_lonlat)))
        # Render to SVG so that it can be checked how many points are there with string comparison
        svg_file = "/tmp/%s.svg"
        mapnik2.render_to_file(m, svg_file)
        num_points_present = len(places_ds.all_features())
        svg = open(svg_file, "r").read()
        num_points_rendered = svg.count("<image ")
        eq_(
            num_points_present,
            num_points_rendered,
            "Not all points were rendered (%d instead of %d) at projection %s"
            % (num_points_rendered, num_points_present, projdescr),
        )
Example #4
0
def test_render_points():

    if not mapnik2.has_cairo(): return

    # create and populate point datasource (WGS84 lat-lon coordinates)
    places_ds = mapnik2.PointDatasource()
    places_ds.add_point(142.48, -38.38, 'Name',
                        'Westernmost Point')  # westernmost
    places_ds.add_point(143.10, -38.60, 'Name',
                        'Southernmost Point')  # southernmost
    # create layer/rule/style
    s = mapnik2.Style()
    r = mapnik2.Rule()
    symb = mapnik2.PointSymbolizer()
    symb.allow_overlap = True
    r.symbols.append(symb)
    s.rules.append(r)
    lyr = mapnik2.Layer('Places', '+proj=latlon +datum=WGS84')
    lyr.datasource = places_ds
    lyr.styles.append('places_labels')
    # latlon bounding box corners
    ul_lonlat = mapnik2.Coord(142.30, -38.20)
    lr_lonlat = mapnik2.Coord(143.40, -38.80)
    # render for different projections
    projs = {
        'latlon': '+proj=latlon +datum=WGS84',
        'merc': '+proj=merc +datum=WGS84 +k=1.0 +units=m +over +no_defs',
        'google': '+proj=merc +ellps=sphere +R=6378137 +a=6378137 +units=m',
        'utm': '+proj=utm +zone=54 +datum=WGS84'
    }
    for projdescr in projs.iterkeys():
        m = mapnik2.Map(1000, 500, projs[projdescr])
        m.append_style('places_labels', s)
        m.layers.append(lyr)
        p = mapnik2.Projection(projs[projdescr])
        m.zoom_to_box(p.forward(mapnik2.Box2d(ul_lonlat, lr_lonlat)))
        # Render to SVG so that it can be checked how many points are there with string comparison
        svg_file = '/tmp/%s.svg'
        mapnik2.render_to_file(m, svg_file)
        num_points_present = len(places_ds.all_features())
        svg = open(svg_file, 'r').read()
        num_points_rendered = svg.count('<image ')
        eq_(
            num_points_present, num_points_rendered,
            "Not all points were rendered (%d instead of %d) at projection %s"
            % (num_points_rendered, num_points_present, projdescr))
Example #5
0
def renderToPng(envLL, filename, sizex, sizey, base='hypsorelief'):
    """Renders the specified Box2d and zoom level as a PNG"""
    layers = [base, 'areas', 'ocean', 'contours', 'features']
    if base == 'landcoverrelief':
        layers.remove('areas')
    images = {}
    for mapname in layers:
        console.debugMessage(' Rendering layer: ' + mapname)
        map = mapnik.Map(sizex, sizey)
        mapnik.load_map(map, mapname + ".xml")
        map.zoom_to_box(LLToMerc(envLL))
        if USE_CAIRO and mapname in ['ocean', 'contours', 'features']:
            assert mapnik.has_cairo()
            surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, sizex, sizey)
            mapnik.render(map, surface)
            images[mapname] = mapnik.Image.from_cairo(surface)
        else:
            images[mapname] = mapnik.Image(sizex, sizey)
            mapnik.render(map, images[mapname])
    image = getComposite([images[m] for m in layers])
    image.save(filename, 'png')
Example #6
0
def renderLayer(name, z, x, y, ntiles, map, suffix='png'):
    """Renders the specified map tile (layer) as a mapnik.Image."""
    if name in CACHE_LAYERS and cachedMetaTileExists(name, z, x, y, 'png'):
        console.debugMessage(' Using cached:    ' + name)
        return mapnik.Image.open(getCachedMetaTilePath(name, z, x, y, 'png'))
    console.debugMessage(' Rendering layer: ' + name)
    env = getMercTileEnv(z, x, y, ntiles, True)
    tilesize = getTileSize(ntiles, True)
    map.zoom_to_box(env)
    if USE_CAIRO and name in CAIRO_LAYERS:
        assert mapnik.has_cairo()
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, tilesize, tilesize)
        mapnik.render(map, surface)
        image = mapnik.Image.from_cairo(surface)
    else:
        image = mapnik.Image(tilesize, tilesize)
        mapnik.render(map, image)
    if name in CACHE_LAYERS:
        ensureDirExists(getCachedMetaTileDir(name, z, x))
        image.save(getCachedMetaTilePath(name, z, x, y, 'png'))
    return image
Example #7
0
def renderToPng(envLL, filename, sizex, sizey, base='hypsorelief'):
    """Renders the specified Box2d and zoom level as a PNG"""
    layers = [base, 'areas', 'ocean', 'contours', 'features']
    if base == 'landcoverrelief':
        layers.remove('areas')
    images = {}
    for mapname in layers:
        console.debugMessage(' Rendering layer: ' + mapname)
        map = mapnik.Map(sizex, sizey)
        mapnik.load_map(map, mapname + ".xml")
        map.zoom_to_box(LLToMerc(envLL))
        if USE_CAIRO and mapname in ['ocean', 'contours', 'features']:
            assert mapnik.has_cairo()
            surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, sizex, sizey)
            mapnik.render(map, surface)
            images[mapname] = mapnik.Image.from_cairo(surface)
        else:
            images[mapname] = mapnik.Image(sizex, sizey)
            mapnik.render(map, images[mapname])
    image = getComposite([images[m] for m in layers])
    image.save(filename, 'png')
Example #8
0
def renderLayer(name, z, x, y, ntiles, map, suffix = 'png'):
    """Renders the specified map tile (layer) as a mapnik.Image."""
    if name in CACHE_LAYERS and cachedMetaTileExists(name, z, x, y, 'png'):
        console.debugMessage(' Using cached:    ' + name)
        return mapnik.Image.open(getCachedMetaTilePath(name, z, x, y, 'png'))
    console.debugMessage(' Rendering layer: ' + name)
    env = getMercTileEnv(z, x, y, ntiles, True)
    tilesize = getTileSize(ntiles, True)
    map.zoom_to_box(env)
    if USE_CAIRO and name in CAIRO_LAYERS:
        assert mapnik.has_cairo()
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, tilesize, tilesize)
        mapnik.render(map, surface)
        image = mapnik.Image.from_cairo(surface)
    else:            
        image = mapnik.Image(tilesize, tilesize)
        mapnik.render(map, image)
    if name in CACHE_LAYERS:
        ensureDirExists(getCachedMetaTileDir(name, z, x))
        image.save(getCachedMetaTilePath(name, z, x, y, 'png'))
    return image
Example #9
0
    svg_surface.finish()
    images_.append('demo.svg')

    pdf_surface = cairo.PDFSurface('demo.pdf', m.width,m.height)
    mapnik2.render(m, pdf_surface)
    images_.append('demo.pdf')
    pdf_surface.finish()

    postscript_surface = cairo.PSSurface('demo.ps', m.width,m.height)
    mapnik2.render(m, postscript_surface)
    images_.append('demo.ps')
    postscript_surface.finish()    

else:
    print '\n\nPycairo not available...',
    if  mapnik2.has_cairo():
        print ' will render Cairo formats using alternative method'
        
        mapnik2.render_to_file(m,'demo.pdf')
        images_.append('demo.pdf')
        mapnik2.render_to_file(m,'demo.ps')
        images_.append('demo.ps')
        mapnik2.render_to_file(m,'demo.svg')
        images_.append('demo.svg')
        mapnik2.render_to_file(m,'demo_cairo_rgb.png','RGB24')
        images_.append('demo_cairo_rgb.png')
        mapnik2.render_to_file(m,'demo_cairo_argb.png','ARGB32')
        images_.append('demo_cairo_argb.png')

print "\n\n", len(images_), "maps have been rendered in the current directory:"
    
Example #10
0
def toposmInfo():
    print "Using mapnik version:", mapnik.mapnik_version()
    print "Has Cairo:", mapnik.has_cairo()
    print "Fonts:"
    for face in mapnik.FontEngine.face_names():
        print "\t", face
Example #11
0
import areas
from JobManager import JobManager

__author__      = "Lars Ahlzen and contributors"
__copyright__   = "(c) Lars Ahlzen and contributors 2008-2011"
__license__     = "GPLv2"


##### Initialize Mapnik

# Import extra fonts
if EXTRA_FONTS_DIR != '':
    mapnik.register_fonts(EXTRA_FONTS_DIR)

# Check for cairo support
if not mapnik.has_cairo():
    print "ERROR: Your mapnik does not have Cairo support."
    sys.exit(1)


##### Render settings

# Set to true to save intermediate layers that are normally
# merged. Primarily useful for debugging and style editing.
SAVE_INTERMEDIATE_TILES = True

# If true, a composite JPEG layer is saved along with
# the standard PNG.
SAVE_JPEG_COMPOSITE = True
JPEG_COMPOSITE_QUALITY = 90
Example #12
0
def toposmInfo():
    print "Using mapnik version:", mapnik.mapnik_version()
    print "Has Cairo:", mapnik.has_cairo()
    print "Fonts:"
    for face in mapnik.FontEngine.face_names():
        print "\t", face
Example #13
0
import NED
import areas
from JobManager import JobManager

__author__ = "Lars Ahlzen and contributors"
__copyright__ = "(c) Lars Ahlzen and contributors 2008-2011"
__license__ = "GPLv2"

##### Initialize Mapnik

# Import extra fonts
if EXTRA_FONTS_DIR != '':
    mapnik.register_fonts(EXTRA_FONTS_DIR)

# Check for cairo support
if not mapnik.has_cairo():
    print "ERROR: Your mapnik does not have Cairo support."
    sys.exit(1)

##### Render settings

# Set to true to save intermediate layers that are normally
# merged. Primarily useful for debugging and style editing.
SAVE_INTERMEDIATE_TILES = True

# If true, a composite JPEG layer is saved along with
# the standard PNG.
SAVE_JPEG_COMPOSITE = True
JPEG_COMPOSITE_QUALITY = 90

# Enable/disable the use of the cairo renderer altogether
Example #14
0
    svg_surface.finish()
    images_.append('demo.svg')

    pdf_surface = cairo.PDFSurface('demo.pdf', m.width, m.height)
    mapnik2.render(m, pdf_surface)
    images_.append('demo.pdf')
    pdf_surface.finish()

    postscript_surface = cairo.PSSurface('demo.ps', m.width, m.height)
    mapnik2.render(m, postscript_surface)
    images_.append('demo.ps')
    postscript_surface.finish()

else:
    print '\n\nPycairo not available...',
    if mapnik2.has_cairo():
        print ' will render Cairo formats using alternative method'

        mapnik2.render_to_file(m, 'demo.pdf')
        images_.append('demo.pdf')
        mapnik2.render_to_file(m, 'demo.ps')
        images_.append('demo.ps')
        mapnik2.render_to_file(m, 'demo.svg')
        images_.append('demo.svg')
        mapnik2.render_to_file(m, 'demo_cairo_rgb.png', 'RGB24')
        images_.append('demo_cairo_rgb.png')
        mapnik2.render_to_file(m, 'demo_cairo_argb.png', 'ARGB32')
        images_.append('demo_cairo_argb.png')

print "\n\n", len(images_), "maps have been rendered in the current directory:"