Search code examples
pythongeopandasfolium

Folium map warning


Based on previuos question Previous Question

i have implemented following code :

import pandas as pd
import folium
import geopandas as gpd
import geopandas
data =pd.read_excel("https://geostat.ge/media/48824/13_Earnings-by-regions_annual.xlsx",skiprows=[0,1])
data.rename(columns={'Unnamed: 0':"Regions"},inplace=True)
data.dropna(axis=0,inplace=True)
data.drop(index=0,inplace=True)
data["Total"] =data.sum(axis=1,numeric_only=True)
data =data[["Regions","Total"]]
data["Country"] ="Georgia"
print(data.head())
url = "https://github.com/wmgeolab/geoBoundaries/raw/905b0ba/" \
      "releaseData/gbOpen/GEO/ADM1/geoBoundaries-GEO-ADM1_simplified.geojson"

pgs = gpd.read_file(url)
pgs["shapeName"] = pgs["shapeName"].replace(
                              {"Adjara": "Adjara A.R.", "–": "-"}, regex=True)

out = pgs.merge(data, left_on="shapeName", right_on="Regions")
# df1.to_crs('+proj=cea').centroid.to_crs(df1.crs)
m = folium.Map(location=[pgs.centroid.y.mean(), pgs.centroid.x.mean()], zoom_start=7)

folium.Choropleth(
    geo_data=out,
    name="choropleth",
    data=data,
    columns=["Regions", "Total"],
    key_on="properties.shapeName",
    fill_color="RdGy",
    fill_opacity=0.8,
    line_opacity=0.8,
    legend_name="Spent Money",
).add_to(m)

m.save("Money_Spent.html")

but it gave me warning :

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.

  m = folium.Map(location=[pgs.centroid.y.mean(), pgs.centroid.x.mean()], zoom_start=7)

i have searched and found this link : https://gis.stackexchange.com/questions/372564/userwarning-when-trying-to-get-centroid-from-a-polygon-geopandas

but did not get exactly how can i apply it to my code? please help me


Solution

  • It's just a warning and in your case, it's not really a big issue because the centroid is used in your code to just find a pair of coordinates that will let us center the map (to point right above the center of Georgia). So, even if the calculations wouldn't be precise enough, the map will got centered due to the small surfaces of the polygons/regions (as explained by @Louis Cotterreau here).

    But if you nevertheless want to get rid of the warning, you can use this :

    #pip install pyproj
    from pyproj import Transformer
    
    transformer = Transformer.from_crs("EPSG:32638", "EPSG:4326", always_xy=True)
    
    x, y = [pgs.to_crs(32638).centroid.x.mean(), pgs.to_crs(32638).centroid.y.mean()]
    
    lon, lat = transformer.transform(x, y)
    
    m = folium.Map(location=(lat, lon), zoom_start=7)