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

Using two spatial weights matrices#

Some functions are using spatial weights for two different purposes. Therefore two matrices have to be passed. We will illustrate this case measuring building adjacency and mean interbuilding distance.

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.

import osmnx as ox

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

buildings['uID'] = momepy.unique_id(buildings)
limit = momepy.buffered_limit(buildings)
tessellation = momepy.Tessellation(buildings, unique_id='uID', limit=limit,
/Users/martin/Git/geopandas/geopandas/ 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)

Building adjacency#

Building adjacency is using spatial_weights_higher to denote the area within which the calculation occurs (required) and spatial_weights to denote adjacency of buildings (optional, the function can do it for us). We can use distance band of 200 meters to define spatial_weights_higher.

import libpysal
dist200 = libpysal.weights.DistanceBand.from_dataframe(buildings, 200,
/opt/miniconda3/envs/geo_dev/lib/python3.9/site-packages/libpysal/weights/ UserWarning: The weights matrix is not fully connected:
 There are 7 disconnected components.
 There are 3 islands with ids: 330, 578, 2495.
adjac = momepy.BuildingAdjacency(
    buildings, spatial_weights_higher=dist200, unique_id='uID')
buildings['adjacency'] = adjac.series
Calculating spatial weights...
Spatial weights ready...
f, ax = plt.subplots(figsize=(10, 10))
buildings.plot(ax=ax, column='adjacency', legend=True, cmap='viridis', scheme='naturalbreaks', k=10)

If we want to specify or reuse spatial_weights, we can generate them as Queen contiguity weights. Using libpysal or momepy (momepy will use the same libpysal method, but you don’t need to import libpysal directly):

queen = libpysal.weights.Queen.from_dataframe(buildings,
queen = momepy.sw_high(k=1, gdf=buildings, ids='uID', contiguity='queen')
buildings['adj2'] = momepy.BuildingAdjacency(buildings,
f, ax = plt.subplots(figsize=(10, 10))
buildings.plot(ax=ax, column='adj2', legend=True, cmap='viridis')

Mean interbuilding distance#

Mean interbuilding distance is similar to neighbour_distance, but it is calculated within vicinity defined in spatial_weights_higher, while spatial_weights captures immediate neighbours.

sw1 = momepy.sw_high(k=1, gdf=tessellation, ids='uID')
sw3 = momepy.sw_high(k=3, gdf=tessellation, ids='uID')
interblg_distance = momepy.MeanInterbuildingDistance(
    buildings, sw1, 'uID', spatial_weights_higher=sw3)
buildings['mean_ib_dist'] = interblg_distance.series
Computing mean interbuilding distances...
/opt/miniconda3/envs/geo_dev/lib/python3.9/site-packages/pygeos/ RuntimeWarning: overflow encountered in distance
  return lib.distance(a, b, **kwargs)

spatial_weights_higher is optional and can be derived from spatial_weights as weights of higher order defined in order.

buildings['mean_ib_dist'] = momepy.MeanInterbuildingDistance(
    buildings, sw1, 'uID', order=3).series
Computing mean interbuilding distances...
/opt/miniconda3/envs/geo_dev/lib/python3.9/site-packages/pygeos/ RuntimeWarning: overflow encountered in distance
  return lib.distance(a, b, **kwargs)
f, ax = plt.subplots(figsize=(10, 10))
buildings.plot(ax=ax, column='mean_ib_dist', scheme='quantiles', k=10, legend=True, cmap='viridis')