File size: 4,472 Bytes
c140c33
 
4f36249
c140c33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f36249
 
 
 
 
c140c33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f36249
 
c140c33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f36249
 
 
 
 
 
 
c140c33
 
 
 
 
 
 
 
4f36249
c140c33
 
 
 
4f36249
 
 
 
 
 
 
 
c140c33
 
 
 
 
 
 
 
 
 
 
 
 
 
4f36249
 
 
 
 
c140c33
 
 
 
 
 
 
 
 
 
 
 
4f36249
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
from ipyleaflet import Polygon, WidgetControl
from ipywidgets import widgets
from ipywidgets.widgets.widget import jsondumps
import leafmap
import logging
import solara
from reacton import component as component

from toy_app.src import map_utils

log = logging.getLogger(__name__)


# Display a cog in leafmap
# Allow the user to draw a shape on the map
# Retrieve the user's shape and run metrics on it
# Used Stac to get the available collections from the 10m Annual Land Use Land Cover (9-class)
libya_fire_url = (
    "https://github.com/opengeos/data/releases/download/raster/Libya-2023-07-01.tif"
)
richness = "https://data.source.coop/cboettig/mobi/species-richness-all/SpeciesRichness_All.tif"
deforest = "https://data.source.coop/vizzuality/lg-land-carbon-data/deforest_carbon_100m_cog.tif"
irrecoverable = (
    "https://data.source.coop/cboettig/carbon/cogs/irrecoverable_c_total_2018.tif"
)
libya_fire_name = "Libya Fire"
libya_layer_visible = solara.reactive("True")
test_poly = {
    "type": "Feature",
    "properties": {},
    "geometry": {
        "type": "Polygon",
        "coordinates": [
            [
                [-72.949219, 42.447781],
                [-73.35022, 42.195969],
                [-73.372192, 41.799983],
                [-73.026123, 41.566142],
                [-72.608643, 41.343825],
                [-72.301025, 41.590797],
                [-71.993408, 41.442726],
                [-71.768188, 41.967659],
                [-71.28479, 42.138968],
                [-72.064819, 42.362603],
                [-72.614136, 42.195969],
                [-72.949219, 42.447781],
            ]
        ],
    },
}
geometry = solara.reactive(jsondumps(test_poly))
lost_carbon_count = solara.reactive(0)


class Map(leafmap.Map):
    """
    This is the map which will be displayed in the solara webapp
    """

    def __init__(self, **kwargs):
        log.warning("Starting Map init")
        kwargs["toolbar_control"] = False
        super().__init__(**kwargs)
        basemap = {
            "url": "https://mt1.google.com/vt/lyrs=s&x={x}&y={y}&z={z}",
            "attribution": "Google",
            "name": "Google Satellite",
            "opacity": 1.0,
        }
        self.add_tile_layer(**basemap, shown=False)
        self.add_layer_manager(opened=False)
        self.add_cog_layer(
            url=libya_fire_url, name=libya_fire_name
        )  # Just display the cog for now, the layer toggle isn't working as expected
        self.add_cog_layer(
            url=irrecoverable,
            name="irrecoverable c",
            palette="reds",
            zoom_to_layer=False,
            opacity=0.8,
        )
        # self.toggle_libya_layer()
        # user_poly = map_utils.get_user_polygon(self)
        # if user_poly:
        #     log.warning(f"Polygon? {user_poly}")
        # self.add_click_detector_widget()
        self.add_button()

    def add_button(self):
        button = widgets.Button(description="Calculate")
        output = widgets.Output()

        def on_button_click(_):
            if self.user_roi is not None:
                geometry.set(self.user_roi)
                selected_polygon = map_utils.read_polygon(self.user_roi)
                selected_on_carbon = map_utils.extract_geom(
                    selected_polygon, irrecoverable
                ).fillna(0)
                area = round(float(map_utils.area_hectares(selected_polygon)))
                carbon_total = round(float(selected_on_carbon.mean()) * area)
                lost_carbon_count.set(carbon_total)

        button.on_click(on_button_click)

        widget = widgets.VBox([button, output])
        self.add_widget(widget)


zoom = solara.reactive(2)
center = solara.reactive((20, 0))


@component
def Page():
    with solara.Column(style={"min_width": "500px"}):
        with solara.Row():
            solara.Text(
                f"Average Carbon Lost within the selected area in 2018: {lost_carbon_count.value:,} Tonnes"
            )
        Map.element(  # type: ignore
            zoom=zoom.value,
            on_zoom=zoom.set,
            center=center.value,
            on_center=center.set,
            scroll_wheel_zoom=True,
            toolbar_ctrl=False,
            draw_control=True,
            height="780px",
        )
        with solara.Row():
            solara.Text(f"Center: {center.value}")
            solara.Text(f"Zoom: {zoom.value}")
            solara.Text(f"UserPoly: {geometry.value}")