Render Leaflet map with Mapnik including GPX

Hey, I have a problem rendering a map with mapnik.

I have a frontend which is using leaflet and the leaflet-gpx plugin. That means I can put gpx tracks on my map.
I send the map details and the gpx files to my backend because I want to render excactly the same map.

In the code of leaflet I saw, that it´s using the following code to simplify lines:

(See “simplify” method)

I am already able to render my map and put gpx track on the map. But the line does not look excactly like in my frontend. I assume it´s because of the different usage of the simplify for gpx track.
In leaflet I saw that it´s reducing the track before using the algorithm for simplifying. But I can´t use excactly the same, because I just have the lat/long coordinates and not the x/y pixel coordinates.

Has someone experience with that or can help me to make the gpx track look like in leaflet?

My python code so far:

import mapnik
from pyproj import Transformer

# Nepal
# x1, y1 = 30.4477, 88.2015
# x2, y2 = 26.3477, 80.0586
# x1, y1 = 28.0049333, 86.8210681
# x2, y2 = 27.6837997, 86.7239873
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(879, 538)

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

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'
m.append_style('GPS_tracking_points', style)

layer = mapnik.Layer('GPS_tracking_points')
layer.datasource = mapnik.Ogr(file='/scripts/20230426_2.gpx', layer='tracks')

mapnik.render_to_file(m, map_output)

1 post - 1 participant

Read full topic

Ce sujet de discussion accompagne la publication sur