Exporting Trajectories¶
![No description has been provided for this image](https://movingpandas.github.io/movingpandas/assets/img/movingpandas.png)
Trajectories and TrajectoryCollections can be converted back to GeoDataFrames that can then be exported to different GIS file formats.
In [ ]:
import pandas as pd
import geopandas as gpd
import movingpandas as mpd
import shapely as shp
import hvplot.pandas
from geopandas import GeoDataFrame, read_file
from shapely.geometry import Point, LineString, Polygon
from datetime import datetime, timedelta
from holoviews import opts
import warnings
warnings.filterwarnings('ignore')
opts.defaults(opts.Overlay(active_tools=['wheel_zoom'], frame_width=500, frame_height=400))
mpd.show_versions()
MovingPandas 0.18.1 SYSTEM INFO ----------- python : 3.10.14 | packaged by conda-forge | (main, Mar 20 2024, 12:40:08) [MSC v.1938 64 bit (AMD64)] executable : c:\Users\Agarkovam\AppData\Local\miniforge3\envs\mpd-ex\python.exe machine : Windows-10-10.0.19045-SP0 GEOS, GDAL, PROJ INFO --------------------- GEOS : None GEOS lib : None GDAL : 3.8.5 GDAL data dir: None PROJ : 9.4.0 PROJ data dir: C:\Users\Agarkovam\AppData\Local\miniforge3\envs\mpd-ex\Library\share\proj PYTHON DEPENDENCIES ------------------- geopandas : 0.14.4 pandas : 2.2.2 fiona : 1.9.6 numpy : 1.26.4 shapely : 2.0.4 rtree : 1.2.0 pyproj : 3.6.1 matplotlib : 3.8.4 mapclassify: 2.6.1 geopy : 2.4.1 holoviews : 1.17.1 hvplot : 0.8.3 geoviews : 1.9.6 stonesoup : 1.2
In [ ]:
gdf = read_file('../data/geolife_small.gpkg')
tc = mpd.TrajectoryCollection(gdf, 'trajectory_id', t='t')
tc
Out[ ]:
TrajectoryCollection with 5 trajectories
In [ ]:
tc.to_point_gdf()
Out[ ]:
id | sequence | trajectory_id | tracker | geometry | |
---|---|---|---|---|---|
t | |||||
2008-12-11 04:42:14 | 1 | 1 | 1 | 19 | POINT (116.39131 39.89857) |
2008-12-11 04:42:16 | 2 | 2 | 1 | 19 | POINT (116.39132 39.89862) |
2008-12-11 04:43:26 | 3 | 3 | 1 | 19 | POINT (116.39093 39.89861) |
2008-12-11 04:43:32 | 4 | 4 | 1 | 19 | POINT (116.39083 39.89863) |
2008-12-11 04:43:47 | 5 | 5 | 1 | 19 | POINT (116.38941 39.89872) |
... | ... | ... | ... | ... | ... |
2009-02-25 14:31:04 | 6993 | 867 | 5 | 2 | POINT (116.33719 39.92623) |
2009-02-25 14:31:09 | 6994 | 868 | 5 | 2 | POINT (116.33721 39.92624) |
2009-02-25 14:31:14 | 6995 | 869 | 5 | 2 | POINT (116.33726 39.92621) |
2009-02-25 14:31:19 | 6996 | 870 | 5 | 2 | POINT (116.33729 39.92620) |
2009-02-25 14:31:24 | 6997 | 871 | 5 | 2 | POINT (116.33733 39.92619) |
5908 rows × 5 columns
Convert to a line GeoDataFrame¶
In [ ]:
tc.to_line_gdf()
Out[ ]:
id | sequence | trajectory_id | tracker | t | prev_t | geometry | |
---|---|---|---|---|---|---|---|
0 | 2 | 2 | 1 | 19 | 2008-12-11 04:42:16 | 2008-12-11 04:42:14 | LINESTRING (116.39131 39.89857, 116.39132 39.8... |
1 | 3 | 3 | 1 | 19 | 2008-12-11 04:43:26 | 2008-12-11 04:42:16 | LINESTRING (116.39132 39.89862, 116.39093 39.8... |
2 | 4 | 4 | 1 | 19 | 2008-12-11 04:43:32 | 2008-12-11 04:43:26 | LINESTRING (116.39093 39.89861, 116.39083 39.8... |
3 | 5 | 5 | 1 | 19 | 2008-12-11 04:43:47 | 2008-12-11 04:43:32 | LINESTRING (116.39083 39.89863, 116.38941 39.8... |
4 | 6 | 6 | 1 | 19 | 2008-12-11 04:43:50 | 2008-12-11 04:43:47 | LINESTRING (116.38941 39.89872, 116.39052 39.8... |
... | ... | ... | ... | ... | ... | ... | ... |
5898 | 6993 | 867 | 5 | 2 | 2009-02-25 14:31:04 | 2009-02-25 14:31:02 | LINESTRING (116.33715 39.92631, 116.33719 39.9... |
5899 | 6994 | 868 | 5 | 2 | 2009-02-25 14:31:09 | 2009-02-25 14:31:04 | LINESTRING (116.33719 39.92623, 116.33721 39.9... |
5900 | 6995 | 869 | 5 | 2 | 2009-02-25 14:31:14 | 2009-02-25 14:31:09 | LINESTRING (116.33721 39.92624, 116.33726 39.9... |
5901 | 6996 | 870 | 5 | 2 | 2009-02-25 14:31:19 | 2009-02-25 14:31:14 | LINESTRING (116.33726 39.92621, 116.33729 39.9... |
5902 | 6997 | 871 | 5 | 2 | 2009-02-25 14:31:24 | 2009-02-25 14:31:19 | LINESTRING (116.33729 39.92620, 116.33733 39.9... |
5903 rows × 7 columns
Convert to a trajectory GeoDataFrame¶
In [ ]:
tc.to_traj_gdf(wkt=True)
Out[ ]:
trajectory_id | start_t | end_t | geometry | length | direction | wkt | |
---|---|---|---|---|---|---|---|
0 | 1 | 2008-12-11 04:42:14 | 2008-12-11 05:15:46 | LINESTRING (116.39131 39.89857, 116.39132 39.8... | 6207.020261 | 186.681376 | LINESTRING M (116.391305 39.898573 1228970534.... |
1 | 2 | 2009-06-29 07:02:25 | 2009-06-29 11:13:12 | LINESTRING (116.59096 40.07196, 116.59091 40.0... | 38764.575483 | 250.585295 | LINESTRING M (116.590957 40.071961 1246258945.... |
2 | 3 | 2009-02-04 04:32:53 | 2009-02-04 11:20:12 | LINESTRING (116.38569 39.89977, 116.38565 39.8... | 12745.157506 | 304.115160 | LINESTRING M (116.385689 39.899773 1233721973.... |
3 | 4 | 2009-03-10 10:36:45 | 2009-03-10 12:01:07 | LINESTRING (116.38805 39.90342, 116.38804 39.9... | 14363.780551 | 300.732843 | LINESTRING M (116.388053 39.903418 1236681405.... |
4 | 5 | 2009-02-25 09:47:03 | 2009-02-25 14:31:24 | LINESTRING (116.38526 39.90027, 116.38525 39.9... | 39259.779560 | 305.200501 | LINESTRING M (116.385256 39.90027 1235555223.0... |
In [ ]:
tc.to_traj_gdf(agg={'tracker':'mode', 'sequence':['min', 'max']})
Out[ ]:
trajectory_id | start_t | end_t | geometry | length | direction | tracker_mode | sequence_min | sequence_max | |
---|---|---|---|---|---|---|---|---|---|
0 | 1 | 2008-12-11 04:42:14 | 2008-12-11 05:15:46 | LINESTRING (116.39131 39.89857, 116.39132 39.8... | 6207.020261 | 186.681376 | 19 | 1 | 466 |
1 | 2 | 2009-06-29 07:02:25 | 2009-06-29 11:13:12 | LINESTRING (116.59096 40.07196, 116.59091 40.0... | 38764.575483 | 250.585295 | 0 | 1090 | 1986 |
2 | 3 | 2009-02-04 04:32:53 | 2009-02-04 11:20:12 | LINESTRING (116.38569 39.89977, 116.38565 39.8... | 12745.157506 | 304.115160 | 2 | 1 | 1810 |
3 | 4 | 2009-03-10 10:36:45 | 2009-03-10 12:01:07 | LINESTRING (116.38805 39.90342, 116.38804 39.9... | 14363.780551 | 300.732843 | 2 | 1 | 1864 |
4 | 5 | 2009-02-25 09:47:03 | 2009-02-25 14:31:24 | LINESTRING (116.38526 39.90027, 116.38525 39.9... | 39259.779560 | 305.200501 | 2 | 1 | 871 |
Exporting to GIS file formats¶
These GeoDataFrames can be exported to different file formats using GeoPandas, as documented in https://geopandas.org/docs/user_guide/io.html
In [ ]:
export_gdf = tc.to_traj_gdf(agg={'sequence':['min', 'max']})
export_gdf.to_file("temp.gpkg", layer='trajectories', driver="GPKG")
In [ ]:
read_file('temp.gpkg').plot()
Out[ ]:
<Axes: >
In [ ]:
read_file('temp.gpkg')
Out[ ]:
trajectory_id | start_t | end_t | length | direction | sequence_min | sequence_max | geometry | |
---|---|---|---|---|---|---|---|---|
0 | 1 | 2008-12-11 04:42:14 | 2008-12-11 05:15:46 | 6207.020261 | 186.681376 | 1 | 466 | LINESTRING (116.39131 39.89857, 116.39132 39.8... |
1 | 2 | 2009-06-29 07:02:25 | 2009-06-29 11:13:12 | 38764.575483 | 250.585295 | 1090 | 1986 | LINESTRING (116.59096 40.07196, 116.59091 40.0... |
2 | 3 | 2009-02-04 04:32:53 | 2009-02-04 11:20:12 | 12745.157506 | 304.115160 | 1 | 1810 | LINESTRING (116.38569 39.89977, 116.38565 39.8... |
3 | 4 | 2009-03-10 10:36:45 | 2009-03-10 12:01:07 | 14363.780551 | 300.732843 | 1 | 1864 | LINESTRING (116.38805 39.90342, 116.38804 39.9... |
4 | 5 | 2009-02-25 09:47:03 | 2009-02-25 14:31:24 | 39259.779560 | 305.200501 | 1 | 871 | LINESTRING (116.38526 39.90027, 116.38525 39.9... |
In [ ]: