Spaces:
Runtime error
Runtime error
| import solara | |
| import geopandas as gpd | |
| import pandas as pd | |
| import solara.lab | |
| import leafmap.foliumap as leafmap | |
| import os | |
| import tempfile | |
| import io | |
| import datetime as dt | |
| import pystac_client | |
| import planetary_computer | |
| import stackstac | |
| import xrspatial | |
| import rioxarray | |
| import xarray | |
| import numpy as np | |
| ##Function to Read ROI | |
| def roi_reader(): | |
| roi_path, set_roi_path = solara.use_state(None) | |
| roi_error, set_roi_error = solara.use_state(None) | |
| def on_file(file): | |
| try: | |
| name = file['name'] | |
| suffix = os.path.splitext(name)[1].lower() | |
| tmp = tempfile.NamedTemporaryFile(delete = False, suffix = suffix) | |
| tmp.write(file['data']) | |
| tmp.close() | |
| set_roi_path(tmp.name) | |
| except Exception as e: | |
| set_roi_error(str(e)) | |
| set_roi_path(None) | |
| #Layout for FileDrop and Date Input | |
| with solara.Columns(widths = [0.5, 2]): | |
| solara.FileDrop( | |
| label = 'Drop Your KML Here!', | |
| lazy = False, | |
| on_file = on_file | |
| ) | |
| with solara.Card( | |
| title = 'Input Date Range', | |
| subtitle = 'Please Select Pre Date (Before Deforestation) and Post Date (After Deforestation)- Assumed to be', | |
| style = { | |
| 'background': "#01144d" | |
| } | |
| ): | |
| with solara.Column(style = {'background': '#ffffff00'}): | |
| with solara.Columns(): | |
| date_pre = solara.use_reactive(dt.date.today()) | |
| date_post = solara.use_reactive(dt.date.today()) | |
| preDate = solara.lab.InputDate( | |
| value = date_pre, | |
| label = 'Select Pre Date', | |
| date_format = "%Y-%m-%d" | |
| ) | |
| postDate = solara.lab.InputDate( | |
| value = date_post, | |
| label = 'Select Post Date', | |
| date_format = "%Y-%m-%d" | |
| ) | |
| solara.Text(f'selected Date Range is {str(date_pre.value)} to {str(date_post.value)}') | |
| #Displaying ROI as geoDataFrame and Map | |
| if roi_path: | |
| try: | |
| roi = gpd.read_file(roi_path) | |
| except Exception as e: | |
| solara.Markdown(f'Faied to read fle: {e}') | |
| else: | |
| solara.Markdown('Uploaded KML Attributes') | |
| solara.display(roi) | |
| #Sentinel Extractor | |
| roi_reprojected = roi.to_crs('EPSG:32755') | |
| def Sentinel_Extractor(startDate: str, endDate: str, CloudCover: int | float): | |
| datetime = startDate + '/' + endDate | |
| catalog = pystac_client.Client.open( | |
| "https://planetarycomputer.microsoft.com/api/stac/v1", | |
| modifier = planetary_computer.sign_inplace | |
| ) | |
| search = catalog.search( | |
| collections = ['sentinel-2-l2a'], | |
| intersects = roi.geometry.union_all().__geo_interface__, | |
| datetime = datetime, | |
| query = {'eo:cloud_cover': {'lt': CloudCover}} | |
| ) | |
| items = search.get_all_items() | |
| stacked = stackstac.stack(items = items, | |
| assets = ['B02', 'B03', 'B04', 'B08', 'B12'], | |
| epsg = 32755, | |
| chunksize = 1024) | |
| stacked_median = stacked.median(dim = 'time', keep_attrs = True) | |
| stacked_clipped = stacked_median.rio.clip(roi_reprojected.geometry, roi_reprojected.crs) | |
| stacked_scaled = stacked_clipped * 0.0001 | |
| return stacked_scaled | |
| preImage = Sentinel_Extractor( | |
| startDate = str(date_pre.value), | |
| endDate = str(date_pre.value + dt.timedelta(days = 10)), | |
| CloudCover = 20 | |
| ) | |
| temp_msi = tempfile.NamedTemporaryFile(delete = False, suffix = '.tif') | |
| temp_save = preImage.rio.write_crs('EPSG:4326') | |
| temp_save.rio.to_raster(temp_msi.name) | |
| m = leafmap.Map() | |
| m.add_basemap('SATELLITE') | |
| m.add_gdf(roi, zoom_to_layer = True) | |
| m.add_raster(temp_msi.name, indexes = [3, 2, 1], nodata = np.nan) | |
| solara.display(m) | |
| def side_bar(): | |
| with solara.Sidebar(): | |
| with solara.Column(align= 'center', style = {'background': '#ffffff00'}, ): | |
| solara.Markdown('##Steps for User') | |
| with solara.Column(margin = 2, align = 'stretch', style = {'background': 'red', | |
| 'font-size': '14px'}): | |
| solara.Text('1. Upload Your Region of Interest as KML') | |
| with solara.Column(margin = 2, align = 'stretch', style = {'background': 'blue', | |
| 'font-size': '14px'}): | |
| solara.Text('2. Select the Date Range') | |
| with solara.Column(margin = 2, align = 'stretch', style = {'background': 'green', | |
| 'font-size': '14px'}): | |
| solara.Text('2. Visualise the Results Interactively') | |
| def app_bar(): | |
| with solara.AppBar(): | |
| with solara.Row(): | |
| with solara.AppBarTitle(): | |
| with solara.Row(style = { | |
| 'background': '#ffffff00' | |
| }, | |
| margin = 0, justify = 'space-evenly', ): | |
| logo = solara.Image('Logo black.png', width = '15%') | |
| theme_toggler = solara.lab.ThemeToggle() | |
| def main_page(): | |
| title = solara.Markdown('#One End tool for Deforestation Monitoring', | |
| style = { | |
| 'font-family': 'Century Gothic', | |
| 'size': '12', | |
| 'color': '#fee391' | |
| }) | |
| subtitle = solara.Text('This Web Application Helps You Monitor the Forest Loss Using NDFI - Spectral Mixture Analysis (N-FINDR)', | |
| style = { | |
| 'font-family': 'Century Gothic' | |
| }) | |
| roi_layout = roi_reader() | |
| def Page(): | |
| app_header = app_bar() | |
| sideBar = side_bar() | |
| heading = main_page() | |
| Page() |