Skip to content
Advertisement

How can you use RD coordinates instead of gps coordinates to plot traffic routes in a folium plot?

In want to show traffic routes on a folium map. These routes are in the Netherlands and the coordinates are in RD (EPSG:28992). I tried to map the routes using the following code:

my_map = folium.Map(location=(52.2130,5.2794), tiles='cartodbpositron', zoom_start=7, control_scale=True)

origin = [rap.rd_to_wgs(lon, lat) for lon, lat in zip(ontwaterd['dpsRDcod_x'],ontwaterd['dpsRDcod_y'])]
dest = [rap.rd_to_wgs(lon, lat) for lon, lat in zip(ontwaterd['dps2RDcod_X'],ontwaterd['dps2RDcod_Y'])]
coords = list(zip(origin, dest))


def line_objects_for_airport(zip_coords, airport_name, color_):
    new_route = folium.FeatureGroup(name = airport_name)
    for coord in zip_coords:
        new_route = new_route.add_child(folium.PolyLine(locations=[coord[0], coord[1]], weight=.5, color=color_))
    return new_route

route = line_objects_for_airport(coords, 'Test', color_ = 'green')
my_map.add_child(route)

folium.LayerControl().add_to(my_map)
my_map.save("test1.html")
webbrowser.open("test1.html")

But this won’t work and this results in a random red line:

enter image description here

However it does work whenever I transform the RD coordinates to GPS coordinates:

   from osgeo.osr import SpatialReference, CoordinateTransformation

epsg28992 = SpatialReference()
epsg4326 = SpatialReference()
epsg28992.ImportFromEPSG(28992)
epsg4326.ImportFromEPSG(4326)
latlon2rd = CoordinateTransformation(epsg4326, epsg28992)
rd2latlon = CoordinateTransformation(epsg28992, epsg4326)


def rd_to_wgs(lon, lat):
    
    gps = rd2latlon.TransformPoint(lon, lat)
    
    return gps[0], gps[1]

origin = [rd_to_wgs(lon, lat) for lon, lat in zip(ontwaterd['dpsRDcod_x'],ontwaterd['dpsRDcod_y'])]
dest = [rd_to_wgs(lon, lat) for lon, lat in zip(ontwaterd['dps2RDcod_X'],ontwaterd['dps2RDcod_Y'])]
coords = list(zip(origin, dest))

enter image description here

This looks good but I rather plot the lines using the RD coordinates. Because then it will be more precise, since this coordinate system is established especially for the Netherlands.

Anyone who knows how to adjust the above code so it works with the original RD coordinates?

Anything would help!

Advertisement

Answer

See this question on GIS stack exchange: https://gis.stackexchange.com/questions/198695/leaflet-changing-base-map-crs. It references the Leaflet WMS docs. See especially the “Notes to GIS users of WMS services”.

The issue is that the base map itself is in a specific CRS. Web mapping platforms are designed to render geographic data on top of tiled images extremely quickly. Because of this, leaflet does not support re-projecting raster data or image tiles at all, and only supports 3 CRS options for tilesets: EPSG:3857 (web mercator), EPSG:3395 (world mercator), and EPSG:4326 (WGS84).

So unfortunately, the answer is that while you can use geopandas and folium to reproject your data and visualize it using leaflet using the tileset’s CRS, you can’t folium/leaflet to reproject map tiles into your CRS :/

Advertisement