-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive_maps.py
More file actions
443 lines (375 loc) · 14.2 KB
/
Copy pathinteractive_maps.py
File metadata and controls
443 lines (375 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
"""
Interactive Map Functions for Elevation From DEMs
==============================================
Provides interactive leaflet maps for selecting points and transects.
Uses ipyleaflet for map display in Jupyter notebooks.
"""
import time
from IPython.display import clear_output, display
from ipyleaflet import (
Map, Marker, Polyline, FullScreenControl,
GeoJSON, WidgetControl, AwesomeIcon, basemaps
)
from ipywidgets import HTML, Button, HBox, Layout, VBox, widgets
from pyproj import Transformer
from rasterio import warp
from rasterio.features import shapes
from shapely.geometry import shape, mapping
from geopy.distance import distance as geopy_distance
from elevation_utils import transform_bounds_to_wgs84
def select_point_interactive(timeout=2):
"""
Interactive map to select a single point with draggable marker.
Displays a leaflet map where users can drag a marker and click
'Confirm' to select coordinates.
Parameters
----------
timeout : int
Maximum seconds to wait for user confirmation
Returns
-------
tuple
(longitude, latitude) in WGS84
"""
global coords
coords = (-54.2, 75)
mid_lat = 75.0
mid_lon = -54.2
# Create a leaflet map widget to find an AOI for the spatial query of the STAC API
m = Map(
basemap=basemaps.Esri.WorldImagery,
scroll_wheel_zoom=True,
center=(mid_lat, mid_lon),
zoom=6,
layout=Layout(height="380px", width="700px"),
)
m.add_control(FullScreenControl())
result_output = widgets.Output()
# Create info display
info_html = HTML(
value="<b>Drag the marker, then click Confirm</b>",
layout=Layout(padding="10px"),
)
# Create confirmation button
confirm_button = Button(
description="Confirm Location",
button_style="success",
disabled=False,
tooltip="Click after placing marker",
)
# Create markers (initially slightly offset from center)
centre_marker = Marker(
# location=(center_y, center_x - 0.1),
location=(mid_lat, mid_lon), # Leaflet expects (lat, lon)
draggable=True,
name="Location",
icon=AwesomeIcon(name="map-pin", marker_color="red"),
)
m.add_layer(centre_marker)
# Update info display when marker moves
def update_display(*args):
# Remember: Leaflet uses (lat, lon) format, but we want to store as (lon, lat)
centre_lat, centre_lon = centre_marker.location[0], centre_marker.location[1]
info_html.value = (
f"<b>Current Position:</b><br>"
f"<span style='color:red'>Location:</span> Lon: {centre_lon:.3f}, Lat: {centre_lat:.3f}<br>"
)
# Handle confirmation
def on_confirm(b):
global coords
# Store as (lon, lat) for consistency with GIS conventions
centre_lat, centre_lon = centre_marker.location[0], centre_marker.location[1]
with result_output:
result_output.clear_output()
print(f"Centre confirmed! (lon, lat): {centre_lon}, {centre_lat}")
global coords
coords = (centre_lon, centre_lat)
# Connect callbacks
confirm_button.on_click(on_confirm)
centre_marker.observe(update_display, names=["location"])
update_display() # Initial display update
# Display all components
display(VBox([info_html, m, HBox([confirm_button]), result_output]))
# Wait for confirmation with timeout
start_time = time.time()
while (time.time() - start_time) < timeout:
time.sleep(0.1)
clear_output(wait=True) # Clean up the display
return coords
def select_transect_interactive(# initial_start=(-54.25, 75.025),
# initial_end=(-54.35, 75.025),
# zoom=6,
timeout=30,
raster_data=None, raster_metadata=None, mask=None):
"""Interactive map to select a transect with two draggable markers.
Displays a leaflet map where users can drag start and end markers
to define a transect. Optionally overlays raster footprint for context.
Parameters
----------
timeout : int
Maximum seconds to wait for user confirmation
raster_data : ndarray, optional
2D raster array for displaying footprint
raster_metadata : dict, optional
Dictionary with 'bounds', 'transform', 'crs' keys
mask : ndarray, optional
Binary mask for valid raster areas (1=valid, 0=invalid)
Returns
-------
tuple
((start_lon, start_lat), (end_lon, end_lat)) in WGS84
"""
global coords
coords = (-54.2, 75, -54.3, 75.05)
center_lon = -54.25
center_lat = 75.025
mid_lat = center_lat
start_lon = coords[0] + (coords[2] - coords[0]) / 3
end_lon = coords[0] + 2 * (coords[2] - coords[0]) / 3
# Create a leaflet map widget to find an AOI for the spatial query of the STAC API
m = Map(
basemap=basemaps.Esri.WorldImagery,
scroll_wheel_zoom=True,
center=(center_lat, center_lon),
zoom=6,
layout=Layout(height="380px", width="700px"),
)
m.add_control(FullScreenControl())
# If raster data is provided, overlay its footprint
if raster_data is not None and raster_metadata is not None and mask is not None:
_add_raster_footprint_to_map(m, raster_metadata, mask)
# Update center based on raster bounds
bounds_wgs84 = transform_bounds_to_wgs84(
raster_metadata['bounds'],
raster_metadata['crs']
)
center_lon = (bounds_wgs84[0] + bounds_wgs84[2]) / 2
center_lat = (bounds_wgs84[1] + bounds_wgs84[3]) / 2
m.center = (center_lat, center_lon)
# Set initial marker positions at 1/3 and 2/3 of raster width
start_lon = bounds_wgs84[0] + (bounds_wgs84[2] - bounds_wgs84[0]) / 3
end_lon = bounds_wgs84[0] + 2 * (bounds_wgs84[2] - bounds_wgs84[0]) / 3
start_lat = (bounds_wgs84[1] + bounds_wgs84[3]) / 2
end_lat = start_lat
# Create widgets
result_output = widgets.Output()
info_html = HTML(
value="<b>Drag the markers, then click Confirm</b>",
layout=Layout(padding="10px"),
)
confirm_button = Button(
description="Confirm Selected Points",
button_style="success",
disabled=False,
tooltip="Click after placing markers",
)
# Create start marker (red play icon)
start_marker = Marker(
location=(mid_lat, start_lon),
draggable=True,
name="Start (A)",
icon=AwesomeIcon(name="play", marker_color="red"),
)
# Create end marker (blue stop icon)
end_marker = Marker(
location=(mid_lat, end_lon),
draggable=True,
name="End (B)",
icon=AwesomeIcon(name="stop", marker_color="blue"),
)
m.add_layer(start_marker)
m.add_layer(end_marker)
# Create line connecting markers
line = Polyline(
locations=[(mid_lat, start_lon), (mid_lat, end_lon)],
color="purple",
weight=3
)
m.add_layer(line)
# Update line when markers move
def update_line(*args):
line.locations = [
(start_marker.location[0], start_marker.location[1]),
(end_marker.location[0], end_marker.location[1]),
]
start_marker.observe(update_line, names=["location"])
end_marker.observe(update_line, names=["location"])
# Update info display
def update_display(*args):
s_lat, s_lon = start_marker.location[0], start_marker.location[1]
e_lat, e_lon = end_marker.location[0], end_marker.location[1]
# Calculate distance
dist = geopy_distance((s_lat, s_lon), (e_lat, e_lon)).kilometers
info_html.value = (
f"<b>Current Positions:</b><br>"
f"<span style='color:red'>Start (A):</span> "
f"Lon: {s_lon:.3f}, Lat: {s_lat:.3f}<br>"
f"<span style='color:blue'>End (B):</span> "
f"Lon: {e_lon:.3f}, Lat: {e_lat:.3f}<br>"
f"<span style='color:purple'>Distance:</span> {dist:.2f} km"
)
# Handle confirmation
def on_confirm(b):
global coords
# Store as (lon, lat) for consistency with GIS conventions
start_coords = (start_marker.location[1], start_marker.location[0])
end_coords = (end_marker.location[1], end_marker.location[0])
with result_output:
result_output.clear_output()
print(
f"Points confirmed!\n\
Start (lon, lat): {start_coords[0]:.3f}, {start_coords[1]:.3f}\n\
End (lon, lat): {end_coords[0]:.3f}, {end_coords[1]:.3f}"
)
# Store results in global variable
global coords
coords = (start_coords, end_coords)
# Connect callbacks
confirm_button.on_click(on_confirm)
# start_marker.observe(update_line, names=["location"])
# end_marker.observe(update_line, names=["location"])
start_marker.observe(update_display, names=["location"])
end_marker.observe(update_display, names=["location"])
update_display() # Initial display
# Show the map
display(VBox([info_html, m, HBox([confirm_button]), result_output]))
# Wait for confirmation
start_time = time.time()
while (time.time() - start_time) < timeout:
time.sleep(0.1)
clear_output(wait=True)
# if selected_points[0] is None:
# print(f"⏰ Timeout after {timeout}s. Using default transect.")
# return (initial_start, initial_end)
return coords
def _add_raster_footprint_to_map(m, raster_metadata, mask):
"""Add raster footprint as a GeoJSON layer to the map.
Parameters
----------
m : ipyleaflet.Map
Map to add the layer to
raster_metadata : dict
Dictionary with 'bounds', 'transform', 'crs' keys
mask : ndarray
Binary mask (1=valid, 0=invalid)
"""
transform = raster_metadata['transform']
crs_src = raster_metadata['crs']
# Extract shapes from mask (this can be slow for large rasters)
shapes_gen = shapes(mask, transform=transform)
polygons = [shape(geom) for geom, val in shapes_gen if val == 1]
if not polygons:
print("Warning: No valid polygons found in raster footprint")
return
# Add each polygon as a GeoJSON layer
for i, poly in enumerate(polygons):
# Transform to WGS84 for display
wgs84_poly = warp.transform_geom(crs_src, "EPSG:4326", mapping(poly))
geojson_poly = GeoJSON(
data=wgs84_poly,
style={
"color": "lime",
"fillColor": "lime",
"opacity": 0.8,
"fillOpacity": 0.2,
"weight": 2,
},
name=f"Raster Boundary {i+1}",
)
m.add_layer(geojson_poly)
def select_point_with_raster_context(raster_data, raster_metadata, mask,
timeout=30):
"""Select a point on a map with raster footprint for context.
Useful when you want to see the DEM extent while selecting coordinates.
Parameters
----------
raster_data : ndarray
2D raster array
raster_metadata : dict
Dictionary with 'bounds', 'transform', 'crs', 'nodata' keys
mask : ndarray
Binary mask for valid data (1=valid)
timeout : int
Maximum seconds to wait
Returns
-------
tuple
(lon, lat) in WGS84
Examples
--------
>>> with rio.open('dem.tif') as src:
... data = src.read(1)
... mask = (data != src.nodata).astype(int)
... metadata = {
... 'bounds': src.bounds,
... 'transform': src.transform,
... 'crs': src.crs,
... }
>>> coords = select_point_with_raster_context(data, metadata, mask)
"""
# Calculate center from raster bounds
bounds_wgs84 = transform_bounds_to_wgs84(
raster_metadata['bounds'],
raster_metadata['crs']
)
center_lon = (bounds_wgs84[0] + bounds_wgs84[2]) / 2
center_lat = (bounds_wgs84[1] + bounds_wgs84[3]) / 2
# Use the transect selection but with both markers
# (we'll just use the start marker's position)
transect = select_transect_interactive(
initial_start=(center_lon - 0.05, center_lat),
initial_end=(center_lon + 0.05, center_lat),
raster_data=raster_data,
raster_metadata=raster_metadata,
mask=mask,
timeout=timeout
)
# Return just the start point
return transect[0]
def select_transect_with_raster_context(raster_data, raster_metadata, mask,
timeout=30):
"""Select a transect on a map with raster footprint for context.
Parameters
----------
raster_data : ndarray
2D raster array
raster_metadata : dict
Dictionary with 'bounds', 'transform', 'crs', 'nodata' keys
mask : ndarray
Binary mask for valid data (1=valid)
timeout : int
Maximum seconds to wait
Returns
-------
tuple
((start_lon, start_lat), (end_lon, end_lat))
Examples
--------
>>> with rio.open('dem.tif') as src:
... data = src.read(1)
... mask = (data != src.nodata).astype(int)
... metadata = {
... 'bounds': src.bounds,
... 'transform': src.transform,
... 'crs': src.crs,
... }
>>> transect = select_transect_with_raster_context(data, metadata, mask)
"""
bounds_wgs84 = transform_bounds_to_wgs84(
raster_metadata['bounds'],
raster_metadata['crs']
)
center_lon = (bounds_wgs84[0] + bounds_wgs84[2]) / 2
center_lat = (bounds_wgs84[1] + bounds_wgs84[3]) / 2
# Set initial markers at 1/3 and 2/3 of raster width
width = bounds_wgs84[2] - bounds_wgs84[0]
start_lon = bounds_wgs84[0] + width / 3
end_lon = bounds_wgs84[0] + 2 * width / 3
return select_transect_interactive(
initial_start=(start_lon, center_lat),
initial_end=(end_lon, center_lat),
raster_data=raster_data,
raster_metadata=raster_metadata,
mask=mask,
timeout=timeout
)