0

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?

Jc LE BERRE
  • 3
  • 1
  • 4

1 Answers1

3
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
linog
  • 5,786
  • 3
  • 14
  • 28
  • Thank you linog for your answer, when i try your code i get the following message: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation. – Jc LE BERRE Jul 01 '21 at 13:10
  • That's normal. Your shapefile uses WGS84 (`df.crs`). You lose some precision in geometric manipulations with this projection. If you don't want this warning, you might use some official projected shapefiles from French administration that use Lambert 93 – linog Jul 01 '21 at 13:45
  • Ok thank you very much i was able to do the job with your code – Jc LE BERRE Jul 01 '21 at 14:02