Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graph-based implementation of mean inter-building distance #11

Merged
merged 1 commit into from
Oct 20, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions measuring/morphometrics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"import geopandas\n",
"import libpysal\n",
"import momepy\n",
"import networkx as nx\n",
"import numpy as np\n",
"import pandas as pd\n",
"import pygeos\n",
Expand Down Expand Up @@ -491,22 +492,25 @@
" \n",
" # mean interbuilding distance\n",
" # define adjacency list from lipysal\n",
" adj_list = w.to_adjlist(remove_symmetric=True)\n",
" adj_list[\"distance\"] = (\n",
" adj_list = w.to_adjlist(remove_symmetric=False)\n",
" adj_list[\"weight\"] = (\n",
" df.buildings.iloc[adj_list.focal]\n",
" .reset_index(drop=True)\n",
" .distance(df.buildings.iloc[adj_list.neighbor].reset_index(drop=True)).values\n",
" )\n",
" adj_list = adj_list.set_index(['focal', 'neighbor'])\n",
"\n",
"\n",
" def mean_interbuilding_distance(x):\n",
" neighbours = [x]\n",
" neighbours += w3.neighbors[x]\n",
" return adj_list.distance.loc[neighbours, neighbours].mean()\n",
"\n",
" G = nx.from_pandas_edgelist(\n",
" adj_list, source=\"focal\", target=\"neighbor\", edge_attr=\"weight\"\n",
" )\n",
" ibd = []\n",
" for i in range(len(df)):\n",
" try:\n",
" sub = nx.ego_graph(G, i, radius=3)\n",
" ibd.append(np.nanmean([x[-1] for x in list(sub.edges.data('weight'))]))\n",
" except:\n",
" ibd.append(np.nan)\n",
"\n",
" df['ltbIBD'] = [mean_interbuilding_distance(x) for x in range(len(df))]\n",
" df['ltbIBD'] = ibd\n",
" \n",
" # Reached neighbors and area on 3 topological steps on tessellation\n",
" df['ltcRea'] = [w3.cardinalities[i] for i in range(len(df))]\n",
Expand Down