Need assistance suppressing specific UserWarning in Python code execution

29 Views Asked by At

I'm encountering a persistent "UserWarning" during the execution of my Python code and have been unable to suppress it despite trying various methods. I'd appreciate some guidance on resolving this issue.

Here's the context:

I have a function in my code called get_water_data, which downloads water data and performs some operations. However, during the execution of this function, I encounter the following warning:

UserWarning: keep_geom_type can not be called on a mixed type GeoDataFrame.

Here's the function:

def get_water_data(north, south, east, west, mask_gdf):
 print("Download water_data in progress....")

 tags= {"natural":["water"], "waterway":["river"]}
 gdf = ox.features_from_bbox(north, south, east, west, tags)
 gdf_reproj = ox.project_gdf(gdf, to_crs="EPSG:3857")

 gdf_clipped= gpd.clip(gdf_reproj, mask_gdf, keep_geom_type=True)
 gdf_clipped = gdf_clipped[(gdf_clipped['geometry'].geom_type =='Polygon') | (gdf_clipped['geometry'].geom_type == 'LineString')]

 print("water_data ...OK\n")
 return gdf_clipped

I've attempted to suppress this warning using methods commonly recommended, including:

1.Importing the warnings module before all others and using warnings.filterwarnings("ignore", category=UserWarning, module="geopandas") in the scope of the function like this :

import warnings
import geopandas as gpd

def get_water_data(north, south, east, west, mask_gdf):
 warnings.filterwarnings("ignore", category=UserWarning, module="geopandas").
 print("Download water_data in progress....")

 tags= {"natural":["water"], "waterway":["river"]}
 gdf = ox.features_from_bbox(north, south, east, west, tags)
 gdf_reproj = ox.project_gdf(gdf, to_crs="EPSG:3857")

 gdf_clipped= gpd.clip(gdf_reproj, mask_gdf, keep_geom_type=True)
 gdf_clipped = gdf_clipped[(gdf_clipped['geometry'].geom_type =='Polygon') | (gdf_clipped['geometry'].geom_type == 'LineString')]

 print("water_data ...OK\n")
 return gdf_clipped

2.Trying a total suppression of warnings using warnings.filterwarnings("ignore"). However, the warning persists.

Could someone provide guidance on effectively suppressing this specific UserWarning? Any insights or alternative approaches would be greatly appreciated.

Thank you in advance!

0

There are 0 best solutions below