|
38 | 38 | "import geopandas as gpd\n", |
39 | 39 | "import movingpandas as mpd\n", |
40 | 40 | "import shapely as shp\n", |
41 | | - "import hvplot.pandas \n", |
| 41 | + "import hvplot.pandas\n", |
42 | 42 | "import matplotlib.pyplot as plt\n", |
43 | 43 | "\n", |
44 | 44 | "from geopandas import GeoDataFrame, read_file\n", |
|
49 | 49 | "from urllib.request import urlretrieve\n", |
50 | 50 | "\n", |
51 | 51 | "import warnings\n", |
52 | | - "warnings.filterwarnings('ignore')\n", |
53 | 52 | "\n", |
54 | | - "plot_defaults = {'linewidth':5, 'capstyle':'round', 'figsize':(9,3), 'legend':True}\n", |
55 | | - "opts.defaults(opts.Overlay(active_tools=['wheel_zoom'], frame_width=300, frame_height=500))\n", |
56 | | - "hvplot_defaults = {'tiles':None, 'cmap':'Viridis', 'colorbar':True}\n", |
| 53 | + "warnings.filterwarnings(\"ignore\")\n", |
| 54 | + "\n", |
| 55 | + "plot_defaults = {\"linewidth\": 5, \"capstyle\": \"round\", \"figsize\": (9, 3), \"legend\": True}\n", |
| 56 | + "opts.defaults(\n", |
| 57 | + " opts.Overlay(active_tools=[\"wheel_zoom\"], frame_width=300, frame_height=500)\n", |
| 58 | + ")\n", |
| 59 | + "hvplot_defaults = {\"tiles\": None, \"cmap\": \"Viridis\", \"colorbar\": True}\n", |
57 | 60 | "\n", |
58 | 61 | "mpd.show_versions()" |
59 | 62 | ] |
|
72 | 75 | "outputs": [], |
73 | 76 | "source": [ |
74 | 77 | "%%time\n", |
75 | | - "df = read_file('../data/gulls.gpkg')\n", |
| 78 | + "df = read_file(\"../data/gulls.gpkg\")\n", |
76 | 79 | "print(f\"Finished reading {len(df)}\")" |
77 | 80 | ] |
78 | 81 | }, |
|
114 | 117 | "metadata": {}, |
115 | 118 | "outputs": [], |
116 | 119 | "source": [ |
117 | | - "df['individual-local-identifier'].unique()" |
| 120 | + "df[\"individual-local-identifier\"].unique()" |
118 | 121 | ] |
119 | 122 | }, |
120 | 123 | { |
|
130 | 133 | "metadata": {}, |
131 | 134 | "outputs": [], |
132 | 135 | "source": [ |
133 | | - "df['individual-local-identifier'].value_counts().plot(kind='bar', figsize=(17,3))" |
| 136 | + "df[\"individual-local-identifier\"].value_counts().plot(kind=\"bar\", figsize=(17, 3))" |
134 | 137 | ] |
135 | 138 | }, |
136 | 139 | { |
|
146 | 149 | "metadata": {}, |
147 | 150 | "outputs": [], |
148 | 151 | "source": [ |
149 | | - "tc = mpd.TrajectoryCollection(df, 'individual-local-identifier', t='timestamp', min_length=100) \n", |
| 152 | + "tc = mpd.TrajectoryCollection(\n", |
| 153 | + " df, \"individual-local-identifier\", t=\"timestamp\", min_length=100\n", |
| 154 | + ")\n", |
150 | 155 | "tc" |
151 | 156 | ] |
152 | 157 | }, |
|
186 | 191 | "metadata": {}, |
187 | 192 | "outputs": [], |
188 | 193 | "source": [ |
189 | | - "filtered = tc.filter('individual-local-identifier', '91916A')\n", |
| 194 | + "filtered = tc.filter(\"individual-local-identifier\", \"91916A\")\n", |
190 | 195 | "my_traj = filtered.trajectories[0].copy()\n", |
191 | 196 | "my_traj.df.head()" |
192 | 197 | ] |
|
197 | 202 | "metadata": {}, |
198 | 203 | "outputs": [], |
199 | 204 | "source": [ |
200 | | - "my_traj.hvplot(title=f'Movement of {my_traj.id}', line_width=2) " |
| 205 | + "my_traj.hvplot(title=f\"Movement of {my_traj.id}\", line_width=2)" |
201 | 206 | ] |
202 | 207 | }, |
203 | 208 | { |
|
215 | 220 | "metadata": {}, |
216 | 221 | "outputs": [], |
217 | 222 | "source": [ |
218 | | - "trips_by_year = mpd.TemporalSplitter(filtered).split(mode='year')\n", |
| 223 | + "trips_by_year = mpd.TemporalSplitter(filtered).split(mode=\"year\")\n", |
219 | 224 | "trips_by_year.to_traj_gdf()" |
220 | 225 | ] |
221 | 226 | }, |
|
232 | 237 | "metadata": {}, |
233 | 238 | "outputs": [], |
234 | 239 | "source": [ |
235 | | - "one_year = trips_by_year.get_trajectory('91916A_2010-12-31 00:00:00')\n", |
| 240 | + "one_year = trips_by_year.get_trajectory(\"91916A_2010-12-31 00:00:00\")\n", |
236 | 241 | "one_year" |
237 | 242 | ] |
238 | 243 | }, |
|
251 | 256 | "metadata": {}, |
252 | 257 | "outputs": [], |
253 | 258 | "source": [ |
254 | | - "one_year.hvplot(title=f'Movement speed of {one_year.id}', \n", |
255 | | - " line_width=5.0, c='speed', cmap='RdYlGn', colorbar=True, clim=(0,20)) " |
| 259 | + "one_year.hvplot(\n", |
| 260 | + " title=f\"Movement speed of {one_year.id}\",\n", |
| 261 | + " line_width=5.0,\n", |
| 262 | + " c=\"speed\",\n", |
| 263 | + " cmap=\"RdYlGn\",\n", |
| 264 | + " colorbar=True,\n", |
| 265 | + " clim=(0, 20),\n", |
| 266 | + ")" |
256 | 267 | ] |
257 | 268 | }, |
258 | 269 | { |
|
270 | 281 | "source": [ |
271 | 282 | "def plot_location_at_timestamp(traj, t, fig_size=250):\n", |
272 | 283 | " loc = GeoDataFrame([traj.get_row_at(t)])\n", |
273 | | - " return (loc.hvplot(title=str(t), geo=True, tiles='OSM', size=200, color='red') * \n", |
274 | | - " traj.hvplot(line_width=1.0, color='black', tiles=False, width=fig_size, height=fig_size))" |
| 284 | + " return loc.hvplot(\n", |
| 285 | + " title=str(t), geo=True, tiles=\"OSM\", size=200, color=\"red\"\n", |
| 286 | + " ) * traj.hvplot(\n", |
| 287 | + " line_width=1.0, color=\"black\", tiles=False, width=fig_size, height=fig_size\n", |
| 288 | + " )" |
275 | 289 | ] |
276 | 290 | }, |
277 | 291 | { |
|
280 | 294 | "metadata": {}, |
281 | 295 | "outputs": [], |
282 | 296 | "source": [ |
283 | | - "( plot_location_at_timestamp(one_year, datetime(2010,9,1)) + \n", |
284 | | - " plot_location_at_timestamp(one_year, datetime(2010,10,1)) +\n", |
285 | | - " plot_location_at_timestamp(one_year, datetime(2010,11,1)) )" |
| 297 | + "(\n", |
| 298 | + " plot_location_at_timestamp(one_year, datetime(2010, 9, 1))\n", |
| 299 | + " + plot_location_at_timestamp(one_year, datetime(2010, 10, 1))\n", |
| 300 | + " + plot_location_at_timestamp(one_year, datetime(2010, 11, 1))\n", |
| 301 | + ")" |
286 | 302 | ] |
287 | 303 | }, |
288 | 304 | { |
|
302 | 318 | " ts = [datetime(year, month, day) for year in traj.df.index.year.unique()]\n", |
303 | 319 | " return plot_locations_at_timestamps(traj, ts, ax=ax)\n", |
304 | 320 | "\n", |
305 | | - "def plot_locations_at_timestamps(traj, ts, ax=None): \n", |
| 321 | + "\n", |
| 322 | + "def plot_locations_at_timestamps(traj, ts, ax=None):\n", |
306 | 323 | " loc = GeoDataFrame([traj.get_row_at(t) for t in ts])\n", |
307 | | - " loc['date_label'] = loc.index.strftime('%Y-%m-%d')\n", |
308 | | - " return (loc.hvplot(title=f'Movement of {traj.id}', c='date_label', size=200, geo=True, tiles='OSM') *\n", |
309 | | - " traj.hvplot(line_width=1.0, color='black', geo=True, tiles=False) )" |
| 324 | + " loc[\"date_label\"] = loc.index.strftime(\"%Y-%m-%d\")\n", |
| 325 | + " return loc.hvplot(\n", |
| 326 | + " title=f\"Movement of {traj.id}\", c=\"date_label\", size=200, geo=True, tiles=\"OSM\"\n", |
| 327 | + " ) * traj.hvplot(line_width=1.0, color=\"black\", geo=True, tiles=False)" |
310 | 328 | ] |
311 | 329 | }, |
312 | 330 | { |
|
334 | 352 | "outputs": [], |
335 | 353 | "source": [ |
336 | 354 | "area_of_interest = Polygon([(30, 25), (50, 25), (50, 15), (30, 15), (30, 25)])\n", |
337 | | - "plotted_area_of_interest = GeoDataFrame(pd.DataFrame([{'geometry': area_of_interest, 'id': 1}]), crs=4326).hvplot(geo=True, color='yellow', alpha=0.5)" |
| 355 | + "plotted_area_of_interest = GeoDataFrame(\n", |
| 356 | + " pd.DataFrame([{\"geometry\": area_of_interest, \"id\": 1}]), crs=4326\n", |
| 357 | + ").hvplot(geo=True, color=\"yellow\", alpha=0.5)" |
338 | 358 | ] |
339 | 359 | }, |
340 | 360 | { |
|
347 | 367 | "print(f\"Found {len(arrivals)} arrivals\")\n", |
348 | 368 | "\n", |
349 | 369 | "for traj in arrivals:\n", |
350 | | - " print(f\"Individual '{traj.df['individual-local-identifier'].iloc[0]}' arrived at {traj.get_start_time()}\")" |
| 370 | + " print(\n", |
| 371 | + " f\"Individual '{traj.df['individual-local-identifier'].iloc[0]}' arrived at {traj.get_start_time()}\"\n", |
| 372 | + " )" |
351 | 373 | ] |
352 | 374 | }, |
353 | 375 | { |
|
356 | 378 | "metadata": {}, |
357 | 379 | "outputs": [], |
358 | 380 | "source": [ |
359 | | - "( plot_locations_at_timestamps(my_traj, [traj.get_start_time() for traj in arrivals]) * plotted_area_of_interest )" |
| 381 | + "(\n", |
| 382 | + " plot_locations_at_timestamps(my_traj, [traj.get_start_time() for traj in arrivals])\n", |
| 383 | + " * plotted_area_of_interest\n", |
| 384 | + ")" |
360 | 385 | ] |
361 | 386 | }, |
362 | 387 | { |
|
376 | 401 | "source": [ |
377 | 402 | "year_of_interest = 2010\n", |
378 | 403 | "trajs_in_aoi = tc.clip(area_of_interest)\n", |
379 | | - "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", |
| 404 | + "relevant = [\n", |
| 405 | + " traj\n", |
| 406 | + " for traj in trajs_in_aoi\n", |
| 407 | + " if traj.get_start_time().year <= year_of_interest\n", |
| 408 | + " and traj.get_end_time().year >= year_of_interest\n", |
| 409 | + "]\n", |
380 | 410 | "print(\"Found {} arrivals\".format(len(relevant)))" |
381 | 411 | ] |
382 | 412 | }, |
|
387 | 417 | "outputs": [], |
388 | 418 | "source": [ |
389 | 419 | "for traj in relevant:\n", |
390 | | - " print(\"Individual '{}' arrived at {} (duration: {})\".format(\n", |
391 | | - " traj.df['individual-local-identifier'].iloc[0], traj.get_start_time().date(), \n", |
392 | | - " traj.get_end_time()-traj.get_start_time()))" |
| 420 | + " print(\n", |
| 421 | + " \"Individual '{}' arrived at {} (duration: {})\".format(\n", |
| 422 | + " traj.df[\"individual-local-identifier\"].iloc[0],\n", |
| 423 | + " traj.get_start_time().date(),\n", |
| 424 | + " traj.get_end_time() - traj.get_start_time(),\n", |
| 425 | + " )\n", |
| 426 | + " )" |
393 | 427 | ] |
394 | 428 | }, |
395 | 429 | { |
|
407 | 441 | "metadata": {}, |
408 | 442 | "outputs": [], |
409 | 443 | "source": [ |
410 | | - "my_traj = tc.get_trajectory('91761A')\n", |
411 | | - "segment = my_traj.get_segment_between(datetime(year_of_interest,1,1), datetime(year_of_interest,12,31))\n", |
| 444 | + "my_traj = tc.get_trajectory(\"91761A\")\n", |
| 445 | + "segment = my_traj.get_segment_between(\n", |
| 446 | + " datetime(year_of_interest, 1, 1), datetime(year_of_interest, 12, 31)\n", |
| 447 | + ")\n", |
412 | 448 | "\n", |
413 | | - "segment.hvplot(color='black', line_width=1.0) * plotted_area_of_interest " |
| 449 | + "segment.hvplot(color=\"black\", line_width=1.0) * plotted_area_of_interest" |
414 | 450 | ] |
415 | 451 | }, |
416 | 452 | { |
|
435 | 471 | "1. [Soccer game](soccer-game.ipynb)\n", |
436 | 472 | "1. [Mars rover & heli](mars-rover.ipynb)\n", |
437 | 473 | "1. [Ever Given](ever-given.ipynb)\n", |
438 | | - "1. [Iceberg](iceberg.ipynb) " |
| 474 | + "1. [Iceberg](iceberg.ipynb)\n", |
| 475 | + "1. [Pollution data](pollution-data.ipynb)" |
439 | 476 | ] |
440 | 477 | } |
441 | 478 | ], |
|
0 commit comments