File size: 4,402 Bytes
dc438fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
501fde7
 
 
dc438fe
 
 
 
501fde7
 
 
 
 
 
 
 
 
 
 
dc438fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import leafmap
from leafmap import pmtiles_style
import solara
import ipyleaflet

# THings to come:
# TODO: Calculate some metrics given the area.
# TODO: add a raster to the toy
# TODO: Dynamically check the center of the map and change what's displayed depending on that.

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

# Euromap info
eurocrops_pmtiles = "https://s3.us-west-2.amazonaws.com/us-west-2.opendata.source.coop/cholmes/eurocrops/eurocrops-all.pmtiles"

original_style = {
    "version": 8,
    "sources": {
        "source": {
            "type": "vector",
            "url": "pmtiles://https://s3.us-west-2.amazonaws.com/us-west-2.opendata.source.coop/cholmes/eurocrops/eurocrops-all.pmtiles",
            "attribution": "PMTiles",
        }
    },
    "layers": [
        {
            "id": "eurocrops_point",
            "source": "source",
            "source-layer": "eurocrops",
            "type": "circle",
            "paint": {"circle-color": "#8dd3c7", "circle-radius": 5},
            "filter": ["==", ["geometry-type"], "Point"],
        },
        {
            "id": "eurocrops_stroke",
            "source": "source",
            "source-layer": "eurocrops",
            "type": "line",
            "paint": {"line-color": "#8dd3c7", "line-width": 1},
            "filter": ["==", ["geometry-type"], "LineString"],
        },
        {
            "id": "eurocrops_fill",
            "source": "source",
            "source-layer": "eurocrops",
            "type": "fill",
            "paint": {"fill-color": "#8dd3c7", "fill-opacity": 0.5},
            "filter": ["==", ["geometry-type"], "Polygon"],
        },
    ],
}
style = original_style
del style["version"]
del style["sources"]

q_url = "https://storage.googleapis.com/ahp-research/overture/pmtiles/overture.pmtiles"

q_style = {
    "layers": [
        {
            "id": "admins",
            "source": "example_source",
            "source-layer": "admins",
            "type": "fill",
            "paint": {"fill-color": "#BDD3C7", "fill-opacity": 0.1},
        },
        {
            "id": "buildings",
            "source": "example_source",
            "source-layer": "buildings",
            "type": "fill",
            "paint": {"fill-color": "#FFFFB3", "fill-opacity": 0.5},
        },
        {
            "id": "places",
            "source": "example_source",
            "source-layer": "places",
            "type": "fill",
            "paint": {"fill-color": "#BEBADA", "fill-opacity": 0.5},
        },
        {
            "id": "roads",
            "source": "example_source",
            "source-layer": "roads",
            "type": "line",
            "paint": {"line-color": "#FB8072"},
        },
    ],
}

# self.add_pmtiles(url, name='PMTiles', style=style)


class Map(leafmap.Map):
    def __init__(self, **kwargs) -> None:
        super().__init__(**kwargs)
        self.add_basemap("CartoDB.DarkMatter")
        self.add_pmtiles(
            eurocrops_pmtiles,
            style=style,
            name="Euro Crops",
            overlay=True,
            show=True,
            zoom_to_layer=True,
        )
        # print(pmtiles_style(eurocrops_pmtiles))
        # print(style)
        # self.add_pmtiles(
        #     q_url,
        #     # style=pmtiles_style(q_url),
        #     style=q_style,
        #     name="Quisheng Layer",
        #     overlay=True,
        #     show=True,
        #     zoom_to_layer=True,
        # )
        # self.add_my_pmtiles()
        # self.add_stac_gui()  ## TODO: make sure I need this

    def add_my_pmtiles(self, **kwargs):
        layer = ipyleaflet.PMTilesLayer(url=eurocrops_pmtiles, style=style)
        self.add(layer)


@solara.component
def Page():
    with solara.Column(style={"min-width": "500px"}):
        # solara components support reactive variables
        # solara.SliderInt(label="Zoom level", value=zoom, min=1, max=20)
        # using 3rd party widget library require wiring up the events manually
        # using zoom.value and zoom.set
        Map.element(  # type: ignore
            # m = 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,
            data_ctrl=False,
            height="780px",
        )