Skip to content

Instantly share code, notes, and snippets.

@sannehombroek
Last active January 14, 2021 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sannehombroek/e535f117a6a70e6cdb5567fbb22b2433 to your computer and use it in GitHub Desktop.
Save sannehombroek/e535f117a6a70e6cdb5567fbb22b2433 to your computer and use it in GitHub Desktop.
streetcolors
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import ipywidgets as widgets\n",
"from ipywidgets import interact, interact_manual\n",
"import osmnx as ox\n",
"import matplotlib.patches as mpatches\n",
"import matplotlib.pyplot as plt\n",
"\n",
"def draw_map(Address,Distance):\n",
" # Give each streettype a color based on the name. If both names occur (eg: Rijksstraatweg), first one in the list wins\n",
" def colourcode(x):\n",
" # if (your_street in x):\n",
" if x==your_street:\n",
" return '#ff6200'\n",
" elif ('laan' in x): \n",
" return 'green'\n",
" elif ('weg' in x):\n",
" return 'grey'\n",
" elif ('straat' in x):\n",
" return 'red'\n",
" elif ('gracht' in x):\n",
" return 'blue'\n",
" elif ('kade' in x):\n",
" return 'deepskyblue'\n",
" elif ('dreef' in x):\n",
" return 'darkgrey'\n",
" elif ('plein' in x):\n",
" return 'silver'\n",
" elif ('steeg' in x):\n",
" return 'purple'\n",
" elif ('burcht' in x):\n",
" return 'saddlebrown'\n",
" elif ('erf' in x):\n",
" return 'springgreen'\n",
" elif ('hof' in x):\n",
" return 'darkgreen'\n",
" elif ('plantsoen' in x):\n",
" return 'lawngreen'\n",
" elif ('pad' in x):\n",
" return 'burlywood'\n",
" elif ('burg' in x):\n",
" return 'dimgray'\n",
" elif ('steijn' in x):\n",
" return 'rosybrown'\n",
" elif (x[-3:]=='lei'):\n",
" return 'black'\n",
" else:\n",
" return 'gainsboro'\n",
" # Give the input street a wider linewidth\n",
" def linethick(x):\n",
" if x==your_street: return 7\n",
" else: return 1\n",
" \n",
" # USE THE USER INPUT TO CREATE A GRAPH AROUND THAT ADDRESS\n",
" G3 = ox.graph_from_address(Address, network_type='drive',dist=Distance, dist_type='bbox', simplify=False)\n",
" edge_attributes = ox.graph_to_gdfs(G3, nodes=False)\n",
" \n",
" # Color every street based on the streettype. First split the address into street and city\n",
" your_street=Address.split(',',1)[0].lower()\n",
" city=Address.split(',',1)[1].lower().strip()\n",
" ec = [colourcode(str(row['name']).lower()) for index, row in edge_attributes.iterrows()]\n",
" lw = [linethick(str(row['name']).lower()) for index, row in edge_attributes.iterrows()]\n",
" fig,ax= ox.plot.plot_graph(G3, bgcolor='white', ax=None, node_size=0, node_color='w', node_edgecolor='gray', node_zorder=2,\n",
" edge_color=ec, edge_linewidth=lw, edge_alpha=1, figsize=(25,25), dpi=300 , show=False, close=False)\n",
" \n",
" # ADD TITLE AND LEGEND\n",
" plt.figtext(0.4, 0.94,'Your Street: ' + your_street.capitalize(), fontsize=40, color='#ff6200', ha ='left')\n",
" plt.figtext(0.4, 0.97, 'Your City: ' + city.capitalize(), fontsize=40, color='black', ha ='left')\n",
" red_patch = mpatches.Patch(color='red', label='Straat')\n",
" blue_patch =mpatches.Patch(color='blue', label='Gracht')\n",
" dblue_patch =mpatches.Patch(color='deepskyblue', label='Kade')\n",
" purple_patch =mpatches.Patch(color='purple', label='Steeg')\n",
" green_patch =mpatches.Patch(color='green', label='Laan')\n",
" sgreen_patch=mpatches.Patch(color='springgreen', label='Erf')\n",
" dgreen_patch=mpatches.Patch(color='darkgreen', label='Hof')\n",
" sbrown_patch=mpatches.Patch(color='saddlebrown', label='Burcht')\n",
" dgrey_patch=mpatches.Patch(color='dimgray', label='Burg')\n",
" grey_patch =mpatches.Patch(color='grey', label='Weg')\n",
" darkgrey_patch =mpatches.Patch(color='darkgrey', label='Dreef')\n",
" silver_patch =mpatches.Patch(color='silver', label='Plein')\n",
" rbrown_patch=mpatches.Patch(color='rosybrown', label='Steijn')\n",
" bwood_patch=mpatches.Patch(color='burlywood', label='Pad')\n",
" black_patch=mpatches.Patch(color='black', label='Lei')\n",
" gains_patch =mpatches.Patch(color='gainsboro', label='Anders')\n",
" plt.legend(fontsize=16, frameon=False,loc='upper center', bbox_to_anchor=(0.5, 1.05), ncol=8, handles=[red_patch,green_patch,sgreen_patch,dgreen_patch,blue_patch,dblue_patch,grey_patch,dgrey_patch,sbrown_patch,bwood_patch,black_patch,darkgrey_patch,purple_patch,silver_patch,rbrown_patch,gains_patch])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"interact_manual(draw_map,Address='Dam, Amsterdam',Distance=widgets.IntSlider(min=500, max=5000, step=500))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.8.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
name: osmnx-examples
channels:
- conda-forge
dependencies:
- osmnx=1.0.0
- python-igraph
- ipywidgets
- matplotlib
numpy
ipywidgets
nbinteract
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment