Spaces:
Runtime error
Runtime error
Merge
Browse files- pages/01_leafmap.py +0 -2
- pages/02_fires.py +93 -0
pages/01_leafmap.py
CHANGED
|
@@ -22,8 +22,6 @@ class Map(leafmap.Map):
|
|
| 22 |
def __init__(self, **kwargs):
|
| 23 |
super().__init__(**kwargs)
|
| 24 |
# Add what you want below
|
| 25 |
-
nps = gpd.read_file("/vsicurl/https://minio.carlboettiger.info/public-biodiversity/NPS.gdb")
|
| 26 |
-
calfire = gpd.read_file("/vsicurl/https://minio.carlboettiger.info/public-biodiversity/fire22_1.gdb", layer = "firep22_1")
|
| 27 |
self.add_gdf(jtree_fires)
|
| 28 |
self.add_stac_gui()
|
| 29 |
|
|
|
|
| 22 |
def __init__(self, **kwargs):
|
| 23 |
super().__init__(**kwargs)
|
| 24 |
# Add what you want below
|
|
|
|
|
|
|
| 25 |
self.add_gdf(jtree_fires)
|
| 26 |
self.add_stac_gui()
|
| 27 |
|
pages/02_fires.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import leafmap
|
| 2 |
+
import solara
|
| 3 |
+
import solara
|
| 4 |
+
import pystac_client
|
| 5 |
+
import planetary_computer
|
| 6 |
+
import odc.stac
|
| 7 |
+
import geopandas as gpd
|
| 8 |
+
import dask.distributed
|
| 9 |
+
import matplotlib.pyplot as plt
|
| 10 |
+
|
| 11 |
+
# Stashed public copies of NPS polygons and CalFire polygons
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
zoom = solara.reactive(2)
|
| 15 |
+
center = solara.reactive((20, 0))
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class Map(leafmap.Map):
|
| 19 |
+
def __init__(self, **kwargs):
|
| 20 |
+
super().__init__(**kwargs)
|
| 21 |
+
# Add what you want below
|
| 22 |
+
nps = gpd.read_file("/vsicurl/https://minio.carlboettiger.info/public-biodiversity/NPS.gdb")
|
| 23 |
+
calfire = gpd.read_file("/vsicurl/https://minio.carlboettiger.info/public-biodiversity/fire22_1.gdb", layer = "firep22_1")
|
| 24 |
+
jtree = nps[nps.PARKNAME == "Joshua Tree"].to_crs(calfire.crs)
|
| 25 |
+
jtree_fires = jtree.overlay(calfire, how="intersection")
|
| 26 |
+
|
| 27 |
+
# Extract a polygon if interest. > 2015 for Sentinel, otherwise we can use LandSat
|
| 28 |
+
recent = jtree_fires[jtree_fires.YEAR_ > "2015"]
|
| 29 |
+
big = recent[recent.Shape_Area == recent.Shape_Area.max()].to_crs("EPSG:4326")
|
| 30 |
+
datetime = big.ALARM_DATE.item() + "/" + big.CONT_DATE.item()
|
| 31 |
+
box = big.buffer(0.005).bounds.to_numpy()[0] # Fire bbox + buffer
|
| 32 |
+
#box = jtree.to_crs("EPSG:4326").bounds.to_numpy()[0] # Park bbox
|
| 33 |
+
|
| 34 |
+
# STAC Search for this imagery in space/time window
|
| 35 |
+
items = (
|
| 36 |
+
pystac_client.Client.
|
| 37 |
+
open("https://planetarycomputer.microsoft.com/api/stac/v1",
|
| 38 |
+
modifier=planetary_computer.sign_inplace).
|
| 39 |
+
search(collections=["sentinel-2-l2a"],
|
| 40 |
+
bbox=box,
|
| 41 |
+
datetime=datetime,
|
| 42 |
+
query={"eo:cloud_cover": {"lt": 10}}).
|
| 43 |
+
item_collection())
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# Time to compute:
|
| 47 |
+
|
| 48 |
+
client = dask.distributed.Client()
|
| 49 |
+
# landsat_bands = ["nir08", "swir16"]
|
| 50 |
+
sentinel_bands = ["B08", "B12", "SCL"]
|
| 51 |
+
|
| 52 |
+
# The magic of gdalwarper. Can also resample, reproject, and aggregate on the fly
|
| 53 |
+
data = odc.stac.load(items,
|
| 54 |
+
bands=sentinel_bands,
|
| 55 |
+
bbox=box
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
swir = data["B12"].astype("float")
|
| 59 |
+
nir = data["B08"].astype("float")
|
| 60 |
+
|
| 61 |
+
# can resample and aggregate in xarray. compute with dask
|
| 62 |
+
nbs = (((nir - swir) / (nir + swir)).
|
| 63 |
+
# resample(time="MS").
|
| 64 |
+
# median("time", keep_attrs=True).
|
| 65 |
+
compute()
|
| 66 |
+
)
|
| 67 |
+
nbs.rio.to_raster(raster_path="nbs.tif", driver="COG")
|
| 68 |
+
|
| 69 |
+
self.add_gdf(jtree)
|
| 70 |
+
self.add_gdf(big)
|
| 71 |
+
self.add_raster("nbs.tif")
|
| 72 |
+
#self.add_stac_gui()
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
@solara.component
|
| 76 |
+
def Page():
|
| 77 |
+
with solara.Column(style={"min-width": "500px"}):
|
| 78 |
+
# solara components support reactive variables
|
| 79 |
+
# solara.SliderInt(label="Zoom level", value=zoom, min=1, max=20)
|
| 80 |
+
# using 3rd party widget library require wiring up the events manually
|
| 81 |
+
# using zoom.value and zoom.set
|
| 82 |
+
Map.element( # type: ignore
|
| 83 |
+
zoom=zoom.value,
|
| 84 |
+
on_zoom=zoom.set,
|
| 85 |
+
center=center.value,
|
| 86 |
+
on_center=center.set,
|
| 87 |
+
scroll_wheel_zoom=True,
|
| 88 |
+
toolbar_ctrl=False,
|
| 89 |
+
data_ctrl=False,
|
| 90 |
+
height="780px",
|
| 91 |
+
)
|
| 92 |
+
solara.Text(f"Zoom: {zoom.value}")
|
| 93 |
+
solara.Text(f"Center: {center.value}")
|