Search code examples
pythonplotlypolygongeojsoncentroid

extract coordinates of the center of polygons in a geoJson file in python


I would like to extract the coordinate of the center of each polygon contained in a geojson file.

here is my geojson file: https://france-geojson.gregoiredavid.fr/repo/departements.geojson

What i want is a list of latitude and longitude of the centers of different departments so i can use it to display the code of the department on a choroplethmap in plotly as a scattermapbox.

How can I do that?


Solution

  • import geopandas as gpd
    
    df = gpd.read_file("https://france-geojson.gregoiredavid.fr/repo/departements.geojson")
    
    df.head(2)
    
    df["lon"] = df["geometry"].centroid.x
    df["lat"] = df["geometry"].centroid.y
    df