Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: fix a case when there's only a single building to be passed to voronoi_frames #621

Merged
merged 16 commits into from
Jun 20, 2024
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
Loading