update app.py
Browse files
app.py
CHANGED
@@ -34,18 +34,11 @@ st.write(
|
|
34 |
""",
|
35 |
unsafe_allow_html=True,
|
36 |
)
|
37 |
-
# Initialize session state for storing results if not already done
|
38 |
-
if 'results' not in st.session_state:
|
39 |
-
st.session_state.results = []
|
40 |
-
if 'last_params' not in st.session_state:
|
41 |
-
st.session_state.last_params = {}
|
42 |
-
if 'map_data' not in st.session_state:
|
43 |
-
st.session_state.map_data = None # Initialize map_data
|
44 |
|
45 |
-
#
|
46 |
earthengine_credentials = os.environ.get("EE_Authentication")
|
47 |
|
48 |
-
# Initialize Earth Engine with credentials
|
49 |
os.makedirs(os.path.expanduser("~/.config/earthengine/"), exist_ok=True)
|
50 |
with open(os.path.expanduser("~/.config/earthengine/credentials"), "w") as f:
|
51 |
f.write(earthengine_credentials)
|
@@ -101,35 +94,6 @@ def read_kml(file_path):
|
|
101 |
gdf = gpd.read_file(file_path, driver='KML')
|
102 |
return gdf
|
103 |
|
104 |
-
# Function to standardize column names
|
105 |
-
def standardize_column_names(df):
|
106 |
-
# Define a mapping of common variations to standardized column names
|
107 |
-
column_mapping = {
|
108 |
-
'latitude': ['latitude', 'lat', 'Latitude', 'Lat'],
|
109 |
-
'longitude': ['longitude', 'lon', 'Longitude', 'Lon'],
|
110 |
-
'name': ['name', 'Name']
|
111 |
-
}
|
112 |
-
|
113 |
-
# Normalize column names
|
114 |
-
df.columns = [col.strip().lower() for col in df.columns] # Convert all column names to lowercase and strip any extra spaces
|
115 |
-
|
116 |
-
# Rename columns based on the mapping
|
117 |
-
for standard_col, variations in column_mapping.items():
|
118 |
-
for variation in variations:
|
119 |
-
if variation.lower() in df.columns:
|
120 |
-
df = df.rename(columns={variation.lower(): standard_col})
|
121 |
-
break
|
122 |
-
|
123 |
-
return df
|
124 |
-
|
125 |
-
# Function to sanitize descriptions (location names)
|
126 |
-
def sanitize_description(description):
|
127 |
-
# Replace spaces and other invalid characters with underscores
|
128 |
-
sanitized = re.sub(r'[^a-zA-Z0-9._-]', '_', description)
|
129 |
-
# Ensure the description is no longer than 100 characters
|
130 |
-
sanitized = sanitized[:100]
|
131 |
-
return sanitized
|
132 |
-
|
133 |
# Ask user whether they want to process 'Point' or 'Polygon' data
|
134 |
shape_type = st.selectbox("Do you want to process 'Point' or 'Polygon' data?", ["Point", "Polygon"])
|
135 |
|
@@ -144,13 +108,13 @@ end_date = st.date_input("End Date", value=pd.to_datetime('2020-12-31'))
|
|
144 |
start_date_str = start_date.strftime('%Y-%m-%d')
|
145 |
end_date_str = end_date.strftime('%Y-%m-%d')
|
146 |
|
147 |
-
#
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
|
155 |
# Function to check if parameters have changed
|
156 |
def parameters_changed():
|
@@ -209,6 +173,14 @@ def calculate_custom_formula(image, geometry, formula):
|
|
209 |
)
|
210 |
return result.get('Custom Index')
|
211 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
212 |
# Process each point
|
213 |
if file_upload:
|
214 |
locations_df = None # Initialize locations_df to None
|
@@ -235,9 +207,6 @@ if file_upload:
|
|
235 |
st.error("Unsupported file type. Please upload a GeoJSON or KML file for polygons.")
|
236 |
|
237 |
if locations_df is not None:
|
238 |
-
# Standardize column names for consistency
|
239 |
-
locations_df = standardize_column_names(locations_df)
|
240 |
-
|
241 |
# Display a preview of the points data
|
242 |
st.write("Preview of the uploaded points data:")
|
243 |
st.dataframe(locations_df.head())
|
@@ -353,3 +322,20 @@ if file_upload:
|
|
353 |
data=export_image.getDownloadURL(),
|
354 |
file_name=f'{sanitized_location_name}_image.tif'
|
355 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
""",
|
35 |
unsafe_allow_html=True,
|
36 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
+
# Authenticate and initialize Earth Engine
|
39 |
earthengine_credentials = os.environ.get("EE_Authentication")
|
40 |
|
41 |
+
# Initialize Earth Engine with secret credentials
|
42 |
os.makedirs(os.path.expanduser("~/.config/earthengine/"), exist_ok=True)
|
43 |
with open(os.path.expanduser("~/.config/earthengine/credentials"), "w") as f:
|
44 |
f.write(earthengine_credentials)
|
|
|
94 |
gdf = gpd.read_file(file_path, driver='KML')
|
95 |
return gdf
|
96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
# Ask user whether they want to process 'Point' or 'Polygon' data
|
98 |
shape_type = st.selectbox("Do you want to process 'Point' or 'Polygon' data?", ["Point", "Polygon"])
|
99 |
|
|
|
108 |
start_date_str = start_date.strftime('%Y-%m-%d')
|
109 |
end_date_str = end_date.strftime('%Y-%m-%d')
|
110 |
|
111 |
+
# Initialize session state for storing results if not already done
|
112 |
+
if 'results' not in st.session_state:
|
113 |
+
st.session_state.results = []
|
114 |
+
if 'last_params' not in st.session_state:
|
115 |
+
st.session_state.last_params = {}
|
116 |
+
if 'map_data' not in st.session_state:
|
117 |
+
st.session_state.map_data = None # Initialize map_data
|
118 |
|
119 |
# Function to check if parameters have changed
|
120 |
def parameters_changed():
|
|
|
173 |
)
|
174 |
return result.get('Custom Index')
|
175 |
|
176 |
+
# Function to sanitize the description for export
|
177 |
+
def sanitize_description(description):
|
178 |
+
# Replace spaces and other invalid characters with underscores
|
179 |
+
sanitized = re.sub(r'[^a-zA-Z0-9._-]', '_', description)
|
180 |
+
# Ensure the description is no longer than 100 characters
|
181 |
+
sanitized = sanitized[:100]
|
182 |
+
return sanitized
|
183 |
+
|
184 |
# Process each point
|
185 |
if file_upload:
|
186 |
locations_df = None # Initialize locations_df to None
|
|
|
207 |
st.error("Unsupported file type. Please upload a GeoJSON or KML file for polygons.")
|
208 |
|
209 |
if locations_df is not None:
|
|
|
|
|
|
|
210 |
# Display a preview of the points data
|
211 |
st.write("Preview of the uploaded points data:")
|
212 |
st.dataframe(locations_df.head())
|
|
|
322 |
data=export_image.getDownloadURL(),
|
323 |
file_name=f'{sanitized_location_name}_image.tif'
|
324 |
)
|
325 |
+
|
326 |
+
# After processing, show the results
|
327 |
+
if st.session_state.results:
|
328 |
+
# Convert the results to a DataFrame for better visualization
|
329 |
+
result_df = pd.DataFrame(st.session_state.results)
|
330 |
+
|
331 |
+
# Show the results in a table format
|
332 |
+
st.write("Processed Results Table:")
|
333 |
+
st.dataframe(result_df[['Location Name', 'Latitude', 'Longitude', 'Calculated Value']])
|
334 |
+
|
335 |
+
# Allow downloading of the results as CSV
|
336 |
+
st.download_button(
|
337 |
+
label="Download results as CSV",
|
338 |
+
data=result_df.to_csv(index=False).encode('utf-8'),
|
339 |
+
file_name="calculated_results.csv",
|
340 |
+
mime='text/csv'
|
341 |
+
)
|