def test_pointsymbolizer_init(): p = mapnik2.PointSymbolizer() eq_(p.allow_overlap, False) eq_(p.opacity,1) eq_(p.filename,'') eq_(p.ignore_placement,False) eq_(p.placement, mapnik2.point_placement.CENTROID) p = mapnik2.PointSymbolizer(mapnik2.PathExpression("../data/images/dummy.png")) p.allow_overlap = True p.opacity = 0.5 p.ignore_placement = True p.placement = mapnik2.point_placement.INTERIOR eq_(p.allow_overlap, True) eq_(p.opacity, 0.5) eq_(p.filename,'../data/images/dummy.png') eq_(p.ignore_placement,True) eq_(p.placement, mapnik2.point_placement.INTERIOR)
def test_pointsymbolizer_pickle(): raise Todo("point_symbolizer pickling currently disabled") p = mapnik2.PointSymbolizer(mapnik2.PathExpression("../data/images/dummy.png")) p2 = pickle.loads(pickle.dumps(p,pickle.HIGHEST_PROTOCOL)) # image type, width, and height only used in contructor... eq_(p.filename, p2.filename) eq_(p.allow_overlap, p2.allow_overlap) eq_(p.opacity, p2.opacity) eq_(p.ignore_placement, p2.ignore_placement) eq_(p.placement, p2.placement)
def test_render_points(): # Test for effectivenes of ticket #402 (borderline points get lost on reprojection) raise Todo("See: http://trac.mapnik2.org/ticket/402") if not mapnik2.has_pycairo(): 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' } from cairo import SVGSurface 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 import StringIO svg_memory_file = StringIO.StringIO() surface = SVGSurface(svg_memory_file, m.width, m.height) mapnik2.render(m, surface) surface.flush() surface.finish() svg = svg_memory_file.getvalue() svg_memory_file.close() num_points_present = len(places_ds.all_features()) 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_introspect_symbolizers(): # create a symbolizer p = mapnik2.PointSymbolizer( mapnik2.PathExpression("../data/images/dummy.png")) p.allow_overlap = True p.opacity = 0.5 eq_(p.allow_overlap, True) eq_(p.opacity, 0.5) eq_(p.filename, '../data/images/dummy.png') # make sure the defaults # are what we think they are eq_(p.allow_overlap, True) eq_(p.opacity, 0.5) eq_(p.filename, '../data/images/dummy.png') # contruct objects to hold it r = mapnik2.Rule() r.symbols.append(p) s = mapnik2.Style() s.rules.append(r) m = mapnik2.Map(0, 0) m.append_style('s', s) # try to figure out what is # in the map and make sure # style is there and the same s2 = m.find_style('s') rules = s2.rules eq_(len(rules), 1) r2 = rules[0] syms = r2.symbols eq_(len(syms), 1) ## TODO here, we can do... sym = syms[0] # this is hackish at best p2 = sym.symbol() assert isinstance(p2, mapnik2.PointSymbolizer) eq_(p2.allow_overlap, True) eq_(p2.opacity, 0.5) eq_(p2.filename, '../data/images/dummy.png') ## but we need to be able to do: p2 = syms[0] # get the actual symbolizer, not the variant object # this will throw for now... assert isinstance(p2, mapnik2.PointSymbolizer) eq_(p2.allow_overlap, True) eq_(p2.opacity, 0.5) eq_(p2.filename, '../data/images/dummy.png')
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))
def main(root,**options): m = mapnik.Map(1,1) idx = 0 layers = [] if hasattr(root,'NamedLayer'): layers.extend(root.NamedLayer) if hasattr(root,'UserLayer'): layers.extend(root.UserLayer) for layer in layers: lyr = mapnik.Layer(str(getattr(layer,'Name',None) or 'Layer')) datasource = options.get('datasource') if datasource and datasource.endswith('shp'): shp_dir = os.path.abspath(datasource).split('.shp')[0] name = datasource.split('.shp')[0] lyr.datasource = mapnik.Shapefile(file=shp_dir) if options.get('srid'): lyr.srs = '+init=epsg:%s' % options.get('srid') m.srs = lyr.srs else: srs = proj4_from_osr(shp_dir) if srs: lyr.srs = srs for user_style in layer.UserStyle: for feature_style in user_style.FeatureTypeStyle: m_sty = mapnik.Style() # TODO = Styles should have title,abstract, etc... sty_name = getattr(feature_style,'Name',None) if not sty_name: sty_name = '%s %s' % (lyr.name,str(idx)) sty_name = str(sty_name) for rule in feature_style.Rule: #print rule.get_childen() m_rule = mapnik.Rule(str(getattr(rule,'Name',''))) ogc_filter = rule.find("{%s}Filter" % rule.nsmap['ogc']) if ogc_filter is not None: # TODO - support ogc:And and oc:Or m_rule.filter = ogc_filter_to_mapnik(ogc_filter) else: if hasattr(rule,'ElseFilter'): m_rule.set_else(True) if hasattr(rule,'MaxScaleDenominator'): m_rule.max_scale = float(rule.MaxScaleDenominator) if hasattr(rule,'MinScaleDenominator'): m_rule.min_scale = float(rule.MinScaleDenominator) if hasattr(rule,'LineSymbolizer'): stroke = rule.LineSymbolizer.Stroke m_stroke = stroke_to_mapnik(stroke) m_rule.symbols.append(mapnik.LineSymbolizer(m_stroke)) if hasattr(rule,'PolygonSymbolizer'): m_poly = mapnik.PolygonSymbolizer() if hasattr(rule.PolygonSymbolizer,'Fill'): fill = rule.PolygonSymbolizer.Fill for css in fill.CssParameter: if css.get('name') == 'fill': m_poly.fill = mapnik.Color(css.text) elif css.get('name') == 'fill-opacity': m_poly.opacity = float(css.text) else: raise Exception('unhanded: ' + css.get('name')) if hasattr(rule.PolygonSymbolizer,'Stroke'): stroke = rule.PolygonSymbolizer.Stroke m_stroke = stroke_to_mapnik(stroke) m_rule.symbols.append(mapnik.LineSymbolizer(m_stroke)) m_rule.symbols.append(m_poly) if hasattr(rule,'PointSymbolizer'): #fill = rule.PolygonSymbolizer.Fill #m_point = point_to_mapnik(point) # TODO m_rule.symbols.append(mapnik.PointSymbolizer()) if hasattr(rule,'TextSymbolizer'): text = rule.TextSymbolizer name = text.Label.find("{%s}PropertyName" % rule.nsmap['ogc']) if not name and hasattr(text,'Label'): name = shlex.split(str(text.Label))[0] face_name = '[%s]' % text.Font face_name = 'DejaVu Sans Book' size = 10 for css in text.Font.CssParameter: if css.get('name') == 'font-family': face_name = css.text elif css.get('name') == 'font-size': size = int(float(css.text)) color = mapnik.Color('black') for css in text.Fill.CssParameter: if css.get('name') == 'fill': color = mapnik.Color(css.text) m_text = mapnik.TextSymbolizer(mapnik.Expression('['+str(name)+']'),str(face_name),int(size),color) if hasattr(text,'LabelPlacement'): if hasattr(text.LabelPlacement,'LinePlacement'): m_text.label_placement = mapnik.label_placement.LINE_PLACEMENT if hasattr(text,'Halo'): h = text.Halo if hasattr(h,'Radius'): m_text.halo_radius = float(h.Radius) if hasattr(h,'Fill'): for css in h.Fill.CssParameter: if css.get('name') == 'fill': m_text.halo_fill = mapnik.Color(css.text) m_rule.symbols.append(m_text) m_sty.rules.append(m_rule) lyr.styles.append(sty_name) m.append_style(sty_name,m_sty) idx+= 1 m.layers.append(lyr) if FIX_HEX: (handle, path) = tempfile.mkstemp(suffix='.xml', prefix='geotools2mapnik-') os.close(handle) open(path,'w').write(mapnik.save_map_to_string(m)) tree = objectify.parse(path) fix_colors(tree) print etree.tostring(tree)#,pretty_print=True) else: print mapnik.save_map_to_string(m)
### START Layer 1 ### # style object to hold rules s = mapnik2.Style() # rule object to hold symbolizers r = mapnik2.Rule() # Lines (outlines of polygons and/or simple lines. Line-Color (RGB) line-thickness #polygon_symbolizer = mapnik2.PolygonSymbolizer(mapnik2.Color('red')) #rgb(5%,5%,5%) # add the polygon_symbolizer to the rule object #r.symbols.append(polygon_symbolizer) # Point Style. Path to marker.png point_symbolizer = mapnik2.PointSymbolizer( mapnik2.PathExpression(point_marker)) # Allow Overlaps and set opacity of marker point_symbolizer.allow_overlap = True point_symbolizer.opacity = 0.7 # add the point_symbolizer to the rule object r.symbols.append(point_symbolizer) # now add the rule(s) to the style s.rules.append(r) # Styles are added to the map m.append_style('My Style', s) # Projection from PostGIS-Layer-Data
def ogc_PointSymbolizer_to_mapnik(sym): #fill = rule.PolygonSymbolizer.Fill #m_point = point_to_mapnik(point) # TODO yield mapnik.PointSymbolizer()