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)