-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlplm_presentation.py
69 lines (58 loc) · 2.52 KB
/
lplm_presentation.py
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
import base64
import ipyleaflet
from ipyleaflet import Choropleth
import json
from branca.colormap import linear
import lplm_io as lpio
from lplm_utils import format_date
def get_date_labels(xds_grd):
return [(format_date(d), d) for d in xds_grd.sortby("date").date.values]
def sar_layer_name(date):
return f"SAR (GRD, VV, gamma0) @ {format_date(date)}"
def make_sar_layer(xds_grd, date):
"""
Creates a raster layer for the SAR data for the given date.
Note: Uses data-uri format so that no files have to be served.
This is ok for small images, like jpegs of our study area.
"""
bounds_4326 = xds_grd.rio.transform_bounds("epsg:4326")
bounds_leaf = (bounds_4326[1], bounds_4326[0]), (bounds_4326[3], bounds_4326[2])
# Use a .jpg version for presentation, due to the smaller file size.
img_path = lpio.get_jpg(date)
# data-uri encode
with open(img_path, 'rb') as img_file:
img_data = img_file.read()
data_url = 'data:image/jpeg;base64,' + base64.b64encode(img_data).decode('utf-8')
return ipyleaflet.ImageOverlay(
url=data_url,
bounds=bounds_leaf,
name=sar_layer_name(date))
def make_feature_layer(gdf, feature):
# Prepare geojson input for ipyleaflet's choropleth interface.
# Thanks to Abdishakur, https://towardsdatascience.com/ipyleaflet-interactive-mapping-in-jupyter-notebook-994f19611e79
geojson = json.loads(gdf.loc[:, ["segment_id", "geometry"]].to_crs("EPSG:4326").to_json())
for gjfeature in geojson["features"]:
properties = gjfeature["properties"]
gjfeature.update(id=properties["segment_id"])
layer = Choropleth(
geo_data = geojson,
choro_data = gdf[feature].to_dict(),
colormap=linear.viridis,
style = { "fillOpacity": 0.5, "weight": 1.0 },
name=f"Segment {feature}")
colorbar = linear.viridis.scale(gdf[feature].min(), gdf[feature].max())
return layer, colorbar
def find_layers(m: ipyleaflet.Map, name: str):
"""Gets map layer(s) with the specified name"""
layers = []
for l in m.layers:
if l.name == name:
layers.append(l)
return layers
def replace_layer(m: ipyleaflet.Map, new_layer: ipyleaflet.Layer, old_name: str = None):
"""Removes any old matching layers, and adds the new one"""
haystack = find_layers(m, new_layer.name) if (old_name == new_layer.name or old_name is None) else [*find_layers(m, old_name), *find_layers(m, new_layer.name)]
for l in haystack:
m.remove(l)
m.add_layer(new_layer)
return None