{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Urban Mobility\n", "\n", "[![Binder](http://mybinder.org/badge_logo.svg)](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", "import movingpandas as mpd\n", "from datetime import timedelta\n", "from utils.dataaccess import get_uber_movement_gdf, get_osm_traces" ] }, { "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": [ "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": [ "COL = 'mean_travel_time'\n", "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", "diff.hvplot.hist(title='Histogram of destination area counts (March 2020 - Feb 2020)', xlim=(-300,300))" ] }, { "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", "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": [ "osm_traces.get_trajectory(0).hvplot(title='Speed (m/s) along track', c='speed', cmap='RdYlBu',\n", " line_width=7, width=700, height=500, tiles='CartoLight', colorbar=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Interactive Trajectory Generalization Application" ] }, { "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", " map_plot = generalized.hvplot(title='Speed along trajectory', c='speed', cmap='Viridis', colorbar=True, clim=(0,20), line_width=10, width=500, height=400)\n", " hist_plot = generalized.df.hvplot.hist('speed', title='Speed histogram', width=300, height=400) \n", " return (map_plot + hist_plot)" ] }, { "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)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.7.10" } }, "nbformat": 4, "nbformat_minor": 4 }