2021-02-10 11:45:33 +00:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Urban Mobility\n",
"\n",
"[](https://mybinder.org/v2/gh/anitagraser/ogd-at-lab/main?urlpath=lab/tree/notebooks/mobility.ipynb)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import hvplot.pandas\n",
2021-02-13 18:56:24 +00:00
"import movingpandas as mpd\n",
"from datetime import timedelta\n",
"from utils.dataaccess import get_uber_movement_gdf, get_osm_traces"
2021-02-10 11:45:33 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Uber Movement\n",
"\n",
"Source: https://movement.uber.com/explore/vienna/travel-times\n",
"\n",
"© 2021 Copyright Uber Technologies. Data made available under the [Creative Commons, Attribution Non-Commercial](https://creativecommons.org/licenses/by-nc/3.0/us/) license"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"gdf = get_uber_movement_gdf()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2021-02-10 17:37:44 +00:00
"def plot_uber(source_id, attribute_col, month, *args, **kwargs):\n",
" return ( gdf[(gdf.sourceid==source_id) & (gdf.month==month)].dropna(subset=[attribute_col]).hvplot(\n",
" geo=True, tiles='OSM', c=attribute_col, title=f'{attribute_col.title()} - Month: 2020-{month}', *args, **kwargs) * \n",
" gdf[gdf.index==source_id].hvplot(geo=True).opts(active_tools=['wheel_zoom']))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2021-02-10 11:45:33 +00:00
"COL = 'mean_travel_time'\n",
2021-02-10 17:37:44 +00:00
"ID = 1\n",
"plot_uber(ID, COL, 1) + plot_uber(ID, COL, 2) + plot_uber(ID, COL, 3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"jan = gdf[gdf.month==1].groupby('sourceid').count()\n",
"feb = gdf[gdf.month==2].groupby('sourceid').count()\n",
"mar = gdf[gdf.month==3].groupby('sourceid').count() "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"joined = feb.join(mar, lsuffix='_feb', rsuffix='_mar')\n",
"diff = joined[f'{COL}_mar'] - joined[f'{COL}_feb'] \n",
2021-02-13 18:56:24 +00:00
"diff.hvplot.hist(title='Histogram of destination area counts (March 2020 - Feb 2020)', xlim=(-300,300))"
2021-02-10 17:37:44 +00:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"joined = jan.join(feb, lsuffix='_jan', rsuffix='_feb')\n",
"diff = joined[f'{COL}_feb'] - joined[f'{COL}_jan'] \n",
2021-02-13 18:56:24 +00:00
"diff.hvplot.hist(title='Histogram of destination area counts (Feb 2020 - Jan 2020)', xlim=(-300,300))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## OpenStreetMap Traces"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Source: https://www.openstreetmap.org/traces"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"gdf = get_osm_traces()\n",
"osm_traces = mpd.TrajectoryCollection(gdf, 'track_fid')\n",
"print(f'The OSM traces download contains {len(osm_traces)} tracks')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for track in osm_traces: print(f'Track {track.id}: length={track.get_length():.0f}m')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"osm_traces = mpd.MinTimeDeltaGeneralizer(osm_traces).generalize(tolerance=timedelta(minutes=1))\n",
"osm_traces.hvplot(title='OSM Traces', line_width=7, width=700, height=500)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2021-09-08 18:42:49 +00:00
"osm_traces.get_trajectory(0).hvplot(title='Speed (m/s) along track', c='speed', cmap='RdYlBu',\n",
2021-02-13 18:56:24 +00:00
" line_width=7, width=700, height=500, tiles='CartoLight', colorbar=True)"
2021-02-10 11:45:33 +00:00
]
},
2021-09-08 18:42:49 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Interactive Trajectory Generalization Application\n",
"\n",
"This demo uses pn.interact() which may not work in Jupyter Lab. Use Jupyter Notebook instead if the application does not update when you change the input parameters. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import panel as pn\n",
"from pyproj import CRS"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def my_plot(traj, tolerance=0.001, generalizer='DouglasPeuckerGeneralizer'):\n",
" if generalizer=='DouglasPeuckerGeneralizer':\n",
" generalized = mpd.DouglasPeuckerGeneralizer(traj).generalize(tolerance=tolerance)\n",
" else:\n",
" generalized = mpd.MinDistanceGeneralizer(traj).generalize(tolerance=tolerance)\n",
" generalized.add_speed(overwrite=True)\n",
" return ( generalized.hvplot(title='Speed along trajectory', c='speed', cmap='Viridis', colorbar=True, clim=(0,20), line_width=10, width=500, height=400) + \n",
" generalized.df['speed'].hvplot.hist(title='Speed histogram', width=300, height=450) \n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"traj = osm_traces.get_trajectory(0).to_crs(CRS(31256))\n",
"kw = dict(traj=traj, tolerance=(0, 1000), generalizer=['DouglasPeuckerGeneralizer', 'MinDistanceGeneralizer'])\n",
"pn.interact(my_plot, **kw)"
]
},
2021-02-10 11:45:33 +00:00
{
"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",
2021-09-08 18:42:49 +00:00
"version": "3.7.10"
2021-02-10 11:45:33 +00:00
}
},
"nbformat": 4,
"nbformat_minor": 4
}