{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Measuring diversity\n", "\n", "As important diversity is for cities, as complicated is to capture it. `momepy` offers several options on how to do that using urban morphometrics. Generally, we can distinguish three types of diversity characters, based on:\n", "\n", "1. Absolute values\n", "2. Relative values\n", "3. Categorization (binning)\n", "\n", "This notebook provides examples from each of them." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import momepy\n", "import geopandas as gpd\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will again use `osmnx` to get the data for our example and after preprocessing of building layer will generate tessellation. " ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Inward offset...\n", "Generating input point array...\n", "Generating Voronoi diagram...\n", "Generating GeoDataFrame...\n", "Dissolving Voronoi polygons...\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/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.\n", " s = pd.Series(data, index=index, name=name, **kwargs)\n" ] } ], "source": [ "import osmnx as ox\n", "\n", "gdf = ox.geometries.geometries_from_place('Kahla, Germany', tags={'building':True})\n", "buildings = ox.projection.project_gdf(gdf)\n", "\n", "buildings['uID'] = momepy.unique_id(buildings)\n", "limit = momepy.buffered_limit(buildings)\n", "tessellation = momepy.Tessellation(buildings, unique_id='uID', limit=limit).tessellation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Queen contiguity\n", "\n", "Morphological tessellation allows using contiguity-based weights matrix. While `libpysal.weights.contiguity.Queen` will do a classic Queen contiguity matrix; 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`. More in [Generating spatial weights](weights_nb.ipynb)." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "sw3 = momepy.sw_high(k=3, gdf=tessellation, ids='uID')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To have a character whose diversity can be measured, we can use the area of tessellation." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "tessellation['area'] = momepy.Area(tessellation).series" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Range\n", "\n", "The range is as simple as it sounds; it measures the range of the values withing all neighbours as captured by `spatial_weights`." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "tags": [] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "8b0fee8ff9c64bbe93af38a93316da87", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/2947 [00:00" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "f, ax = plt.subplots(figsize=(10, 10))\n", "tessellation.plot(ax=ax, column='area_rng', legend=True, cmap='Spectral_r')\n", "buildings.plot(ax=ax, color=\"white\", alpha=0.4)\n", "ax.set_axis_off()\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, as we can see from the plot above, there is a massive effect of large-scale buildings, which can be seen as outliers. For that reason, we can define `rng` keyword argument to limit the range taken into account. To get the interquartile range:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "tags": [] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "0e4887ef41724e72baaaac445beba21e", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/2947 [00:00" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "f, ax = plt.subplots(figsize=(10, 10))\n", "tessellation.plot(ax=ax, column='area_IQR', legend=True, cmap='Spectral_r')\n", "buildings.plot(ax=ax, color=\"white\", alpha=0.4)\n", "ax.set_axis_off()\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The effect of outliers has been successfully eliminated.\n", "\n", "### Theil index\n", "\n", "Theil index is a measure of inequality (as Gini index is). `momepy` is using `pysal`'s implementation of the Theil index to do the calculation." ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "tags": [] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "ee336946426c4fdaa215bbbd50d897ce", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/2947 [00:00" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "f, ax = plt.subplots(figsize=(10, 10))\n", "tessellation.plot(ax=ax, column='area_Theil', scheme='fisherjenks', k=10, legend=True, cmap='plasma')\n", "buildings.plot(ax=ax, color=\"white\", alpha=0.4)\n", "ax.set_axis_off()\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Again, the outlier effect is present. We can use the same keyword as above to limit it and measure the Theil index on the inter-decile range." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "tags": [] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "bbbb2c8f59c84856be6d3d3f8e3338bc", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/2947 [00:00" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "f, ax = plt.subplots(figsize=(10, 10))\n", "tessellation.plot(ax=ax, column='area_Theil_ID', scheme='fisherjenks', k=10, legend=True, cmap='plasma')\n", "buildings.plot(ax=ax, color=\"white\", alpha=0.4)\n", "ax.set_axis_off()\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Simpson's diversity index\n", "\n", "Simpson's diversity index is one of the most used indices capturing diversity. However, we need to be careful using it for continuous values, as it depends on the binning of these values to categories. The effect of different binning could be significant. `momepy` uses Head/tail Breaks as a large number of morphometric characters follows power-law distribution (for which Head/tail Breaks are designed). However, you can use any binning provided by `mapclassify` (including user-defined). The default Head/tail Breaks:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "tags": [] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "043bb1b7f9e3428999e648535bccc412", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/2947 [00:00" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "f, ax = plt.subplots(figsize=(10, 10))\n", "tessellation.plot(ax=ax, column='area_simpson', legend=True, cmap='viridis')\n", "buildings.plot(ax=ax, color=\"white\", alpha=0.4)\n", "ax.set_axis_off()\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And binning based on quantiles (into 7 bins of equal size):" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "tags": [] }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "f8c2e86a7e8848ce96952929da91d782", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/2947 [00:00" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "f, ax = plt.subplots(figsize=(10, 10))\n", "tessellation.plot(ax=ax, column='area_simpson_q7', legend=True, cmap='viridis')\n", "buildings.plot(ax=ax, color=\"white\", alpha=0.4)\n", "ax.set_axis_off()\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Always consider whether your binning is the optimal one." ] } ], "metadata": { "celltoolbar": "Tags", "kernelspec": { "display_name": "geo_dev", "language": "python", "name": "geo_dev" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.2" } }, "nbformat": 4, "nbformat_minor": 4 }