Carl Boettiger commited on
Commit
d66d6a2
1 Parent(s): 431fd88
Files changed (2) hide show
  1. app.py +46 -37
  2. requirements.txt +2 -1
app.py CHANGED
@@ -11,17 +11,22 @@
11
  # See the License for the specific language governing permissions and
12
  # limitations under the License.
13
 
 
14
  import leafmap.foliumap as leafmap
15
  import rioxarray
16
  import geopandas as gpd
17
  import streamlit as st
18
- import altair as alt
19
- import ibis
20
- from ibis import _
21
- import ibis.selectors as s
22
  from streamlit_folium import st_folium
23
  import json
24
 
 
 
 
 
 
 
 
 
25
  def extract_geom(gdf, cog):
26
  x = (rioxarray.
27
  open_rasterio('/vsicurl/'+cog, masked=True).
@@ -40,15 +45,6 @@ def area_hectares(gdf):
40
  return area
41
 
42
 
43
- import pystac_client
44
- import planetary_computer
45
- import odc.stac
46
- import geopandas as gpd
47
- import dask.distributed
48
- import matplotlib.pyplot as plt
49
- import rioxarray
50
- from datetime import datetime, timedelta
51
-
52
 
53
  def stac_search(box, datetime):
54
  # STAC Search for this imagery in space/time window
@@ -64,16 +60,18 @@ def stac_search(box, datetime):
64
  item_collection())
65
  return items
66
 
67
- def compute_nbs(items, box):
68
  # Time to compute:
69
- client = dask.distributed.Client()
70
  # landsat_bands = ["nir08", "swir16"]
71
  sentinel_bands = ["B08", "B12", "SCL"] # NIR, SWIR, and Cloud Mask
72
 
73
  # The magic of gdalwarper. Can also resample, reproject, and aggregate on the fly
74
  data = odc.stac.load(items,
75
- bands=sentinel_bands,
76
- bbox=box
 
 
77
  )
78
  times = [0,data.time.size-1]
79
 
@@ -108,6 +106,31 @@ def nbs(box, date):
108
  # nbs.isel(time=(nbs.time.size-1)).rio.to_raster(raster_path="after.tif", driver="COG")
109
 
110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
 
112
  # +
113
  import copy
@@ -164,7 +187,10 @@ m = leafmap.Map(center=[35, -110], zoom=6)
164
  ## Map controls sidebar
165
  with st.sidebar:
166
 
167
- date = st.date_input("select date", min_value = datetime.strptime("2015-06-27", "%Y-%m-%d")) # sentinel2 records begin
 
 
 
168
 
169
  with st.expander("🔥 Wildfire"):
170
  # Fire Polygons, USGS
@@ -200,24 +226,7 @@ if polygon is not None:
200
  #out
201
  n = len(out["features"])
202
  st.markdown(f"{n} images found. Computing NBR...")
203
-
204
- nbs = compute_nbs(items, box)
205
-
206
- #import matplotlib as plt
207
- #cmap = plt.colormaps.get_cmap('viridis') # viridis is the default colormap for imshow
208
- #cmap.set_bad(color='black')
209
- #im = nbs.plot.imshow(row="time", cmap=cmap, add_colorbar=False, size=4)
210
- # st.pyplot(im.fig, use_container_width=False)
211
-
212
- m2 = leafmap.Map(center=[35, -110], zoom=6)
213
-
214
- # doesn't work
215
- # m2.add_raster(nbs.isel(time=0), colormap="terrain", layer_name="NBR") # empty?
216
-
217
- # write first and last date to tif
218
-
219
- nbs.isel(time=0).rio.to_raster(raster_path="static/before.tif", driver="COG")
220
- nbs.isel(time=(nbs.time.size-1)).rio.to_raster(raster_path="static/after.tif", driver="COG")
221
- m2.split_map("static/before.tif", "static/after.tif", left_label = "NBR before fire", right_label = "NBR after fire")
222
  m2.to_streamlit()
 
223
 
 
11
  # See the License for the specific language governing permissions and
12
  # limitations under the License.
13
 
