Note

This page was generated from user_guide/elements/tessellation.ipynb.
Interactive online version: Binder badge

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 scipy.spatial.Voronoi. We’ll explain key parts of tessellation and explore its application in the real world.

Using exemplary data#

[1]:
import geopandas as gpd
import matplotlib.pyplot as plt
import momepy
[2]:
buildings = gpd.read_file(
    momepy.datasets.get_path("bubenec"), layer="buildings"
)
/Users/martin/Git/geopandas/geopandas/geodataframe.py:580: RuntimeWarning: Sequential read of iterator was interrupted. Resetting iterator. This can negatively impact the performance.
  for feature in features_lst:
[3]:
f, ax = plt.subplots(figsize=(10, 10))
buildings.plot(ax=ax)
ax.set_axis_off()
plt.show()
../../_images/user_guide_elements_tessellation_3_0.png

To generate MT, each building needs to have an unique_id assigned, which will later link generated cell to its parent building. Exemplar GeoDataFrame comes with such ID in uID column.

[4]:
buildings.head()
[4]:
uID geometry
0 1 POLYGON ((1603599.221 6464369.816, 1603602.984...
1 2 POLYGON ((1603042.880 6464261.498, 1603038.961...
2 3 POLYGON ((1603044.650 6464178.035, 1603049.192...
3 4 POLYGON ((1603036.557 6464141.467, 1603036.969...
4 5 POLYGON ((1603082.387 6464142.022, 1603081.574...

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.

[5]:
limit = momepy.buffered_limit(buildings, buffer=100)
limit
[5]:
../../_images/user_guide_elements_tessellation_7_0.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.

[6]:
tessellation = momepy.Tessellation(buildings, unique_id="uID", limit=limit)
Inward offset...
Generating input point array...
Generating Voronoi diagram...
Generating GeoDataFrame...
Dissolving Voronoi polygons...
[7]:
tessellation
[7]:
<momepy.elements.Tessellation at 0x16f56e790>

GeoDataFrame containing the tessellation itself can be accessed using tessellation attribute. Similarly, used values and input geometry can be accessed using shrink, segment, limit of gdf attributes.

[8]:
tessellation_gdf = tessellation.tessellation
[9]:
tessellation.shrink
[9]:
0.4
[10]:
f, ax = plt.subplots(figsize=(10, 10))
tessellation_gdf.plot(ax=ax, edgecolor="white")
buildings.plot(ax=ax, color="white", alpha=0.5)
ax.set_axis_off()
plt.show()
../../_images/user_guide_elements_tessellation_14_0.png

Generated tessellation can be linked to buildings using unique_id, being the common column between both GeoDataFrames.

[11]:
tessellation_gdf.head()
[11]:
uID geometry
0 1 POLYGON ((1603575.793 6464350.951, 1603575.513...
1 2 POLYGON ((1603166.394 6464326.522, 1603166.485...
2 3 POLYGON ((1603005.886 6464167.312, 1603009.689...
3 4 POLYGON ((1603057.535 6464094.260, 1603057.149...
4 5 POLYGON ((1603089.080 6464106.238, 1603088.611...

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.

[12]:
import osmnx as ox

gdf = ox.geometries.geometries_from_place(
    "Kahla, Germany", tags={"building": True}
)
gdf_projected = ox.projection.project_gdf(gdf)
[13]:
f, ax = plt.subplots(figsize=(10, 10))
gdf_projected.plot(ax=ax)
ax.set_axis_off()
plt.show()
../../_images/user_guide_elements_tessellation_20_0.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.

[14]:
buildings = momepy.preprocess(
    gdf_projected.reset_index(), size=30, compactness=0.2, islands=True
)
Loop 1 out of 2.
Loop 2 out of 2.

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 two other arguments: unique ID and limit. We will generate unique ID using momepy.unique_id and limit of tessellation using the same buffer method as above.

[15]:
buildings["uID"] = momepy.unique_id(buildings)
limit = momepy.buffered_limit(buildings)

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

[16]:
tessellation = momepy.Tessellation(buildings, unique_id="uID", limit=limit)
tessellation_gdf = tessellation.tessellation
Inward offset...
Generating input point array...
Generating Voronoi diagram...
Generating GeoDataFrame...
Dissolving Voronoi polygons...
/Users/martin/Git/geopandas/geopandas/geoseries.py:190: DeprecationWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning.
  s = pd.Series(data, index=index, name=name, **kwargs)
[17]:
f, ax = plt.subplots(figsize=(10, 10))
tessellation_gdf.plot(ax=ax)
buildings.plot(ax=ax, color="white", alpha=0.5)
ax.set_axis_off()
plt.show()
../../_images/user_guide_elements_tessellation_27_0.png

Zooming closer to check the result:

[18]:
f, ax = plt.subplots(figsize=(10, 10))
tessellation_gdf.plot(ax=ax, edgecolor="white", linewidth=0.2)
buildings.plot(ax=ax, color="white", alpha=0.5)
ax.set_axis_off()
ax.set_xlim(681500, 682500)
ax.set_ylim(5631000, 5632000)
plt.show()
../../_images/user_guide_elements_tessellation_29_0.png

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

Troubleshooting#

In some cases, momepy.Tessellation raises errors or warnings. In 99% of cases, this is due to errors in input data. Two types of warnings are possible:

  • Tessellation does not fully match buildings. 10 element(s) collapsed "during generation - unique_id: [list of ids]

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.

  • Tessellation contains MultiPolygon elements. Initial objects should be edited. unique_id of affected elements: [list of ids]

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.