TransWikia.com

How to use custom projection in Folium

Geographic Information Systems Asked by FLOWMEEN on October 5, 2021

I think Folium uses EPSG3857 by default, but I have a custom .prj file that I need to use. Is there a way to load that in? I’m not reading in the projection file at all right now.

This is my current code:

gdf = gpd.read_file('test.shp')
# gdf_wgs84 = gdf.to_crs({'init': 'epsg:4326'})
gj = json.loads(gdf.to_json())
m = folium.Map(location=[36.6, -119.6], zoom_start=9, tiles=basemap)
folium.GeoJson(gj, style_function=lambda feature: {
    # 'fillColor': feature['properties']['RGBA'],
    # 'color' : feature['properties']['RGBA'],
    'weight' : 1,
    'fillOpacity' : 0,
}).add_to(m)

folium.LayerControl().add_to(m)

# Save map
m.save(save_name("name", ".html"))

One Answer

You can use this solution to convert you .prj file to proj4 string and then use this string to convert your coordinates to lat-lon before plotting with Folium:

import folium
import pandas as pd
import pyproj

#create dataset
data = {'y':[67480.7, 67500.7, 67520.5],
        'x':[220850.6, 220868.4, 220888.7]}

df = pd.DataFrame(data)


# proj4 string
p = pyproj.Proj("+proj=lcc +lat_1=51.16666723333333 +lat_2=49.8333339 +lat_0=90 +lon_0=4.367486666666666 +x_0=150000.013 +y_0=5400088.438 +ellps=intl +towgs84=-99.059,53.322,-112.486,0.419,-0.830,1.885,-1")


def convert_to_LatLon(df, p):
    lon, lat = p(df['x'], df['y'], inverse=True)
    df['lat'] = lat
    df['lon'] = lon
    return df

df = df.apply(convert_to_LatLon, p=p, axis=1)
df.head()

# change location and zoom accordingly
m =folium.Map(location=[49.914623, 5.355284], zoom_start=16, max_zoom=25)
for index, row in df.iterrows():
    lat, lon = row[['lat', 'lon']]
    folium.CircleMarker(
        radius=5,
        location=[lat, lon],
        popup=index,
        color='crimson',
        fill=False,
    ).add_to(m)
m

Answered by GStav on October 5, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP