Skip to content
Merged
Show file tree
Hide file tree
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
107 changes: 72 additions & 35 deletions 2-analysis-examples/bird-migration.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"import geopandas as gpd\n",
"import movingpandas as mpd\n",
"import shapely as shp\n",
"import hvplot.pandas \n",
"import hvplot.pandas\n",
"import matplotlib.pyplot as plt\n",
"\n",
"from geopandas import GeoDataFrame, read_file\n",
Expand All @@ -49,11 +49,14 @@
"from urllib.request import urlretrieve\n",
"\n",
"import warnings\n",
"warnings.filterwarnings('ignore')\n",
"\n",
"plot_defaults = {'linewidth':5, 'capstyle':'round', 'figsize':(9,3), 'legend':True}\n",
"opts.defaults(opts.Overlay(active_tools=['wheel_zoom'], frame_width=300, frame_height=500))\n",
"hvplot_defaults = {'tiles':None, 'cmap':'Viridis', 'colorbar':True}\n",
"warnings.filterwarnings(\"ignore\")\n",
"\n",
"plot_defaults = {\"linewidth\": 5, \"capstyle\": \"round\", \"figsize\": (9, 3), \"legend\": True}\n",
"opts.defaults(\n",
" opts.Overlay(active_tools=[\"wheel_zoom\"], frame_width=300, frame_height=500)\n",
")\n",
"hvplot_defaults = {\"tiles\": None, \"cmap\": \"Viridis\", \"colorbar\": True}\n",
"\n",
"mpd.show_versions()"
]
Expand All @@ -72,7 +75,7 @@
"outputs": [],
"source": [
"%%time\n",
"df = read_file('../data/gulls.gpkg')\n",
"df = read_file(\"../data/gulls.gpkg\")\n",
"print(f\"Finished reading {len(df)}\")"
]
},
Expand Down Expand Up @@ -114,7 +117,7 @@
"metadata": {},
"outputs": [],
"source": [
"df['individual-local-identifier'].unique()"
"df[\"individual-local-identifier\"].unique()"
]
},
{
Expand All @@ -130,7 +133,7 @@
"metadata": {},
"outputs": [],
"source": [
"df['individual-local-identifier'].value_counts().plot(kind='bar', figsize=(17,3))"
"df[\"individual-local-identifier\"].value_counts().plot(kind=\"bar\", figsize=(17, 3))"
]
},
{
Expand All @@ -146,7 +149,9 @@
"metadata": {},
"outputs": [],
"source": [
"tc = mpd.TrajectoryCollection(df, 'individual-local-identifier', t='timestamp', min_length=100) \n",
"tc = mpd.TrajectoryCollection(\n",
" df, \"individual-local-identifier\", t=\"timestamp\", min_length=100\n",
")\n",
"tc"
]
},
Expand Down Expand Up @@ -186,7 +191,7 @@
"metadata": {},
"outputs": [],
"source": [
"filtered = tc.filter('individual-local-identifier', '91916A')\n",
"filtered = tc.filter(\"individual-local-identifier\", \"91916A\")\n",
"my_traj = filtered.trajectories[0].copy()\n",
"my_traj.df.head()"
]
Expand All @@ -197,7 +202,7 @@
"metadata": {},
"outputs": [],
"source": [
"my_traj.hvplot(title=f'Movement of {my_traj.id}', line_width=2) "
"my_traj.hvplot(title=f\"Movement of {my_traj.id}\", line_width=2)"
]
},
{
Expand All @@ -215,7 +220,7 @@
"metadata": {},
"outputs": [],
"source": [
"trips_by_year = mpd.TemporalSplitter(filtered).split(mode='year')\n",
"trips_by_year = mpd.TemporalSplitter(filtered).split(mode=\"year\")\n",
"trips_by_year.to_traj_gdf()"
]
},
Expand All @@ -232,7 +237,7 @@
"metadata": {},
"outputs": [],
"source": [
"one_year = trips_by_year.get_trajectory('91916A_2010-12-31 00:00:00')\n",
"one_year = trips_by_year.get_trajectory(\"91916A_2010-12-31 00:00:00\")\n",
"one_year"
]
},
Expand All @@ -251,8 +256,14 @@
"metadata": {},
"outputs": [],
"source": [
"one_year.hvplot(title=f'Movement speed of {one_year.id}', \n",
" line_width=5.0, c='speed', cmap='RdYlGn', colorbar=True, clim=(0,20)) "
"one_year.hvplot(\n",
" title=f\"Movement speed of {one_year.id}\",\n",
" line_width=5.0,\n",
" c=\"speed\",\n",
" cmap=\"RdYlGn\",\n",
" colorbar=True,\n",
" clim=(0, 20),\n",
")"
]
},
{
Expand All @@ -270,8 +281,11 @@
"source": [
"def plot_location_at_timestamp(traj, t, fig_size=250):\n",
" loc = GeoDataFrame([traj.get_row_at(t)])\n",
" return (loc.hvplot(title=str(t), geo=True, tiles='OSM', size=200, color='red') * \n",
" traj.hvplot(line_width=1.0, color='black', tiles=False, width=fig_size, height=fig_size))"
" return loc.hvplot(\n",
" title=str(t), geo=True, tiles=\"OSM\", size=200, color=\"red\"\n",
" ) * traj.hvplot(\n",
" line_width=1.0, color=\"black\", tiles=False, width=fig_size, height=fig_size\n",
" )"
]
},
{
Expand All @@ -280,9 +294,11 @@
"metadata": {},
"outputs": [],
"source": [
"( plot_location_at_timestamp(one_year, datetime(2010,9,1)) + \n",
" plot_location_at_timestamp(one_year, datetime(2010,10,1)) +\n",
" plot_location_at_timestamp(one_year, datetime(2010,11,1)) )"
"(\n",
" plot_location_at_timestamp(one_year, datetime(2010, 9, 1))\n",
" + plot_location_at_timestamp(one_year, datetime(2010, 10, 1))\n",
" + plot_location_at_timestamp(one_year, datetime(2010, 11, 1))\n",
")"
]
},
{
Expand All @@ -302,11 +318,13 @@
" ts = [datetime(year, month, day) for year in traj.df.index.year.unique()]\n",
" return plot_locations_at_timestamps(traj, ts, ax=ax)\n",
"\n",
"def plot_locations_at_timestamps(traj, ts, ax=None): \n",
"\n",
"def plot_locations_at_timestamps(traj, ts, ax=None):\n",
" loc = GeoDataFrame([traj.get_row_at(t) for t in ts])\n",
" loc['date_label'] = loc.index.strftime('%Y-%m-%d')\n",
" return (loc.hvplot(title=f'Movement of {traj.id}', c='date_label', size=200, geo=True, tiles='OSM') *\n",
" traj.hvplot(line_width=1.0, color='black', geo=True, tiles=False) )"
" loc[\"date_label\"] = loc.index.strftime(\"%Y-%m-%d\")\n",
" return loc.hvplot(\n",
" title=f\"Movement of {traj.id}\", c=\"date_label\", size=200, geo=True, tiles=\"OSM\"\n",
" ) * traj.hvplot(line_width=1.0, color=\"black\", geo=True, tiles=False)"
]
},
{
Expand Down Expand Up @@ -334,7 +352,9 @@
"outputs": [],
"source": [
"area_of_interest = Polygon([(30, 25), (50, 25), (50, 15), (30, 15), (30, 25)])\n",
"plotted_area_of_interest = GeoDataFrame(pd.DataFrame([{'geometry': area_of_interest, 'id': 1}]), crs=4326).hvplot(geo=True, color='yellow', alpha=0.5)"
"plotted_area_of_interest = GeoDataFrame(\n",
" pd.DataFrame([{\"geometry\": area_of_interest, \"id\": 1}]), crs=4326\n",
").hvplot(geo=True, color=\"yellow\", alpha=0.5)"
]
},
{
Expand All @@ -347,7 +367,9 @@
"print(f\"Found {len(arrivals)} arrivals\")\n",
"\n",
"for traj in arrivals:\n",
" print(f\"Individual '{traj.df['individual-local-identifier'].iloc[0]}' arrived at {traj.get_start_time()}\")"
" print(\n",
" f\"Individual '{traj.df['individual-local-identifier'].iloc[0]}' arrived at {traj.get_start_time()}\"\n",
" )"
]
},
{
Expand All @@ -356,7 +378,10 @@
"metadata": {},
"outputs": [],
"source": [
"( plot_locations_at_timestamps(my_traj, [traj.get_start_time() for traj in arrivals]) * plotted_area_of_interest )"
"(\n",
" plot_locations_at_timestamps(my_traj, [traj.get_start_time() for traj in arrivals])\n",
" * plotted_area_of_interest\n",
")"
]
},
{
Expand All @@ -376,7 +401,12 @@
"source": [
"year_of_interest = 2010\n",
"trajs_in_aoi = tc.clip(area_of_interest)\n",
"relevant = [ traj for traj in trajs_in_aoi if traj.get_start_time().year <= year_of_interest and traj.get_end_time().year >= year_of_interest]\n",
"relevant = [\n",
" traj\n",
" for traj in trajs_in_aoi\n",
" if traj.get_start_time().year <= year_of_interest\n",
" and traj.get_end_time().year >= year_of_interest\n",
"]\n",
"print(\"Found {} arrivals\".format(len(relevant)))"
]
},
Expand All @@ -387,9 +417,13 @@
"outputs": [],
"source": [
"for traj in relevant:\n",
" print(\"Individual '{}' arrived at {} (duration: {})\".format(\n",
" traj.df['individual-local-identifier'].iloc[0], traj.get_start_time().date(), \n",
" traj.get_end_time()-traj.get_start_time()))"
" print(\n",
" \"Individual '{}' arrived at {} (duration: {})\".format(\n",
" traj.df[\"individual-local-identifier\"].iloc[0],\n",
" traj.get_start_time().date(),\n",
" traj.get_end_time() - traj.get_start_time(),\n",
" )\n",
" )"
]
},
{
Expand All @@ -407,10 +441,12 @@
"metadata": {},
"outputs": [],
"source": [
"my_traj = tc.get_trajectory('91761A')\n",
"segment = my_traj.get_segment_between(datetime(year_of_interest,1,1), datetime(year_of_interest,12,31))\n",
"my_traj = tc.get_trajectory(\"91761A\")\n",
"segment = my_traj.get_segment_between(\n",
" datetime(year_of_interest, 1, 1), datetime(year_of_interest, 12, 31)\n",
")\n",
"\n",
"segment.hvplot(color='black', line_width=1.0) * plotted_area_of_interest "
"segment.hvplot(color=\"black\", line_width=1.0) * plotted_area_of_interest"
]
},
{
Expand All @@ -435,7 +471,8 @@
"1. [Soccer game](soccer-game.ipynb)\n",
"1. [Mars rover & heli](mars-rover.ipynb)\n",
"1. [Ever Given](ever-given.ipynb)\n",
"1. [Iceberg](iceberg.ipynb) "
"1. [Iceberg](iceberg.ipynb)\n",
"1. [Pollution data](pollution-data.ipynb)"
]
}
],
Expand Down
Loading