Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -410,145 +410,8 @@ def create_map(df, location_col):
|
|
410 |
|
411 |
return m._repr_html_(), processed_count
|
412 |
|
413 |
-
|
414 |
-
|
415 |
-
return None, "No file uploaded", None
|
416 |
-
|
417 |
-
try:
|
418 |
-
if hasattr(file, 'name'):
|
419 |
-
df = pd.read_excel(file.name)
|
420 |
-
elif isinstance(file, bytes):
|
421 |
-
df = pd.read_excel(io.BytesIO(file))
|
422 |
-
else:
|
423 |
-
df = pd.read_excel(file)
|
424 |
-
|
425 |
-
print(f"Spalten in der Excel-Tabelle: {list(df.columns)}")
|
426 |
-
|
427 |
-
if places_column not in df.columns:
|
428 |
-
return None, f"Spalte '{places_column}' wurde in der Excel-Datei nicht gefunden. Verfügbare Spalten: {', '.join(df.columns)}", None
|
429 |
-
|
430 |
-
# Create a copy of the dataframe to avoid modifying the original
|
431 |
-
result_df = df.copy()
|
432 |
-
|
433 |
-
# Add coordinate columns if they don't exist
|
434 |
-
if 'latitude' not in result_df.columns:
|
435 |
-
result_df['latitude'] = None
|
436 |
-
if 'longitude' not in result_df.columns:
|
437 |
-
result_df['longitude'] = None
|
438 |
-
|
439 |
-
geocoder = SafeGeocoder()
|
440 |
-
coords = []
|
441 |
-
marker_cluster = MarkerCluster(name="Locations")
|
442 |
-
processed_count = 0
|
443 |
-
|
444 |
-
# Create map instance
|
445 |
-
m = folium.Map(
|
446 |
-
location=[20, 0],
|
447 |
-
zoom_start=2,
|
448 |
-
control_scale=True
|
449 |
-
)
|
450 |
-
|
451 |
-
folium.TileLayer(
|
452 |
-
tiles=MAP_TILES["GreenMap"]["url"],
|
453 |
-
attr=MAP_TILES["GreenMap"]["attr"],
|
454 |
-
name="GreenMap",
|
455 |
-
overlay=False,
|
456 |
-
control=False
|
457 |
-
).add_to(m)
|
458 |
-
|
459 |
-
Fullscreen().add_to(m)
|
460 |
-
MeasureControl(position='topright', primary_length_unit='kilometers').add_to(m)
|
461 |
-
marker_cluster.add_to(m)
|
462 |
-
|
463 |
-
# Process each location and store coordinates
|
464 |
-
for idx, row in result_df.iterrows():
|
465 |
-
if pd.isna(row[places_column]):
|
466 |
-
continue
|
467 |
-
|
468 |
-
location = str(row[places_column]).strip()
|
469 |
-
|
470 |
-
additional_info = ""
|
471 |
-
for col in result_df.columns:
|
472 |
-
if col != places_column and not pd.isna(row[col]):
|
473 |
-
additional_info += f"<br><b>{col}:</b> {row[col]}"
|
474 |
-
|
475 |
-
try:
|
476 |
-
locations = [loc.strip() for loc in location.split(',') if loc.strip()]
|
477 |
-
if not locations:
|
478 |
-
locations = [location]
|
479 |
-
except:
|
480 |
-
locations = [location]
|
481 |
-
|
482 |
-
for loc in locations:
|
483 |
-
point = geocoder.get_coords(loc)
|
484 |
-
if point:
|
485 |
-
# Store coordinates in the dataframe
|
486 |
-
result_df.at[idx, 'latitude'] = point[0]
|
487 |
-
result_df.at[idx, 'longitude'] = point[1]
|
488 |
-
|
489 |
-
# Add marker to map
|
490 |
-
popup_content = f"""
|
491 |
-
<div style="min-width: 200px; max-width: 300px">
|
492 |
-
<h4 style="font-family: 'Source Sans Pro', sans-serif; margin-bottom: 5px;">{loc}</h4>
|
493 |
-
<div style="font-family: 'Source Sans Pro', sans-serif; font-size: 14px;">
|
494 |
-
{additional_info}
|
495 |
-
</div>
|
496 |
-
</div>
|
497 |
-
"""
|
498 |
-
|
499 |
-
folium.Marker(
|
500 |
-
location=point,
|
501 |
-
popup=folium.Popup(popup_content, max_width=300),
|
502 |
-
tooltip=loc,
|
503 |
-
icon=folium.Icon(color="blue", icon="info-sign")
|
504 |
-
).add_to(marker_cluster)
|
505 |
-
|
506 |
-
coords.append(point)
|
507 |
-
processed_count += 1
|
508 |
-
break # Use first successful geocode
|
509 |
-
|
510 |
-
# Fit map to coordinates if any were found
|
511 |
-
if coords:
|
512 |
-
m.fit_bounds(coords)
|
513 |
-
|
514 |
-
# Add custom CSS to map
|
515 |
-
custom_css = """
|
516 |
-
<style>
|
517 |
-
@import url('https://fonts.googleapis.com/css2?family=Source+Sans+Pro:wght@400;600&display=swap');
|
518 |
-
.leaflet-container {
|
519 |
-
font-family: 'Source Sans Pro', sans-serif;
|
520 |
-
}
|
521 |
-
.leaflet-popup-content {
|
522 |
-
font-family: 'Source Sans Pro', sans-serif;
|
523 |
-
}
|
524 |
-
.leaflet-popup-content h4 {
|
525 |
-
font-weight: 600;
|
526 |
-
margin-bottom: 8px;
|
527 |
-
}
|
528 |
-
</style>
|
529 |
-
"""
|
530 |
-
m.get_root().header.add_child(folium.Element(custom_css))
|
531 |
-
|
532 |
-
# Save the updated dataframe to Excel
|
533 |
-
with tempfile.NamedTemporaryFile(suffix=".xlsx", delete=False) as tmp:
|
534 |
-
processed_path = tmp.name
|
535 |
-
result_df.to_excel(processed_path, index=False)
|
536 |
-
|
537 |
-
total_locations = result_df[places_column].count()
|
538 |
-
success_rate = (processed_count / total_locations * 100) if total_locations > 0 else 0
|
539 |
-
|
540 |
-
stats = f"Gefunden: {processed_count} von {total_locations} Orten ({success_rate:.1f}%)"
|
541 |
-
|
542 |
-
# Print the dataframe to debug
|
543 |
-
print("DataFrame with coordinates:")
|
544 |
-
print(result_df.head())
|
545 |
-
|
546 |
-
return m._repr_html_(), stats, processed_path
|
547 |
-
except Exception as e:
|
548 |
-
import traceback
|
549 |
-
trace = traceback.format_exc()
|
550 |
-
print(f"Error processing file: {e}\n{trace}")
|
551 |
-
return None, f"Fehler bei der Verarbeitung der Datei: {str(e)}", None
|
552 |
|
553 |
custom_css = """
|
554 |
<style>
|
|
|
410 |
|
411 |
return m._repr_html_(), processed_count
|
412 |
|
413 |
+
|
414 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
415 |
|
416 |
custom_css = """
|
417 |
<style>
|