Spaces:
Sleeping
Sleeping
File size: 5,422 Bytes
e6b0edb 6581617 9cfc059 f756c68 e6b0edb f756c68 e6b0edb f756c68 e6b0edb f756c68 e6b0edb f756c68 e6b0edb f756c68 6581617 f756c68 e6b0edb f756c68 e6b0edb f756c68 e6b0edb 84e6e82 e3721b9 e6b0edb 84e6e82 e6b0edb 84e6e82 e6b0edb f756c68 84e6e82 e6b0edb 6581617 e6b0edb e3721b9 e6b0edb 6581617 e6b0edb f756c68 e6b0edb 3c28c40 e6b0edb 6581617 e6b0edb 6581617 e6b0edb |
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 |
import ee
import geemap
import solara
import plotly.io as pio
pio.renderers.default='iframe'
class Map(geemap.Map):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.dataset = ee.Image("UMD/hansen/global_forest_change_2023_v1_11")
self.add_forest_loss_gain_data()
self.add_plot_gui()
def add_forest_loss_gain_data(self):
self.add_basemap("Esri.WorldImagery")
treeloss = self.dataset.select(["loss"]).selfMask()
treegain = self.dataset.select(["gain"]).selfMask()
self.add_layer(treeloss, {"palette": "red"}, "Tree loss")
self.add_layer(treegain, {"palette": "yellow"}, "Tree gain")
self.add("layer_manager")
def download_data(self):
treecover = self.dataset.select(["treecover2000"])
threshold = 10
treecover_bin = treecover.gte(threshold).selfMask()
countries = ee.FeatureCollection(geemap.examples.get_ee_path("countries"))
style = {"color": "#000000ff", "fillColor": "#00000000"}
self.add_layer(countries.style(**style), {}, "Countries")
geemap.zonal_stats(
treecover_bin,
countries,
"forest_cover.csv",
stat_type="SUM",
denominator=1e6,
scale=1000,
)
class PieChartPlotter:
def __init__(self, map_object):
if map_object is not None:
self.geemap = geemap # Access geemap instance from Map
self.zonal_forest_area_by_country()
def zonal_forest_area_by_country(self):
self.geemap.pie_chart(
"data/forest_cover.csv", names="NAME", values="sum", max_rows=20, height=400
).show()
class BarChartPlotter:
def __init__(self, map_object):
if map_object is not None:
self.geemap = geemap # Access geemap instance from Map
self.zonal_forest_area_by_country()
def zonal_forest_area_by_country(self):
self.geemap.bar_chart(
"data/forest_cover.csv", x="NAME", y="sum", max_rows=20, height=400,
x_label="Country", y_label="Forest area (km2)",
).show()
@solara.component
def Page():
with solara.Column(style={"min-width": "500px"}):
map_instance = Map()
Map.element(
map_object= map_instance,
center=[40, -100],
zoom=4,
height="600px",
)
with solara.Column(align="center"):
markdown = """
## Forest cover gain and loss mapping
### Forest cover mapping
**For this analysis we will be using [Hansen Global Forest Change v1.11 (2000-2023) dataset](https://developers.google.com/earth-engine/datasets/catalog/UMD_hansen_global_forest_change_2023_v1_11) and geemap. First, we will compute the zonal statistics to identify the countries with the largest forest area, and then plot them. Here the base tree cover imagery is taken from 2000**
```python
dataset = ee.Image("UMD/hansen/global_forest_change_2023_v1_11")
treecover = dataset.select(["treecover2000"])
threshold = 10
treecover_bin = treecover.gte(threshold).selfMask()
countries = ee.FeatureCollection(geemap.examples.get_ee_path("countries"))
style = {"color": "#000000ff", "fillColor": "#00000000"}
self.add_layer(countries.style(**style), {}, "Countries")
geemap.zonal_stats(
treecover_bin,
countries,
"forest_cover.csv",
stat_type="SUM",
denominator=1e6,
scale=1000,
)
self.geemap.pie_chart(
"data/forest_cover.csv", names="NAME", values="sum", max_rows=20, height=400
).show()
```
"""
solara.Markdown(markdown)
with solara.Column(style={"min-width": "500px"}):
plotter = PieChartPlotter(map_instance)
with solara.Column(align="center"):
markdown = """
**The above give us the percentage overall but not the actual number. Let's plot a bar chart that shows the numbers too**
```python
self.geemap.bar_chart(
"data/forest_cover.csv", x="NAME", y="sum", max_rows=20, height=400,
x_label="Country", y_label="Forest area (km2)",
).show()
```
"""
solara.Markdown(markdown)
with solara.Column(style={"min-width": "500px"}):
bar_chart_plotter = BarChartPlotter(map_instance)
with solara.Column(align="center"):
markdown = """
**Now we can calculate the forest loss area by country and plot it**
```python
treeloss_year = dataset.select(["lossyear"])
geemap.zonal_stats( treeloss.gt(0), countries, "treeloss.csv", stat_type="SUM", denominator=1e6, scale=1000,)
geemap.bar_chart( "treeloss.csv", x="NAME", y="sum", max_rows=20, x_label="Country", y_label="Forest loss area (km2)",).show()
```
"""
solara.Markdown(markdown)
|