Skip to content

Commit 3010a07

Browse files
authored
Add explore() function examples to tutorials and analysis examples (#35)
1 parent 6e22892 commit 3010a07

File tree

9 files changed

+620
-76
lines changed

9 files changed

+620
-76
lines changed

1-tutorials/1-getting-started.ipynb

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,20 @@
2929
"import geopandas as gpd\n",
3030
"import movingpandas as mpd\n",
3131
"import shapely as shp\n",
32-
"import hvplot.pandas \n",
32+
"import hvplot.pandas\n",
3333
"\n",
3434
"from geopandas import GeoDataFrame, read_file\n",
3535
"from shapely.geometry import Point, LineString, Polygon\n",
3636
"from datetime import datetime, timedelta\n",
3737
"from holoviews import opts\n",
3838
"\n",
3939
"import warnings\n",
40-
"warnings.filterwarnings('ignore')\n",
4140
"\n",
42-
"opts.defaults(opts.Overlay(active_tools=['wheel_zoom'], frame_width=500, frame_height=400))\n",
41+
"warnings.filterwarnings(\"ignore\")\n",
42+
"\n",
43+
"opts.defaults(\n",
44+
" opts.Overlay(active_tools=[\"wheel_zoom\"], frame_width=500, frame_height=400)\n",
45+
")\n",
4346
"\n",
4447
"mpd.show_versions()"
4548
]
@@ -62,12 +65,14 @@
6265
"metadata": {},
6366
"outputs": [],
6467
"source": [
65-
"df = pd.DataFrame([\n",
66-
" {'geometry':Point(0,0), 't':datetime(2018,1,1,12,0,0)},\n",
67-
" {'geometry':Point(6,0), 't':datetime(2018,1,1,12,6,0)},\n",
68-
" {'geometry':Point(6,6), 't':datetime(2018,1,1,12,10,0)},\n",
69-
" {'geometry':Point(9,9), 't':datetime(2018,1,1,12,15,0)}\n",
70-
"]).set_index('t')\n",
68+
"df = pd.DataFrame(\n",
69+
" [\n",
70+
" {\"geometry\": Point(0, 0), \"t\": datetime(2018, 1, 1, 12, 0, 0)},\n",
71+
" {\"geometry\": Point(6, 0), \"t\": datetime(2018, 1, 1, 12, 6, 0)},\n",
72+
" {\"geometry\": Point(6, 6), \"t\": datetime(2018, 1, 1, 12, 10, 0)},\n",
73+
" {\"geometry\": Point(9, 9), \"t\": datetime(2018, 1, 1, 12, 15, 0)},\n",
74+
" ]\n",
75+
").set_index(\"t\")\n",
7176
"gdf = GeoDataFrame(df, crs=31256)\n",
7277
"toy_traj = mpd.Trajectory(gdf, 1)\n",
7378
"toy_traj"
@@ -131,7 +136,7 @@
131136
"metadata": {},
132137
"outputs": [],
133138
"source": [
134-
"df = pd.read_csv('../data/geolife_small.csv', delimiter=';')\n",
139+
"df = pd.read_csv(\"../data/geolife_small.csv\", delimiter=\";\")\n",
135140
"df"
136141
]
137142
},
@@ -141,7 +146,7 @@
141146
"metadata": {},
142147
"outputs": [],
143148
"source": [
144-
"traj_collection = mpd.TrajectoryCollection(df, 'trajectory_id', t='t', x='X', y='Y')\n",
149+
"traj_collection = mpd.TrajectoryCollection(df, \"trajectory_id\", t=\"t\", x=\"X\", y=\"Y\")\n",
145150
"print(traj_collection)"
146151
]
147152
},
@@ -151,7 +156,7 @@
151156
"metadata": {},
152157
"outputs": [],
153158
"source": [
154-
"traj_collection.plot(column='trajectory_id', legend=True, figsize=(9,5))"
159+
"traj_collection.plot(column=\"trajectory_id\", legend=True, figsize=(9, 5))"
155160
]
156161
},
157162
{
@@ -168,7 +173,7 @@
168173
"metadata": {},
169174
"outputs": [],
170175
"source": [
171-
"gdf = read_file('../data/geolife_small.gpkg')\n",
176+
"gdf = read_file(\"../data/geolife_small.gpkg\")\n",
172177
"gdf"
173178
]
174179
},
@@ -186,7 +191,7 @@
186191
"metadata": {},
187192
"outputs": [],
188193
"source": [
189-
"traj_collection = mpd.TrajectoryCollection(gdf, 'trajectory_id', t='t')\n",
194+
"traj_collection = mpd.TrajectoryCollection(gdf, \"trajectory_id\", t=\"t\")\n",
190195
"print(traj_collection)"
191196
]
192197
},
@@ -196,7 +201,7 @@
196201
"metadata": {},
197202
"outputs": [],
198203
"source": [
199-
"traj_collection.plot(column='trajectory_id', legend=True, figsize=(9,5))"
204+
"traj_collection.plot(column=\"trajectory_id\", legend=True, figsize=(9, 5))"
200205
]
201206
},
202207
{
@@ -208,6 +213,22 @@
208213
"traj_collection.plot()"
209214
]
210215
},
216+
{
217+
"cell_type": "markdown",
218+
"metadata": {},
219+
"source": [
220+
"To visualize trajectories in their geographical context, we can also create interactive plots with basemaps:"
221+
]
222+
},
223+
{
224+
"cell_type": "code",
225+
"execution_count": null,
226+
"metadata": {},
227+
"outputs": [],
228+
"source": [
229+
"traj_collection.explore(column=\"trajectory_id\", cmap=\"plasma\", tiles=\"CartoDB positron\")"
230+
]
231+
},
211232
{
212233
"attachments": {},
213234
"cell_type": "markdown",
@@ -232,15 +253,24 @@
232253
"metadata": {},
233254
"outputs": [],
234255
"source": [
235-
"my_traj.plot(linewidth=5, capstyle='round', figsize=(9,3))"
256+
"my_traj.plot(linewidth=5, capstyle=\"round\", figsize=(9, 3))"
236257
]
237258
},
238259
{
239260
"attachments": {},
240261
"cell_type": "markdown",
241262
"metadata": {},
242263
"source": [
243-
"To visualize trajectories in their geographical context, we can also create interactive plots with basemaps:"
264+
"To visualize trajectories in their geographical context, we can again create interactive plots with basemaps:"
265+
]
266+
},
267+
{
268+
"cell_type": "code",
269+
"execution_count": null,
270+
"metadata": {},
271+
"outputs": [],
272+
"source": [
273+
"my_traj.hvplot(line_width=7.0, tiles=\"OSM\")"
244274
]
245275
},
246276
{
@@ -249,7 +279,7 @@
249279
"metadata": {},
250280
"outputs": [],
251281
"source": [
252-
"my_traj.hvplot(line_width=7.0, tiles='OSM')"
282+
"my_traj.explore(tiles=\"CartoDB positron\", style_kwds={\"weight\": 4})"
253283
]
254284
},
255285
{
@@ -274,7 +304,7 @@
274304
" }\n",
275305
")\n",
276306
"gdf = gpd.GeoDataFrame(df, crs=4326)\n",
277-
"tc = mpd.TrajectoryCollection(gdf, traj_id_col='trajectory_id', t='t')\n",
307+
"tc = mpd.TrajectoryCollection(gdf, traj_id_col=\"trajectory_id\", t=\"t\")\n",
278308
"tc"
279309
]
280310
},

