0

(I manged to solve this by installing anaconda and installing geopandas in a new environment)

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

IntCastingNaNError                        Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_19804/2462211871.py in
----> 1 geopandas.overlay(df1, df2, how='union')

~\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

2
  • this post may help you,stackoverflow.com/questions/53546775/…, df1.union(df2) works Commented Mar 3, 2022 at 10:55
  • df1.union(df2) works but give me one union, and not all possible geometries as the overlay with how="union" method would have given me. I have read the post, but I don't find a solution to my problem. I have installed Rtree, but when it comes to libspatialindex, I'm not sure exactly what to do. Commented Mar 4, 2022 at 12:33

1 Answer 1

0

code has no problem checkout your geopandas version by :

import geopandas as gpd
gpd.__version__

if it was not 0.10.2 update it

Sign up to request clarification or add additional context in comments.

1 Comment

I checked and my geopandas version is 0.10.2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.