def test_get_color(): colorizer = mapnik2.RasterColorizer() # Setup the color bands. band[N].color will apply to all # values 'v' if band[N].value <= v < band[N+1].color # If no color is found then "transparent" will be assigned bands = [(value, mapnik2.Color(color)) for value, color in [ ( 0, "#0044cc"), ( 10, "#00cc00"), ( 20, "#ffff00"), ( 30, "#ff7f00"), ( 40, "#ff0000"), ( 50, "#ff007f"), ( 60, "#ff00ff"), ( 70, "#cc00cc"), ( 80, "#990099"), ( 90, "#660066"), ( 200, "#ffffff"), ]] for value, color in bands: colorizer.append_band(value, color) eq_(colorizer.get_color(-1), mapnik2.Color("transparent")) eq_(colorizer.get_color(0), bands[0][1]) eq_(colorizer.get_color(5), bands[0][1]) eq_(colorizer.get_color(10), bands[1][1]) # last value is used if it matches exactly eq_(colorizer.get_color(200), bands[-1][1]) # values greater than the last value are mapped to "transparent" eq_(colorizer.get_color(201), mapnik2.Color("transparent"))
def test_linesymbolizer_init(): l = mapnik2.LineSymbolizer() eq_(l.stroke.width, 1) eq_(l.stroke.opacity, 1) eq_(l.stroke.color, mapnik2.Color('black')) eq_(l.stroke.line_cap, mapnik2.line_cap.BUTT_CAP) eq_(l.stroke.line_join, mapnik2.line_join.MITER_JOIN) l = mapnik2.LineSymbolizer(mapnik2.Color('blue'), 5.0) eq_(l.stroke.width, 5) eq_(l.stroke.opacity, 1) eq_(l.stroke.color, mapnik2.Color('blue')) eq_(l.stroke.line_cap, mapnik2.line_cap.BUTT_CAP) eq_(l.stroke.line_join, mapnik2.line_join.MITER_JOIN) s = mapnik2.Stroke(mapnik2.Color('blue'), 5.0) l = mapnik2.LineSymbolizer(s) eq_(l.stroke.width, 5) eq_(l.stroke.opacity, 1) eq_(l.stroke.color, mapnik2.Color('blue')) eq_(l.stroke.line_cap, mapnik2.line_cap.BUTT_CAP) eq_(l.stroke.line_join, mapnik2.line_join.MITER_JOIN)
def ogc_TextSymbolizer_to_mapnik(text): 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('[%s]' % 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) yield m_text
def test_textsymbolizer_init(): ts = mapnik2.TextSymbolizer(mapnik2.Expression('[Field_Name]'), 'Font Name', 8, mapnik2.Color('black')) eq_(str(ts.name), str(mapnik2.Expression('[Field_Name]'))) eq_(ts.face_name, 'Font Name') eq_(ts.text_size, 8) eq_(ts.fill, mapnik2.Color('black')) eq_(ts.label_placement, mapnik2.label_placement.POINT_PLACEMENT)
def test_shieldsymbolizer_init(): s = mapnik2.ShieldSymbolizer( mapnik2.Expression('[Field Name]'), 'DejaVu Sans Bold', 6, mapnik2.Color('#000000'), mapnik2.PathExpression('../data/images/dummy.png')) eq_(s.anchor, ( 0.0, 0.5, )) eq_(s.displacement, (0.0, 0.0)) eq_(s.allow_overlap, False) eq_(s.avoid_edges, False) eq_(s.character_spacing, 0) eq_(str(s.name), str(mapnik2.Expression('[Field Name]'))) eq_(s.face_name, 'DejaVu Sans Bold') eq_(s.allow_overlap, False) eq_(s.fill, mapnik2.Color('#000000')) eq_(s.force_odd_labels, False) eq_(s.halo_fill, mapnik2.Color('rgb(255,255,255)')) eq_(s.halo_radius, 0) eq_(s.label_placement, mapnik2.label_placement.POINT_PLACEMENT) eq_(s.minimum_distance, 0.0) eq_(s.text_ratio, 0) eq_(s.text_size, 6) eq_(s.wrap_width, 0) eq_(s.vertical_alignment, mapnik2.vertical_alignment.MIDDLE) eq_(s.label_spacing, 0) eq_(s.label_position_tolerance, 0) # 22.5 * M_PI/180.0 initialized by default assert_almost_equal(s.max_char_angle_delta, 0.39269908169872414) eq_(s.wrap_character, ' ') eq_(s.text_transform, mapnik2.text_transform.NONE) eq_(s.line_spacing, 0) eq_(s.character_spacing, 0) # r1341 eq_(s.wrap_before, False) eq_(s.horizontal_alignment, mapnik2.horizontal_alignment.MIDDLE) eq_(s.justify_alignment, mapnik2.justify_alignment.MIDDLE) eq_(s.opacity, 1.0) # r2300 eq_(s.minimum_padding, 0.0) # was mixed with s.opacity eq_(s.text_opacity, 1.0) eq_(s.shield_displacement, (0.0, 0.0)) # TODO - the pattern in bindings seems to be to get/set # strings for PathExpressions... should we pass objects? eq_(s.filename, '../data/images/dummy.png') eq_(s.transform, 'matrix(1, 0, 0, 1, 0, 0)') raise Todo( "FontSet pickling support needed: http://trac.mapnik2.org/ticket/348") eq_(s.fontset, '')
def test_polygonsymbolizer_init(): p = mapnik2.PolygonSymbolizer() eq_(p.fill, mapnik2.Color('gray')) eq_(p.fill_opacity, 1) p = mapnik2.PolygonSymbolizer(mapnik2.Color('blue')) eq_(p.fill, mapnik2.Color('blue')) eq_(p.fill_opacity, 1)
def test_setting_alpha(): w, h = 256, 256 im1 = mapnik2.Image(w, h) # white, half transparent im1.background = mapnik2.Color('rgba(255,255,255,.5)') # pure white im2 = mapnik2.Image(w, h) im2.background = mapnik2.Color('rgba(255,255,255,1)') im2.set_alpha(.5) eq_(len(im1.tostring()), len(im2.tostring()))
def test_color_pickle(): c = mapnik2.Color('blue') eq_(pickle.loads(pickle.dumps(c)), c) c = mapnik2.Color(0, 64, 128) eq_(pickle.loads(pickle.dumps(c)), c) c = mapnik2.Color(0, 64, 128, 192) eq_(pickle.loads(pickle.dumps(c)), c)
def test_get_color_with_max_value(): colorizer = mapnik2.RasterColorizer() c1 = mapnik2.Color("#0044cc") colorizer.append_band(0, c1) c2 = mapnik2.Color("#0055dd") colorizer.append_band(1, 2, c2) eq_(colorizer.get_color(-1), mapnik2.Color("transparent")) eq_(colorizer.get_color(0), c1) eq_(colorizer.get_color(0.5), c1) eq_(colorizer.get_color(1), c2) eq_(colorizer.get_color(1.5), c2) eq_(colorizer.get_color(2), mapnik2.Color("transparent"))
def test_polygonsymbolizer_init(): p = mapnik2.PolygonSymbolizer() eq_(p.fill, mapnik2.Color('gray')) eq_(p.fill_opacity, 1) eq_(p.placement, mapnik2.point_placement.CENTROID) p = mapnik2.PolygonSymbolizer(mapnik2.Color('blue')) p.placement = mapnik2.point_placement.INTERIOR eq_(p.fill, mapnik2.Color('blue')) eq_(p.fill_opacity, 1) eq_(p.placement, mapnik2.point_placement.INTERIOR)
def test_textsymbolizer_pickle(): ts = mapnik2.TextSymbolizer(mapnik2.Expression('[Field_Name]'), 'Font Name', 8, mapnik2.Color('black')) eq_(str(ts.name), str(mapnik2.Expression('[Field_Name]'))) eq_(ts.face_name, 'Font Name') eq_(ts.text_size, 8) eq_(ts.fill, mapnik2.Color('black')) raise Todo("text_symbolizer pickling currently disabled") ts2 = pickle.loads(pickle.dumps(ts, pickle.HIGHEST_PROTOCOL)) eq_(ts.name, ts2.name) eq_(ts.face_name, ts2.face_name) eq_(ts.allow_overlap, ts2.allow_overlap) eq_(ts.displacement, ts2.displacement) eq_(ts.anchor, ts2.anchor) eq_(ts.fill, ts2.fill) eq_(ts.force_odd_labels, ts2.force_odd_labels) eq_(ts.halo_fill, ts2.halo_fill) eq_(ts.halo_radius, ts2.halo_radius) eq_(ts.label_placement, ts2.label_placement) eq_(ts.minimum_distance, ts2.minimum_distance) eq_(ts.text_ratio, ts2.text_ratio) eq_(ts.text_size, ts2.text_size) eq_(ts.wrap_width, ts2.wrap_width) eq_(ts.vertical_alignment, ts2.vertical_alignment) eq_(ts.label_spacing, ts2.label_spacing) eq_(ts.label_position_tolerance, ts2.label_position_tolerance) # 22.5 * M_PI/180.0 initialized by default assert_almost_equal(s.max_char_angle_delta, 0.39269908169872414) eq_(ts.wrap_character, ts2.wrap_character) eq_(ts.text_transform, ts2.text_transform) eq_(ts.line_spacing, ts2.line_spacing) eq_(ts.character_spacing, ts2.character_spacing) # r1341 eq_(ts.wrap_before, ts2.wrap_before) eq_(ts.horizontal_alignment, ts2.horizontal_alignment) eq_(ts.justify_alignment, ts2.justify_alignment) eq_(ts.opacity, ts2.opacity) # r2300 eq_(s.minimum_padding, 0.0) raise Todo( "FontSet pickling support needed: http://trac.mapnik2.org/ticket/348") eq_(ts.fontset, ts2.fontset)
def stroke_to_mapnik(stroke): m_stroke = mapnik.Stroke() for css in stroke.CssParameter: if css.get('name') == 'stroke': m_stroke.color = mapnik.Color(css.text) elif css.get('name') == 'stroke-width': m_stroke.width = float(css.text) elif css.get('name') == 'stroke-opacity': m_stroke.opacity = float(css.text) elif css.get('name') == 'stroke-dasharray': dashes = map(float, css.text.strip().split(' ')) assert len(dashes) % 2 == 0, dashes for i in xrange(0, len(dashes), 2): m_stroke.add_dash(dashes[i], dashes[i + 1]) elif css.get('name') == 'stroke-linecap': m_stroke.line_cap = get_cap(css.text) elif css.get('name') == 'stroke-join': m_stroke.line_join = get_join(css.text) elif css.get('name') == 'stroke-linejoin': m_stroke.line_join = get_join(css.text) elif css.get('name') == 'stroke-dashoffset': m_stroke.dash_offset = float(css.text) else: raise Exception('unhanded: ' + css.get('name')) return m_stroke
def create_grid_map(width,height): places_ds = mapnik2.PointDatasource() places_ds.add_point(143.10,-38.60,'Name','South East') places_ds.add_point(142.48,-38.60,'Name','South West') places_ds.add_point(142.48,-38.38,'Name','North West') places_ds.add_point(143.10,-38.38,'Name','North East') s = mapnik2.Style() r = mapnik2.Rule() #symb = mapnik2.PointSymbolizer() symb = mapnik2.MarkersSymbolizer() symb.allow_overlap = True r.symbols.append(symb) label = mapnik2.TextSymbolizer(mapnik2.Expression('[Name]'), 'DejaVu Sans Book', 10, mapnik2.Color('black') ) label.allow_overlap = True label.displacement = (0,-10) #r.symbols.append(label) s.rules.append(r) lyr = mapnik2.Layer('Places') lyr.datasource = places_ds lyr.styles.append('places_labels') m = mapnik2.Map(width,height) m.append_style('places_labels',s) m.layers.append(lyr) return m
def add_gpx_style(self, gpx_style): """Adds mapnik style for styling GPX track This is blue 40% transparent 4 width line. Parameters --------- gpx_style : str Name of the style layer Note ---- This doesn't work for some reason. Style is added and if mapnik XML is saved and read it is used. """ self._lazy_init_map() style = mapnik.Style() style.filter_mode = mapnik.filter_mode.FIRST rule = mapnik.Rule() line_symbolizer = mapnik.LineSymbolizer() line_symbolizer.stroke = mapnik.Color('rgb(0%,0%,100%)') line_symbolizer.stroke_width = 4 line_symbolizer.stroke_opacity = 0.4 #line_symbolizer.simplify = 0.1 rule.symbols.append(line_symbolizer) style.rules.append(rule) self.m.append_style(gpx_style, style) print("Making style")
def rpc_renderCoordOld(self, lat, lon, zoom): """ Renders a map for the given coordinates. """ Map = None LandmassShapefile = 'cape/Interface/ne_110m_admin_0_countries.shp' im = mapnik.Image(self.mapsize[0], self.mapsize[1]) m = mapnik.Map(self.mapsize[0], self.mapsize[1]) m.background = mapnik.Color(self.backgroundColor) s = mapnik.Style() r = mapnik.Rule() polygon_symbolizer = mapnik.PolygonSymbolizer( mapnik.Color(self.foregroundColor)) r.symbols.append(polygon_symbolizer) line_symbolizer = mapnik.LineSymbolizer( mapnik.Color('rgb(50%,50%,50%)'), 0.1) r.symbols.append(line_symbolizer) s.rules.append(r) m.append_style('My Style', s) ds = mapnik.Shapefile(file=LandmassShapefile) layer = mapnik.Layer('world') layer.datasource = ds layer.styles.append('My Style') m.layers.append(layer) center = mapnik.Coord(lat, lon) transform = mapnik.ProjTransform(self.longlat, self.merc) merc_center = transform.forward(center) dx = (20037508.34 * 2 * (self.mapsize[0] / 2)) / (256 * (2**(zoom))) minx = merc_center.x - dx maxx = merc_center.x + dx m.aspect_fix_mode = mapnik.aspect_fix_mode.ADJUST_BBOX_HEIGHT merc_bbox = mapnik.Box2d(minx, merc_center.y - 1, maxx, merc_center.y + 1) m.zoom_to_box(merc_bbox) mapnik.render(m, im) Map = im.tostring('png') return (True, Map)
def test_polygonsymbolizer_pickle(): p = mapnik2.PolygonSymbolizer(mapnik2.Color('black')) p.fill_opacity = .5 # does not work for some reason... #eq_(pickle.loads(pickle.dumps(p)), p) p2 = pickle.loads(pickle.dumps(p,pickle.HIGHEST_PROTOCOL)) eq_(p.fill, p2.fill) eq_(p.fill_opacity, p2.fill_opacity)
def test_stroke_pickle(): s = mapnik2.Stroke(mapnik2.Color('black'),4.5) eq_(s.width, 4.5) eq_(s.color, mapnik2.Color('black')) s.add_dash(1,2) s.add_dash(3,4) s.add_dash(5,6) s2 = pickle.loads(pickle.dumps(s,pickle.HIGHEST_PROTOCOL)) eq_(s.color, s2.color) eq_(s.width, s2.width) eq_(s.opacity, s2.opacity) eq_(s.get_dashes(), s2.get_dashes()) eq_(s.line_cap, s2.line_cap) eq_(s.line_join, s2.line_join)
def test_color_init(): c = mapnik2.Color('blue') eq_(c.a, 255) eq_(c.r, 0) eq_(c.g, 0) eq_(c.b, 255) eq_(c.to_hex_string(), '#0000ff') c = mapnik2.Color('#f2eff9') eq_(c.a, 255) eq_(c.r, 242) eq_(c.g, 239) eq_(c.b, 249) eq_(c.to_hex_string(), '#f2eff9') c = mapnik2.Color('rgb(50%,50%,50%)') eq_(c.a, 255) eq_(c.r, 128) eq_(c.g, 128) eq_(c.b, 128) eq_(c.to_hex_string(), '#808080') c = mapnik2.Color(0, 64, 128) eq_(c.a, 255) eq_(c.r, 0) eq_(c.g, 64) eq_(c.b, 128) eq_(c.to_hex_string(), '#004080') c = mapnik2.Color(0, 64, 128, 192) eq_(c.a, 192) eq_(c.r, 0) eq_(c.g, 64) eq_(c.b, 128) eq_(c.to_hex_string(), '#004080')
def test_dataraster_coloring(): srs = '+init=epsg:32630' lyr = mapnik2.Layer('dataraster') lyr.datasource = mapnik2.Gdal( file='../data/raster/dataraster.tif', band=1, ) lyr.srs = srs _map = mapnik2.Map(256, 256, srs) style = mapnik2.Style() rule = mapnik2.Rule() sym = mapnik2.RasterSymbolizer() # Assigning a colorizer to the RasterSymbolizer tells the later # that it should use it to colorize the raw data raster sym.colorizer = mapnik2.RasterColorizer(mapnik2.COLORIZER_DISCRETE, mapnik2.Color("transparent")) for value, color in [ (0, "#0044cc"), (10, "#00cc00"), (20, "#ffff00"), (30, "#ff7f00"), (40, "#ff0000"), (50, "#ff007f"), (60, "#ff00ff"), (70, "#cc00cc"), (80, "#990099"), (90, "#660066"), (200, "transparent"), ]: sym.colorizer.add_stop(value, mapnik2.Color(color)) rule.symbols.append(sym) style.rules.append(rule) _map.append_style('foo', style) lyr.styles.append('foo') _map.layers.append(lyr) _map.zoom_to_box(lyr.envelope()) im = mapnik2.Image(_map.width, _map.height) mapnik2.render(_map, im) # save a png somewhere so we can see it save_data('test_dataraster_coloring.png', im.tostring('png')) imdata = im.tostring() # we have some values in the [20,30) interval so check that they're colored assert contains_word('\xff\xff\x00\xff', imdata)
def test_render_grid(): places_ds = mapnik2.PointDatasource() places_ds.add_point(143.10, -38.60, 'Name', 'South East') places_ds.add_point(142.48, -38.60, 'Name', 'South West') places_ds.add_point(142.48, -38.38, 'Name', 'North West') places_ds.add_point(143.10, -38.38, 'Name', 'North East') s = mapnik2.Style() r = mapnik2.Rule() #symb = mapnik2.PointSymbolizer() symb = mapnik2.MarkersSymbolizer() symb.allow_overlap = True r.symbols.append(symb) label = mapnik2.TextSymbolizer(mapnik2.Expression('[Name]'), 'DejaVu Sans Book', 10, mapnik2.Color('black')) label.allow_overlap = True label.displacement = (0, -10) #r.symbols.append(label) s.rules.append(r) lyr = mapnik2.Layer('Places') lyr.datasource = places_ds lyr.styles.append('places_labels') m = mapnik2.Map(256, 256) m.append_style('places_labels', s) m.layers.append(lyr) ul_lonlat = mapnik2.Coord(142.30, -38.20) lr_lonlat = mapnik2.Coord(143.40, -38.80) m.zoom_to_box(mapnik2.Box2d(ul_lonlat, lr_lonlat)) grid = mapnik2.render_grid(m, 0, key='Name', resolution=4, fields=['Name']) eq_(grid, grid_correct) eq_(resolve(grid, 0, 0), None) # check every pixel of the nw symbol expected = {"Name": "North West"} # top row eq_(resolve(grid, 23, 9), expected) eq_(resolve(grid, 23, 10), expected) eq_(resolve(grid, 23, 11), expected) # core eq_(resolve(grid, 24, 8), expected) eq_(resolve(grid, 24, 9), expected) eq_(resolve(grid, 24, 10), expected) eq_(resolve(grid, 24, 11), expected) eq_(resolve(grid, 24, 12), expected) eq_(resolve(grid, 25, 8), expected) eq_(resolve(grid, 25, 9), expected) eq_(resolve(grid, 25, 10), expected) eq_(resolve(grid, 25, 11), expected) eq_(resolve(grid, 25, 12), expected) # bottom row eq_(resolve(grid, 26, 9), expected) eq_(resolve(grid, 26, 10), expected) eq_(resolve(grid, 26, 11), expected)
def test_stroke_init(): s = mapnik2.Stroke() eq_(s.width, 1) eq_(s.opacity, 1) eq_(s.color, mapnik2.Color('black')) eq_(s.line_cap, mapnik2.line_cap.BUTT_CAP) eq_(s.line_join, mapnik2.line_join.MITER_JOIN) eq_(s.gamma,1.0) s = mapnik2.Stroke(mapnik2.Color('blue'), 5.0) s.gamma = .5 eq_(s.width, 5) eq_(s.opacity, 1) eq_(s.color, mapnik2.Color('blue')) eq_(s.gamma, .5) eq_(s.line_cap, mapnik2.line_cap.BUTT_CAP) eq_(s.line_join, mapnik2.line_join.MITER_JOIN)
def to_mapnik(self): stroke = mapnik.Stroke(mapnik.Color(str(self.color)), self.width) stroke.opacity = self.opacity or stroke.opacity stroke.line_cap = self.cap or stroke.line_cap stroke.line_join = self.join or stroke.line_join if self.dashes: stroke.add_dash(*self.dashes.values) sym = mapnik.LineSymbolizer(stroke) return sym
def test_render_image_to_string(): i = mapnik2.Image(256, 256) i.background = mapnik2.Color('black') s = i.tostring() eq_(s, 256 * 256 * '\x00\x00\x00\xff') s = i.tostring('png')
def to_mapnik(self): sym = mapnik.ShieldSymbolizer( mapnik.Expression(self.name), self.face_name, self.size, mapnik.Color(str(self.color)) if self.color else None, self.file, self.type, self.width, self.height) sym.character_spacing = self.character_spacing or sym.character_spacing sym.line_spacing = self.line_spacing or sym.line_spacing sym.spacing = self.spacing or sym.line_spacing sym.minimum_distance = self.min_distance or sym.minimum_distance return sym
def ogc_RasterSymbolizer_to_mapnik(sym): if hasattr(sym, 'ColorMap'): m_sym = mapnik.RasterSymbolizer() type = sym.ColorMap.attrib.get('type', 'ramp') type_map = { 'ramp': mapnik.COLORIZER_LINEAR, 'intervals': mapnik.COLORIZER_DISCRETE, 'values': mapnik.COLORIZER_EXACT, } mode = type_map[type] colorizer = mapnik.RasterColorizer(mode, mapnik.Color('transparent')) for c in sym.ColorMap.ColorMapEntry: color = mapnik.Color(c.attrib['color']) color.a = int(float(c.attrib.get('opacity', 1)) * 255) value = float(c.attrib['quantity']) label = c.attrib.get('label', '') stop = mapnik.ColorizerStop(value, mapnik.COLORIZER_INHERIT, color) stop.label = label colorizer.add_stop(stop) m_sym.colorizer = colorizer yield m_sym
def to_mapnik(self): sym = mapnik.TextSymbolizer(mapnik.Expression(self.name), self.face_name, self.size, mapnik.Color(str(self.color))) sym.wrap_width = self.wrap_width or sym.wrap_width sym.label_spacing = self.spacing or sym.label_spacing sym.label_position_tolerance = self.label_position_tolerance or sym.label_position_tolerance sym.max_char_angle_delta = self.max_char_angle_delta or sym.max_char_angle_delta sym.halo_fill = mapnik.Color(str( self.halo_color)) if self.halo_color else sym.halo_fill sym.halo_radius = self.halo_radius or sym.halo_radius sym.character_spacing = self.character_spacing or sym.character_spacing sym.line_spacing = self.line_spacing or sym.line_spacing sym.avoid_edges = self.avoid_edges.value if self.avoid_edges else sym.avoid_edges sym.minimum_distance = self.min_distance or sym.minimum_distance sym.allow_overlap = self.allow_overlap.value if self.allow_overlap else sym.allow_overlap sym.text_transform = self.text_transform if self.text_transform else sym.text_transform sym.displacement(self.dx or 0, self.dy or 0) return sym
def generate_mapnik_style(self, user_id, global_id): style = mapnik.Style() for color in self.load_colors(user_id, global_id): c = mapnik.Color(str(color)) line = mapnik.LineSymbolizer(c, 1.5) line.stroke.opacity = 0.7 poly = mapnik.PolygonSymbolizer(c) poly.fill_opacity = 0.5 rule = mapnik.Rule() rule.filter = mapnik.Filter(str("[color] = '%s'" % (color,))) rule.symbols.extend([poly,line]) style.rules.append(rule) return style
def get(self): mapfile = os.path.join(DATA_PATH, 'hazard', 'flood.shp') #mapfile = os.path.join(DATA_PATH, 'impact', 'impact.json') filename = os.path.join(DATA_PATH, 'pdf', 'report_map.pdf') filename2 = os.path.join(DATA_PATH, 'pdf', 'report_map.png') map = mapnik2.Map(600, 400) map.background = mapnik2.Color('steelblue') s = mapnik2.Style() r = mapnik2.Rule() polygon_symbolizer = mapnik2.PolygonSymbolizer( mapnik2.Color('#f2eff9')) r.symbols.append(polygon_symbolizer) line_symbolizer = mapnik2.LineSymbolizer( mapnik2.Color('rgb(50%,50%,50%)'), 0.1) r.symbols.append(line_symbolizer) s.rules.append(r) map.append_style('My Style', s) ds = mapnik2.Shapefile(file=mapfile) layer = mapnik2.Layer('world') layer.datasource = ds layer.styles.append('My Style') map.layers.append(layer) map.zoom_all() mapnik2.render_to_file(map, filename2, 'png') #bbox = mapnik.Envelope(-180.0,-90.0,180.0,90.0) #map.zoom_to_box(bbox) surface = cairo.PDFSurface(filename, map.width, map.height) mapnik2.render(map, surface) surface.finish() with open(filename) as data: pdf = data.read() data.close() self.set_header("Content-Type", "application/pdf") self.write(pdf)
def test_get_color_discrete(): #setup colorizer = mapnik2.RasterColorizer() colorizer.default_color = mapnik2.Color(0, 0, 0, 0) colorizer.default_mode = mapnik2.COLORIZER_DISCRETE colorizer.add_stop(10, mapnik2.Color(100, 100, 100, 100)) colorizer.add_stop(20, mapnik2.Color(200, 200, 200, 200)) #should be default colour eq_(colorizer.get_color(-50), mapnik2.Color(0, 0, 0, 0)) eq_(colorizer.get_color(0), mapnik2.Color(0, 0, 0, 0)) #now in stop 1 eq_(colorizer.get_color(10), mapnik2.Color(100, 100, 100, 100)) eq_(colorizer.get_color(19), mapnik2.Color(100, 100, 100, 100)) #now in stop 2 eq_(colorizer.get_color(20), mapnik2.Color(200, 200, 200, 200)) eq_(colorizer.get_color(1000), mapnik2.Color(200, 200, 200, 200))
def generate_mapnik_map(self, user_id, global_id): m = mapnik.Map(256, 256) style = self.generate_mapnik_style(user_id, global_id) m.background = mapnik.Color("transparent") m.append_style("ZIP_CODES STYLE", style) m.srs = "+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" layer = mapnik.Layer('ZIP_CODES') layer.datasource = self.create_mapnik_datasource(user_id, global_id) layer.styles.append("ZIP_CODES STYLE") m.layers.append(layer) return m