1-tutorials/5-intersecting-with-polygons.ipynb

Lines changed: 70 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,21 @@
2626
"import geopandas as gpd\n",
2727
"import movingpandas as mpd\n",
2828
"import shapely as shp\n",
29-
"import hvplot.pandas \n",
29+
"import hvplot.pandas\n",
30+
"import folium\n",
3031
"\n",
3132
"from geopandas import GeoDataFrame, read_file\n",
3233
"from shapely.geometry import Point, LineString, Polygon\n",
3334
"from datetime import datetime, timedelta\n",
3435
"from holoviews import opts\n",
3536
"\n",
3637
"import warnings\n",
37-
"warnings.filterwarnings('ignore')\n",
3838
"\n",
39-
"opts.defaults(opts.Overlay(active_tools=['wheel_zoom'], frame_width=500, frame_height=400))\n",
39+
"warnings.filterwarnings(\"ignore\")\n",
40+
"\n",
41+
"opts.defaults(\n",
42+
" opts.Overlay(active_tools=[\"wheel_zoom\"], frame_width=500, frame_height=400)\n",
43+
")\n",
4044
"\n",
4145
"mpd.show_versions()"
4246
]
@@ -47,8 +51,8 @@
4751
"metadata": {},
4852
"outputs": [],
4953
"source": [
50-
"gdf = read_file('../data/geolife_small.gpkg')\n",
51-
"tc = mpd.TrajectoryCollection(gdf, 'trajectory_id', t='t')"
54+
"gdf = read_file(\"../data/geolife_small.gpkg\")\n",
55+
"tc = mpd.TrajectoryCollection(gdf, \"trajectory_id\", t=\"t\")"
5256
]
5357
},
5458
{
@@ -65,12 +69,14 @@
6569
"metadata": {},
6670
"outputs": [],
6771
"source": [
68-
"xmin, xmax, ymin, ymax = 116.365035,116.3702945,39.904675,39.907728\n",
69-
"polygon = Polygon([(xmin,ymin), (xmin,ymax), (xmax,ymax), (xmax,ymin), (xmin,ymin)])\n",
72+
"xmin, xmax, ymin, ymax = 116.365035, 116.3702945, 39.904675, 39.907728\n",
73+
"polygon = Polygon(\n",
74+
" [(xmin, ymin), (xmin, ymax), (xmax, ymax), (xmax, ymin), (xmin, ymin)]\n",
75+
")\n",
7076
"\n",
7177
"my_traj = tc.trajectories[2]\n",
7278
"intersections = my_traj.clip(polygon)\n",
73-
" \n",
79+
"\n",
7480
"print(\"Found {} intersections\".format(len(intersections)))"
7581
]
7682
},
@@ -81,8 +87,24 @@
8187
"outputs": [],
8288
"source": [
8389
"ax = my_traj.plot()\n",
84-
"gpd.GeoSeries(polygon).plot(ax=ax, color='lightgray')\n",
85-
"intersections.plot(ax=ax, color='red', linewidth=5)"
90+
"gpd.GeoSeries(polygon).plot(ax=ax, color=\"lightgray\")\n",
91+
"intersections.plot(ax=ax, color=\"red\", linewidth=5)"
92+
]
93+
},
94+
{
95+
"cell_type": "code",
96+
"execution_count": null,
97+
"metadata": {},
98+
"outputs": [],
99+
"source": [
100+
"m = my_traj.explore(color=\"blue\", style_kwds={\"weight\": 4}, name=\"Trajectory\")\n",
101+
"\n",
102+
"intersections.explore(m=m, color=\"red\", style_kwds={\"weight\": 4}, name=\"Intersection\")\n",
103+
"\n",
104+
"folium.TileLayer(\"OpenStreetMap\").add_to(m)\n",
105+
"folium.LayerControl().add_to(m)\n",
106+
"\n",
107+
"m"
86108
]
87109
},
88110
{
@@ -120,6 +142,20 @@
120142
"clipped.plot()"
121143
]
122144
},
145+
{
146+
"cell_type": "code",
147+
"execution_count": null,
148+
"metadata": {},
149+
"outputs": [],
150+
"source": [
151+
"clipped.explore(\n",
152+
" column=\"trajectory_id\",\n",
153+
" cmap=\"cool\",\n",
154+
" tiles=\"CartoDB positron\",\n",
155+
" style_kwds={\"weight\": 4},\n",
156+
")"
157+
]
158+
},
123159
{
124160
"attachments": {},
125161
"cell_type": "markdown",
@@ -134,10 +170,7 @@
134170
"metadata": {},
135171
"outputs": [],
136172
"source": [
137-
"polygon_feature = {\n",
138-
" \"geometry\": polygon,\n",
139-
" \"properties\": {'field1': 'abc'}\n",
140-
"}"
173+
"polygon_feature = {\"geometry\": polygon, \"properties\": {\"field1\": \"abc\"}}"
141174
]
142175
},
143176
{
@@ -160,6 +193,15 @@
160193
"intersections.plot()"
161194
]
162195
},
196+
{
197+
"cell_type": "code",
198+
"execution_count": null,
199+
"metadata": {},
200+
"outputs": [],
201+
"source": [
202+
"intersections.explore(color=\"blue\", style_kwds={\"weight\": 4})"
203+
]
204+
},
163205
{
164206
"cell_type": "code",
165207
"execution_count": null,
@@ -195,6 +237,20 @@
195237
"intersections.plot()"
196238
]
197239
},
240+
{
241+
"cell_type": "code",
242+
"execution_count": null,
243+
"metadata": {},
244+
"outputs": [],
245+
"source": [
246+
"intersections.explore(\n",
247+
" column=\"trajectory_id\",\n",
248+
" cmap=\"autumn\",\n",
249+
" tiles=\"CartoDB positron\",\n",
250+
" style_kwds={\"weight\": 4},\n",
251+
")"
252+
]
253+
},
198254
{
199255
"cell_type": "code",
200256
"execution_count": null,

0 commit comments

Comments
 (0)