Spaces:
Running
Running
update app.py
Browse files
app.py
CHANGED
@@ -493,12 +493,6 @@ if shape_type.lower() == "point":
|
|
493 |
index=0,
|
494 |
help="Choose 'Point' for exact point calculation, or a kernel size for area averaging."
|
495 |
)
|
496 |
-
|
497 |
-
# Initialize column names in session state
|
498 |
-
if 'lat_col' not in st.session_state:
|
499 |
-
st.session_state.lat_col = None
|
500 |
-
if 'lon_col' not in st.session_state:
|
501 |
-
st.session_state.lon_col = None
|
502 |
elif shape_type.lower() == "polygon":
|
503 |
include_boundary = st.checkbox(
|
504 |
"Include Boundary Pixels",
|
@@ -515,42 +509,49 @@ if file_upload is not None:
|
|
515 |
# Read the CSV file
|
516 |
locations_df = pd.read_csv(file_upload)
|
517 |
|
518 |
-
#
|
519 |
-
|
|
|
520 |
|
521 |
-
|
522 |
-
|
523 |
-
|
524 |
-
|
525 |
-
|
526 |
-
|
527 |
-
|
528 |
-
|
529 |
-
|
530 |
-
|
531 |
-
|
532 |
-
|
533 |
-
|
534 |
-
|
535 |
-
|
536 |
-
|
537 |
-
|
538 |
-
|
539 |
-
|
540 |
-
|
541 |
-
|
542 |
-
|
543 |
-
|
544 |
-
|
545 |
-
|
546 |
-
|
547 |
-
|
|
|
|
|
|
|
548 |
|
549 |
elif file_upload.name.endswith('.geojson'):
|
550 |
locations_df = gpd.read_file(file_upload)
|
551 |
if 'geometry' in locations_df.columns:
|
552 |
locations_df['latitude'] = locations_df['geometry'].y
|
553 |
locations_df['longitude'] = locations_df['geometry'].x
|
|
|
|
|
|
|
554 |
elif file_upload.name.endswith('.kml'):
|
555 |
kml_string = file_upload.read().decode('utf-8')
|
556 |
try:
|
@@ -575,11 +576,8 @@ if file_upload is not None:
|
|
575 |
except Exception as e:
|
576 |
st.error(f"Error parsing KML file: {str(e)}")
|
577 |
|
578 |
-
# Display map for points
|
579 |
if not locations_df.empty and 'latitude' in locations_df.columns and 'longitude' in locations_df.columns:
|
580 |
-
st.write("Preview of Uploaded Points Data:")
|
581 |
-
st.dataframe(locations_df.head())
|
582 |
-
|
583 |
m = leafmap.Map(center=[locations_df['latitude'].mean(), locations_df['longitude'].mean()], zoom=10)
|
584 |
for _, row in locations_df.iterrows():
|
585 |
latitude = row['latitude']
|
@@ -595,6 +593,9 @@ if file_upload is not None:
|
|
595 |
st.error("CSV upload not supported for polygons. Please upload a GeoJSON or KML file.")
|
596 |
elif file_upload.name.endswith('.geojson'):
|
597 |
locations_df = gpd.read_file(file_upload)
|
|
|
|
|
|
|
598 |
elif file_upload.name.endswith('.kml'):
|
599 |
kml_string = file_upload.read().decode('utf-8')
|
600 |
try:
|
@@ -617,11 +618,8 @@ if file_upload is not None:
|
|
617 |
except Exception as e:
|
618 |
st.error(f"Error parsing KML file: {str(e)}")
|
619 |
|
620 |
-
# Display map for polygons
|
621 |
if not locations_df.empty and 'geometry' in locations_df.columns:
|
622 |
-
st.write("Preview of Uploaded Polygons Data:")
|
623 |
-
st.dataframe(locations_df.head())
|
624 |
-
|
625 |
centroid_lat = locations_df.geometry.centroid.y.mean()
|
626 |
centroid_lon = locations_df.geometry.centroid.x.mean()
|
627 |
m = leafmap.Map(center=[centroid_lat, centroid_lon], zoom=10)
|
@@ -672,4 +670,4 @@ if st.button(f"Calculate {custom_formula}"):
|
|
672 |
except Exception as e:
|
673 |
st.error(f"An error occurred during processing: {str(e)}")
|
674 |
else:
|
675 |
-
st.warning("Please upload a file to proceed.")
|
|
|
493 |
index=0,
|
494 |
help="Choose 'Point' for exact point calculation, or a kernel size for area averaging."
|
495 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
496 |
elif shape_type.lower() == "polygon":
|
497 |
include_boundary = st.checkbox(
|
498 |
"Include Boundary Pixels",
|
|
|
509 |
# Read the CSV file
|
510 |
locations_df = pd.read_csv(file_upload)
|
511 |
|
512 |
+
# Show the first few rows to help user identify columns
|
513 |
+
st.write("Preview of your uploaded data (first 5 rows):")
|
514 |
+
st.dataframe(locations_df.head())
|
515 |
|
516 |
+
# Let user input their column names
|
517 |
+
col1, col2 = st.columns(2)
|
518 |
+
with col1:
|
519 |
+
lat_col = st.text_input(
|
520 |
+
"Enter the exact name of your Latitude column",
|
521 |
+
value="latitude",
|
522 |
+
help="Case-sensitive column name from your CSV file"
|
523 |
+
)
|
524 |
+
with col2:
|
525 |
+
lon_col = st.text_input(
|
526 |
+
"Enter the exact name of your Longitude column",
|
527 |
+
value="longitude",
|
528 |
+
help="Case-sensitive column name from your CSV file"
|
529 |
+
)
|
530 |
+
|
531 |
+
# Validate the columns exist
|
532 |
+
if lat_col not in locations_df.columns or lon_col not in locations_df.columns:
|
533 |
+
st.error(f"Error: Could not find columns '{lat_col}' and/or '{lon_col}' in your file. Available columns are: {', '.join(locations_df.columns)}")
|
534 |
+
st.stop()
|
535 |
+
|
536 |
+
# Rename the columns to standard names for processing
|
537 |
+
locations_df = locations_df.rename(columns={
|
538 |
+
lat_col: 'latitude',
|
539 |
+
lon_col: 'longitude'
|
540 |
+
})
|
541 |
+
|
542 |
+
# Check for valid numeric data
|
543 |
+
if not pd.api.types.is_numeric_dtype(locations_df['latitude']) or not pd.api.types.is_numeric_dtype(locations_df['longitude']):
|
544 |
+
st.error("Error: Latitude and Longitude columns must contain numeric values")
|
545 |
+
st.stop()
|
546 |
|
547 |
elif file_upload.name.endswith('.geojson'):
|
548 |
locations_df = gpd.read_file(file_upload)
|
549 |
if 'geometry' in locations_df.columns:
|
550 |
locations_df['latitude'] = locations_df['geometry'].y
|
551 |
locations_df['longitude'] = locations_df['geometry'].x
|
552 |
+
else:
|
553 |
+
st.error("GeoJSON file doesn't contain geometry column")
|
554 |
+
st.stop()
|
555 |
elif file_upload.name.endswith('.kml'):
|
556 |
kml_string = file_upload.read().decode('utf-8')
|
557 |
try:
|
|
|
576 |
except Exception as e:
|
577 |
st.error(f"Error parsing KML file: {str(e)}")
|
578 |
|
579 |
+
# Display map for points if we have valid data
|
580 |
if not locations_df.empty and 'latitude' in locations_df.columns and 'longitude' in locations_df.columns:
|
|
|
|
|
|
|
581 |
m = leafmap.Map(center=[locations_df['latitude'].mean(), locations_df['longitude'].mean()], zoom=10)
|
582 |
for _, row in locations_df.iterrows():
|
583 |
latitude = row['latitude']
|
|
|
593 |
st.error("CSV upload not supported for polygons. Please upload a GeoJSON or KML file.")
|
594 |
elif file_upload.name.endswith('.geojson'):
|
595 |
locations_df = gpd.read_file(file_upload)
|
596 |
+
if 'geometry' not in locations_df.columns:
|
597 |
+
st.error("GeoJSON file doesn't contain geometry column")
|
598 |
+
st.stop()
|
599 |
elif file_upload.name.endswith('.kml'):
|
600 |
kml_string = file_upload.read().decode('utf-8')
|
601 |
try:
|
|
|
618 |
except Exception as e:
|
619 |
st.error(f"Error parsing KML file: {str(e)}")
|
620 |
|
621 |
+
# Display map for polygons if we have valid data
|
622 |
if not locations_df.empty and 'geometry' in locations_df.columns:
|
|
|
|
|
|
|
623 |
centroid_lat = locations_df.geometry.centroid.y.mean()
|
624 |
centroid_lon = locations_df.geometry.centroid.x.mean()
|
625 |
m = leafmap.Map(center=[centroid_lat, centroid_lon], zoom=10)
|
|
|
670 |
except Exception as e:
|
671 |
st.error(f"An error occurred during processing: {str(e)}")
|
672 |
else:
|
673 |
+
st.warning("Please upload a valid file to proceed.")
|