Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -348,12 +348,19 @@ def extract_info(template, text):
|
|
348 |
return f"❌ Fehler: {str(e)}", "{}"
|
349 |
@spaces.GPU
|
350 |
def create_map(df, location_col):
|
|
|
|
|
|
|
|
|
|
|
|
|
351 |
m = folium.Map(
|
352 |
location=[20, 0],
|
353 |
zoom_start=2,
|
354 |
control_scale=True
|
355 |
)
|
356 |
|
|
|
357 |
folium.TileLayer(
|
358 |
tiles=MAP_TILES["GreenMap"]["url"],
|
359 |
attr=MAP_TILES["GreenMap"]["attr"],
|
@@ -365,32 +372,63 @@ def create_map(df, location_col):
|
|
365 |
Fullscreen().add_to(m)
|
366 |
MeasureControl(position='topright', primary_length_unit='kilometers').add_to(m)
|
367 |
|
|
|
368 |
geocoder = SafeGeocoder()
|
369 |
coords = []
|
370 |
marker_cluster = MarkerCluster(name="Locations").add_to(m)
|
371 |
processed_count = 0
|
372 |
|
|
|
|
|
|
|
|
|
|
|
373 |
for idx, row in df.iterrows():
|
|
|
|
|
|
|
|
|
374 |
if pd.isna(row[location_col]):
|
|
|
|
|
375 |
continue
|
376 |
|
377 |
location = str(row[location_col]).strip()
|
|
|
|
|
378 |
|
|
|
379 |
additional_info = ""
|
380 |
for col in df.columns:
|
381 |
if col != location_col and not pd.isna(row[col]):
|
382 |
additional_info += f"<br><b>{col}:</b> {row[col]}"
|
383 |
|
|
|
384 |
try:
|
385 |
locations = [loc.strip() for loc in location.split(',') if loc.strip()]
|
386 |
if not locations:
|
387 |
locations = [location]
|
388 |
-
except:
|
|
|
|
|
389 |
locations = [location]
|
390 |
|
|
|
|
|
|
|
|
|
391 |
for loc in locations:
|
|
|
|
|
|
|
|
|
392 |
point = geocoder.get_coords(loc)
|
|
|
|
|
|
|
|
|
393 |
if point:
|
|
|
394 |
popup_content = f"""
|
395 |
<div style="min-width: 200px; max-width: 300px">
|
396 |
<h4 style="font-family: 'Source Sans Pro', sans-serif; margin-bottom: 5px;">{loc}</h4>
|
@@ -400,19 +438,57 @@ def create_map(df, location_col):
|
|
400 |
</div>
|
401 |
"""
|
402 |
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
tooltip=loc,
|
407 |
-
icon=folium.Icon(color="blue", icon="info-sign")
|
408 |
-
).add_to(marker_cluster)
|
409 |
|
410 |
-
|
411 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
412 |
|
|
|
413 |
if coords:
|
414 |
m.fit_bounds(coords)
|
415 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
416 |
custom_css = """
|
417 |
<style>
|
418 |
@import url('https://fonts.googleapis.com/css2?family=Source+Sans+Pro:wght@400;600&display=swap');
|
|
|
348 |
return f"❌ Fehler: {str(e)}", "{}"
|
349 |
@spaces.GPU
|
350 |
def create_map(df, location_col):
|
351 |
+
# Create a log file to track what's happening
|
352 |
+
with open("map_debug.log", "w") as log_file:
|
353 |
+
log_file.write(f"Starting map creation at {time.strftime('%Y-%m-%d %H:%M:%S')}\n")
|
354 |
+
log_file.write(f"Data shape: {df.shape}, Location column: {location_col}\n")
|
355 |
+
|
356 |
+
# Create the map
|
357 |
m = folium.Map(
|
358 |
location=[20, 0],
|
359 |
zoom_start=2,
|
360 |
control_scale=True
|
361 |
)
|
362 |
|
363 |
+
# Add base map and controls
|
364 |
folium.TileLayer(
|
365 |
tiles=MAP_TILES["GreenMap"]["url"],
|
366 |
attr=MAP_TILES["GreenMap"]["attr"],
|
|
|
372 |
Fullscreen().add_to(m)
|
373 |
MeasureControl(position='topright', primary_length_unit='kilometers').add_to(m)
|
374 |
|
375 |
+
# Setup for geocoding and markers
|
376 |
geocoder = SafeGeocoder()
|
377 |
coords = []
|
378 |
marker_cluster = MarkerCluster(name="Locations").add_to(m)
|
379 |
processed_count = 0
|
380 |
|
381 |
+
# Log marker creation process
|
382 |
+
with open("map_debug.log", "a") as log_file:
|
383 |
+
log_file.write("\nStarting to process locations...\n")
|
384 |
+
|
385 |
+
# Process each row in dataframe
|
386 |
for idx, row in df.iterrows():
|
387 |
+
# Log the current row we're processing
|
388 |
+
with open("map_debug.log", "a") as log_file:
|
389 |
+
log_file.write(f"\nProcessing row {idx}\n")
|
390 |
+
|
391 |
if pd.isna(row[location_col]):
|
392 |
+
with open("map_debug.log", "a") as log_file:
|
393 |
+
log_file.write(f" - Empty location, skipping\n")
|
394 |
continue
|
395 |
|
396 |
location = str(row[location_col]).strip()
|
397 |
+
with open("map_debug.log", "a") as log_file:
|
398 |
+
log_file.write(f" - Location: {location}\n")
|
399 |
|
400 |
+
# Build additional info string for the popup
|
401 |
additional_info = ""
|
402 |
for col in df.columns:
|
403 |
if col != location_col and not pd.isna(row[col]):
|
404 |
additional_info += f"<br><b>{col}:</b> {row[col]}"
|
405 |
|
406 |
+
# Split location if it contains multiple comma-separated places
|
407 |
try:
|
408 |
locations = [loc.strip() for loc in location.split(',') if loc.strip()]
|
409 |
if not locations:
|
410 |
locations = [location]
|
411 |
+
except Exception as e:
|
412 |
+
with open("map_debug.log", "a") as log_file:
|
413 |
+
log_file.write(f" - Error splitting location: {str(e)}\n")
|
414 |
locations = [location]
|
415 |
|
416 |
+
with open("map_debug.log", "a") as log_file:
|
417 |
+
log_file.write(f" - Parsed locations: {locations}\n")
|
418 |
+
|
419 |
+
# Process each individual location
|
420 |
for loc in locations:
|
421 |
+
with open("map_debug.log", "a") as log_file:
|
422 |
+
log_file.write(f" - Processing: {loc}\n")
|
423 |
+
|
424 |
+
# Get coordinates from geocoder
|
425 |
point = geocoder.get_coords(loc)
|
426 |
+
|
427 |
+
with open("map_debug.log", "a") as log_file:
|
428 |
+
log_file.write(f" - Coordinates: {point}\n")
|
429 |
+
|
430 |
if point:
|
431 |
+
# Create popup content - this is the part that was disappearing
|
432 |
popup_content = f"""
|
433 |
<div style="min-width: 200px; max-width: 300px">
|
434 |
<h4 style="font-family: 'Source Sans Pro', sans-serif; margin-bottom: 5px;">{loc}</h4>
|
|
|
438 |
</div>
|
439 |
"""
|
440 |
|
441 |
+
# Log the popup content to see exactly what's being created
|
442 |
+
with open("map_debug.log", "a") as log_file:
|
443 |
+
log_file.write(f" - Created popup content for {loc}\n")
|
|
|
|
|
|
|
444 |
|
445 |
+
# Add the marker to the map
|
446 |
+
try:
|
447 |
+
folium.Marker(
|
448 |
+
location=point,
|
449 |
+
popup=folium.Popup(popup_content, max_width=300),
|
450 |
+
tooltip=loc,
|
451 |
+
icon=folium.Icon(color="blue", icon="info-sign")
|
452 |
+
).add_to(marker_cluster)
|
453 |
+
|
454 |
+
coords.append(point)
|
455 |
+
processed_count += 1
|
456 |
+
|
457 |
+
with open("map_debug.log", "a") as log_file:
|
458 |
+
log_file.write(f" - Added marker for {loc}\n")
|
459 |
+
except Exception as e:
|
460 |
+
with open("map_debug.log", "a") as log_file:
|
461 |
+
log_file.write(f" - Error adding marker: {str(e)}\n")
|
462 |
|
463 |
+
# Fit map bounds if we have coordinates
|
464 |
if coords:
|
465 |
m.fit_bounds(coords)
|
466 |
|
467 |
+
# Log completion information
|
468 |
+
with open("map_debug.log", "a") as log_file:
|
469 |
+
log_file.write(f"\nMap creation completed at {time.strftime('%Y-%m-%d %H:%M:%S')}\n")
|
470 |
+
log_file.write(f"Processed {processed_count} locations\n")
|
471 |
+
|
472 |
+
# Add custom CSS for better display
|
473 |
+
custom_css = """
|
474 |
+
<style>
|
475 |
+
@import url('https://fonts.googleapis.com/css2?family=Source+Sans+Pro:wght@400;600&display=swap');
|
476 |
+
.leaflet-container {
|
477 |
+
font-family: 'Source Sans Pro', sans-serif;
|
478 |
+
}
|
479 |
+
.leaflet-popup-content {
|
480 |
+
font-family: 'Source Sans Pro', sans-serif;
|
481 |
+
}
|
482 |
+
.leaflet-popup-content h4 {
|
483 |
+
font-weight: 600;
|
484 |
+
margin-bottom: 8px;
|
485 |
+
}
|
486 |
+
</style>
|
487 |
+
"""
|
488 |
+
m.get_root().header.add_child(folium.Element(custom_css))
|
489 |
+
|
490 |
+
return m._repr_html_(), processed_count
|
491 |
+
|
492 |
custom_css = """
|
493 |
<style>
|
494 |
@import url('https://fonts.googleapis.com/css2?family=Source+Sans+Pro:wght@400;600&display=swap');
|