def test_good_files(): good_files = glob.glob("../data/good_maps/*.xml") good_files.extend(glob.glob("../visual_tests/styles/*.xml")) failures = [] for ds_init in [ mapnik.threading_mode.deferred, mapnik.threading_mode.asynchronous ]: for filename in good_files: try: m = mapnik.Map(512, 512) strict = True mapnik.load_map(m, filename, strict, datasource_init=ds_init) base_path = os.path.dirname(filename) with open(filename, 'rb') as f: m = mapnik.Map(512, 512) mapnik.load_map_from_string(m, f.read(), strict, base_path, datasource_init=ds_init) except RuntimeError as e: # only test datasources that we have installed if not 'Could not create datasource' in str(e) \ and not 'could not connect' in str(e): failures.append('Failed to load valid map %s (%s)' % (filename, e)) eq_(len(failures), 0, '\n' + '\n'.join(failures))
def test_arbitrary_parameters_attached_to_map(): m = mapnik.Map(256, 256) mapnik.load_map(m, '../data/good_maps/extra_arbitary_map_parameters.xml') eq_(len(m.parameters), 5) eq_(m.parameters['key'], 'value2') eq_(m.parameters['key3'], 'value3') eq_(m.parameters['unicode'], u'iván') eq_(m.parameters['integer'], 10) eq_(m.parameters['decimal'], .999) m2 = mapnik.Map(256, 256) for k, v in m.parameters: m2.parameters.append(mapnik.Parameter(k, v)) eq_(len(m2.parameters), 5) eq_(m2.parameters['key'], 'value2') eq_(m2.parameters['key3'], 'value3') eq_(m2.parameters['unicode'], u'iván') eq_(m2.parameters['integer'], 10) eq_(m2.parameters['decimal'], .999) map_string = mapnik.save_map_to_string(m) m3 = mapnik.Map(256, 256) mapnik.load_map_from_string(m3, map_string) eq_(len(m3.parameters), 5) eq_(m3.parameters['key'], 'value2') eq_(m3.parameters['key3'], 'value3') eq_(m3.parameters['unicode'], u'iván') eq_(m3.parameters['integer'], 10) eq_(m3.parameters['decimal'], .999)
def test_can_parse_xml_with_deprecated_properties(): default_logging_severity = mapnik.logger.get_severity() mapnik.logger.set_severity(getattr(mapnik.severity_type, "None")) files_with_deprecated_props = glob.glob("../data/deprecated_maps/*.xml") failures = [] for filename in files_with_deprecated_props: try: m = mapnik.Map(512, 512) strict = True mapnik.load_map(m, filename, strict) base_path = os.path.dirname(filename) mapnik.load_map_from_string( m, open( filename, 'rb').read(), strict, base_path) except RuntimeError as e: # only test datasources that we have installed if not 'Could not create datasource' in str(e) \ and not 'could not connect' in str(e): failures.append( 'Failed to load valid map %s (%s)' % (filename, e)) eq_(len(failures), 0, '\n' + '\n'.join(failures)) mapnik.logger.set_severity(default_logging_severity)
def test_create_mvt_merc_style_level_filter_1(): style = """ <Map srs="+init=epsg:3857"> <Layer name="polygon" srs="+init=epsg:4326"> <Datasource> <Parameter name="type">geojson</Parameter> <Parameter name="inline"> {"type":"Polygon","coordinates":[[ [ 10, 10], [-10, 10], [-10, -10], [ 10, -10], [ 10, 10] ]]} </Parameter> </Datasource> </Layer> </Map> """ mapnikMap = mapnik.Map(256, 256) mapnik.load_map_from_string(mapnikMap, style) wafer = mapnik.create_mvt_wafer_merc(mapnikMap, 0, 0, 3, 8) eq_(len(wafer), 64) tile_buffer = wafer[3 * 8 + 3] tile = mapnik.VectorTileInfo() tile.parse_from_string(tile_buffer); eq_(tile.layers_size(), 1); eq_(tile.layers(0).features_size(), 1)
def _add_legend(self, name, place, datetime, attributes): features = [] box = self._map.envelope() # add name, place and date point = Point((box.minx, box.maxy)) features.append(Feature(geometry=point, properties={ 'name': name, 'place': place, 'date': datetime#strftime('%d.%m.%Y %H:%M') })) # add properties if (attributes and len(attributes) > 0): cell_size = ((box.maxy - box.miny) / 11.) offset = cell_size / 3 x = box.minx + offset y = box.miny + offset for i, (k, v) in enumerate(attributes): point = Point((x, y+offset*i)) properties = {'key': k, 'value': v} features.append(Feature(geometry=point, properties=properties)) # add osm copyright properties = { 'type': 'copyright', 'text': 'Tiles © OpenStreetMap contributers, CC-BY-SA' } point = Point((box.maxx, box.miny)) features.append(Feature(geometry=point, properties=properties)) # render them collection = json.dumps(FeatureCollection(features)) xml_str = get_xml("styles/legend.xml").format(collection).encode() load_map_from_string(self._map, xml_str)
def test_preview_mvt_custom_style(): style = """ <Map srs="+init=epsg:3857" background-color="white"> <Style name="preview"> <Rule> <LineSymbolizer stroke="black" stroke-width="2" /> </Rule> </Style> <Layer name="preview"> <StyleName>preview</StyleName> </Layer> </Map> """ mvt = mapnik.VectorTileMerc(28, 12, 5) with open('data/tile3.mvt', 'rb') as f: buf = f.read() eq_(isinstance(buf, bytes), True) mapnik.merge_compressed_buffer(mvt, buf) mapnikMap = mapnik.Map(256, 256) mapnik.load_map_from_string(mapnikMap, style) im = mapnik.Image(4096, 4096) mapnik.preview_mvt_merc(mvt, mapnikMap, im) expected = 'images/mvt/tile3.preview.custom.expected.png' actual = 'images/mvt/tile3.preview.custom.actual.png' im.save(actual, 'png32') eq_(compare_file_size(actual, expected, 100), True)
def loadXML(self, xmlfile=None, strict=False, xmlstring='', basepath=''): """Loads and stores a complete map definition in leiu of a set of layers. Stores an empty style of the same name, which is the only option for rendering. The map will be rendered as prescribed in the XML.""" config = ConfigParser.SafeConfigParser() map_wms_srs = None if self.configpath: config.readfp(open(self.configpath)) if config.has_option('map', 'wms_srs'): map_wms_srs = config.get('map', 'wms_srs') tmp_map = Map(0,0) if xmlfile: load_map(tmp_map, xmlfile, strict) elif xmlstring: load_map_from_string(tmp_map, xmlstring, strict, basepath) else: raise ServerConfigurationError("Mapnik configuration XML is not specified - 'xmlfile' and 'xmlstring' variables are empty.\ Please set one of this variables to load mapnik map object.") # parse map level attributes if tmp_map.background: self.map_attributes['bgcolor'] = tmp_map.background if tmp_map.buffer_size: self.map_attributes['buffer_size'] = tmp_map.buffer_size if xmlfile is None : # Map objects have no name, so just call it default. layer_name = 'default' else : # The layer name is the basename of the xml file or the # whole file name layer_name = xmlfile fname_match = re.match('([A-Za-z0-9_\-\.]+).xml', xmlfile) if fname_match is not None : layer_name = fname_match.group(1) else : layer_name = xmlfile style_name = layer_name style_obj = Style() # Make the map have attributes expected of layers tmp_map.name = layer_name map_p = common.Projection(tmp_map.srs) if map_wms_srs is None : tmp_map.wms_srs = map_p.epsgstring() else : tmp_map.wms_srs = map_wms_srs tmp_map.queryable = False # set map extent from config file. geog_coords = config.get('map','wms_extent').split(',') geog_box = Box2d(float(geog_coords[0]), float(geog_coords[1]), float(geog_coords[2]), float(geog_coords[3])) proj_box = geog_box.forward(map_p) tmp_map.zoom_to_box(proj_box) tmp_map.maximum_extent = proj_box self.register_style(style_name, style_obj) self.register_layer(tmp_map, style_name)
def serialize(xml, options): try: import mapnik except: sys.exit( color_text( 1, 'Error: saving xml requires Mapnik python bindings to be installed' )) m = mapnik.Map(1, 1) if options.from_string: mapnik.load_map_from_string(m, xml, True) else: mapnik.load_map(m, xml, True) if options.output: mapnik.save_map(m, options.output) else: if hasattr(mapnik, 'mapnik_version') and mapnik.mapnik_version() >= 700: print mapnik.save_map_to_string(m) else: sys.exit( color_text( 1, 'Minor error: printing XML to stdout requires Mapnik >=0.7.0, please provide a second argument to save the output to a file' ))
def test_arbitrary_parameters_attached_to_map(): m = mapnik.Map(256, 256) mapnik.load_map(m, "../data/good_maps/extra_arbitary_map_parameters.xml") eq_(len(m.parameters), 5) eq_(m.parameters["key"], "value2") eq_(m.parameters["key3"], "value3") eq_(m.parameters["unicode"], u"iván") eq_(m.parameters["integer"], 10) eq_(m.parameters["decimal"], 0.999) m2 = mapnik.Map(256, 256) for k, v in m.parameters: m2.parameters.append(mapnik.Parameter(k, v)) eq_(len(m2.parameters), 5) eq_(m2.parameters["key"], "value2") eq_(m2.parameters["key3"], "value3") eq_(m2.parameters["unicode"], u"iván") eq_(m2.parameters["integer"], 10) eq_(m2.parameters["decimal"], 0.999) map_string = mapnik.save_map_to_string(m) m3 = mapnik.Map(256, 256) mapnik.load_map_from_string(m3, map_string) eq_(len(m3.parameters), 5) eq_(m3.parameters["key"], "value2") eq_(m3.parameters["key3"], "value3") eq_(m3.parameters["unicode"], u"iván") eq_(m3.parameters["integer"], 10) eq_(m3.parameters["decimal"], 0.999)
def test_arbitrary_parameters_attached_to_map(): m = mapnik.Map(256,256) mapnik.load_map(m,'../data/good_maps/extra_arbitary_map_parameters.xml') eq_(len(m.parameters),5) eq_(m.parameters['key'],'value2') eq_(m.parameters['key3'],'value3') eq_(m.parameters['unicode'],u'iván') eq_(m.parameters['integer'],10) eq_(m.parameters['decimal'],.999) m2 = mapnik.Map(256,256) for k,v in m.parameters: m2.parameters.append(mapnik.Parameter(k,v)) eq_(len(m2.parameters),5) eq_(m2.parameters['key'],'value2') eq_(m2.parameters['key3'],'value3') eq_(m2.parameters['unicode'],u'iván') eq_(m2.parameters['integer'],10) eq_(m2.parameters['decimal'],.999) map_string = mapnik.save_map_to_string(m) m3 = mapnik.Map(256,256) mapnik.load_map_from_string(m3,map_string) eq_(len(m3.parameters),5) eq_(m3.parameters['key'],'value2') eq_(m3.parameters['key3'],'value3') eq_(m3.parameters['unicode'],u'iván') eq_(m3.parameters['integer'],10) eq_(m3.parameters['decimal'],.999)
def test_adding_datasource_to_layer(): map_string = """<?xml version="1.0" encoding="utf-8"?> <Map> <Layer name="world_borders"> <StyleName>world_borders_style</StyleName> <StyleName>point_style</StyleName> <!-- leave datasource empty --> <!-- <Datasource> <Parameter name="file">../data/shp/world_merc.shp</Parameter> <Parameter name="type">shape</Parameter> </Datasource> --> </Layer> </Map> """ m = mapnik.Map(256, 256) try: mapnik.load_map_from_string(m, map_string) # validate it loaded fine eq_(m.layers[0].styles[0], "world_borders_style") eq_(m.layers[0].styles[1], "point_style") eq_(len(m.layers), 1) # also assign a variable reference to that layer # below we will test that this variable references # the same object that is attached to the map lyr = m.layers[0] # ensure that there was no datasource for the layer... eq_(m.layers[0].datasource, None) eq_(lyr.datasource, None) # also note that since the srs was black it defaulted to wgs84 eq_(m.layers[0].srs, "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs") eq_(lyr.srs, "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs") # now add a datasource one... ds = mapnik.Shapefile(file="../data/shp/world_merc.shp") m.layers[0].datasource = ds # now ensure it is attached eq_(m.layers[0].datasource.describe()["name"], "shape") eq_(lyr.datasource.describe()["name"], "shape") # and since we have now added a shapefile in spherical mercator, adjust the projection lyr.srs = "+proj=merc +lon_0=0 +lat_ts=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs" # test that assignment eq_(m.layers[0].srs, "+proj=merc +lon_0=0 +lat_ts=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs") eq_(lyr.srs, "+proj=merc +lon_0=0 +lat_ts=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs") except RuntimeError, e: # only test datasources that we have installed if not "Could not create datasource" in str(e): raise RuntimeError(e)
def setMapXML_(self, xml): map = mapnik.Map(256,256) mapnik.load_map_from_string(map, xml) self.projectionString = map.srs self.projection = mapnik.Projection(self.projectionString) self.cache = dict() self.render_thread.loadMapString_(xml)
def test_serializing_arbitrary_parameters(): m = mapnik.Map(256,256) m.parameters.append(mapnik.Parameter('width',m.width)) m.parameters.append(mapnik.Parameter('height',m.height)) m2 = mapnik.Map(1,1) mapnik.load_map_from_string(m2,mapnik.save_map_to_string(m)) eq_(m2.parameters['width'],m.width) eq_(m2.parameters['height'],m.height)
def loadMapString_(self, xml): self.cancelTiles() self.map_lock.lock() self.map = mapnik.Map(256,256) self.map.buffer_size = 128 mapnik.load_map_from_string(self.map, xml) self.prj = mapnik.Projection(self.map.srs) self.map_lock.unlock()
def test_serializing_arbitrary_parameters(): m = mapnik.Map(256, 256) m.parameters.append(mapnik.Parameter('width', m.width)) m.parameters.append(mapnik.Parameter('height', m.height)) m2 = mapnik.Map(1, 1) mapnik.load_map_from_string(m2, mapnik.save_map_to_string(m)) eq_(m2.parameters['width'], m.width) eq_(m2.parameters['height'], m.height)
def _add_features(self, features): # add all features (as features are rendered on top of each other in the # order we provide it to mapnik, make sure markers are on top) types = ['Polygon', 'LineString', 'Point'] getter = lambda x: types.index(x['geometry']['type']) entries = sorted([strip(f) for f in features], key=getter) collection = json.dumps(FeatureCollection(entries)) xml_str = get_xml("styles/features.xml").format(collection).encode() load_map_from_string(self._map, xml_str)
def render_map(_map, mimetype='application/pdf'): 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' ) longlat = mapnik.Projection( '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs') transform = mapnik.ProjTransform(longlat, merc) bbox = mapnik.Box2d(*_map.bbox) merc_bbox = transform.forward(bbox) mapnik_map = mapnik.Map(int(merc_bbox.width()), int(merc_bbox.height())) mapnik_map.zoom_to_box(merc_bbox) mapnik_map.buffer_size = 5 # add osm data mapnik.load_map(mapnik_map, current_app.config['MAPNIK_OSM_XML']) # add grid data = _map.grid path = os.path.join(SRC_PATH, "maps-xml/grid.xml") xml_str = get_xml(path).format(json.dumps(data)).encode() mapnik.load_map_from_string(mapnik_map, xml_str) # add all features features = FeatureCollection([f.to_dict() for f in _map.features]) path = os.path.join(SRC_PATH, "maps-xml/features.xml") xml_str = get_xml(path).format(json.dumps(features)).encode() mapnik.load_map_from_string(mapnik_map, xml_str) # add legend add_legend(mapnik_map, _map.legend) # export as in-memory file f = io.BytesIO() if mimetype == 'image/svg+xml': surface = cairo.SVGSurface(f, mapnik_map.width, mapnik_map.height) elif mimetype == 'image/png': ratio = float(mapnik_map.height) / mapnik_map.width mapnik.height = 800 mapnik.width = int(600 * ratio) surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, mapnik_map.width, mapnik_map.height) else: surface = cairo.PDFSurface(f, mapnik_map.width, mapnik_map.height) mapnik.render(mapnik_map, surface) if mimetype == 'image/png': surface.write_to_png(f) surface.finish() f.seek(0) return f
def assert_loads_successfully(file): m = mapnik.Map(512, 512) strict = True mapnik.load_map(m, file, strict) # libxml2 is not smart about paths, and clips the last directory off # of a path if it does not end in a trailing slash base_path = os.path.dirname(file) + '/' mapnik.load_map_from_string(m,open(file,'rb').read(),strict,base_path)
def render_tile(self) -> bytes: """ generate tile or load it from redis cache :return: tile as png image in bytes format """ tile_name: str = '/tmp/{}-{}-{}-{}.png'.format( self.request_date_to_string(), self.zoom, self.x_pixel, self.y_pixel) if env.bool("CACHE", default=False): # try if exists to get cache style_xml try: tile_content = cache.get(tile_name) if tile_content: return tile_content except (redis.exceptions.ConnectionError, redis.exceptions.DataError) as exc: print(exc) os.chdir(os.environ['STYLE_PATH']) map: mapnik.Map = mapnik.Map(self.width, self.height) mapnik.load_map_from_string(map, self.generate_date_style_xml()) prj: mapnik.Projection = 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 +wktext +no_defs +over" ) p0 = self.from_px_to_ll( (self.width * self.x_pixel, self.height * (self.y_pixel + 1)), self.zoom) p1 = self.from_px_to_ll( (self.width * (self.x_pixel + 1), self.height * self.y_pixel), self.zoom) c0 = prj.forward(mapnik.Coord(p0[0], p0[1])) c1 = prj.forward(mapnik.Coord(p1[0], p1[1])) bbox: mapnik.Envelope = mapnik.Envelope(c0.x, c0.y, c1.x, c1.y) map.zoom_to_box(bbox) image: mapnik.Image = mapnik.Image(self.width, self.height) mapnik.render(map, image) # todo generate tile without save it to hdd mapnik.render_to_file(map, tile_name) tile_content: bytes = open(tile_name, 'rb').read() os.remove(tile_name) if env.bool("CACHE", default=False): # upload style_xml file to redis cache cache.set(tile_name, tile_content, ex=env.int("CAHCE_EXPIRE_TIME", default=3600)) return tile_content
def create_thumbnail(xmlmap, filepath): shape = (230, 200) m = mapnik.Map(*shape) mapnik.load_map_from_string(m, xmlmap) box = m.layers[0].envelope() prj = mapnik.Projection(m.srs) prj_box = box.forward(prj) m.zoom_to_box(prj_box) im = mapnik.Image(*shape) mapnik.render(m, im) im.save(filepath, 'png256')
def render_map(map_obj, out_fname, out_format='png', scale=1.0, debug=False): import mapnik if debug: map_obj.write_xml("a.xml") out_dir = os.path.dirname(out_fname) if not os.path.exists(out_dir): os.makedirs(out_dir) m = mapnik.Map(map_obj.width, map_obj.height, map_obj.proj) mapnik.load_map_from_string(m, ET.tostring(map_obj.to_xml())) m.zoom_all() mapnik.render_to_file(m, out_fname, out_format, scale)
def main(imagefilename, xmlfilename): m = mapnik.Map(image_width, image_height) m.srs = shapefile_projection m.background = mapnik.Color('lightgrey') style, envelope = build_stylesheet(shapefile, shapefile_projection, AGS_prefixes) mapnik.load_map_from_string(m, style) m.zoom_to_box(envelope) mapnik.render_to_file(m, imagefilename) if xmlfilename: mapnik.save_map(m, xmlfilename)
def render_png(tile, _zoom, xml, overscan): """ Render the tile as a .png TODO: Actually handling zoom levels. """ # mapnik is installed in a non-standard way. # It confuses pylint. # pylint: disable=no-member,too-many-locals map_tile_size = TILE_SIZE + (overscan * 2) logger = get_logger() ctx = mapnik.Context() map_tile = mapnik.Map(map_tile_size, map_tile_size) # scale_denom = 1 << (BASE_ZOOM - int(zoom or 1)) # scale_factor = scale_denom / map_tile.scale_denominator() # map_tile.zoom(scale_factor) # Is overriden by zoom_to_box. mapnik.load_map_from_string(map_tile, xml) box_min = -overscan box_max = TILE_SIZE + overscan - 1 map_tile.zoom_to_box(mapnik.Box2d(box_min, box_min, box_max, box_max)) for (name, features) in tile.items(): name = name.encode('ascii', 'ignore') source = mapnik.MemoryDatasource() map_layer = mapnik.Layer(name) map_layer.datasource = source for feature in features: feat = mapnik.Feature(ctx, 0) try: feat.geometry = mapnik.Geometry.from_wkb(feature) except RuntimeError: try: wkt = mapnik.Geometry.from_wkb(feature).to_wkt() logger.error('Invalid feature: %s', wkt) except RuntimeError: logger.error('Corrupt feature: %s', feature.encode('hex')) source.add_feature(feat) map_layer.styles.append(name) map_tile.layers.append(map_layer) image = mapnik.Image(TILE_SIZE, TILE_SIZE) # tile, image, scale, offset_x, offset_y mapnik.render(map_tile, image, 1, overscan, overscan) return image.tostring('png')
def test_filter_init(): m = mapnik.Map(1, 1) mapnik.load_map_from_string(m, map_) filters = [] filters.append(mapnik.Filter("([region]>=0) and ([region]<=50)")) filters.append(mapnik.Filter("(([region]>=0) and ([region]<=50))")) filters.append(mapnik.Filter("((([region]>=0) and ([region]<=50)))")) filters.append(mapnik.Filter("((([region]>=0) and ([region]<=50)))")) filters.append(mapnik.Filter("""((([region]>=0) and ([region]<=50)))""")) filters.append( mapnik.Filter( """ ((([region]>=0) and ([region]<=50))) """ ) ) filters.append( mapnik.Filter( """ ([region]>=0) and ([region]<=50) """ ) ) filters.append( mapnik.Filter( """ ([region] >= 0) and ([region] <= 50) """ ) ) s = m.find_style("s") for r in s.rules: filters.append(r.filter) first = filters[0] for f in filters: eq_(str(first), str(f)) s = m.find_style("s2") eq_(s.filter_mode, mapnik.filter_mode.FIRST)
def render_png(tile, zoom, xml, overscan): map_tile_size = TILE_SIZE + (overscan * 2) # mapnik is installed in a non-standard way. # It confuses pylint. # pylint: disable=no-member """ Render the tile for the given zoom """ logger = get_logger() ctx = mapnik.Context() map_tile = mapnik.Map(map_tile_size, map_tile_size) # scale_denom = 1 << (BASE_ZOOM - int(zoom or 1)) # scale_factor = scale_denom / map_tile.scale_denominator() # map_tile.zoom(scale_factor) # Is overriden by zoom_to_box. mapnik.load_map_from_string(map_tile, xml) box_min = -overscan box_max = TILE_SIZE + overscan - 1 map_tile.zoom_to_box(mapnik.Box2d(box_min, box_min, box_max, box_max)) for (name, features) in tile.items(): name = name.encode('ascii', 'ignore') source = mapnik.MemoryDatasource() map_layer = mapnik.Layer(name) map_layer.datasource = source for feature in features: feat = mapnik.Feature(ctx, 0) try: feat.add_geometries_from_wkb(feature) except RuntimeError: from mapnik import Path # pylint: disable=no-name-in-module try: wkt = Path.from_wkb(feature).to_wkt() logger.error('Invalid feature: %s', wkt) except RuntimeError: logger.error('Corrupt feature: %s', feature.encode('hex')) source.add_feature(feat) map_layer.styles.append(name) map_tile.layers.append(map_layer) image = mapnik.Image(TILE_SIZE, TILE_SIZE) # tile, image, scale, offset_x, offset_y mapnik.render(map_tile, image, 1, overscan, overscan) return image.tostring('png')
def test_good_files(): good_files = glob.glob("../data/good_maps/*.xml") failures = []; for filename in good_files: try: m = mapnik.Map(512, 512) strict = True mapnik.load_map(m, filename, strict) base_path = os.path.dirname(filename) mapnik.load_map_from_string(m,open(filename,'rb').read(),strict,base_path) except RuntimeError, e: # only test datasources that we have installed if not 'Could not create datasource' in str(e): failures.append('Failed to load valid map (%s)!' % filename)
def add_legend(mapnik_map, data): features = [] box = mapnik_map.envelope() offset = ((box.maxy - box.miny) / 12) / len(data) x = box.minx + 1.5 * offset y = box.miny + offset for i, k in enumerate(data.keys()): point = Point((x, y + offset * (1 + i))) properties = {'key': k, 'value': data[k]} features.append(Feature(geometry=point, properties=properties)) collection = FeatureCollection(features) path = os.path.join(SRC_PATH, "maps-xml/legend.xml") xml_str = get_xml(path).format(json.dumps(collection)).encode() mapnik.load_map_from_string(mapnik_map, xml_str)
def assert_loads_successfully(file): m = mapnik.Map(512, 512) try: strict = True mapnik.load_map(m, file, strict) # libxml2 is not smart about paths, and clips the last directory off # of a path if it does not end in a trailing slash base_path = os.path.dirname(file) + '/' mapnik.load_map_from_string(m,open(file,'rb').read(),strict,base_path) except RuntimeError, e: # only test datasources that we have installed if not 'Could not create datasource' in str(e): raise RuntimeError(e)
def __init__(self, tile_dir, xmlmap, q, printLock, maxZoom, fields=None, layer_id=None): self.tile_dir = tile_dir self.q = q self.render_size = 256 self.m = mapnik.Map(self.render_size, self.render_size) self.g = mapnik.Grid(self.render_size, self.render_size) self.printLock = printLock # Load style XML mapnik.load_map_from_string(self.m, xmlmap) # Obtain <Map> projection self.prj = mapnik.Projection(self.m.srs) # Projects between tile pixel co-ordinates and LatLong (EPSG:4326) self.tileproj = GoogleProjection(maxZoom+1) self.layer_id = layer_id self.fields = fields or []
def test_load_save_load_map(): map = mapnik.Map(256,256) in_map = "../data/good_maps/glyph_symbolizer.xml" mapnik.load_map(map, in_map) style = map.find_style('arrows') sym = style.rules[0].symbols[0] assert isinstance(sym, mapnik.GlyphSymbolizer) assert sym.angle_mode == mapnik.angle_mode.AZIMUTH out_map = mapnik.save_map_to_string(map).decode('utf8') map = mapnik.Map(256,256) mapnik.load_map_from_string(map, out_map.encode('utf8')) assert 'GlyphSymbolizer' in out_map # make sure non-ascii characters are well supported since most interesting # glyphs for symbology are usually in that range assert u'í' in out_map, out_map
def render_tile(self) -> bytes: """ Render tile png Raises: RenderErrorNoDate: [description] Returns: bytes -- tile png as bytes """ if not self.request_date: raise RenderErrorNoDate( "Trying to generate a tile without to set a date!") # move to openstreetmap-carto folder os.chdir(self.osm_cato_path) # create empty mapnik map mapnik_map: mapnik.Map = mapnik.Map(self.width, self.height) # load style_xml style_xml: str = "" if self.use_cache: style_key: str = "{}-style.xml".format( self.request_date.strftime("%Y-%m-%d")) style_xml = cache.get_or_set(style_key, self.generate_date_style_xml()) else: style_xml = self.generate_date_style_xml() # fill map with content mapnik.load_map_from_string(mapnik_map, style_xml) mapnik_map.zoom_to_box(self.get_bbox()) # empty mapnik image with set size image: mapnik.Image = mapnik.Image(self.width, self.height) # generate tile png mapnik.render(mapnik_map, image) # create a tmp folder for generating the tile png # https://docs.python.org/3/library/tempfile.html with TemporaryDirectory() as temp_dir: file_path = "{}/tile.png".format(temp_dir) image.save(file_path) return open(file_path, "rb").read()
def serialize(xml,options): try: import mapnik except: sys.exit(color_text(1,'Error: saving xml requires Mapnik python bindings to be installed')) m = mapnik.Map(1,1) if options.from_string: mapnik.load_map_from_string(m,xml,True) else: mapnik.load_map(m,xml,True) if options.output: mapnik.save_map(m,options.output) else: if hasattr(mapnik,'mapnik_version') and mapnik.mapnik_version() >= 700: print mapnik.save_map_to_string(m) else: sys.exit(color_text(1,'Minor error: printing XML to stdout requires Mapnik >=0.7.0, please provide a second argument to save the output to a file'))
def init_mapnik(self): style = file(options.style).read() style = style.replace('"dbname": "gis"', '"dbname": "%s"' % options.db_name) style = style.replace('"dbname"><![CDATA[gis]]>', '"dbname"><![CDATA[%s]]>' % options.db_name) for type in ('point', 'line', 'roads', 'polygon'): style = style.replace('planet_osm_%s' % type, self.viewprefix + '_' + type) m = mapnik.Map(tile_size, tile_size) m.buffer_size = 128 # mapnik takes 700ms in db init mapnik.load_map_from_string(m, style, True, '/zdata/osm/openstreetmap-carto') m.resize(tile_size, tile_size) self.m = m
def test_good_files(): good_files = glob.glob("../data/good_maps/*.xml") failures = []; for filename in good_files: try: m = mapnik.Map(512, 512) strict = True mapnik.load_map(m, filename, strict) # libxml2 is not smart about paths, and clips the last directory off # of a path if it does not end in a trailing slash base_path = os.path.dirname(filename) + '/' mapnik.load_map_from_string(m,open(filename,'rb').read(),strict,base_path) except RuntimeError, e: # only test datasources that we have installed if not 'Could not create datasource' in str(e): failures.append('Failed to load valid map (%s)!' % filename)
def define_map(self, map): subquery = self.get_subqueries() schema = self.get_stylesheet() mapnik.load_map_from_string(map, schema) connection_params = dict(dbname=DB_NAME, user=DB_USER, host=DB_HOST, password=DB_PASSWORD, port=DB_PORT, ) ds = mapnik.PostGIS(table='mtlocation_cityborder', **connection_params) map.layers[0].datasource = ds ds = mapnik.PostGIS(table=subquery[1], geometry_table="mtlocation_region", geometry_field='area', **connection_params) map.layers[1].datasource = ds ds = mapnik.PostGIS(table=subquery[2], geometry_table="mtlocation_location", geometry_field='point', **connection_params) map.layers[2].datasource = ds ds = mapnik.PostGIS(table='mtlocation_ctaraillines', **connection_params) map.layers[3].datasource = ds ds = mapnik.PostGIS(table=subquery[4], geometry_table="mtlocation_location", geometry_field='point', **connection_params) map.layers[4].datasource = ds self.map = map
def test_filter_init(): m = mapnik.Map(1, 1) mapnik.load_map_from_string(m, map_) filters = [] filters.append(mapnik.Filter("([region]>=0) and ([region]<=50)")) filters.append(mapnik.Filter("(([region]>=0) and ([region]<=50))")) filters.append(mapnik.Filter("((([region]>=0) and ([region]<=50)))")) filters.append(mapnik.Filter('((([region]>=0) and ([region]<=50)))')) filters.append(mapnik.Filter('''((([region]>=0) and ([region]<=50)))''')) filters.append( mapnik.Filter(''' ((([region]>=0) and ([region]<=50))) ''')) filters.append( mapnik.Filter(''' ([region]>=0) and ([region]<=50) ''')) filters.append( mapnik.Filter(''' ([region] >= 0) and ([region] <= 50) ''')) s = m.find_style('s') for r in s.rules: filters.append(r.filter) first = filters[0] for f in filters: eq_(str(first), str(f)) s = m.find_style('s2') eq_(s.filter_mode, mapnik.filter_mode.FIRST)
def pure_tile_rendering(args): tile_dir = args[0] mapfile = args[1] maxZoom = args[2] tile_uri = args[3] x = args[4] y = args[5] z = args[6] #not needed anymore, as the mapnik.map is sent directly to the function m = mapnik.Map(256, 256) # Load style XML mapnik.load_map_from_string(m, mapfile) # Obtain <Map> projection prj = mapnik.Projection(m.srs) # Projects between tile pixel co-ordinates and LatLong (EPSG:4326) tileproj = GoogleProjection(maxZoom+1) tile = (x,y) #print 'Tile: ', tile # Calculate pixel positions of bottom-left & top-right p0 = (tile[0] * 256, (tile[1] + 1) * 256) p1 = ((tile[0] + 1) * 256, tile[1] * 256) # Convert to LatLong (EPSG:4326) l0 = tileproj.fromPixelToLL(p0, z) l1 = tileproj.fromPixelToLL(p1, z) # Convert to map projection (e.g. mercator co-ords EPSG:900913) c0 = prj.forward(mapnik.Coord(l0[0],l0[1])) c1 = prj.forward(mapnik.Coord(l1[0],l1[1])) #c0, c1 = calcTileCoordinates(tile, z) tile_extent = (c0.x,c0.y, c1.x,c1.y) # Bounding box for the tile bbox = mapnik.Envelope(c0.x,c0.y, c1.x,c1.y) m.zoom_to_box(bbox) # Render image with default Agg renderer im = mapnik.Image(m.height, m.width) mapnik.render(m, im) im.save(tile_uri, 'png256') return m.scale()/0.00028
def pure_tile_rendering(args): tile_dir = args[0] mapfile = args[1] maxZoom = args[2] tile_uri = args[3] x = args[4] y = args[5] z = args[6] #not needed anymore, as the mapnik.map is sent directly to the function m = mapnik.Map(256, 256) # Load style XML mapnik.load_map_from_string(m, mapfile) # Obtain <Map> projection prj = mapnik.Projection(m.srs) # Projects between tile pixel co-ordinates and LatLong (EPSG:4326) tileproj = GoogleProjection(maxZoom + 1) tile = (x, y) #print 'Tile: ', tile # Calculate pixel positions of bottom-left & top-right p0 = (tile[0] * 256, (tile[1] + 1) * 256) p1 = ((tile[0] + 1) * 256, tile[1] * 256) # Convert to LatLong (EPSG:4326) l0 = tileproj.fromPixelToLL(p0, z) l1 = tileproj.fromPixelToLL(p1, z) # Convert to map projection (e.g. mercator co-ords EPSG:900913) c0 = prj.forward(mapnik.Coord(l0[0], l0[1])) c1 = prj.forward(mapnik.Coord(l1[0], l1[1])) #c0, c1 = calcTileCoordinates(tile, z) tile_extent = (c0.x, c0.y, c1.x, c1.y) # Bounding box for the tile bbox = mapnik.Envelope(c0.x, c0.y, c1.x, c1.y) m.zoom_to_box(bbox) # Render image with default Agg renderer im = mapnik.Image(m.height, m.width) mapnik.render(m, im) im.save(tile_uri, 'png256') return m.scale() / 0.00028
def render_image(self, extent, img_size, settings): # Выбираем объекты по экстенту feature_query = self.layer.feature_query() feature_query.intersects(box(*extent, srid=self.layer.srs_id)) feature_query.geom() features = feature_query() ds = mapnik.MemoryDatasource() for (id, f) in enumerate(features): if mapnik.mapnik_version() < 200100: feature = mapnik.Feature(id) else: feature = mapnik.Feature(mapnik.Context(), id) feature.add_geometries_from_wkb(f.geom.wkb) ds.add_feature(feature) style_content = str(self.style_content) m = mapnik.Map(img_size[0], img_size[1]) mapnik.load_map_from_string(m, style_content) m.zoom_to_box(mapnik.Box2d(*extent)) layer = mapnik.Layer('main') layer.datasource = ds root = ET.fromstring(style_content) styles = [s.attrib.get('name') for s in root.iter('Style')] for s in styles: layer.styles.append(s) m.layers.append(layer) img = mapnik.Image(img_size[0], img_size[1]) mapnik.render(m, img) data = img.tostring('png') # Преобразуем изображение из PNG в объект PIL buf = StringIO() buf.write(data) buf.seek(0) img = Image.open(buf) return img
def get_mapnikMap(mapfile): """ Get a new mapnik.Map instance for a mapfile """ mmap = mapnik.Map(0, 0) if mapfile.startswith('<?xml'): mapnik.load_map_from_string(mmap, str(mapfile)) elif exists(mapfile): mapnik.load_map(mmap, str(mapfile)) else: handle, filename = mkstemp() os.write(handle, urlopen(mapfile).read()) os.close(handle) mapnik.load_map(mmap, filename) os.unlink(filename) return mmap
def test_filter_init(): m = mapnik.Map(1, 1) mapnik.load_map_from_string(m, map_) filters = [] filters.append(mapnik.Expression("([region]>=0) and ([region]<=50)")) filters.append(mapnik.Expression("(([region]>=0) and ([region]<=50))")) filters.append(mapnik.Expression("((([region]>=0) and ([region]<=50)))")) filters.append(mapnik.Expression('((([region]>=0) and ([region]<=50)))')) filters.append(mapnik.Expression('''((([region]>=0) and ([region]<=50)))''')) filters.append(mapnik.Expression(''' ((([region]>=0) and ([region]<=50))) ''')) filters.append(mapnik.Expression(''' ([region]>=0) and ([region]<=50) ''')) filters.append(mapnik.Expression(''' ([region] >= 0) and ([region] <= 50) ''')) s = m.find_style('s') for r in s.rules: filters.append(r.filter) first = filters[0] for f in filters: eq_(str(first), str(f)) s = m.find_style('s2') eq_(s.filter_mode, mapnik.filter_mode.FIRST)
def test_load_save_load_map(): map = mapnik.Map(256, 256) in_map = "../data/good_maps/glyph_symbolizer.xml" try: mapnik.load_map(map, in_map) style = map.find_style('arrows') sym = style.rules[0].symbols[0] assert isinstance(sym, mapnik.GlyphSymbolizer) assert sym.angle_mode == mapnik.angle_mode.AZIMUTH out_map = mapnik.save_map_to_string(map).decode('utf8') map = mapnik.Map(256, 256) mapnik.load_map_from_string(map, out_map.encode('utf8')) assert 'GlyphSymbolizer' in out_map # make sure non-ascii characters are well supported since most interesting # glyphs for symbology are usually in that range assert u'í' in out_map, out_map except RuntimeError, e: # only test datasources that we have installed if not 'Could not create datasource' in str(e): raise RuntimeError(e)
def test_good_files(): good_files = glob.glob("../data/good_maps/*.xml") good_files.extend(glob.glob("../visual_tests/styles/*.xml")) failures = [] for filename in good_files: try: m = mapnik.Map(512, 512) strict = True mapnik.load_map(m, filename, strict) base_path = os.path.dirname(filename) with open(filename, 'rb') as f: mapnik.load_map_from_string(m, f.read(), strict, base_path) except RuntimeError as e: # only test datasources that we have installed if not 'Could not create datasource' in str(e) \ and not 'could not connect' in str(e): failures.append( 'Failed to load valid map %s (%s)' % (filename, e)) eq_(len(failures), 0, '\n' + '\n'.join(failures))
def test_load_save_load_map(): map = mapnik.Map(256, 256) in_map = "../data/good_maps/glyph_symbolizer.xml" try: mapnik.load_map(map, in_map) style = map.find_style("arrows") sym = style.rules[0].symbols[0] assert isinstance(sym, mapnik.GlyphSymbolizer) assert sym.angle_mode == mapnik.angle_mode.AZIMUTH out_map = mapnik.save_map_to_string(map).decode("utf8") map = mapnik.Map(256, 256) mapnik.load_map_from_string(map, out_map.encode("utf8")) assert "GlyphSymbolizer" in out_map # make sure non-ascii characters are well supported since most interesting # glyphs for symbology are usually in that range assert u"í" in out_map, out_map except RuntimeError, e: # only test datasources that we have installed if not "Could not create datasource" in str(e): raise RuntimeError(e)
def test_map_init_from_string(): map_string = '''<Map bgcolor="steelblue" srs="+proj=latlong +datum=WGS84"> <Style name="My Style"> <Rule> <PolygonSymbolizer> <CssParameter name="fill">#f2eff9</CssParameter> </PolygonSymbolizer> <LineSymbolizer> <CssParameter name="stroke">rgb(50%,50%,50%)</CssParameter> <CssParameter name="stroke-width">0.1</CssParameter> </LineSymbolizer> </Rule> </Style> <Layer name="boundaries"> <StyleName>My Style</StyleName> <Datasource> <Parameter name="type">shape</Parameter> <Parameter name="file">../../demo/data/boundaries</Parameter> </Datasource> </Layer> </Map>''' m = mapnik.Map(600, 300) mapnik.load_map_from_string(m, map_string) mapnik.load_map_from_string(m, map_string, False, "") mapnik.load_map_from_string(m, map_string, True, "") raise(Todo("Need to write more map property tests in 'object_test.py'..."))
def test_map_init_from_string(): map_string = '''<Map background-color="steelblue" base="./" srs="+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"> <Style name="My Style"> <Rule> <PolygonSymbolizer> <CssParameter name="fill">#f2eff9</CssParameter> </PolygonSymbolizer> <LineSymbolizer> <CssParameter name="stroke">rgb(50%,50%,50%)</CssParameter> <CssParameter name="stroke-width">0.1</CssParameter> </LineSymbolizer> </Rule> </Style> <Layer name="boundaries"> <StyleName>My Style</StyleName> <Datasource> <Parameter name="type">shape</Parameter> <Parameter name="file">../../demo/data/boundaries</Parameter> </Datasource> </Layer> </Map>''' m = mapnik.Map(600, 300) eq_(m.base, '') try: mapnik.load_map_from_string(m, map_string) eq_(m.base, './') mapnik.load_map_from_string(m, map_string, False, "") # this "" will have no effect eq_(m.base, './') tmp_dir = tempfile.gettempdir() try: mapnik.load_map_from_string(m, map_string, False, tmp_dir) except RuntimeError: pass # runtime error expected because shapefile path should be wrong and datasource will throw eq_( m.base, tmp_dir ) # tmp_dir will be set despite the exception because load_map mostly worked m.base = 'foo' mapnik.load_map_from_string(m, map_string, True, ".") eq_(m.base, '.') raise (Todo( "Need to write more map property tests in 'object_test.py'...")) except RuntimeError, e: # only test datasources that we have installed if not 'Could not create datasource' in str(e): raise RuntimeError(e)
def main(shapefile, label_field=None, output="style.xml"): # getAreaStatistics(shapefile) projection = getProj4FromShapefile(shapefile) label_style = '' if label_field: label_style = getLabelStyleForZooms(shapefile, label_field) stylesheet = buildStylesheet(shapefile, projection, label_style) image = 'tmp.png' m = mapnik.Map(900, 600) mapnik.load_map_from_string(m, stylesheet) m.zoom_all() m.zoom(0.05) # m.zoom(0.25) # m.zoom(0.5) mapnik.render_to_file(m, image) # print(mapnik.save_map_to_string(m)) mapnik.save_map(m, output)
def __init__(self, tile_dir, xmlmap, q, printLock, maxZoom, fields=None, layer_id=None): self.tile_dir = tile_dir self.q = q self.render_size = 256 self.m = mapnik.Map(self.render_size, self.render_size) self.g = mapnik.Grid(self.render_size, self.render_size) self.printLock = printLock # Load style XML mapnik.load_map_from_string(self.m, xmlmap) # Obtain <Map> projection self.prj = mapnik.Projection(self.m.srs) # Projects between tile pixel co-ordinates and LatLong (EPSG:4326) self.tileproj = GoogleProjection(maxZoom + 1) self.layer_id = layer_id self.fields = fields or []
def test_map_init_from_string(): map_string = '''<Map background-color="steelblue" base="./" srs="+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"> <Style name="My Style"> <Rule> <PolygonSymbolizer> <CssParameter name="fill">#f2eff9</CssParameter> </PolygonSymbolizer> <LineSymbolizer> <CssParameter name="stroke">rgb(50%,50%,50%)</CssParameter> <CssParameter name="stroke-width">0.1</CssParameter> </LineSymbolizer> </Rule> </Style> <Layer name="boundaries"> <StyleName>My Style</StyleName> <Datasource> <Parameter name="type">shape</Parameter> <Parameter name="file">../../demo/data/boundaries</Parameter> </Datasource> </Layer> </Map>''' m = mapnik.Map(600, 300) eq_(m.base, '') try: mapnik.load_map_from_string(m, map_string) eq_(m.base, './') mapnik.load_map_from_string(m, map_string, False, "") # this "" will have no effect eq_(m.base, './') tmp_dir = tempfile.gettempdir() try: mapnik.load_map_from_string(m, map_string, False, tmp_dir) except RuntimeError: pass # runtime error expected because shapefile path should be wrong and datasource will throw eq_(m.base, tmp_dir) # tmp_dir will be set despite the exception because load_map mostly worked m.base = 'foo' mapnik.load_map_from_string(m, map_string, True, ".") eq_(m.base, '.') raise(Todo("Need to write more map property tests in 'object_test.py'...")) except RuntimeError, e: # only test datasources that we have installed if not 'Could not create datasource' in str(e): raise RuntimeError(e)
def test_map_init_from_string(): map_string = """<Map background-color="steelblue" base="./" srs="+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"> <Style name="My Style"> <Rule> <PolygonSymbolizer fill="#f2eff9"/> <LineSymbolizer stroke="rgb(50%,50%,50%)" stroke-width="0.1"/> </Rule> </Style> <Layer name="boundaries"> <StyleName>My Style</StyleName> <Datasource> <Parameter name="type">shape</Parameter> <Parameter name="file">../../demo/data/boundaries</Parameter> </Datasource> </Layer> </Map>""" m = mapnik.Map(600, 300) eq_(m.base, "") try: mapnik.load_map_from_string(m, map_string) eq_(m.base, "./") mapnik.load_map_from_string(m, map_string, False, "") # this "" will have no effect eq_(m.base, "./") tmp_dir = tempfile.gettempdir() try: mapnik.load_map_from_string(m, map_string, False, tmp_dir) except RuntimeError: pass # runtime error expected because shapefile path should be wrong and datasource will throw eq_(m.base, tmp_dir) # tmp_dir will be set despite the exception because load_map mostly worked m.base = "foo" mapnik.load_map_from_string(m, map_string, True, ".") eq_(m.base, ".") except RuntimeError, e: # only test datasources that we have installed if not "Could not create datasource" in str(e): raise RuntimeError(e)
# reading style xml into memory for preprocessing if options.style == '-': style_xml = sys.stdin.read() style_path = '' else: with open(options.style, 'r') as style_file: style_xml = style_file.read() style_path = os.path.dirname(options.style) if options.base: style_path = options.base if 'vars' in options and options.vars is not None: style_xml = xml_vars(style_xml, options.vars) # for layer processing we need to create the Map object m = mapnik.Map(100, 100) # temporary size, will be changed before output mapnik.load_map_from_string(m, style_xml, False, style_path) m.srs = p3857.params() # register non-standard fonts if options.fonts: for f in options.fonts: add_fonts(f) # get bbox from layer extents if options.fit: bbox = layer_bbox(m, options.fit.split(','), bbox) # here's where we can fix scale, no new bboxes below if bbox and fix_scale: scale = scale / math.cos(math.radians(transform.backward(bbox.center()).y)) # expand bbox with padding in mm if bbox and options.padding and (scale or size):
def loadXML(self, xmlfile=None, strict=False, xmlstring='', basepath=''): config = ConfigParser.SafeConfigParser() map_wms_srs = None if self.configpath: config.readfp(open(self.configpath)) if config.has_option('map', 'wms_srs'): map_wms_srs = config.get('map', 'wms_srs') tmp_map = Map(0, 0) if xmlfile: load_map(tmp_map, xmlfile, strict) elif xmlstring: load_map_from_string(tmp_map, xmlstring, strict, basepath) else: raise ServerConfigurationError( "Mapnik configuration XML is not specified - 'xmlfile' and 'xmlstring' variables are empty.\ Please set one of this variables to load mapnik map object.") # get the map scale if tmp_map.parameters: if tmp_map.parameters['scale']: self.map_scale = float(tmp_map.parameters['scale']) # parse map level attributes if tmp_map.background: self.map_attributes['bgcolor'] = tmp_map.background if tmp_map.buffer_size: self.map_attributes['buffer_size'] = tmp_map.buffer_size for lyr in tmp_map.layers: layer_section = 'layer_%s' % lyr.name layer_wms_srs = None if config.has_option(layer_section, 'wms_srs'): layer_wms_srs = config.get(layer_section, 'wms_srs') else: layer_wms_srs = map_wms_srs if config.has_option(layer_section, 'title'): lyr.title = config.get(layer_section, 'title') else: lyr.title = '' if config.has_option(layer_section, 'abstract'): lyr.abstract = config.get(layer_section, 'abstract') else: lyr.abstract = '' style_count = len(lyr.styles) if style_count == 0: raise ServerConfigurationError( "Cannot register Layer '%s' without a style" % lyr.name) elif style_count == 1: style_obj = tmp_map.find_style(lyr.styles[0]) style_name = lyr.styles[0] meta_s = extract_named_rules(style_obj) if meta_s: self.meta_styles['%s_meta' % lyr.name] = meta_s if hasattr(lyr, 'abstract'): name_ = lyr.abstract else: name_ = lyr.name meta_layer_name = '%s:%s' % (name_, '-'.join(meta_s.names)) meta_layer_name = meta_layer_name.replace(' ', '_') self.meta_styles[meta_layer_name] = meta_s meta_lyr = common.copy_layer(lyr) meta_lyr.meta_style = meta_layer_name meta_lyr.name = meta_layer_name meta_lyr.wmsextrastyles = () meta_lyr.defaultstyle = meta_layer_name meta_lyr.wms_srs = layer_wms_srs self.ordered_layers.append(meta_lyr) self.meta_layers[meta_layer_name] = meta_lyr print meta_layer_name if style_name not in self.aggregatestyles.keys( ) and style_name not in self.styles.keys(): self.register_style(style_name, style_obj) # must copy layer here otherwise we'll segfault lyr_ = common.copy_layer(lyr) lyr_.wms_srs = layer_wms_srs self.register_layer(lyr_, style_name, extrastyles=(style_name, )) elif style_count > 1: for style_name in lyr.styles: style_obj = tmp_map.find_style(style_name) meta_s = extract_named_rules(style_obj) if meta_s: self.meta_styles['%s_meta' % lyr.name] = meta_s if hasattr(lyr, 'abstract'): name_ = lyr.abstract else: name_ = lyr.name meta_layer_name = '%s:%s' % (name_, '-'.join( meta_s.names)) meta_layer_name = meta_layer_name.replace(' ', '_') self.meta_styles[meta_layer_name] = meta_s meta_lyr = common.copy_layer(lyr) meta_lyr.meta_style = meta_layer_name print meta_layer_name meta_lyr.name = meta_layer_name meta_lyr.wmsextrastyles = () meta_lyr.defaultstyle = meta_layer_name meta_lyr.wms_srs = layer_wms_srs self.ordered_layers.append(meta_lyr) self.meta_layers[meta_layer_name] = meta_lyr if style_name not in self.aggregatestyles.keys( ) and style_name not in self.styles.keys(): self.register_style(style_name, style_obj) aggregates = tuple([sty for sty in lyr.styles]) aggregates_name = '%s_aggregates' % lyr.name self.register_aggregate_style(aggregates_name, aggregates) # must copy layer here otherwise we'll segfault lyr_ = common.copy_layer(lyr) lyr_.wms_srs = layer_wms_srs self.register_layer(lyr_, aggregates_name, extrastyles=aggregates) if 'default' in aggregates: sys.stderr.write( "Warning: Multi-style layer '%s' contains a regular style named 'default'. \ This style will effectively be hidden by the 'all styles' default style for multi-style layers.\n" % lyr_.name)
def getDatasourceFromString(string,chosen_layer): print 'Chosen layer: ' + chosen_layer m = mapnik.Map(256,256) mapnik.load_map_from_string(m,string) for layer in m.layers.__iter__(): if layer.name == chosen_layer: params = layer.datasource.params() type = params.get('type') contents = {} contents['type'] = type #print type if type == 'shape': datasource = params.get('file') if datasource.find('..') != -1: print "Correct datasource related to: '../'" cache = datasource.split('..') datasource = "" cache[0] = cache[0].split('/') for u in range(1, len(cache[0])-2): datasource = datasource +"/"+ cache[0][u] datasource = datasource + cache[1] #print datasource if datasource.find(' ') != -1: datasource = datasource.replace(' ', '\\ ') if datasource.find('-') != -1: replaced_datasource = datasource.replace('-', '_') exists = os.path.isfile(replaced_datasource) if exists == False: command = "ogr2ogr -f 'ESRI Shapefile' "+ replaced_datasource +" " + datasource +" -overwrite" test = os.system(command) # func.writeToLog('%s is no valid name for processing with ogr!!!' %datasource) # if test == 0: # func.writeToLog('It was necessary to rename and copy "%s" to: "%s"' %(datasource, replaced_datasource)) # else: # func.writeToLog('It was NOT possible to process following command: ', command) datasource = replaced_datasource contents['file'] = datasource elif type == 'postgis': content = 'DB: %s\nTable: %s '%(params.get('dbname'), params.get('table')) contents['dbname'] = params.get('dbname') contents['table'] = params.get('table') contents['host'] = params.get('host') contents['port'] = params.get('port') contents['user'] = params.get('user') contents['password'] = params.get('password') else: print 'Please implement that ('+ type +') special case!!!' """ response = xml.fromstring(string) tree = xml.ElementTree(response) datasource = '' type = '' for elem in tree.iter(tag = 'Layer'): if elem.attrib['name'] == chosen_layer: #print elem.attrib['name'] for part in elem.iter(tag='Parameter'): if part.attrib['name'] == 'type': type = part.text for part in elem.iter(tag='Parameter'): if type == 'shape': #print part.attrib if part.attrib['name'] == 'file': datasource = part.text elif type == 'postgis': print 'Postgis is not implemented yet' else: print 'Please implement that ('+ type +') special case!!!' # ogr2ogr -f 'ESRI Shapefile' lines_vgtl_27_01_12.shp lines-vgtl-27-01-12.shp """ #ogr has problems in reading '-'-characters #so we have to copy the source with clean name #print contents return contents
def getDatasourceFromString(string, chosen_layer): print 'Chosen layer: ' + chosen_layer m = mapnik.Map(256, 256) mapnik.load_map_from_string(m, string) for layer in m.layers.__iter__(): if layer.name == chosen_layer: params = layer.datasource.params() type = params.get('type') contents = {} contents['type'] = type #print type if type == 'shape': datasource = params.get('file') if datasource.find('..') != -1: print "Correct datasource related to: '../'" cache = datasource.split('..') datasource = "" cache[0] = cache[0].split('/') for u in range(1, len(cache[0]) - 2): datasource = datasource + "/" + cache[0][u] datasource = datasource + cache[1] #print datasource if datasource.find(' ') != -1: datasource = datasource.replace(' ', '\\ ') if datasource.find('-') != -1: replaced_datasource = datasource.replace('-', '_') exists = os.path.isfile(replaced_datasource) if exists == False: command = "ogr2ogr -f 'ESRI Shapefile' " + replaced_datasource + " " + datasource + " -overwrite" test = os.system(command) # func.writeToLog('%s is no valid name for processing with ogr!!!' %datasource) # if test == 0: # func.writeToLog('It was necessary to rename and copy "%s" to: "%s"' %(datasource, replaced_datasource)) # else: # func.writeToLog('It was NOT possible to process following command: ', command) datasource = replaced_datasource contents['file'] = datasource elif type == 'postgis': content = 'DB: %s\nTable: %s ' % (params.get('dbname'), params.get('table')) contents['dbname'] = params.get('dbname') contents['table'] = params.get('table') contents['host'] = params.get('host') contents['port'] = params.get('port') contents['user'] = params.get('user') contents['password'] = params.get('password') else: print 'Please implement that (' + type + ') special case!!!' """ response = xml.fromstring(string) tree = xml.ElementTree(response) datasource = '' type = '' for elem in tree.iter(tag = 'Layer'): if elem.attrib['name'] == chosen_layer: #print elem.attrib['name'] for part in elem.iter(tag='Parameter'): if part.attrib['name'] == 'type': type = part.text for part in elem.iter(tag='Parameter'): if type == 'shape': #print part.attrib if part.attrib['name'] == 'file': datasource = part.text elif type == 'postgis': print 'Postgis is not implemented yet' else: print 'Please implement that ('+ type +') special case!!!' # ogr2ogr -f 'ESRI Shapefile' lines_vgtl_27_01_12.shp lines-vgtl-27-01-12.shp """ #ogr has problems in reading '-'-characters #so we have to copy the source with clean name #print contents return contents
def _add_grid(self, grid): xml_str = get_xml("styles/grid.xml").format(json.dumps(grid)).encode() load_map_from_string(self._map, xml_str)