OSRM Wien
OSRM routing
For this project we use OSRM to calculate foot routes on an OpenStreetMap export of Vienna. To install it follow build instructions. For usage, check http api doc. We also use the voting district (Zählbezirk) data in shp format from data.wien.gv.
Running OSRM
- Save export as
map_wien.osm
- Extract foot routing from osm dump:
osrm-extract map_wien.osm -p osrm-backend-master/profiles/foot.lua
- Process extracted data (takes a while)
osrm-contract map_wien.osrm
- Start server (localhost 5000)
osrm-routed map_wien.osrm
Prepare python environment
# --- notebook magic
# Make plots appear inline
%matplotlib inline
# --- imports
import requests # To access OSRM over http API
import json # Convert results into python
import numpy as np # Numerical python
import geopandas as gpd # Geospatial data frames
# --- Globals
DATA_DIR = "OPENDATA/" # Path
FIGSIZE = (15, 10) # Size of plots
BEZIRK = 9
MODE = "foot" # Must mach profile in osrm-extract
ENDPOINT= "http://127.0.0.1:5000/route/v1" # OSRM server route
# --- Helpers
# Read a shapefile
read_shp = lambda x: gpd.read_file(os.path.join(DATA_DIR, x))
# Create a matplotlib figure and axis
fig_ax = lambda: plt.subplots(figsize=FIGSIZE)
# return variable type and value as string
vardump = lambda x: "{}: {}".format(type(x), x)
# Load data set
zbzk = read_shp("ZAEHLBEZIRKOGD")
zbzk["BEZNR"]=zbzk["BEZNR"].apply(lambda x: int(x))
zbzk["ZBEZNR"]=zbzk["ZBEZNR"].apply(lambda x: int(x))
# Create bezirk
bzk = zbzk[["geometry", "BEZNR"]].dissolve(by="BEZNR")
def route_query(start, end, endpoint=ENDPOINT, mode=MODE):
r = requests.get(
"{}/{}/"
"{:.4f},{:.4f};{:.4f},{:.4f}?steps=true".format(
endpoint, mode, start[0], start[1], end[0], end[1]))
r.connection.close()
return json.loads(r.text)
def get_maneuvers(route):
maneuvers = []
for leg in route["routes"][0]["legs"]:
for step in leg["steps"]:
maneuvers.append(step["maneuver"]["location"])
return np.array(maneuvers)
fig, ax = fig_ax()
zbez.plot(ax=ax)
bez.plot(ax=ax, linewidth=8)
centroid.plot(ax=ax, markersize=20, zorder=2, color="b")
zcentroid.plot(ax=ax, markersize=15, zorder=2, color="r")
<matplotlib.axes._subplots.AxesSubplot at 0x10c3064e0>
# Extract BEZRIK
zbez = zbzk[zbzk["BEZNR"]==BEZIRK]
bez = gpd.GeoDataFrame(geometry=[bzk.loc[BEZIRK].geometry])
bounds = bez.bounds
centroid = bez.centroid
zbounds = zbez.bounds
zcentroid = zbez.centroid
cx, cy = centroid[0].x, centroid[0].y
fig, ax = fig_ax()
zbez.plot(ax=ax)
bez.plot(ax=ax, linewidth=8)
centroid.plot(ax=ax, markersize=20, zorder=4, color="b")
zcentroid.plot(ax=ax, markersize=15, zorder=4, color="r")
ax.plot(cx, cy, "ob", zorder=3)
plt.axis('equal')
for coord in zcentroid:
route = route_query((cx, cy), (coord.x, coord.y))
maneuvers = get_maneuvers(route)
ax.plot(maneuvers[:, 0], maneuvers[:, 1], "g-", lw=6, zorder=3)