Morphological tessellation

One of the main features of momepy is the ability to generate and analyse morphological tessellation (MT). One can imagine MT like Voronoi tessellation generated around building polygons instead of points. The similarity is not accidental - the core of MT is a Voronoi diagram generated by shapely.voronoi_polygons. We’ll explain key parts of tessellation and explore its application in the real world.

Using exemplary data

import geopandas as gpd
import momepy
buildings = gpd.read_file(
    momepy.datasets.get_path("bubenec"), layer="buildings"
)
ax = buildings.plot(figsize=(8, 8))
ax.set_axis_off()
../../_images/c5c2c31db5e57a96fb0ee8f922f99ef3166639449f7b5d75908bfd9da94e6661.png

As Voronoi tessellation tends to go to infinity for edge points, we have to define a limit for tessellation. It can be the area of your case study represented as a Polygon or MultiPolygon or you can use momepy.buffered_limit to generate such limit as a set maximal distance from buildings.

limit = momepy.buffered_limit(buildings, buffer=100)
limit
../../_images/794685f50852279eb3d692f7096383ca1d296b2abfda0fec00471db5b3db4535.svg

Other crucial attributes of tessellation algorithm are segment and shrink. Both are predefined as balanced values between the computational demands and a quality of a result. Segment defines the maximal distance between points generated to represent building footprint, shrink defines how much should be building buffered (inwards) to generate a gap between adjacent polygons. If you want to reduce memory requirements, you can use larger segment distance, but it may cause imprecision.

tessellation = momepy.morphological_tessellation(buildings, clip=limit)
tessellation.head()
geometry
0 POLYGON ((1603536.56 6464392.264, 1603541.262 ...
1 POLYGON ((1603167.679 6464323.194, 1603167.552...
2 POLYGON ((1603078.787 6464172.1, 1603077.665 6...
3 POLYGON ((1603070.306 6464154.611, 1603070.081...
4 POLYGON ((1603083.134 6464103.971, 1603077.387...
ax = tessellation.plot(edgecolor="white", figsize=(8, 8))
buildings.plot(ax=ax, color="white", alpha=0.5)
ax.set_axis_off()
../../_images/a5c5c4b6873cad52903e25fd49247242a3d7ad0fb78123c8b6fe40fa9af02d3d.png

Generated tessellation can be linked to buildings using index, which is directly inherited from buildings.

Generating tessellation based on OpenStreetmap

To illustrate a more real-life example, let’s try to generate tessellation based on a small town retrieved from OSM. We will use osmnx package to get the data.

import osmnx as ox

gdf = ox.features_from_place("Kahla, Germany", tags={"building": True})
gdf_projected = ox.projection.project_gdf(gdf)
ax = gdf_projected.plot(figsize=(8, 8))
ax.set_axis_off()
../../_images/651a5833df0c5f2b74a6eaeb346448c7ef31caeb665998cf6f7e15394db07860.png

While working with real-life data, we often face issues with their quality. To avoid some of the possible errors, we should preprocess (clean) the data. It is often done semi-manually within the GIS environment. momepy offers (experimental) momepy.preprocess to handle some of the expected issues.

buildings = momepy.preprocess(
    gdf_projected.reset_index(), size=30, compactness=0.2, islands=True
)
Loop 1 out of 2.
Loop 2 out of 2.
/tmp/ipykernel_4386/2536666774.py:1: FutureWarning: `preprocess` is deprecated and will be removed in momepy 1.0. Use the explicit tools available in the package `geoplanar`. See https://geoplanar.readthedocs.io for details.
  buildings = momepy.preprocess(

What has happened?

  1. All auxiliary buildings (smaller than 30 square meters defined in size) were dropped.

  2. Possible adjacent structures of specific circular compactness values (long and narrow) were joined to their parental buildings.

  3. All buildings fully within other buildings (share 100% of the exterior boundary) were joined to their parental buildings.

Tessellation requires a limit. We will generate it using the same buffer method as above but this time using the adaptive buffer distance to partially mitigate edge effects.

limit = momepy.buffered_limit(buildings, buffer="adaptive", max_buffer=50)

At this moment, we have everything we need to generate morphological tessellation. It might take a while for larger GeoDataFrames.

tessellation = momepy.morphological_tessellation(buildings, clip=limit)
ax = tessellation.plot(figsize=(8, 8))
buildings.plot(ax=ax, color="white", alpha=0.5)
ax.set_axis_off()
../../_images/4f81352e28c2336c633e0e80e426f70f95c50a6cbfbcf44b030e8108dafd1934.png

Zooming closer to check the result:

ax = tessellation.plot(edgecolor="white", linewidth=0.2, figsize=(8, 8))
buildings.plot(ax=ax, color="white", alpha=0.5)
ax.set_axis_off()
ax.set_xlim(681500, 682500)
ax.set_ylim(5631000, 5632000)
(5631000.0, 5632000.0)
../../_images/d246f6e95c969aa7cd22e713625fb1746b6d13a8368f9b14a01eb5ca8b72d726.png

And we are done. Morphological tessellation is generated and ready for any further analysis.

Troubleshooting

In some cases, the result may not be optimal. You can use momepy.verify_tessellation function to check the result. In 99% of cases, issues are due to errors in input data. Two types of issue are possible:

  1. Collaped buildings

In this case, some of the building shapes collapsed during the shrinkage. It should not happen as shrink distance is usually quite small, but you might be able to resolve it by setting smaller shrink distance. However, we would recommend fixing the data manually.

  1. MultiPolygons

This is a more common issue, which is again caused by imprecise data. Often caused by long and extremely narrow shapes or overlap of buildings. While some of the analysis might work even with MultiPolygon geometry, it does not really make sense, so we would recommend fixing the data beforehand.

However, for most of the data of higher quality, you should not see any of these warnings.

collapsed, multipolygons = momepy.verify_tessellation(tessellation, buildings)
/tmp/ipykernel_4386/3617767949.py:1: UserWarning: Tessellation contains MultiPolygon elements. Initial objects should  be edited. Index of affected elements: [385, 390, 684, 870, 1033, 1080, 1261, 1488, 1681, 2031, 2120, 2427, 2469, 2551].
  collapsed, multipolygons = momepy.verify_tessellation(tessellation, buildings)
print(f"Collapsed: {collapsed}\n\nMultiPolygons: {multipolygons}")
Collapsed: Index([], dtype='object')

MultiPolygons: Index([385, 390, 684, 870, 1033, 1080, 1261, 1488, 1681, 2031, 2120, 2427,
       2469, 2551],
      dtype='int64')