Draw line on map with mapnik

67 Views Asked by At

I want to draw a line on a map by the pixel or lat/long coordinates with mapnik in python. I tried many different ways and wasn´t able to make it. The map is printed, but the layer with the Line is not appearing.

Can someone please tell me where the mistake is?

Here is my code so far:

import mapnik
from pyproj import Transformer

x1, y1 = 27.86639552869006, 86.81653976440431
x2, y2 = 27.82556560989474, 86.74109458923341

transformer = Transformer.from_crs(“EPSG:4326”, “EPSG:3857”)
x1, y1 = transformer.transform(x1, y1)
x2, y2 = transformer.transform(x2, y2)

mapnik_xml = “/home/renderer/src/openstreetmap-carto/mapnik.xml”
map_output = “/scripts/output.svg”

m = mapnik.Map(2496, 582)

mapnik.load_map(m, mapnik_xml)
bbox = mapnik.Box2d(x1, y1, x2, y2)
m.zoom_to_box(bbox)

style = mapnik.Style()
rule = mapnik.Rule()
line_symbolizer = mapnik.LineSymbolizer()
line_symbolizer.allow_overlap = True
line_symbolizer.opacity = 1
line_symbolizer.stroke = mapnik.Color(‘rgb(0%,48.2%,100%)’)
line_symbolizer.stroke_width = 3
line_symbolizer.stroke_linejoin = ‘round’
line_symbolizer.stroke_linecap = ‘round’
line_symbolizer.smooth = 1
line_symbolizer.simplify = 5
line_symbolizer.simplify_algorithm = ‘douglas-peucker’
rule.symbols.append(line_symbolizer)
style.rules.append(rule)
m.append_style(‘GPS_tracking_points’, style)

layer = mapnik.Layer(‘GPS_tracking_points’)

md = mapnik.MemoryDatasource()
feature = mapnik.Feature(mapnik.Context(), 1)
feature.geometry = mapnik.Geometry.from_wkt(‘LINESTRING(456 280, 456 281, 456 270, 462 277)’)
md.add_feature(feature)

layer.datasource = md

layer.styles.append(‘GPS_tracking_points’)
m.layers.append(layer)

mapnik.render_to_file(m, map_output)
0

There are 0 best solutions below