Spaces:
Sleeping
Sleeping
Update pages/01_teste.py
Browse files- pages/01_teste.py +138 -138
pages/01_teste.py
CHANGED
@@ -1,139 +1,139 @@
|
|
1 |
-
import ee
|
2 |
-
import geemap
|
3 |
-
import plotly.express as px
|
4 |
-
import pandas as pd
|
5 |
-
import solara
|
6 |
-
import dataclasses
|
7 |
-
from typing import Callable, cast
|
8 |
-
|
9 |
-
df = pd.read_csv('
|
10 |
-
columns = list(df.columns)
|
11 |
-
#df = px.data.iris()
|
12 |
-
|
13 |
-
@dataclasses.dataclass
|
14 |
-
class ClickPoint:
|
15 |
-
row_index: int
|
16 |
-
x_column: str
|
17 |
-
y_column: str
|
18 |
-
|
19 |
-
def find_row_index(fig, click_data):
|
20 |
-
# goes from trace index and point index to row index in a dataframe
|
21 |
-
# requires passing df.index as to custom_data
|
22 |
-
trace_index = click_data["points"]["trace_indexes"][0]
|
23 |
-
point_index = click_data["points"]["point_indexes"][0]
|
24 |
-
trace = fig.data[trace_index]
|
25 |
-
return trace.customdata[point_index][0]
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
class Map(geemap.Map):
|
31 |
-
def __init__(self, **kwargs):
|
32 |
-
super().__init__(**kwargs)
|
33 |
-
self.add_ee_data()
|
34 |
-
self.add("layer_manager")
|
35 |
-
self.add("inspector")
|
36 |
-
|
37 |
-
def add_ee_data(self):
|
38 |
-
# Add Earth Engine dataset
|
39 |
-
|
40 |
-
esa = ee.ImageCollection('ESA/WorldCover/v200').first()
|
41 |
-
|
42 |
-
ucs = ee.FeatureCollection("projects/ee-curso-gee-rhamon/assets/ucs_estaduais_rj")
|
43 |
-
rj = ee.FeatureCollection('FAO/GAUL/2015/level1').filter(ee.Filter.eq('ADM1_NAME', 'Rio De Janeiro'))
|
44 |
-
|
45 |
-
|
46 |
-
esa = esa.clipToCollection(ucs)
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
# Set visualization parameters.
|
51 |
-
|
52 |
-
visualization = {
|
53 |
-
"bands": ['Map'],
|
54 |
-
}
|
55 |
-
|
56 |
-
|
57 |
-
fc_vis_params = {
|
58 |
-
"color": "000000FF",
|
59 |
-
'lineType': 'solid',
|
60 |
-
"width": 1.5,
|
61 |
-
"fillColor": "00000000",
|
62 |
-
}
|
63 |
-
|
64 |
-
# Add Earth Engine layers to Map
|
65 |
-
|
66 |
-
self.centerObject(ucs, zoom=8)
|
67 |
-
self.addLayer(ucs.style(**fc_vis_params), {}, "UCs Estaduais RJ")
|
68 |
-
self.addLayer(esa, visualization, "Uso de Solo ESA")
|
69 |
-
self.addLayer(rj.style(**fc_vis_params), {}, "Limite Estadual RJ")
|
70 |
-
self.add_legend(title="Land Cover Type", builtin_legend='ESA_WorldCover', position="bottomright")
|
71 |
-
|
72 |
-
|
73 |
-
@solara.component
|
74 |
-
def Page():
|
75 |
-
with solara.Column(style={"max-width": "100%",'margin': "10vw"}):
|
76 |
-
Map.element(
|
77 |
-
center=[40, -100],
|
78 |
-
zoom=4,
|
79 |
-
height="600px",
|
80 |
-
)
|
81 |
-
x = solara.use_state(x)
|
82 |
-
#fig = px.pie(df,values= x, names="Classes", title='Cobertura Florestal UCs Estaduais RJ')
|
83 |
-
#fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species")
|
84 |
-
#solara.FigurePlotly(fig)
|
85 |
-
|
86 |
-
@solara.component
|
87 |
-
def ClickScatter(df, y, click_row, on_click: Callable[[ClickPoint], None]):
|
88 |
-
|
89 |
-
y, set_y = solara.use_state(y)
|
90 |
-
|
91 |
-
#y, set_y = solara.use_state(y)
|
92 |
-
#fig = px.scatter(df, x, y, color=color, custom_data=[df.index])
|
93 |
-
fig = px.pie(df,values= y , names='Classes', title='Uso de Solo UCs Estaduais RJ')
|
94 |
-
|
95 |
-
def on_click_trace(click_data):
|
96 |
-
# sanity checks
|
97 |
-
assert click_data["event_type"] == "plotly_click"
|
98 |
-
row_index = find_row_index(fig, click_data)
|
99 |
-
on_click(ClickPoint(row_index, y))
|
100 |
-
|
101 |
-
if click_row:
|
102 |
-
click_y = df[y].values[click_row]
|
103 |
-
#fig.add_trace(px.scatter(x=, y=[click_y], text=["⭐️"]).data[0])
|
104 |
-
fig.add_trace(px.pie(values= [click_y], names='Classes', title='Uso de Solo UCs Estaduais RJ').data[0])
|
105 |
-
# make the figure a bit smaller
|
106 |
-
fig.update_layout(width=1500,height=800)
|
107 |
-
with solara.Column(style={"width": "100%",'margin': "10vw"}) as main:
|
108 |
-
solara.FigurePlotly(fig, on_click=on_click_trace)
|
109 |
-
solara.Select(label="Escolha aqui a Unidade de Conservação a ter o Uso de solo avaliado", value=y, values=columns, on_value=set_y)
|
110 |
-
return main
|
111 |
-
|
112 |
-
@solara.component
|
113 |
-
def Page():
|
114 |
-
with solara.Column(style={"min-width": "500px", "max-width": "100%",'margin': "10vw"}):
|
115 |
-
Map.element(
|
116 |
-
center=[-22, -43],
|
117 |
-
zoom=8,
|
118 |
-
height="600px",
|
119 |
-
)
|
120 |
-
|
121 |
-
|
122 |
-
click_point, set_click_point = solara.use_state(cast(ClickPoint, None))
|
123 |
-
if click_point:
|
124 |
-
clicked_row = click_point.row_index
|
125 |
-
else:
|
126 |
-
clicked_row = None
|
127 |
-
|
128 |
-
with solara.Row(justify="center", style={"flex-wrap": "wrap"}):
|
129 |
-
ClickScatter(df, 'nome', clicked_row, on_click=set_click_point)
|
130 |
-
if click_point is not None:
|
131 |
-
clicked_row = click_point.row_index
|
132 |
-
solara.Success(f"Clicked on row {clicked_row}. Which is highlighted in the both plots.")
|
133 |
-
solara.Markdown(
|
134 |
-
f"""
|
135 |
-
```python
|
136 |
-
row_data = {df.iloc[clicked_row].to_dict()}
|
137 |
-
```"""
|
138 |
-
)
|
139 |
|
|
|
1 |
+
import ee
|
2 |
+
import geemap
|
3 |
+
import plotly.express as px
|
4 |
+
import pandas as pd
|
5 |
+
import solara
|
6 |
+
import dataclasses
|
7 |
+
from typing import Callable, cast
|
8 |
+
|
9 |
+
df = pd.read_csv('pages/esa_stats_ucs_rj2.csv')
|
10 |
+
columns = list(df.columns)
|
11 |
+
#df = px.data.iris()
|
12 |
+
|
13 |
+
@dataclasses.dataclass
|
14 |
+
class ClickPoint:
|
15 |
+
row_index: int
|
16 |
+
x_column: str
|
17 |
+
y_column: str
|
18 |
+
|
19 |
+
def find_row_index(fig, click_data):
|
20 |
+
# goes from trace index and point index to row index in a dataframe
|
21 |
+
# requires passing df.index as to custom_data
|
22 |
+
trace_index = click_data["points"]["trace_indexes"][0]
|
23 |
+
point_index = click_data["points"]["point_indexes"][0]
|
24 |
+
trace = fig.data[trace_index]
|
25 |
+
return trace.customdata[point_index][0]
|
26 |
+
|
27 |
+
|
28 |
+
|
29 |
+
|
30 |
+
class Map(geemap.Map):
|
31 |
+
def __init__(self, **kwargs):
|
32 |
+
super().__init__(**kwargs)
|
33 |
+
self.add_ee_data()
|
34 |
+
self.add("layer_manager")
|
35 |
+
self.add("inspector")
|
36 |
+
|
37 |
+
def add_ee_data(self):
|
38 |
+
# Add Earth Engine dataset
|
39 |
+
|
40 |
+
esa = ee.ImageCollection('ESA/WorldCover/v200').first()
|
41 |
+
|
42 |
+
ucs = ee.FeatureCollection("projects/ee-curso-gee-rhamon/assets/ucs_estaduais_rj")
|
43 |
+
rj = ee.FeatureCollection('FAO/GAUL/2015/level1').filter(ee.Filter.eq('ADM1_NAME', 'Rio De Janeiro'))
|
44 |
+
|
45 |
+
|
46 |
+
esa = esa.clipToCollection(ucs)
|
47 |
+
|
48 |
+
|
49 |
+
|
50 |
+
# Set visualization parameters.
|
51 |
+
|
52 |
+
visualization = {
|
53 |
+
"bands": ['Map'],
|
54 |
+
}
|
55 |
+
|
56 |
+
|
57 |
+
fc_vis_params = {
|
58 |
+
"color": "000000FF",
|
59 |
+
'lineType': 'solid',
|
60 |
+
"width": 1.5,
|
61 |
+
"fillColor": "00000000",
|
62 |
+
}
|
63 |
+
|
64 |
+
# Add Earth Engine layers to Map
|
65 |
+
|
66 |
+
self.centerObject(ucs, zoom=8)
|
67 |
+
self.addLayer(ucs.style(**fc_vis_params), {}, "UCs Estaduais RJ")
|
68 |
+
self.addLayer(esa, visualization, "Uso de Solo ESA")
|
69 |
+
self.addLayer(rj.style(**fc_vis_params), {}, "Limite Estadual RJ")
|
70 |
+
self.add_legend(title="Land Cover Type", builtin_legend='ESA_WorldCover', position="bottomright")
|
71 |
+
|
72 |
+
|
73 |
+
@solara.component
|
74 |
+
def Page():
|
75 |
+
with solara.Column(style={"max-width": "100%",'margin': "10vw"}):
|
76 |
+
Map.element(
|
77 |
+
center=[40, -100],
|
78 |
+
zoom=4,
|
79 |
+
height="600px",
|
80 |
+
)
|
81 |
+
x = solara.use_state(x)
|
82 |
+
#fig = px.pie(df,values= x, names="Classes", title='Cobertura Florestal UCs Estaduais RJ')
|
83 |
+
#fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species")
|
84 |
+
#solara.FigurePlotly(fig)
|
85 |
+
|
86 |
+
@solara.component
|
87 |
+
def ClickScatter(df, y, click_row, on_click: Callable[[ClickPoint], None]):
|
88 |
+
|
89 |
+
y, set_y = solara.use_state(y)
|
90 |
+
|
91 |
+
#y, set_y = solara.use_state(y)
|
92 |
+
#fig = px.scatter(df, x, y, color=color, custom_data=[df.index])
|
93 |
+
fig = px.pie(df,values= y , names='Classes', title='Uso de Solo UCs Estaduais RJ')
|
94 |
+
|
95 |
+
def on_click_trace(click_data):
|
96 |
+
# sanity checks
|
97 |
+
assert click_data["event_type"] == "plotly_click"
|
98 |
+
row_index = find_row_index(fig, click_data)
|
99 |
+
on_click(ClickPoint(row_index, y))
|
100 |
+
|
101 |
+
if click_row:
|
102 |
+
click_y = df[y].values[click_row]
|
103 |
+
#fig.add_trace(px.scatter(x=, y=[click_y], text=["⭐️"]).data[0])
|
104 |
+
fig.add_trace(px.pie(values= [click_y], names='Classes', title='Uso de Solo UCs Estaduais RJ').data[0])
|
105 |
+
# make the figure a bit smaller
|
106 |
+
fig.update_layout(width=1500,height=800)
|
107 |
+
with solara.Column(style={"width": "100%",'margin': "10vw"}) as main:
|
108 |
+
solara.FigurePlotly(fig, on_click=on_click_trace)
|
109 |
+
solara.Select(label="Escolha aqui a Unidade de Conservação a ter o Uso de solo avaliado", value=y, values=columns, on_value=set_y)
|
110 |
+
return main
|
111 |
+
|
112 |
+
@solara.component
|
113 |
+
def Page():
|
114 |
+
with solara.Column(style={"min-width": "500px", "max-width": "100%",'margin': "10vw"}):
|
115 |
+
Map.element(
|
116 |
+
center=[-22, -43],
|
117 |
+
zoom=8,
|
118 |
+
height="600px",
|
119 |
+
)
|
120 |
+
|
121 |
+
|
122 |
+
click_point, set_click_point = solara.use_state(cast(ClickPoint, None))
|
123 |
+
if click_point:
|
124 |
+
clicked_row = click_point.row_index
|
125 |
+
else:
|
126 |
+
clicked_row = None
|
127 |
+
|
128 |
+
with solara.Row(justify="center", style={"flex-wrap": "wrap"}):
|
129 |
+
ClickScatter(df, 'nome', clicked_row, on_click=set_click_point)
|
130 |
+
if click_point is not None:
|
131 |
+
clicked_row = click_point.row_index
|
132 |
+
solara.Success(f"Clicked on row {clicked_row}. Which is highlighted in the both plots.")
|
133 |
+
solara.Markdown(
|
134 |
+
f"""
|
135 |
+
```python
|
136 |
+
row_data = {df.iloc[clicked_row].to_dict()}
|
137 |
+
```"""
|
138 |
+
)
|
139 |
|