Skip to content

Commit

Permalink
BUG: fix a case when there's only a single building to be passed to v…
Browse files Browse the repository at this point in the history
…oronoi_frames (#621)

* generate tesselations for individual buildings

* simplify code

* simplify code

* PR changes

* changed assignment

* changed assignment

* simplification & single building catch

* test versionning

* test versionning

* single building bug

* single building bug

* cleanup

---------

Co-authored-by: Martin Fleischmann <martin@martinfleischmann.net>
  • Loading branch information
u3ks and martinfleis authored Jun 20, 2024
1 parent ee0bc64 commit 804ca9c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
8 changes: 7 additions & 1 deletion momepy/functional/_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,16 @@ def _tess(ix, poly, blg, threshold, shrink, segment, enclosure_id):
tess[enclosure_id] = ix
return tess

## in case a single building is left in blg
if len(blg) == 1:
assigned_ix = blg.index[0]
else:
assigned_ix = -1

return GeoDataFrame(
{enclosure_id: ix},
geometry=[poly],
index=[-1],
index=[assigned_ix],
crs=blg.crs,
)

Expand Down
23 changes: 23 additions & 0 deletions momepy/functional/tests/test_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,29 @@ def test_blocks_inner(self):
else:
assert len(blocks.sindex.query_bulk(blocks.geometry, "overlaps")[0]) == 0

def test_tess_single_building_edge_case(self):
tessellations = mm.enclosed_tessellation(
self.df_buildings, self.enclosures.geometry, n_jobs=-1
)
orig_grouper = tessellations.groupby("enclosure_index")
idxs = ~self.df_buildings.index.isin(orig_grouper.get_group(8).index)
idxs[1] = True
idxs[21] = False
idxs[23] = False

new_blg = self.df_buildings[idxs]
new_blg.loc[22, "geometry"] = new_blg.loc[22, "geometry"].buffer(20)
new_tess = mm.enclosed_tessellation(new_blg, self.enclosures.geometry, n_jobs=1)

# assert that buildings 1 and 22 intersect the same enclosure
inp, res = self.enclosures.sindex.query(
new_blg.geometry, predicate="intersects"
)
assert np.isclose(new_blg.iloc[inp[res == 8]].index.values, [1, 22]).all()

# assert that there is a tessellation for building 1
assert 1 in new_tess.index


class TestElementsEquivalence:
def setup_method(self):
Expand Down

0 comments on commit 804ca9c

Please sign in to comment.