-1

I would like to use Folium map to plot markers. My locations are in France. I have latitude and longitude information. So I create POINT geometry in order to implement them in Folium map.

df = pd.read_csv('./data/addresses_geocoded.csv', sep = ';', encoding = 'latin-1')
geometry = [Point(xy) for xy in zip(df['latitude'], df['longitude'])] 
geo_df = gpd.GeoDataFrame (df[['type', 'contrat']], geometry = geometry, crs={'init':'epsg:4326'})

Then, I create a map and add geodata.

map_contrat = folium.Map(location=[45.7174, 4.9036], tiles='openstreetmap', zoom_start=12)

As I want to be able to select or hide data from legend, I add geodata using features.

folium.features.GeoJson(geo_df[geo_df['contract'] == "A"], name="A").add_to(map_contrat)
folium.features.GeoJson(geo_df[geo_df['contract'] == "B"], name="B").add_to(map_contrat)

But, as I understand, due to wrong crs or epsg, my data is not located it the right place.

I am a bit lost with all possibility to chose epsg. It may be a trick to knwow which one to use ?

Thanks in advance

  • `geometry = [Point(xy) for xy in zip(df['latitude'], df['longitude'])]` <-- I think these are in the wrong order. x = lon, y = lat. – Michael Delgado Aug 25 '22 at 19:36

1 Answers1

0

The most typical CRS is EPSG:4326 which you have used. Have used a CSV that contains cities in the world and select 100 French cities. If I use longitude as latitude and latitude as longitude (erroneously transpose them) then the cities appear in Africa! As demonstrated by red markers in folium map. Correct order (blue) they are in France.

import pandas as pd
import geopandas as gpd

# 100 cities in france from CSV
df = (
    pd.read_csv(
        "https://raw.githubusercontent.com/dr5hn/countries-states-cities-database/master/csv/cities.csv"
    )
    .loc[lambda d: d["country_code"].eq("FR")]
    .sample(100)
)

# incorrect order of lat / lon.  appears in africae
m = gpd.GeoDataFrame(
    df, geometry=gpd.points_from_xy(df["latitude"], df["longitude"]), crs="epsg:4386"
).explore(color="red", width=300, height=300, name="wrong")

# correct order - all good in France
gpd.GeoDataFrame(
    df, geometry=gpd.points_from_xy(df["longitude"], df["latitude"]), crs="epsg:4386"
).explore(m=m, width=300, height=300, name="correct")

enter image description here

Rob Raymond
  • 29,118
  • 3
  • 14
  • 30