This page was generated from user_guide/weights/weights_nb.ipynb.
Interactive online version: Binder badge

Generating spatial weights

momepy is using libpysal to handle spatial weights, but also builds on top of it. This notebook will show how to use different weights.

import momepy
import geopandas as gpd
import matplotlib.pyplot as plt

We will again use osmnx to get the data for our example and after preprocessing of building layer will generate tessellation layer.

import osmnx as ox

gdf = ox.geometries.geometries_from_place('Kahla, Germany', tags={'building':True})
gdf_projected = ox.projection.project_gdf(gdf)

buildings = momepy.preprocess(gdf_projected, size=30,
                              compactness=True, islands=True)
buildings['uID'] = momepy.unique_id(buildings)
limit = momepy.buffered_limit(buildings)
tessellation = momepy.Tessellation(buildings, unique_id='uID', limit=limit).tessellation
Loop 1 out of 2.
Identifying changes: 100%|██████████| 2932/2932 [00:00<00:00, 6196.10it/s]
Changing geometry: 100%|██████████| 630/630 [00:04<00:00, 143.29it/s]
Loop 2 out of 2.
Identifying changes: 100%|██████████| 2115/2115 [00:00<00:00, 16000.91it/s]
Changing geometry: 100%|██████████| 172/172 [00:00<00:00, 195.82it/s]
Inward offset...
  6%|▌         | 112/2008 [00:00<00:01, 1119.47it/s]
Generating input point array...
100%|██████████| 2008/2008 [00:01<00:00, 1840.40it/s]
Generating Voronoi diagram...
Generating GeoDataFrame...
Vertices to Polygons: 100%|██████████| 249298/249298 [00:05<00:00, 48790.35it/s]
Dissolving Voronoi polygons...
Preparing limit for edge resolving...
Building R-tree...
Identifying edge cells...
0it [00:00, ?it/s]

Queen contiguity

Morphological tessellation allows using contiguity-based weights matrix. While libpysal.weights.contiguity.Queen will do the standard Queen contiguity matrix of the first order; it might not be enough to capture proper context. For that reason, we can use momepy.sw_high to capture all neighbours within set topological distance k. It generates spatial weights of higher orders under the hood and joins them together.

sw3 = momepy.sw_high(k=3, gdf=tessellation, ids='uID')

Queen contiguity of morphological tessellation can capture the comparable level of information across the study area - the number of the neighbour is relatively similar and depends on the morphology of urban form. We can visualize it by counting the number of neighbours (as captured by sw3).

tessellation['neighbours'] = momepy.Neighbors(tessellation, sw3,'uID').series
100%|██████████| 2005/2005 [00:00<00:00, 94463.12it/s]
f, ax = plt.subplots(figsize=(10, 10))
tessellation.plot(ax=ax, column='neighbours', legend=True, cmap='Spectral_r')
buildings.plot(ax=ax, color="white", alpha=0.4)


Often we want to define the neighbours based on metric distance. We will look at two options - distance band and k-nearest neighbour.

Distance band

We can imagine distance band as a buffer of a set radius around centroid of each object, for example, 400 meters. For that, we can use libpysal.weights.DistanceBand:

import libpysal
dist400 = libpysal.weights.DistanceBand.from_dataframe(buildings, 400,
/opt/miniconda3/envs/momepy_guide/lib/python3.8/site-packages/libpysal/weights/ UserWarning: The weights matrix is not fully connected:
 There are 2 disconnected components.
 There is 1 island with id: 212.

Because we have defined spatial weights using uID, we can use dist400 generated on buildings and use it on tessellation:

tessellation['neighbours400'] = momepy.Neighbors(tessellation, dist400, 'uID').series
100%|██████████| 2005/2005 [00:00<00:00, 92447.50it/s]
f, ax = plt.subplots(figsize=(10, 10))
tessellation.plot(ax=ax, column='neighbours400', legend=True, cmap='Spectral_r')
buildings.plot(ax=ax, color="white", alpha=0.4)

K nearest neighbor

If we want fixed number of neighbours, we can use libpysal.weights.KNN:

knn = libpysal.weights.KNN.from_dataframe(buildings, k=200, ids='uID')
tessellation['neighboursKNN'] = momepy.Neighbors(tessellation, knn,'uID').series
100%|██████████| 2005/2005 [00:00<00:00, 46032.72it/s]

Note: As all tessellation cells have the same number of neighbours (due to KNN), they all have the same colour.

f, ax = plt.subplots(figsize=(10, 10))
tessellation.plot(ax=ax, column='neighboursKNN', legend=True, cmap='Spectral_r')
buildings.plot(ax=ax, color="white", alpha=0.4)

All of them can be used within morphometric analysis. Theoretical and practical differences are discussed in Fleischmann, Romice and Porta (2019).

For the other options on generating spatial weights see lipysal API.