Spaces:
Sleeping
Sleeping
Added Slope
Browse files
app.py
CHANGED
@@ -13,7 +13,8 @@ from shapely import wkb
|
|
13 |
from shapely.ops import transform
|
14 |
import geemap.foliumap as geemapfolium
|
15 |
from streamlit_folium import st_folium
|
16 |
-
|
|
|
17 |
|
18 |
# Enable fiona driver
|
19 |
fiona.drvsupport.supported_drivers['LIBKML'] = 'rw'
|
@@ -208,7 +209,7 @@ if uploaded_file is not None:
|
|
208 |
|
209 |
####### YoY Profile ########
|
210 |
start_year = 2019
|
211 |
-
end_year =
|
212 |
|
213 |
# Create an empty resultant dataframe
|
214 |
columns = ['Date', 'Imagery', 'AvgNDVI_Inside', 'Avg_NDVI_Buffer', 'Ratio']
|
@@ -216,35 +217,40 @@ if uploaded_file is not None:
|
|
216 |
|
217 |
max_ndvi_geoms = []
|
218 |
max_ndvi_buffered_geoms = []
|
|
|
219 |
for year in range(start_year, end_year+1):
|
|
|
|
|
|
|
|
|
220 |
|
221 |
-
|
222 |
-
|
223 |
-
end_ddmm = str(year)+pd.to_datetime(end_date).strftime("-%m-%d")
|
224 |
-
|
225 |
-
# Filter data based on the date, bounds, cloud coverage and select NIR and Red Band
|
226 |
-
collection = ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED").filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', max_cloud_cover)).filter(ee.Filter.date(start_ddmm, end_ddmm)).select(['B4', 'B8'])
|
227 |
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
|
|
241 |
|
242 |
-
|
243 |
-
|
244 |
|
245 |
-
|
246 |
-
|
|
|
|
|
247 |
|
|
|
248 |
# Write the final table
|
249 |
st.write(combined_df)
|
250 |
|
@@ -253,12 +259,15 @@ if uploaded_file is not None:
|
|
253 |
st.line_chart(combined_df[['AvgNDVI_Inside', 'Avg_NDVI_Buffer', 'Date']].set_index('Date'))
|
254 |
|
255 |
# Create a DataFrame for YoY profile
|
256 |
-
yoy_df = pd.DataFrame({'Year':
|
257 |
yoy_df['Ratio'] = yoy_df['NDVI_Inside'] / yoy_df['NDVI_Buffer']
|
|
|
258 |
|
259 |
# plot the time series
|
260 |
st.write("Year on Year Plot using Maximum NDVI Composite (computed for given duration)")
|
261 |
st.line_chart(yoy_df[['NDVI_Inside', 'NDVI_Buffer', 'Ratio', 'Year']].set_index('Year'))
|
|
|
|
|
262 |
|
263 |
# Visualize map on ESRI basemap
|
264 |
st.write("Map Visualization")
|
|
|
13 |
from shapely.ops import transform
|
14 |
import geemap.foliumap as geemapfolium
|
15 |
from streamlit_folium import st_folium
|
16 |
+
import datetime import datetime
|
17 |
+
import numpy as np
|
18 |
|
19 |
# Enable fiona driver
|
20 |
fiona.drvsupport.supported_drivers['LIBKML'] = 'rw'
|
|
|
209 |
|
210 |
####### YoY Profile ########
|
211 |
start_year = 2019
|
212 |
+
end_year = datetime.now().year
|
213 |
|
214 |
# Create an empty resultant dataframe
|
215 |
columns = ['Date', 'Imagery', 'AvgNDVI_Inside', 'Avg_NDVI_Buffer', 'Ratio']
|
|
|
217 |
|
218 |
max_ndvi_geoms = []
|
219 |
max_ndvi_buffered_geoms = []
|
220 |
+
years=[]
|
221 |
for year in range(start_year, end_year+1):
|
222 |
+
try:
|
223 |
+
# Construct start and end dates for every year
|
224 |
+
start_ddmm = str(year)+pd.to_datetime(start_date).strftime("-%m-%d")
|
225 |
+
end_ddmm = str(year)+pd.to_datetime(end_date).strftime("-%m-%d")
|
226 |
|
227 |
+
# Filter data based on the date, bounds, cloud coverage and select NIR and Red Band
|
228 |
+
collection = ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED").filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', max_cloud_cover)).filter(ee.Filter.date(start_ddmm, end_ddmm)).select(['B4', 'B8'])
|
|
|
|
|
|
|
|
|
229 |
|
230 |
+
# Get Zonal Max composite NDVI based on collection and geometries (Original KML and Buffered KML)
|
231 |
+
max_ndvi_geoms.append(get_zonal_ndviYoY(collection.filterBounds(geom_ee_object), geom_ee_object))
|
232 |
+
max_ndvi_buffered_geoms.append(get_zonal_ndviYoY(collection.filterBounds(buffered_ee_object), buffered_ee_object))
|
233 |
+
years.append(year)
|
234 |
+
|
235 |
+
# Get Zonal NDVI
|
236 |
+
df_geom = get_zonal_ndvi(collection.filterBounds(geom_ee_object), geom_ee_object)
|
237 |
+
df_buffered_geom = get_zonal_ndvi(collection.filterBounds(buffered_ee_object), buffered_ee_object)
|
238 |
+
|
239 |
+
# Merge both Zonalstats and create resultant dataframe
|
240 |
+
resultant_df = pd.merge(df_geom, df_buffered_geom, on='Date', how='inner')
|
241 |
+
resultant_df = resultant_df.rename(columns={'NDVI_x': 'AvgNDVI_Inside', 'NDVI_y': 'Avg_NDVI_Buffer', 'Imagery_x': 'Imagery'})
|
242 |
+
resultant_df['Ratio'] = resultant_df['AvgNDVI_Inside'] / resultant_df['Avg_NDVI_Buffer']
|
243 |
+
resultant_df.drop(columns=['Imagery_y'], inplace=True)
|
244 |
|
245 |
+
# Re-order the columns of the resultant dataframe
|
246 |
+
resultant_df = resultant_df[['Date', 'Imagery', 'AvgNDVI_Inside', 'Avg_NDVI_Buffer', 'Ratio']]
|
247 |
|
248 |
+
# Append to empty dataframe
|
249 |
+
combined_df = pd.concat([combined_df, resultant_df], ignore_index=True)
|
250 |
+
except:
|
251 |
+
continue
|
252 |
|
253 |
+
|
254 |
# Write the final table
|
255 |
st.write(combined_df)
|
256 |
|
|
|
259 |
st.line_chart(combined_df[['AvgNDVI_Inside', 'Avg_NDVI_Buffer', 'Date']].set_index('Date'))
|
260 |
|
261 |
# Create a DataFrame for YoY profile
|
262 |
+
yoy_df = pd.DataFrame({'Year': years, 'NDVI_Inside': max_ndvi_geoms, 'NDVI_Buffer': max_ndvi_buffered_geoms})
|
263 |
yoy_df['Ratio'] = yoy_df['NDVI_Inside'] / yoy_df['NDVI_Buffer']
|
264 |
+
slope, intercept = np.polyfit(yoy_df['Year'], yoy_df['NDVI_Inside'], 1)
|
265 |
|
266 |
# plot the time series
|
267 |
st.write("Year on Year Plot using Maximum NDVI Composite (computed for given duration)")
|
268 |
st.line_chart(yoy_df[['NDVI_Inside', 'NDVI_Buffer', 'Ratio', 'Year']].set_index('Year'))
|
269 |
+
st.write("Slope (trend) and Intercept are {}, {} respectively. ".format(np.round(slope, 4), np.round(intercept, 4)))
|
270 |
+
|
271 |
|
272 |
# Visualize map on ESRI basemap
|
273 |
st.write("Map Visualization")
|