14
+ # +
15
  import leafmap.foliumap as leafmap
16
  import rioxarray
17
  import geopandas as gpd
18
  import streamlit as st
 
 
 
 
19
  from streamlit_folium import st_folium
20
  import json
21
 
22
+ import pystac_client
23
+ import planetary_computer
24
+ import odc.stac
25
+ from datetime import datetime, timedelta
26
+
27
+
28
+ # -
29
+
30
  def extract_geom(gdf, cog):
31
  x = (rioxarray.
32
  open_rasterio('/vsicurl/'+cog, masked=True).
 
45
  return area
46
 
47
 
 
 
 
 
 
 
 
 
 
48
 
49
  def stac_search(box, datetime):
50
  # STAC Search for this imagery in space/time window
 
60
  item_collection())
61
  return items
62
 
63
+ def compute_nbs(items, box, zoom=6):
64
  # Time to compute:
65
+ # client = dask.distributed.Client()
66
  # landsat_bands = ["nir08", "swir16"]
67
  sentinel_bands = ["B08", "B12", "SCL"] # NIR, SWIR, and Cloud Mask
68
 
69
  # The magic of gdalwarper. Can also resample, reproject, and aggregate on the fly
70
  data = odc.stac.load(items,
71
+ bands=sentinel_bands,
72
+ bbox=box,
73
+ # resolution = 10 * 2**zoom,
74
+ # chunks={}, # <-- use Dask
75
  )
76
  times = [0,data.time.size-1]
77
 
 
106
  # nbs.isel(time=(nbs.time.size-1)).rio.to_raster(raster_path="after.tif", driver="COG")
107
 
108
 
109
+ # +
110
+
111
+ def compute_map(items, box):
112
+ nbs = compute_nbs(items, box)
113
+
114
+ # doesn't work without serializing data....
115
+ # m2.add_raster(nbs.isel(time=0), colormap="terrain", layer_name="NBR") # empty?
116
+
117
+ # write first and last date to tif
118
+ nbs.isel(time=0).rio.to_raster(raster_path="static/before.tif", driver="COG")
119
+ nbs.isel(time=(nbs.time.size-1)).rio.to_raster(raster_path="static/after.tif", driver="COG")
120
+ m2 = leafmap.Map(center=[35, -110], zoom=6)
121
+ m2.split_map("static/before.tif", "static/after.tif",
122
+ left_label = "NBR before fire", right_label = "NBR after fire")
123
+ return m2
124
+
125
+ def use_pyplot(items, box):
126
+ nbs = compute_nbs(items, box)
127
+ ## Show map with pyplot
128
+ import matplotlib as plt
129
+ cmap = plt.colormaps.get_cmap('viridis') # viridis is the default colormap for imshow
130
+ cmap.set_bad(color='black')
131
+ im = nbs.plot.imshow(row="time", cmap=cmap, add_colorbar=False, size=4)
132
+ st.pyplot(im.fig, use_container_width=False)
133
+
134
 
135
  # +
136
  import copy
 
187
  ## Map controls sidebar
188
  with st.sidebar:
189
 
190
+ date = st.date_input("select date",
191
+ value = datetime.strptime("2021-06-17", "%Y-%m-%d"),
192
+ min_value = datetime.strptime("2015-06-27", "%Y-%m-%d")
193
+ ) # sentinel2 records begin
194
 
195
  with st.expander("🔥 Wildfire"):
196
  # Fire Polygons, USGS
 
226
  #out
227
  n = len(out["features"])
228
  st.markdown(f"{n} images found. Computing NBR...")
229
+ m2 = compute_map(items, box)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230
  m2.to_streamlit()
231
+
232
 
requirements.txt CHANGED
@@ -1,7 +1,8 @@
1
  pystac_client
2
  planetary_computer
3
  odc.stac
4
- dask[distributed]
 
5
  pandas
6
  pydeck
7
  pmtiles
 
1
  pystac_client
2
  planetary_computer
3
  odc.stac
4
+ dask
5
+ distributed
6
  pandas
7
  pydeck
8
  pmtiles