YashMK89 commited on
Commit
d155b1c
·
verified ·
1 Parent(s): 231cd32

update app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -123
app.py CHANGED
@@ -152,116 +152,116 @@ shape_type = st.selectbox("Do you want to process 'Point' or 'Polygon' data?", [
152
  # Ask user to upload a file based on shape type (case-insensitive)
153
  file_upload = st.file_uploader(f"Upload your {shape_type} data (CSV, GeoJSON, KML)", type=["csv", "geojson", "kml"])
154
 
155
- # if file_upload is not None:
156
- # # Read the user-uploaded file
157
- # if shape_type.lower() == "point":
158
- # # Handle different file types for Point data
159
- # if file_upload.name.endswith('.csv'):
160
- # locations_df = pd.read_csv(file_upload)
161
- # elif file_upload.name.endswith('.geojson'):
162
- # locations_df = gpd.read_file(file_upload)
163
- # elif file_upload.name.endswith('.kml'):
164
- # locations_df = gpd.read_file(file_upload)
165
- # else:
166
- # st.error("Unsupported file format. Please upload CSV, GeoJSON, or KML.")
167
- # locations_df = pd.DataFrame()
168
-
169
- # # # Check if the file contains polygons when the user selected "Point"
170
- # # if 'geometry' in locations_df.columns:
171
- # # # Check if the geometry type is Polygon or MultiPolygon
172
- # # if locations_df.geometry.geom_type.isin(['Polygon', 'MultiPolygon']).any():
173
- # # st.warning("The uploaded file contains polygon data. Please select 'Polygon' for processing.")
174
- # # st.stop() # Stop further processing if polygons are detected
175
-
176
- # # Processing the point data
177
- # with st.spinner('Processing data...'):
178
- # if locations_df is not None and not locations_df.empty:
179
- # # For GeoJSON data, the coordinates are in the geometry column
180
- # if 'geometry' in locations_df.columns:
181
- # # Extract latitude and longitude from the geometry column
182
- # locations_df['latitude'] = locations_df['geometry'].y
183
- # locations_df['longitude'] = locations_df['geometry'].x
184
-
185
- # # Ensure the necessary columns exist in the dataframe
186
- # if 'latitude' not in locations_df.columns or 'longitude' not in locations_df.columns:
187
- # st.error("Uploaded file is missing required 'latitude' or 'longitude' columns.")
188
- # else:
189
- # # Display a preview of the points data
190
- # st.write("Preview of the uploaded points data:")
191
- # st.dataframe(locations_df.head())
192
-
193
- # # Create a LeafMap object to display the points
194
- # m = leafmap.Map(center=[locations_df['latitude'].mean(), locations_df['longitude'].mean()], zoom=10)
195
-
196
- # # Add points to the map using a loop
197
- # for _, row in locations_df.iterrows():
198
- # latitude = row['latitude']
199
- # longitude = row['longitude']
200
 
201
- # # Check if latitude or longitude are NaN and skip if they are
202
- # if pd.isna(latitude) or pd.isna(longitude):
203
- # continue # Skip this row and move to the next one
204
 
205
- # m.add_marker(location=[latitude, longitude], popup=row.get('name', 'No Name'))
206
-
207
- # # Display map
208
- # st.write("Map of Uploaded Points:")
209
- # m.to_streamlit()
210
-
211
- # # Store the map in session_state
212
- # st.session_state.map_data = m
213
-
214
- # elif shape_type.lower() == "polygon":
215
- # # Handle different file types for Polygon data:
216
- # if file_upload.name.endswith('.csv'):
217
- # locations_df = pd.read_csv(file_upload)
218
- # elif file_upload.name.endswith('.geojson'):
219
- # locations_df = gpd.read_file(file_upload)
220
- # elif file_upload.name.endswith('.kml'):
221
- # locations_df = gpd.read_file(file_upload)
222
- # else:
223
- # st.error("Unsupported file format. Please upload CSV, GeoJSON, or KML.")
224
- # locations_df = pd.DataFrame()
225
-
226
- # # # Check if the file contains points when the user selected "Polygon"
227
- # # if 'geometry' in locations_df.columns:
228
- # # # Check if the geometry type is Point or MultiPoint
229
- # # if locations_df.geometry.geom_type.isin(['Point', 'MultiPoint']).any():
230
- # # st.warning("The uploaded file contains point data. Please select 'Point' for processing.")
231
- # # st.stop() # Stop further processing if point data is detected
232
-
233
- # # Processing the polygon data
234
- # with st.spinner('Processing data...'):
235
- # if locations_df is not None and not locations_df.empty:
236
- # # Ensure the 'geometry' column exists in the dataframe
237
- # if 'geometry' not in locations_df.columns:
238
- # st.error("Uploaded file is missing required 'geometry' column.")
239
- # else:
240
- # # Display a preview of the polygons data
241
- # st.write("Preview of the uploaded polygons data:")
242
- # st.dataframe(locations_df.head())
243
-
244
- # # Create a LeafMap object to display the polygons
245
- # # Calculate the centroid of the polygons for the map center
246
- # centroid_lat = locations_df.geometry.centroid.y.mean()
247
- # centroid_lon = locations_df.geometry.centroid.x.mean()
248
-
249
- # m = leafmap.Map(center=[centroid_lat, centroid_lon], zoom=10)
250
-
251
- # # Add polygons to the map using a loop
252
- # for _, row in locations_df.iterrows():
253
- # polygon = row['geometry']
254
- # if polygon.is_valid: # Check if polygon is valid
255
- # # Create a GeoDataFrame for this polygon
256
- # gdf = gpd.GeoDataFrame([row], geometry=[polygon], crs=locations_df.crs)
257
- # m.add_gdf(gdf=gdf, layer_name=row.get('name', 'Unnamed Polygon'))
258
-
259
- # # Display map
260
- # st.write("Map of Uploaded Polygons:")
261
- # m.to_streamlit()
262
-
263
- # # Store the map in session_state
264
- # st.session_state.map_data = m
265
 
266
 
267
  # Date Input for Start and End Dates
@@ -593,16 +593,6 @@ if st.button(f"Calculate ({index_choice})"):
593
  if file_upload is not None:
594
  # Read the user-uploaded file
595
  if shape_type.lower() == "point":
596
- if file_upload.name.endswith('.csv'):
597
- locations_df = read_csv(file_upload)
598
- elif file_upload.name.endswith('.geojson'):
599
- locations_df = read_geojson(file_upload)
600
- elif file_upload.name.endswith('.kml'):
601
- locations_df = read_kml(file_upload)
602
- else:
603
- st.error("Unsupported file format. Please upload CSV, GeoJSON, or KML.")
604
- locations_df = pd.DataFrame()
605
-
606
  # Process results for the selected aggregation period
607
  results = process_aggregation(
608
  locations_df,
@@ -638,11 +628,6 @@ if st.button(f"Calculate ({index_choice})"):
638
  st.warning("No results were generated.")
639
 
640
  elif shape_type.lower() == "polygon":
641
- if file_upload.name.endswith('.geojson'):
642
- locations_df = read_geojson(file_upload)
643
- else:
644
- st.error("Please upload a valid GeoJSON file for polygons.")
645
-
646
  # Process results for the selected aggregation period
647
  results = process_aggregation(
648
  locations_df,
 
152
  # Ask user to upload a file based on shape type (case-insensitive)
153
  file_upload = st.file_uploader(f"Upload your {shape_type} data (CSV, GeoJSON, KML)", type=["csv", "geojson", "kml"])
154
 
155
+ if file_upload is not None:
156
+ # Read the user-uploaded file
157
+ if shape_type.lower() == "point":
158
+ # Handle different file types for Point data
159
+ if file_upload.name.endswith('.csv'):
160
+ locations_df = pd.read_csv(file_upload)
161
+ elif file_upload.name.endswith('.geojson'):
162
+ locations_df = gpd.read_file(file_upload)
163
+ elif file_upload.name.endswith('.kml'):
164
+ locations_df = gpd.read_file(file_upload)
165
+ else:
166
+ st.error("Unsupported file format. Please upload CSV, GeoJSON, or KML.")
167
+ locations_df = pd.DataFrame()
168
+
169
+ # Check if the file contains polygons when the user selected "Point"
170
+ if 'geometry' in locations_df.columns:
171
+ # Check if the geometry type is Polygon or MultiPolygon
172
+ if locations_df.geometry.geom_type.isin(['Polygon', 'MultiPolygon']).any():
173
+ st.warning("The uploaded file contains polygon data. Please select 'Polygon' for processing.")
174
+ st.stop() # Stop further processing if polygons are detected
175
+
176
+ # Processing the point data
177
+ with st.spinner('Processing data...'):
178
+ if locations_df is not None and not locations_df.empty:
179
+ # For GeoJSON data, the coordinates are in the geometry column
180
+ if 'geometry' in locations_df.columns:
181
+ # Extract latitude and longitude from the geometry column
182
+ locations_df['latitude'] = locations_df['geometry'].y
183
+ locations_df['longitude'] = locations_df['geometry'].x
184
+
185
+ # Ensure the necessary columns exist in the dataframe
186
+ if 'latitude' not in locations_df.columns or 'longitude' not in locations_df.columns:
187
+ st.error("Uploaded file is missing required 'latitude' or 'longitude' columns.")
188
+ else:
189
+ # Display a preview of the points data
190
+ st.write("Preview of the uploaded points data:")
191
+ st.dataframe(locations_df.head())
192
+
193
+ # Create a LeafMap object to display the points
194
+ m = leafmap.Map(center=[locations_df['latitude'].mean(), locations_df['longitude'].mean()], zoom=10)
195
+
196
+ # Add points to the map using a loop
197
+ for _, row in locations_df.iterrows():
198
+ latitude = row['latitude']
199
+ longitude = row['longitude']
200
 
201
+ # Check if latitude or longitude are NaN and skip if they are
202
+ if pd.isna(latitude) or pd.isna(longitude):
203
+ continue # Skip this row and move to the next one
204
 
205
+ m.add_marker(location=[latitude, longitude], popup=row.get('name', 'No Name'))
206
+
207
+ # Display map
208
+ st.write("Map of Uploaded Points:")
209
+ m.to_streamlit()
210
+
211
+ # Store the map in session_state
212
+ st.session_state.map_data = m
213
+
214
+ elif shape_type.lower() == "polygon":
215
+ # Handle different file types for Polygon data:
216
+ if file_upload.name.endswith('.csv'):
217
+ locations_df = pd.read_csv(file_upload)
218
+ elif file_upload.name.endswith('.geojson'):
219
+ locations_df = gpd.read_file(file_upload)
220
+ elif file_upload.name.endswith('.kml'):
221
+ locations_df = gpd.read_file(file_upload)
222
+ else:
223
+ st.error("Unsupported file format. Please upload CSV, GeoJSON, or KML.")
224
+ locations_df = pd.DataFrame()
225
+
226
+ # Check if the file contains points when the user selected "Polygon"
227
+ if 'geometry' in locations_df.columns:
228
+ # Check if the geometry type is Point or MultiPoint
229
+ if locations_df.geometry.geom_type.isin(['Point', 'MultiPoint']).any():
230
+ st.warning("The uploaded file contains point data. Please select 'Point' for processing.")
231
+ st.stop() # Stop further processing if point data is detected
232
+
233
+ # Processing the polygon data
234
+ with st.spinner('Processing data...'):
235
+ if locations_df is not None and not locations_df.empty:
236
+ # Ensure the 'geometry' column exists in the dataframe
237
+ if 'geometry' not in locations_df.columns:
238
+ st.error("Uploaded file is missing required 'geometry' column.")
239
+ else:
240
+ # Display a preview of the polygons data
241
+ st.write("Preview of the uploaded polygons data:")
242
+ st.dataframe(locations_df.head())
243
+
244
+ # Create a LeafMap object to display the polygons
245
+ # Calculate the centroid of the polygons for the map center
246
+ centroid_lat = locations_df.geometry.centroid.y.mean()
247
+ centroid_lon = locations_df.geometry.centroid.x.mean()
248
+
249
+ m = leafmap.Map(center=[centroid_lat, centroid_lon], zoom=10)
250
+
251
+ # Add polygons to the map using a loop
252
+ for _, row in locations_df.iterrows():
253
+ polygon = row['geometry']
254
+ if polygon.is_valid: # Check if polygon is valid
255
+ # Create a GeoDataFrame for this polygon
256
+ gdf = gpd.GeoDataFrame([row], geometry=[polygon], crs=locations_df.crs)
257
+ m.add_gdf(gdf=gdf, layer_name=row.get('name', 'Unnamed Polygon'))
258
+
259
+ # Display map
260
+ st.write("Map of Uploaded Polygons:")
261
+ m.to_streamlit()
262
+
263
+ # Store the map in session_state
264
+ st.session_state.map_data = m
265
 
266
 
267
  # Date Input for Start and End Dates
 
593
  if file_upload is not None:
594
  # Read the user-uploaded file
595
  if shape_type.lower() == "point":
 
 
 
 
 
 
 
 
 
 
596
  # Process results for the selected aggregation period
597
  results = process_aggregation(
598
  locations_df,
 
628
  st.warning("No results were generated.")
629
 
630
  elif shape_type.lower() == "polygon":
 
 
 
 
 
631
  # Process results for the selected aggregation period
632
  results = process_aggregation(
633
  locations_df,