Skip to content

Commit 77b2c29

Browse files
Blizzeqfneum
andauthored
Fix compute_indicatormatrix and compute_intersectionmatrix for non-default index (#534)
* Fix compute_indicatormatrix and compute_intersectionmatrix for non-default index * Add pull request link to release note --------- Co-authored-by: Fabian Neumann <fabian.neumann@outlook.de>
1 parent 01a5ced commit 77b2c29

3 files changed

Lines changed: 45 additions & 16 deletions

File tree

‎atlite/gis.py‎

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,8 @@ def compute_indicatormatrix(
185185
orig = orig.geometry if isinstance(orig, gpd.GeoDataFrame) else orig
186186
dest = dest.geometry if isinstance(dest, gpd.GeoDataFrame) else dest
187187
dest = reproject_shapes(dest, dest_crs, orig_crs)
188-
orig_list: list[Any] | pd.Series = (
189-
list(orig) if not isinstance(orig, pd.Series) else orig
190-
)
191-
dest_list: list[Any] | pd.Series = (
192-
list(dest) if not isinstance(dest, pd.Series) else dest
193-
)
188+
orig_list: list[Any] = list(orig)
189+
dest_list: list[Any] = list(dest)
194190
indicator = sp.sparse.lil_matrix((len(dest_list), len(orig_list)), dtype=float)
195191
tree = STRtree(orig_list)
196192
idx = {hash(o.wkt): i for i, o in enumerate(orig_list)}
@@ -199,9 +195,11 @@ def compute_indicatormatrix(
199195
for o in tree.query(d):
200196
# STRtree query returns a list of indices for shapely >= v2.0
201197
if isinstance(o, (int | np.integer)):
202-
o = orig_list[o]
203-
if o.intersects(d):
198+
j = int(o)
199+
o = orig_list[j]
200+
else:
204201
j = idx[hash(o.wkt)]
202+
if o.intersects(d):
205203
area = d.intersection(o).area
206204
indicator[i, j] = area / o.area
207205

@@ -242,12 +240,8 @@ def compute_intersectionmatrix(
242240
orig = orig.geometry if isinstance(orig, gpd.GeoDataFrame) else orig
243241
dest = dest.geometry if isinstance(dest, gpd.GeoDataFrame) else dest
244242
dest = reproject_shapes(dest, dest_crs, orig_crs)
245-
orig_list: list[Any] | pd.Series = (
246-
list(orig) if not isinstance(orig, pd.Series) else orig
247-
)
248-
dest_list: list[Any] | pd.Series = (
249-
list(dest) if not isinstance(dest, pd.Series) else dest
250-
)
243+
orig_list: list[Any] = list(orig)
244+
dest_list: list[Any] = list(dest)
251245
intersection = sp.sparse.lil_matrix((len(dest_list), len(orig_list)), dtype=float)
252246
tree = STRtree(orig_list)
253247
idx = {hash(o.wkt): i for i, o in enumerate(orig_list)}
@@ -256,8 +250,10 @@ def compute_intersectionmatrix(
256250
for o in tree.query(d):
257251
# STRtree query returns a list of indices for shapely >= v2.0
258252
if isinstance(o, (int | np.integer)):
259-
o = orig_list[o]
260-
j = idx[hash(o.wkt)]
253+
j = int(o)
254+
o = orig_list[j]
255+
else:
256+
j = idx[hash(o.wkt)]
261257
intersection[i, j] = o.intersects(d)
262258

263259
return intersection

‎docs/release-notes.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ SPDX-License-Identifier: CC-BY-4.0
7373
`codes`, and an empty list excludes no cells
7474
([#525](https://github.com/PyPSA/atlite/pull/525)).
7575
- Fix `Cutout.hydro` failing with `pandas>=3.1` ([#542](https://github.com/PyPSA/atlite/pull/542)).
76+
- Fix `compute_indicatormatrix` and `compute_intersectionmatrix` failing with
77+
`KeyError` or mixing up shapes when geometries are passed as a series with a
78+
non-default index ([#534](https://github.com/PyPSA/atlite/pull/534)).
7679

7780
## [v0.7.0](https://github.com/PyPSA/atlite/releases/tag/v0.7.0) (3rd October 2026)
7881

‎test/test_gis.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
from atlite import Cutout
3030
from atlite.gis import (
3131
ExclusionContainer,
32+
compute_indicatormatrix,
33+
compute_intersectionmatrix,
3234
pad_extent,
3335
padded_transform_and_shape,
3436
regrid,
@@ -721,3 +723,31 @@ def test_invalid_geometry_rejected_at_computation(self, shapes, raster):
721723
})
722724
with pytest.raises(ValueError, match="Unsupported buffer geometry"):
723725
shape_availability(shapes, excluder)
726+
727+
728+
def test_compute_indicatormatrix_and_intersectionmatrix_non_default_index():
729+
poly1 = box(0, 0, 1, 1)
730+
poly2 = box(1, 0, 2, 1)
731+
dest_poly = box(0, 0, 1.5, 1)
732+
733+
orig_default = gpd.GeoSeries([poly1, poly2])
734+
dest_default = gpd.GeoSeries([dest_poly])
735+
expected_indicator = compute_indicatormatrix(orig_default, dest_default)
736+
expected_intersection = compute_intersectionmatrix(orig_default, dest_default)
737+
738+
orig_custom = gpd.GeoSeries([poly1, poly2], index=[10, 20])
739+
dest_custom = gpd.GeoSeries([dest_poly], index=["target"])
740+
res_indicator = compute_indicatormatrix(orig_custom, dest_custom)
741+
res_intersection = compute_intersectionmatrix(orig_custom, dest_custom)
742+
743+
assert (res_indicator.toarray() == expected_indicator.toarray()).all()
744+
assert (res_intersection.toarray() == expected_intersection.toarray()).all()
745+
746+
orig_reversed = gpd.GeoSeries([poly2, poly1], index=[1, 0])
747+
res_indicator_rev = compute_indicatormatrix(orig_reversed, dest_default)
748+
res_intersection_rev = compute_intersectionmatrix(orig_reversed, dest_default)
749+
750+
assert isclose(res_indicator_rev[0, 0], 0.5)
751+
assert isclose(res_indicator_rev[0, 1], 1.0)
752+
assert res_intersection_rev[0, 0] == 1.0
753+
assert res_intersection_rev[0, 1] == 1.0

0 commit comments

Comments
 (0)