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.footprints.footprints_from_place(place='Kahla, Germany')
gdf_projected = ox.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
```

## 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,
ids='uID')
```

```
adjac = momepy.BuildingAdjacency(
buildings, spatial_weights_higher=dist200, unique_id='uID')
buildings['adjacency'] = adjac.series
```

```
f, ax = plt.subplots(figsize=(10, 10))
buildings.plot(ax=ax, column='adjacency', legend=True, cmap='viridis')
ax.set_axis_off()
plt.show()
```

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,
silence_warnings=True,
ids='uID')
queen = momepy.sw_high(k=1, gdf=buildings, ids='uID', contiguity='queen')
```

```
buildings['adj2'] = momepy.BuildingAdjacency(buildings,
spatial_weights_higher=dist200,
unique_id='uID',
spatial_weights=queen).series
```

```
f, ax = plt.subplots(figsize=(10, 10))
buildings.plot(ax=ax, column='adj2', legend=True, cmap='viridis')
ax.set_axis_off()
plt.show()
```

Both results are the same:

```
(buildings.adjacency == buildings.adj2).all()
```

```
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
```

`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
```

```
f, ax = plt.subplots(figsize=(10, 10))
buildings.plot(ax=ax, column='mean_ib_dist', scheme='quantiles', k=10, legend=True, cmap='viridis')
ax.set_axis_off()
plt.show()
```