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
68 changes: 49 additions & 19 deletions 1-tutorials/1-getting-started.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,20 @@
"import geopandas as gpd\n",
"import movingpandas as mpd\n",
"import shapely as shp\n",
"import hvplot.pandas \n",
"import hvplot.pandas\n",
"\n",
"from geopandas import GeoDataFrame, read_file\n",
"from shapely.geometry import Point, LineString, Polygon\n",
"from datetime import datetime, timedelta\n",
"from holoviews import opts\n",
"\n",
"import warnings\n",
"warnings.filterwarnings('ignore')\n",
"\n",
"opts.defaults(opts.Overlay(active_tools=['wheel_zoom'], frame_width=500, frame_height=400))\n",
"warnings.filterwarnings(\"ignore\")\n",
"\n",
"opts.defaults(\n",
" opts.Overlay(active_tools=[\"wheel_zoom\"], frame_width=500, frame_height=400)\n",
")\n",
"\n",
"mpd.show_versions()"
]
Expand All @@ -62,12 +65,14 @@
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame([\n",
" {'geometry':Point(0,0), 't':datetime(2018,1,1,12,0,0)},\n",
" {'geometry':Point(6,0), 't':datetime(2018,1,1,12,6,0)},\n",
" {'geometry':Point(6,6), 't':datetime(2018,1,1,12,10,0)},\n",
" {'geometry':Point(9,9), 't':datetime(2018,1,1,12,15,0)}\n",
"]).set_index('t')\n",
"df = pd.DataFrame(\n",
" [\n",
" {\"geometry\": Point(0, 0), \"t\": datetime(2018, 1, 1, 12, 0, 0)},\n",
" {\"geometry\": Point(6, 0), \"t\": datetime(2018, 1, 1, 12, 6, 0)},\n",
" {\"geometry\": Point(6, 6), \"t\": datetime(2018, 1, 1, 12, 10, 0)},\n",
" {\"geometry\": Point(9, 9), \"t\": datetime(2018, 1, 1, 12, 15, 0)},\n",
" ]\n",
").set_index(\"t\")\n",
"gdf = GeoDataFrame(df, crs=31256)\n",
"toy_traj = mpd.Trajectory(gdf, 1)\n",
"toy_traj"
Expand Down Expand Up @@ -131,7 +136,7 @@
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv('../data/geolife_small.csv', delimiter=';')\n",
"df = pd.read_csv(\"../data/geolife_small.csv\", delimiter=\";\")\n",
"df"
]
},
Expand All @@ -141,7 +146,7 @@
"metadata": {},
"outputs": [],
"source": [
"traj_collection = mpd.TrajectoryCollection(df, 'trajectory_id', t='t', x='X', y='Y')\n",
"traj_collection = mpd.TrajectoryCollection(df, \"trajectory_id\", t=\"t\", x=\"X\", y=\"Y\")\n",
"print(traj_collection)"
]
},
Expand All @@ -151,7 +156,7 @@
"metadata": {},
"outputs": [],
"source": [
"traj_collection.plot(column='trajectory_id', legend=True, figsize=(9,5))"
"traj_collection.plot(column=\"trajectory_id\", legend=True, figsize=(9, 5))"
]
},
{
Expand All @@ -168,7 +173,7 @@
"metadata": {},
"outputs": [],
"source": [
"gdf = read_file('../data/geolife_small.gpkg')\n",
"gdf = read_file(\"../data/geolife_small.gpkg\")\n",
"gdf"
]
},
Expand All @@ -186,7 +191,7 @@
"metadata": {},
"outputs": [],
"source": [
"traj_collection = mpd.TrajectoryCollection(gdf, 'trajectory_id', t='t')\n",
"traj_collection = mpd.TrajectoryCollection(gdf, \"trajectory_id\", t=\"t\")\n",
"print(traj_collection)"
]
},
Expand All @@ -196,7 +201,7 @@
"metadata": {},
"outputs": [],
"source": [
"traj_collection.plot(column='trajectory_id', legend=True, figsize=(9,5))"
"traj_collection.plot(column=\"trajectory_id\", legend=True, figsize=(9, 5))"
]
},
{
Expand All @@ -208,6 +213,22 @@
"traj_collection.plot()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To visualize trajectories in their geographical context, we can also create interactive plots with basemaps:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"traj_collection.explore(column=\"trajectory_id\", cmap=\"plasma\", tiles=\"CartoDB positron\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
Expand All @@ -232,15 +253,24 @@
"metadata": {},
"outputs": [],
"source": [
"my_traj.plot(linewidth=5, capstyle='round', figsize=(9,3))"
"my_traj.plot(linewidth=5, capstyle=\"round\", figsize=(9, 3))"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"To visualize trajectories in their geographical context, we can also create interactive plots with basemaps:"
"To visualize trajectories in their geographical context, we can again create interactive plots with basemaps:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"my_traj.hvplot(line_width=7.0, tiles=\"OSM\")"
]
},
{
Expand All @@ -249,7 +279,7 @@
"metadata": {},
"outputs": [],
"source": [
"my_traj.hvplot(line_width=7.0, tiles='OSM')"
"my_traj.explore(tiles=\"CartoDB positron\", style_kwds={\"weight\": 4})"
]
},
{
Expand All @@ -274,7 +304,7 @@
" }\n",
")\n",
"gdf = gpd.GeoDataFrame(df, crs=4326)\n",
"tc = mpd.TrajectoryCollection(gdf, traj_id_col='trajectory_id', t='t')\n",
"tc = mpd.TrajectoryCollection(gdf, traj_id_col=\"trajectory_id\", t=\"t\")\n",
"tc"
]
},
Expand Down
84 changes: 70 additions & 14 deletions 1-tutorials/5-intersecting-with-polygons.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,21 @@
"import geopandas as gpd\n",
"import movingpandas as mpd\n",
"import shapely as shp\n",
"import hvplot.pandas \n",
"import hvplot.pandas\n",
"import folium\n",
"\n",
"from geopandas import GeoDataFrame, read_file\n",
"from shapely.geometry import Point, LineString, Polygon\n",
"from datetime import datetime, timedelta\n",
"from holoviews import opts\n",
"\n",
"import warnings\n",
"warnings.filterwarnings('ignore')\n",
"\n",
"opts.defaults(opts.Overlay(active_tools=['wheel_zoom'], frame_width=500, frame_height=400))\n",
"warnings.filterwarnings(\"ignore\")\n",
"\n",
"opts.defaults(\n",
" opts.Overlay(active_tools=[\"wheel_zoom\"], frame_width=500, frame_height=400)\n",
")\n",
"\n",
"mpd.show_versions()"
]
Expand All @@ -47,8 +51,8 @@
"metadata": {},
"outputs": [],
"source": [
"gdf = read_file('../data/geolife_small.gpkg')\n",
"tc = mpd.TrajectoryCollection(gdf, 'trajectory_id', t='t')"
"gdf = read_file(\"../data/geolife_small.gpkg\")\n",
"tc = mpd.TrajectoryCollection(gdf, \"trajectory_id\", t=\"t\")"
]
},
{
Expand All @@ -65,12 +69,14 @@
"metadata": {},
"outputs": [],
"source": [
"xmin, xmax, ymin, ymax = 116.365035,116.3702945,39.904675,39.907728\n",
"polygon = Polygon([(xmin,ymin), (xmin,ymax), (xmax,ymax), (xmax,ymin), (xmin,ymin)])\n",
"xmin, xmax, ymin, ymax = 116.365035, 116.3702945, 39.904675, 39.907728\n",
"polygon = Polygon(\n",
" [(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin), (xmin, ymin)]\n",
")\n",
"\n",
"my_traj = tc.trajectories[2]\n",
"intersections = my_traj.clip(polygon)\n",
" \n",
"\n",
"print(\"Found {} intersections\".format(len(intersections)))"
]
},
Expand All @@ -81,8 +87,24 @@
"outputs": [],
"source": [
"ax = my_traj.plot()\n",
"gpd.GeoSeries(polygon).plot(ax=ax, color='lightgray')\n",
"intersections.plot(ax=ax, color='red', linewidth=5)"
"gpd.GeoSeries(polygon).plot(ax=ax, color=\"lightgray\")\n",
"intersections.plot(ax=ax, color=\"red\", linewidth=5)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"m = my_traj.explore(color=\"blue\", style_kwds={\"weight\": 4}, name=\"Trajectory\")\n",
"\n",
"intersections.explore(m=m, color=\"red\", style_kwds={\"weight\": 4}, name=\"Intersection\")\n",
"\n",
"folium.TileLayer(\"OpenStreetMap\").add_to(m)\n",
"folium.LayerControl().add_to(m)\n",
"\n",
"m"
]
},
{
Expand Down Expand Up @@ -120,6 +142,20 @@
"clipped.plot()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"clipped.explore(\n",
" column=\"trajectory_id\",\n",
" cmap=\"cool\",\n",
" tiles=\"CartoDB positron\",\n",
" style_kwds={\"weight\": 4},\n",
")"
]
},
{
"attachments": {},
"cell_type": "markdown",
Expand All @@ -134,10 +170,7 @@
"metadata": {},
"outputs": [],
"source": [
"polygon_feature = {\n",
" \"geometry\": polygon,\n",
" \"properties\": {'field1': 'abc'}\n",
"}"
"polygon_feature = {\"geometry\": polygon, \"properties\": {\"field1\": \"abc\"}}"
]
},
{
Expand All @@ -160,6 +193,15 @@
"intersections.plot()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"intersections.explore(color=\"blue\", style_kwds={\"weight\": 4})"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -195,6 +237,20 @@
"intersections.plot()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"intersections.explore(\n",
" column=\"trajectory_id\",\n",
" cmap=\"autumn\",\n",
" tiles=\"CartoDB positron\",\n",
" style_kwds={\"weight\": 4},\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
Loading