def test_render_points(): if not mapnik.has_cairo(): return # create and populate point datasource (WGS84 lat-lon coordinates) ds = mapnik.MemoryDatasource() context = mapnik.Context() context.push('Name') f = mapnik.Feature(context, 1) f['Name'] = 'Westernmost Point' f.geometry = mapnik.Geometry.from_wkt('POINT (142.48 -38.38)') ds.add_feature(f) f = mapnik.Feature(context, 2) f['Name'] = 'Southernmost Point' f.geometry = mapnik.Geometry.from_wkt('POINT (143.10 -38.60)') ds.add_feature(f) # create layer/rule/style s = mapnik.Style() r = mapnik.Rule() symb = mapnik.PointSymbolizer() symb.allow_overlap = True r.symbols.append(symb) s.rules.append(r) lyr = mapnik.Layer('Places', '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs') lyr.datasource = ds lyr.styles.append('places_labels') # latlon bounding box corners ul_lonlat = mapnik.Coord(142.30, -38.20) lr_lonlat = mapnik.Coord(143.40, -38.80) # render for different projections projs = { 'google': '+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over', 'latlon': '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs', 'merc': '+proj=merc +datum=WGS84 +k=1.0 +units=m +over +no_defs', 'utm': '+proj=utm +zone=54 +datum=WGS84' } for projdescr in projs: m = mapnik.Map(1000, 500, projs[projdescr]) m.append_style('places_labels', s) m.layers.append(lyr) dest_proj = mapnik.Projection(projs[projdescr]) src_proj = mapnik.Projection('+init=epsg:4326') tr = mapnik.ProjTransform(src_proj, dest_proj) m.zoom_to_box(tr.forward(mapnik.Box2d(ul_lonlat, lr_lonlat))) # Render to SVG so that it can be checked how many points are there # with string comparison svg_file = os.path.join(tempfile.gettempdir(), 'mapnik-render-points-%s.svg' % projdescr) mapnik.render_to_file(m, svg_file) num_points_present = len(list(ds.all_features())) with open(svg_file, 'r') as f: svg = f.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))
def test_render_points(): if not mapnik.has_cairo(): return # create and populate point datasource (WGS84 lat-lon coordinates) ds = mapnik.MemoryDatasource() context = mapnik.Context() context.push('Name') f = mapnik.Feature(context, 1) f['Name'] = 'Westernmost Point' f.add_geometries_from_wkt('POINT (142.48 -38.38)') ds.add_feature(f) f = mapnik.Feature(context, 2) f['Name'] = 'Southernmost Point' f.add_geometries_from_wkt('POINT (143.10 -38.60)') ds.add_feature(f) # create layer/rule/style s = mapnik.Style() r = mapnik.Rule() symb = mapnik.PointSymbolizer() symb.allow_overlap = True r.symbols.append(symb) s.rules.append(r) lyr = mapnik.Layer('Places', '+proj=latlon +datum=WGS84') lyr.datasource = ds lyr.styles.append('places_labels') # latlon bounding box corners ul_lonlat = mapnik.Coord(142.30, -38.20) lr_lonlat = mapnik.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 = mapnik.Map(1000, 500, projs[projdescr]) m.append_style('places_labels', s) m.layers.append(lyr) p = mapnik.Projection(projs[projdescr]) m.zoom_to_box(p.forward(mapnik.Box2d(ul_lonlat, lr_lonlat))) # Render to SVG so that it can be checked how many points are there with string comparison svg_file = os.path.join(tempfile.gettempdir(), '%s.svg') mapnik.render_to_file(m, svg_file) num_points_present = len(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))
def test_render_points(): if not mapnik.has_cairo(): return # create and populate point datasource (WGS84 lat-lon coordinates) ds = mapnik.MemoryDatasource() context = mapnik.Context() context.push('Name') f = mapnik.Feature(context,1) f['Name'] = 'Westernmost Point' f.add_geometries_from_wkt('POINT (142.48 -38.38)') ds.add_feature(f) f = mapnik.Feature(context,2) f['Name'] = 'Southernmost Point' f.add_geometries_from_wkt('POINT (143.10 -38.60)') ds.add_feature(f) # create layer/rule/style s = mapnik.Style() r = mapnik.Rule() symb = mapnik.PointSymbolizer() symb.allow_overlap = True r.symbols.append(symb) s.rules.append(r) lyr = mapnik.Layer('Places','+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs') lyr.datasource = ds lyr.styles.append('places_labels') # latlon bounding box corners ul_lonlat = mapnik.Coord(142.30,-38.20) lr_lonlat = mapnik.Coord(143.40,-38.80) # render for different projections projs = { 'google': '+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0.0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs +over', 'latlon': '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs', 'merc': '+proj=merc +datum=WGS84 +k=1.0 +units=m +over +no_defs', 'utm': '+proj=utm +zone=54 +datum=WGS84' } for projdescr in projs.iterkeys(): m = mapnik.Map(1000, 500, projs[projdescr]) m.append_style('places_labels',s) m.layers.append(lyr) dest_proj = mapnik.Projection(projs[projdescr]) src_proj = mapnik.Projection('+init=epsg:4326') tr = mapnik.ProjTransform(src_proj,dest_proj) m.zoom_to_box(tr.forward(mapnik.Box2d(ul_lonlat,lr_lonlat))) # Render to SVG so that it can be checked how many points are there with string comparison svg_file = os.path.join(tempfile.gettempdir(), 'mapnik-render-points-%s.svg' % projdescr) mapnik.render_to_file(m, svg_file) num_points_present = len(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))
def renderLayerMerc(name, env, xsize, ysize, map): """Renders the specified layer to an image. ENV must be in spherical mercator projection.""" map.zoom_to_box(env) if USE_CAIRO and name in CAIRO_LAYERS: assert mapnik.has_cairo() surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, xsize, ysize) mapnik.render(map, surface) image = mapnik.Image.from_cairo(surface) else: image = mapnik.Image(xsize, ysize) mapnik.render(map, image) return image
def test_render_points(): if not mapnik.has_cairo(): return # create and populate point datasource (WGS84 lat-lon coordinates) places_ds = mapnik.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 = mapnik.Style() r = mapnik.Rule() symb = mapnik.PointSymbolizer() symb.allow_overlap = True r.symbols.append(symb) s.rules.append(r) lyr = mapnik.Layer('Places','+proj=latlon +datum=WGS84') lyr.datasource = places_ds lyr.styles.append('places_labels') # latlon bounding box corners ul_lonlat = mapnik.Coord(142.30,-38.20) lr_lonlat = mapnik.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 = mapnik.Map(1000, 500, projs[projdescr]) m.append_style('places_labels',s) m.layers.append(lyr) p = mapnik.Projection(projs[projdescr]) m.zoom_to_box(p.forward(mapnik.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' mapnik.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))
def _render_surface(self, map, map_size): if self.format.startswith('image/'): # AGG f = str(self.format.split('/')[1].lower()) im = mapnik.Image(*map_size) mapnik.render(map, im) return im.tostring(f) elif self.format in ('application/pdf', 'application/postscript', 'text/svg+xml'): # CAIRO try: # first, check if Cairo is available import cairo # newer Mapniks have better ways of checking whether they were actually # compiled with Cairo support if hasattr(mapnik, 'has_cairo') and (not mapnik.has_cairo()): raise ImportError if hasattr(mapnik, 'has_pycairo') and (not mapnik.has_pycairo()): raise ImportError except ImportError: raise ImportError( "Mapnik wasn't compiled with Cairo support, or Cairo isn't installed." ) surface_file = StringIO() if (self.format == 'application/pdf'): surface = cairo.PDFSurface(surface_file, *map_size) elif (self.format == 'application/postscript'): surface = cairo.PSSurface(surface_file, *map_size) elif (self.format == 'text/svg+xml'): surface = cairo.SVGSurface(surface_file, *map_size) mapnik.render(map, surface) surface.finish() return surface_file.getvalue() else: raise ValueError("Unknown format: %s" % self.format)
def _render_surface(self, map, map_size): if self.format.startswith('image/'): # AGG f = str(self.format.split('/')[1].lower()) im = mapnik.Image(*map_size) mapnik.render(map, im) return im.tostring(f) elif self.format in ('application/pdf', 'application/postscript', 'text/svg+xml'): # CAIRO try: # first, check if Cairo is available import cairo # newer Mapniks have better ways of checking whether they were actually # compiled with Cairo support if hasattr(mapnik, 'has_cairo') and (not mapnik.has_cairo()): raise ImportError if hasattr(mapnik, 'has_pycairo') and (not mapnik.has_pycairo()): raise ImportError except ImportError: raise ImportError("Mapnik wasn't compiled with Cairo support, or Cairo isn't installed.") surface_file = StringIO() if (self.format == 'application/pdf'): surface = cairo.PDFSurface(surface_file, *map_size) elif (self.format == 'application/postscript'): surface = cairo.PSSurface(surface_file, *map_size) elif (self.format == 'text/svg+xml'): surface = cairo.SVGSurface(surface_file, *map_size) mapnik.render(map, surface) surface.finish() return surface_file.getvalue() else: raise ValueError("Unknown format: %s" % self.format)
im.save('demo.tif', 'tiff') images_.append('demo.tif') if mapnik.has_webp(): im.save('demo.webp', 'webp') # default quality is 90 images_.append('demo.webp') im.save('demo_highest.webp', 'webp:quality=100') images_.append('demo_med.webp') im.save('demo_low.webp', 'webp:quality=50') images_.append('demo_low.webp') # Render cairo examples if mapnik.has_cairo(): mapnik.render_to_file(m,'demo.pdf') images_.append('demo.pdf') mapnik.render_to_file(m,'demo.ps') images_.append('demo.ps') mapnik.render_to_file(m,'demo.svg') images_.append('demo.svg') mapnik.render_to_file(m,'demo_cairo_rgb24.png','RGB24') images_.append('demo_cairo_rgb.png') mapnik.render_to_file(m,'demo_cairo_argb32.png','ARGB32') images_.append('demo_cairo_argb.png') print ("\n\n", len(images_), "maps have been rendered in the current directory:") for im_ in images_: print ("-", im_)
import sys import os.path from compare import compare, compare_grids try: import json except ImportError: import simplejson as json visual_output_dir = "/tmp/mapnik-visual-images" defaults = { 'sizes': [(500, 100)], 'scales':[1.0,2.0], 'agg': True, 'cairo': mapnik.has_cairo(), 'grid': mapnik.has_grid_renderer() } cairo_threshold = 10 if 'Linux' == os.uname()[0]: # we assume if linux then you are running packaged cairo # which is older than the 1.12.14 version we used on OS X # to generate the expected images, so we'll rachet back the threshold # https://github.com/mapnik/mapnik/issues/1868 cairo_threshold = 181 def render_cairo(m, output, scale_factor): mapnik.render_to_file(m, output, 'ARGB32', scale_factor) # open and re-save as png8 to save space new_im = mapnik.Image.open(output)
image_surface = cairo.ImageSurface(cairo.FORMAT_RGB24, m.width, m.height) mapnik.render(m, image_surface) image_surface.write_to_png('demo_cairo_rgb24.png') images_.append('demo_cairo_argb24.png') image_surface.finish() image_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, m.width, m.height) mapnik.render(m, image_surface) image_surface.write_to_png('demo_cairo_argb32.png') images_.append('demo_cairo_argb32.png') image_surface.finish() else: print '\n\nPycairo not available...', if mapnik.has_cairo(): print ' will render Cairo formats using alternative method' mapnik.render_to_file(m, 'demo.pdf') images_.append('demo.pdf') mapnik.render_to_file(m, 'demo.ps') images_.append('demo.ps') mapnik.render_to_file(m, 'demo.svg') images_.append('demo.svg') mapnik.render_to_file(m, 'demo_cairo_rgb24.png', 'RGB24') images_.append('demo_cairo_rgb.png') mapnik.render_to_file(m, 'demo_cairo_argb32.png', 'ARGB32') images_.append('demo_cairo_argb.png') print "\n\n", len(images_), "maps have been rendered in the current directory:"
import sys import os.path from compare import compare, compare_grids try: import json except ImportError: import simplejson as json visual_output_dir = "/tmp/mapnik-visual-images" defaults = { 'sizes': [(500, 100)], 'scales':[1.0,2.0], 'agg': False, 'cairo': mapnik.has_cairo(), 'grid': True, } sizes_many_in_big_range = [(800, 100), (600, 100), (400, 100), (300, 100), (250, 100), (150, 100), (100, 100)] sizes_few_square = [(800, 800), (600, 600), (400, 400), (200, 200)] sizes_many_in_small_range = [(490, 100), (495, 100), (497, 100), (498, 100), (499, 100), (500, 100), (501, 100), (502, 100), (505, 100), (510, 100)] default_text_box = mapnik.Box2d(-0.05, -0.01, 0.95, 0.01) dirname = os.path.dirname(__file__) files = [
import mapnik mapnik.logger.set_severity(mapnik.severity_type.None) import sys import os.path from compare import compare, compare_grids try: import json except ImportError: import simplejson as json visual_output_dir = "/tmp/mapnik-visual-images" defaults = {"sizes": [(500, 100)], "scales": [1.0, 2.0], "agg": False, "cairo": mapnik.has_cairo(), "grid": True} sizes_many_in_big_range = [(800, 100), (600, 100), (400, 100), (300, 100), (250, 100), (150, 100), (100, 100)] sizes_few_square = [(800, 800), (600, 600), (400, 400), (200, 200)] sizes_many_in_small_range = [ (490, 100), (495, 100), (497, 100), (498, 100), (499, 100), (500, 100), (501, 100), (502, 100), (505, 100), (510, 100),
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
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 = False # Enables/disables saving the composite layers SAVE_PNG_COMPOSITE = True SAVE_JPEG_COMPOSITE = True JPEG_COMPOSITE_QUALITY = 90 # Enable/disable the use of the cairo renderer altogether
import sys import os.path from compare import compare, compare_grids try: import json except ImportError: import simplejson as json visual_output_dir = "/tmp/mapnik-visual-images" defaults = { "sizes": [(500, 100)], "scales": [1.0, 2.0], "agg": True, "cairo": mapnik.has_cairo(), "grid": mapnik.has_grid_renderer(), } cairo_threshold = 10 agg_threshold = 0 if "Linux" == os.uname()[0]: # we assume if linux then you are running packaged cairo # which is older than the 1.12.14 version we used on OS X # to generate the expected images, so we'll rachet back the threshold # https://github.com/mapnik/mapnik/issues/1868 cairo_threshold = 230 agg_threshold = 12 def render_cairo(m, output, scale_factor):