update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,10 @@ import json
|
|
3 |
import ee
|
4 |
import geemap
|
5 |
import os
|
6 |
-
import time
|
|
|
|
|
|
|
7 |
|
8 |
st.set_page_config(layout="wide")
|
9 |
|
@@ -55,63 +58,123 @@ custom_formula = ""
|
|
55 |
if index_choice == 'Custom Formula':
|
56 |
custom_formula = st.text_input("Enter Custom Formula (e.g., 'B5 - B4 / B5 + B4')")
|
57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
# Define functions for index calculations
|
59 |
-
def calculate_ndvi(image):
|
60 |
-
return image.normalizedDifference(['B5', 'B4']).rename('NDVI')
|
|
|
|
|
|
|
|
|
61 |
|
62 |
-
def calculate_ndwi(image):
|
63 |
-
return image.normalizedDifference(['B3', 'B5']).rename('NDWI')
|
|
|
|
|
|
|
|
|
64 |
|
65 |
-
def calculate_avg_no2(image):
|
66 |
return image.select('NO2').reduceRegion(
|
67 |
reducer=ee.Reducer.mean(),
|
68 |
-
geometry=
|
69 |
scale=1000
|
70 |
).get('NO2')
|
71 |
|
72 |
-
def calculate_custom_formula(image, formula):
|
73 |
-
return image.expression(formula).rename('Custom Index')
|
|
|
|
|
|
|
|
|
74 |
|
75 |
-
# Step
|
76 |
-
result_image = None
|
77 |
if st.button("Calculate Index"):
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
#
|
108 |
-
|
|
|
|
|
|
|
109 |
Map = geemap.Map()
|
110 |
Map.setCenter(-100.0, 40.0, 4) # Adjusted for example
|
111 |
-
Map.addLayer(
|
112 |
Map.to_streamlit(height=600)
|
113 |
-
else:
|
114 |
-
st.warning("Result image not generated; check parameters.")
|
115 |
|
116 |
-
|
117 |
-
|
|
|
|
|
|
3 |
import ee
|
4 |
import geemap
|
5 |
import os
|
6 |
+
import time
|
7 |
+
import pandas as pd
|
8 |
+
import geopandas as gpd
|
9 |
+
from shapely.geometry import shape
|
10 |
|
11 |
st.set_page_config(layout="wide")
|
12 |
|
|
|
58 |
if index_choice == 'Custom Formula':
|
59 |
custom_formula = st.text_input("Enter Custom Formula (e.g., 'B5 - B4 / B5 + B4')")
|
60 |
|
61 |
+
# Step 5: File Upload (CSV, GeoJSON, KML)
|
62 |
+
uploaded_file = st.file_uploader("Upload CSV, GeoJSON, or KML file", type=["csv", "geojson", "kml"])
|
63 |
+
|
64 |
+
def parse_file(uploaded_file):
|
65 |
+
"""
|
66 |
+
Parse the uploaded file (CSV, GeoJSON, or KML) and return the geometry.
|
67 |
+
"""
|
68 |
+
if uploaded_file is not None:
|
69 |
+
# Determine file type
|
70 |
+
file_type = uploaded_file.type
|
71 |
+
|
72 |
+
# Handle CSV file
|
73 |
+
if file_type == "text/csv":
|
74 |
+
df = pd.read_csv(uploaded_file)
|
75 |
+
st.write("CSV file content:")
|
76 |
+
st.write(df.head())
|
77 |
+
# Assuming the CSV has columns named 'latitude' and 'longitude' for points
|
78 |
+
if 'latitude' in df.columns and 'longitude' in df.columns:
|
79 |
+
# Convert to GeoDataFrame with points
|
80 |
+
geometry = gpd.GeoSeries.from_xy(df.longitude, df.latitude)
|
81 |
+
return geometry
|
82 |
+
|
83 |
+
# Handle GeoJSON file
|
84 |
+
elif file_type == "application/geo+json":
|
85 |
+
gdf = gpd.read_file(uploaded_file)
|
86 |
+
st.write("GeoJSON file content:")
|
87 |
+
st.write(gdf.head())
|
88 |
+
return gdf.geometry
|
89 |
+
|
90 |
+
# Handle KML file
|
91 |
+
elif file_type == "application/vnd.google-earth.kml+xml":
|
92 |
+
gdf = gpd.read_file(uploaded_file)
|
93 |
+
st.write("KML file content:")
|
94 |
+
st.write(gdf.head())
|
95 |
+
return gdf.geometry
|
96 |
+
|
97 |
+
else:
|
98 |
+
st.error("Unsupported file format. Please upload a CSV, GeoJSON, or KML file.")
|
99 |
+
return None
|
100 |
+
else:
|
101 |
+
st.warning("Please upload a file.")
|
102 |
+
return None
|
103 |
+
|
104 |
+
# Parse the file if uploaded
|
105 |
+
geometry = parse_file(uploaded_file)
|
106 |
+
|
107 |
# Define functions for index calculations
|
108 |
+
def calculate_ndvi(image, geometry):
|
109 |
+
return image.normalizedDifference(['B5', 'B4']).rename('NDVI').reduceRegion(
|
110 |
+
reducer=ee.Reducer.mean(),
|
111 |
+
geometry=geometry,
|
112 |
+
scale=30
|
113 |
+
)
|
114 |
|
115 |
+
def calculate_ndwi(image, geometry):
|
116 |
+
return image.normalizedDifference(['B3', 'B5']).rename('NDWI').reduceRegion(
|
117 |
+
reducer=ee.Reducer.mean(),
|
118 |
+
geometry=geometry,
|
119 |
+
scale=30
|
120 |
+
)
|
121 |
|
122 |
+
def calculate_avg_no2(image, geometry):
|
123 |
return image.select('NO2').reduceRegion(
|
124 |
reducer=ee.Reducer.mean(),
|
125 |
+
geometry=geometry,
|
126 |
scale=1000
|
127 |
).get('NO2')
|
128 |
|
129 |
+
def calculate_custom_formula(image, geometry, formula):
|
130 |
+
return image.expression(formula).rename('Custom Index').reduceRegion(
|
131 |
+
reducer=ee.Reducer.mean(),
|
132 |
+
geometry=geometry,
|
133 |
+
scale=30
|
134 |
+
)
|
135 |
|
136 |
+
# Step 6: Perform the calculation based on user choice
|
|
|
137 |
if st.button("Calculate Index"):
|
138 |
+
if geometry is not None:
|
139 |
+
try:
|
140 |
+
# Verify if the dataset_id is correct
|
141 |
+
dataset_id = sub_options[sub_selection] # Earth Engine dataset ID
|
142 |
+
if not ee.data.getInfo(dataset_id):
|
143 |
+
st.error(f"The dataset '{dataset_id}' does not exist or is not accessible.")
|
144 |
+
st.stop()
|
145 |
+
|
146 |
+
# Load dataset
|
147 |
+
collection = ee.ImageCollection(dataset_id).filterDate('2020-01-01', '2020-12-31')
|
148 |
+
image = collection.first()
|
149 |
+
|
150 |
+
# Choose calculation based on user selection
|
151 |
+
if index_choice == 'NDVI':
|
152 |
+
result = calculate_ndvi(image, geometry)
|
153 |
+
st.write("NDVI result:", result.getInfo())
|
154 |
+
elif index_choice == 'NDWI':
|
155 |
+
result = calculate_ndwi(image, geometry)
|
156 |
+
st.write("NDWI result:", result.getInfo())
|
157 |
+
elif index_choice == 'Average NO₂':
|
158 |
+
result = calculate_avg_no2(image, geometry)
|
159 |
+
st.write("Average NO₂ Concentration:", result.getInfo())
|
160 |
+
elif index_choice == 'Custom Formula' and custom_formula:
|
161 |
+
result = calculate_custom_formula(image, geometry, custom_formula)
|
162 |
+
st.write("Custom Index result:", result.getInfo())
|
163 |
+
|
164 |
+
# Visualization Parameters for Index Layers
|
165 |
+
vis_params = {"min": 0, "max": 1, "palette": ["blue", "white", "green"]}
|
166 |
+
|
167 |
+
# Add loading spinner while the map is loading
|
168 |
+
with st.spinner('Loading the map, please wait...'):
|
169 |
+
time.sleep(2) # Simulate processing delay (you can adjust/remove this in production)
|
170 |
+
|
171 |
+
# If an index image is generated, add it to the map
|
172 |
Map = geemap.Map()
|
173 |
Map.setCenter(-100.0, 40.0, 4) # Adjusted for example
|
174 |
+
Map.addLayer(image, vis_params, index_choice)
|
175 |
Map.to_streamlit(height=600)
|
|
|
|
|
176 |
|
177 |
+
except ee.EEException as e:
|
178 |
+
st.error(f"Earth Engine Error: {e}")
|
179 |
+
else:
|
180 |
+
st.error("No geometry data found in the uploaded file.")
|