Reputation: 1
My original problem:
I have several polygons stacked on top of each other and I'm trying to use the geopandas overlay with union method to get all those possible geometries returned.
I did not get it to work so I tried the example code directly, ref. https://geopandas.org/en/stable/docs/user_guide/set_operations.html:
from shapely.geometry import Polygon
import geopandas
polys1 = geopandas.GeoSeries([Polygon([(0,0), (2,0), (2,2), (0,2)]),
Polygon([(2,2), (4,2), (4,4), (2,4)])])
polys2 = geopandas.GeoSeries([Polygon([(1,1), (3,1), (3,3), (1,3)]),
Polygon([(3,3), (5,3), (5,5), (3,5)])])
df1 = geopandas.GeoDataFrame({'geometry': polys1, 'df1':[1,2]})
df2 = geopandas.GeoDataFrame({'geometry': polys2, 'df2':[1,2]})
ax = df1.plot(color='red');
df2.plot(ax=ax, color='green', alpha=0.5);
res_union = df1.overlay(df2, how='union')
res_union
But i get the following error:
IntCastingNaNError: Cannot convert non-finite values (NA or inf) to integer
I have tried all the other methods as well: ['intersection', 'union', 'identity', 'symmetric_difference', 'difference'] but the only ones that are working are the 'intersection' and 'difference '.
------- Addedd 08.03.2022 ------- This is the full path from the error which is thrown
----> 1 geopandas.overlay(df1, df2, how='union')IntCastingNaNError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_19804/2462211871.py in
~\AppData\Local\Programs\Python\Python39\lib\site-packages\geopandas\tools\overlay.py in overlay(df1, df2, how, keep_geom_type, make_valid) 319 result = _overlay_symmetric_diff(df1, df2) 320 elif how == "union": --> 321 result = _overlay_union(df1, df2) 322 elif how == "identity": 323 dfunion = _overlay_union(df1, df2)
~\AppData\Local\Programs\Python\Python39\lib\site-packages\geopandas\tools\overlay.py in _overlay_union(df1, df2) 135 """ 136 dfinter = _overlay_intersection(df1, df2) --> 137 dfsym = _overlay_symmetric_diff(df1, df2) 138 dfunion = pd.concat([dfinter, dfsym], ignore_index=True, sort=False) 139 # keep geometry column last
~\AppData\Local\Programs\Python\Python39\lib\site-packages\geopandas\tools\overlay.py in _overlay_symmetric_diff(df1, df2) 115 _ensure_geometry_column(dfdiff2) 116 # combine both 'difference' dataframes --> 117 dfsym = dfdiff1.merge( 118 dfdiff2, on=["__idx1", "__idx2"], how="outer", suffixes=("_1", "_2") 119 )
~\AppData\Local\Programs\Python\Python39\lib\site-packages\geopandas\geodataframe.py in merge(self, *args, **kwargs) 1376 1377 """ -> 1378 result = DataFrame.merge(self, *args, **kwargs) 1379 geo_col = self._geometry_column_name 1380 if isinstance(result, DataFrame) and geo_col in result:
~\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\frame.py in merge(self, right, how, on, left_on, right_on, left_index, right_index, sort, suffixes, copy, indicator, validate) 9189
from pandas.core.reshape.merge import merge 9190 -> 9191 return merge( 9192 self, 9193 right,~\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\reshape\merge.py in merge(left, right, how, on, left_on, right_on, left_index, right_index, sort, suffixes, copy, indicator, validate) 118 validate=validate, 119 ) --> 120 return op.get_result() 121 122
~\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\reshape\merge.py in get_result(self) 734 result = self._indicator_post_merge(result) 735 --> 736 self._maybe_add_join_keys(result, left_indexer, right_indexer) 737 738 self._maybe_restore_index_levels(result)
~\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\reshape\merge.py in _maybe_add_join_keys(self, result, left_indexer, right_indexer) 915 916 if result._is_label_reference(name): --> 917 result[name] = Series( 918 key_col, dtype=result_dtype, index=result.index 919 )
~\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\series.py in init(self, data, index, dtype, name, copy, fastpath) 381 if dtype is not None: 382 # astype copies --> 383 data = data.astype(dtype) 384 else: 385 # GH#24096 we need to ensure the index remains immutable
~\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\indexes\numeric.py in astype(self, dtype, copy) 221 # TODO(jreback); this can change once we have an EA Index type 222 # GH 13149 --> 223 arr = astype_nansafe(self._values, dtype=dtype) 224 return Int64Index(arr, name=self.name) 225
~\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\dtypes\cast.py in astype_nansafe(arr, dtype, copy, skipna) 1166 1167 elif np.issubdtype(arr.dtype, np.floating) and np.issubdtype(dtype, np.integer): -> 1168 return astype_float_to_int_nansafe(arr, dtype, copy) 1169 1170 elif is_object_dtype(arr):
~\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\dtypes\cast.py in astype_float_to_int_nansafe(values, dtype, copy) 1211 """
1212 if not np.isfinite(values).all(): -> 1213 raise IntCastingNaNError( 1214 "Cannot convert non-finite values (NA or inf) to integer" 1215 )IntCastingNaNError: Cannot convert non-finite values (NA or inf) to integer
Upvotes: 0
Views: 537
Reputation: 1
code has no problem checkout your geopandas version by :
import geopandas as gpd
gpd.__version__
if it was not 0.10.2 update it
Upvotes: 0