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 issue with blocks within another blocks #351

Merged
merged 3 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/psf/black
rev: 21.4b2
hooks:
- id: black
language_version: python3
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
language_version: python3
36 changes: 7 additions & 29 deletions momepy/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,19 +594,6 @@ class Blocks:
Examples
--------
>>> blocks = mm.Blocks(tessellation_df, streets_df, buildings_df, 'bID', 'uID')
Buffering streets...
Generating spatial index...
Difference...
Defining adjacency...
Defining street-based blocks...
Defining block ID...
Generating centroids...
Spatial join...
Attribute join (tesselation)...
Generating blocks...
Multipart to singlepart...
Attribute join (buildings)...
Attribute join (tesselation)...
>>> blocks.blocks.head()
bID geometry
0 1.0 POLYGON ((1603560.078648818 6464202.366899694,...
Expand Down Expand Up @@ -648,14 +635,14 @@ def __init__(self, tessellation, edges, buildings, id_name, unique_id, **kwargs)
buildings_c,
cut[[cut.geometry.name, "component"]],
how="left",
predicate="intersects",
predicate="within",
Comment on lines -651 to +638
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

within is faster and with point in polygon results in the same thing

)
else:
centroids_tempID = gpd.sjoin(
buildings_c,
cut[[cut.geometry.name, "component"]],
how="left",
op="intersects",
op="within",
)

cells_copy = tessellation[[unique_id, tessellation.geometry.name]].merge(
Expand All @@ -668,30 +655,21 @@ def __init__(self, tessellation, edges, buildings, id_name, unique_id, **kwargs)
cells_copy.dissolve(by="component").explode().reset_index(drop=True)
)
blocks[id_name] = range(len(blocks))
blocks[blocks.geometry.name] = gpd.GeoSeries(
pygeos.polygons(blocks.exterior.values.data), crs=blocks.crs
)
Comment on lines -671 to -673
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally did this to cleanup interior after complex dissolve but it seems that GEOS got much better on it so it may not be needed. This is what was causing the issue in the first place, so if we want some cleaning procedure, it needs to be different.

blocks = blocks[[id_name, blocks.geometry.name]]

if GPD_10:
centroids_w_bl_ID2 = gpd.sjoin(
buildings_c, blocks, how="left", predicate="intersects"
buildings_c, blocks, how="left", predicate="within"
)
else:
centroids_w_bl_ID2 = gpd.sjoin(
buildings_c, blocks, how="left", op="intersects"
)
bl_ID_to_uID = centroids_w_bl_ID2[[unique_id, id_name]]
centroids_w_bl_ID2 = gpd.sjoin(buildings_c, blocks, how="left", op="within")

buildings_m = buildings[[unique_id]].merge(
bl_ID_to_uID, on=unique_id, how="left"
)
self.buildings_id = buildings_m[id_name]
self.buildings_id.index = self.buildings.index
self.buildings_id = centroids_w_bl_ID2[id_name]

cells_m = tessellation[[unique_id]].merge(
bl_ID_to_uID, on=unique_id, how="left"
centroids_w_bl_ID2[[unique_id, id_name]], on=unique_id, how="left"
)

self.tessellation_id = cells_m[id_name]
self.tessellation_id.index = self.tessellation.index

Expand Down
4 changes: 2 additions & 2 deletions momepy/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ def __init__(self, gdf, areas=None):
radius = hull.apply(
lambda g: _circle_radius(list(g.coords)) if g is not None else None
)
self.series = areas / (np.pi * radius ** 2)
self.series = areas / (np.pi * radius**2)


class SquareCompactness:
Expand Down Expand Up @@ -1107,7 +1107,7 @@ def __init__(self, gdf):
bbox = gdf.geometry.apply(lambda g: g.minimum_rotated_rectangle)
a = bbox.area
p = bbox.length
cond1 = p ** 2
cond1 = p**2
cond2 = 16 * a
bigger = cond1 >= cond2
sqrt = np.empty(len(a))
Expand Down
2 changes: 1 addition & 1 deletion momepy/weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def sw_high(k, gdf=None, weights=None, ids=None, contiguity="queen", silent=True
if k > 1:
id_order = first_order.id_order
w = first_order.sparse
wk = sum(map(lambda x: w ** x, range(2, k + 1)))
wk = sum(map(lambda x: w**x, range(2, k + 1)))
rk, ck = wk.nonzero()
sk = set(zip(rk, ck))
sk = set([(i, j) for i, j in sk if i != j])
Expand Down
19 changes: 19 additions & 0 deletions tests/test_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,25 @@ def test_Blocks_non_default_index(self):
assert_index_equal(tessellation.index, blocks.tessellation_id.index)
assert_index_equal(buildings.index, blocks.buildings_id.index)

def test_Blocks_inner(self):
streets = self.df_streets.copy()
streets.loc[35] = (
self.df_buildings.geometry.iloc[141]
.representative_point()
.buffer(20)
.exterior
)
blocks = mm.Blocks(
self.df_tessellation, streets, self.df_buildings, "bID", "uID"
)
assert not blocks.tessellation_id.isna().any()
assert not blocks.buildings_id.isna().any()
assert len(blocks.blocks) == 9
assert (
len(blocks.blocks.sindex.query_bulk(blocks.blocks.geometry, "overlaps")[0])
== 0
)

def test_get_network_id(self):
buildings_id = mm.get_network_id(self.df_buildings, self.df_streets, "nID")
assert not buildings_id.isna().any()
Expand Down
24 changes: 9 additions & 15 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,11 @@ def test_gdf_to_nx(self):
)

dual = mm.gdf_to_nx(self.df_streets, approach="dual", angle="ang")
assert (
dual.edges[
(1603499.42326969, 6464328.7520580515),
(1603510.1061735682, 6464204.555117119),
0,
]
== {"ang": 117.18288698243317}
)
assert dual.edges[
(1603499.42326969, 6464328.7520580515),
(1603510.1061735682, 6464204.555117119),
0,
] == {"ang": 117.18288698243317}

dual = mm.gdf_to_nx(
self.df_streets, approach="dual", angles=False, multigraph=False
Expand All @@ -85,13 +82,10 @@ def test_gdf_to_nx(self):

dual = mm.gdf_to_nx(self.df_streets, approach="dual", multigraph=False)
assert isinstance(nx, networkx.Graph)
assert (
dual.edges[
(1603499.42326969, 6464328.7520580515),
(1603510.1061735682, 6464204.555117119),
]
== {"angle": 117.18288698243317}
)
assert dual.edges[
(1603499.42326969, 6464328.7520580515),
(1603510.1061735682, 6464204.555117119),
] == {"angle": 117.18288698243317}

with pytest.raises(ValueError):
mm.gdf_to_nx(self.df_streets, approach="dual", directed=True)
Expand Down