Skip to content

Commit

Permalink
API: return morphological tessellation as a GeoDataFrame (#602)
Browse files Browse the repository at this point in the history
* API: return morphological tessellation as a GeoDataFrame

* update notebooks
  • Loading branch information
martinfleis committed Jun 14, 2024
1 parent 5d440a5 commit 2e6e816
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 42 deletions.
4 changes: 1 addition & 3 deletions docs/examples/clustering.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1143,9 +1143,7 @@
"source": [
"buildings = buildings.drop(collapsed)\n",
"limit = momepy.buffered_limit(buildings, \"adaptive\")\n",
"tessellation = momepy.morphological_tessellation(\n",
" buildings, clip=limit\n",
").to_frame(\"geometry\")"
"tessellation = momepy.morphological_tessellation(buildings, clip=limit)"
]
},
{
Expand Down
5 changes: 2 additions & 3 deletions docs/user_guide/combined/distribution.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@
"buildings = ox.projection.project_gdf(gdf).reset_index(drop=True)\n",
"\n",
"limit = momepy.buffered_limit(buildings)\n",
"tess = momepy.morphological_tessellation(buildings, clip=limit)\n",
"tessellation = tess.to_frame(\"geometry\")"
"tessellation = momepy.morphological_tessellation(buildings, clip=limit)"
]
},
{
Expand Down Expand Up @@ -513,4 +512,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
}
}
12 changes: 4 additions & 8 deletions docs/user_guide/combined/intensity.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,14 @@
"source": [
"point = (40.731603, -73.977857)\n",
"dist = 1000\n",
"gdf = ox.features_from_point(\n",
" point, dist=dist, tags={\"building\": True}\n",
")\n",
"gdf = ox.features_from_point(point, dist=dist, tags={\"building\": True})\n",
"gdf_projected = ox.projection.project_gdf(gdf)\n",
"buildings = gdf_projected[\n",
" gdf_projected.geom_type.isin([\"Polygon\", \"MultiPolygon\"])\n",
"].reset_index(drop=True)\n",
"\n",
"limit = momepy.buffered_limit(buildings)\n",
"tessellation = momepy.morphological_tessellation(\n",
" buildings, clip=limit\n",
").to_frame('geometry')"
"tessellation = momepy.morphological_tessellation(buildings, clip=limit)"
]
},
{
Expand Down Expand Up @@ -166,7 +162,7 @@
"metadata": {},
"outputs": [],
"source": [
"tessellation[\"FAR\"] = buildings[\"floor_area\"] / tessellation.area"
"tessellation[\"FAR\"] = buildings[\"floor_area\"] / tessellation.area"
]
},
{
Expand Down Expand Up @@ -227,4 +223,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
}
}
3 changes: 1 addition & 2 deletions docs/user_guide/getting_started.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,6 @@
"metadata": {},
"outputs": [],
"source": [
"tessellation = tessellation.to_frame(\"geometry\")\n",
"tessellation[\"CAR\"] = buildings.area / tessellation.area"
]
},
Expand Down Expand Up @@ -803,4 +802,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
}
}
3 changes: 1 addition & 2 deletions docs/user_guide/simple/dimension.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@
"metadata": {},
"outputs": [],
"source": [
"tessellation = tessellation.to_frame(\"geometry\")\n",
"tessellation[\"axis_length\"] = momepy.longest_axis_length(tessellation)"
]
},
Expand Down Expand Up @@ -277,4 +276,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
}
}
5 changes: 2 additions & 3 deletions docs/user_guide/simple/shape.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@
"buildings = ox.projection.project_gdf(gdf)\n",
"\n",
"limit = momepy.buffered_limit(buildings)\n",
"tess = momepy.morphological_tessellation(buildings, clip=limit)\n",
"tessellation = tess.to_frame(\"geometry\")"
"tessellation = momepy.morphological_tessellation(buildings, clip=limit)"
]
},
{
Expand Down Expand Up @@ -492,4 +491,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
}
}
5 changes: 2 additions & 3 deletions docs/user_guide/weights/diversity.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@
"buildings = ox.projection.project_gdf(gdf).reset_index(drop=True)\n",
"\n",
"limit = momepy.buffered_limit(buildings)\n",
"tess = momepy.morphological_tessellation(buildings, clip=limit)\n",
"tessellation = tess.to_frame(\"geometry\")"
"tessellation = momepy.morphological_tessellation(buildings, clip=limit)"
]
},
{
Expand Down Expand Up @@ -418,4 +417,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
}
}
7 changes: 3 additions & 4 deletions docs/user_guide/weights/examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@
"buildings = ox.projection.project_gdf(gdf).reset_index(drop=True)\n",
"\n",
"limit = momepy.buffered_limit(buildings)\n",
"tess = momepy.morphological_tessellation(buildings, clip=limit)\n",
"tessellation = tess.to_frame(\"geometry\")"
"tessellation = momepy.morphological_tessellation(buildings, clip=limit)"
]
},
{
Expand Down Expand Up @@ -512,7 +511,7 @@
"limit = momepy.buffered_limit(buildings)\n",
"tessellation = momepy.morphological_tessellation(\n",
" buildings, clip=limit\n",
").to_frame(\"geometry\")"
")"
]
},
{
Expand Down Expand Up @@ -723,4 +722,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
}
}
5 changes: 2 additions & 3 deletions docs/user_guide/weights/two.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@
"buildings = ox.projection.project_gdf(gdf).reset_index(drop=True)\n",
"\n",
"limit = momepy.buffered_limit(buildings)\n",
"tess = momepy.morphological_tessellation(buildings, clip=limit)\n",
"tessellation = tess.to_frame(\"geometry\")"
"tessellation = momepy.morphological_tessellation(buildings, clip=limit)"
]
},
{
Expand Down Expand Up @@ -204,4 +203,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
}
}
5 changes: 2 additions & 3 deletions docs/user_guide/weights/weights_nb.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@
"buildings = ox.projection.project_gdf(gdf).reset_index(drop=True)\n",
"\n",
"limit = momepy.buffered_limit(buildings)\n",
"tess = momepy.morphological_tessellation(buildings, clip=limit)\n",
"tessellation = tess.to_frame(\"geometry\")"
"tessellation = momepy.morphological_tessellation(buildings, clip=limit)"
]
},
{
Expand Down Expand Up @@ -347,4 +346,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
}
}
18 changes: 10 additions & 8 deletions momepy/functional/_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def morphological_tessellation(
clip: str | shapely.Geometry | GeoSeries | GeoDataFrame | None = "bounding_box",
shrink: float = 0.4,
segment: float = 0.5,
) -> GeoSeries:
) -> GeoDataFrame:
"""Generate morphological tessellation.
Morpohological tessellation is a method to divide space into cells based on
Expand Down Expand Up @@ -74,8 +74,8 @@ def morphological_tessellation(
Returns
-------
GeoSeries
GeoSeries with an index matching the index of input geometry
GeoDataFrame
GeoDataFrame with an index matching the index of input geometry
See also
--------
Expand All @@ -92,7 +92,7 @@ def morphological_tessellation(
shrink=shrink,
segment=segment,
return_input=False,
as_gdf=False,
as_gdf=True,
)


Expand Down Expand Up @@ -275,7 +275,7 @@ def _tess(ix, poly, blg, threshold, shrink, segment, enclosure_id):
)


def verify_tessellation(tesselation, geometry):
def verify_tessellation(tessellation, geometry):
"""Check whether result matches buildings and contains only Polygons.
Checks if the generated tessellation fully matches the input buildings, i.e. if
Expand All @@ -287,7 +287,7 @@ def verify_tessellation(tesselation, geometry):
Parameters
----------
tesselation : GeoSeries | GeoDataFrame
tessellation : GeoSeries | GeoDataFrame
tessellation geometry
geometry : GeoSeries | GeoDataFrame
building geometry used to generate tessellation
Expand All @@ -299,7 +299,7 @@ def verify_tessellation(tesselation, geometry):
"""
# check against input layer
ids_original = geometry.index
ids_generated = tesselation.index
ids_generated = tessellation.index
collapsed = pd.Index([])
if len(ids_original) != len(ids_generated):
collapsed = ids_original.difference(ids_generated)
Expand All @@ -314,7 +314,9 @@ def verify_tessellation(tesselation, geometry):
)

# check MultiPolygons - usually caused by error in input geometry
multipolygons = tesselation[tesselation.geometry.geom_type == "MultiPolygon"].index
multipolygons = tessellation[
tessellation.geometry.geom_type == "MultiPolygon"
].index
if len(multipolygons) > 0:
warnings.warn(
message=(
Expand Down
1 change: 1 addition & 0 deletions momepy/functional/tests/test_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def test_morphological_tessellation(self):
assert (tessellation.geom_type == "Polygon").all()
assert tessellation.crs == self.df_buildings.crs
assert_index_equal(tessellation.index, self.df_buildings.index)
assert isinstance(tessellation, gpd.GeoDataFrame)

clipped = mm.morphological_tessellation(
self.df_buildings,
Expand Down

0 comments on commit 2e6e816

Please sign in to comment.