oberbics commited on
Commit
f1455ac
·
verified ·
1 Parent(s): 3f5ec44

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -4
app.py CHANGED
@@ -346,18 +346,123 @@ def process_excel(file, places_column):
346
  if places_column not in df.columns:
347
  return None, f"Spalte '{places_column}' wurde in der Excel-Datei nicht gefunden. Verfügbare Spalten: {', '.join(df.columns)}", None
348
 
349
- map_html, processed_count = create_map(df, places_column)
 
350
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
351
  with tempfile.NamedTemporaryFile(suffix=".xlsx", delete=False) as tmp:
352
  processed_path = tmp.name
353
- df.to_excel(processed_path, index=False)
354
 
355
- total_locations = df[places_column].count()
356
  success_rate = (processed_count / total_locations * 100) if total_locations > 0 else 0
357
 
358
  stats = f"Gefunden: {processed_count} von {total_locations} Orten ({success_rate:.1f}%)"
359
 
360
- return map_html, stats, processed_path
 
 
 
 
361
  except Exception as e:
362
  import traceback
363
  trace = traceback.format_exc()
 
346
  if places_column not in df.columns:
347
  return None, f"Spalte '{places_column}' wurde in der Excel-Datei nicht gefunden. Verfügbare Spalten: {', '.join(df.columns)}", None
348
 
349
+ # Create a copy of the dataframe to avoid modifying the original
350
+ result_df = df.copy()
351
 
352
+ # Add coordinate columns if they don't exist
353
+ if 'latitude' not in result_df.columns:
354
+ result_df['latitude'] = None
355
+ if 'longitude' not in result_df.columns:
356
+ result_df['longitude'] = None
357
+
358
+ geocoder = SafeGeocoder()
359
+ coords = []
360
+ marker_cluster = MarkerCluster(name="Locations")
361
+ processed_count = 0
362
+
363
+ # Create map instance
364
+ m = folium.Map(
365
+ location=[20, 0],
366
+ zoom_start=2,
367
+ control_scale=True
368
+ )
369
+
370
+ folium.TileLayer(
371
+ tiles=MAP_TILES["GreenMap"]["url"],
372
+ attr=MAP_TILES["GreenMap"]["attr"],
373
+ name="GreenMap",
374
+ overlay=False,
375
+ control=False
376
+ ).add_to(m)
377
+
378
+ Fullscreen().add_to(m)
379
+ MeasureControl(position='topright', primary_length_unit='kilometers').add_to(m)
380
+ marker_cluster.add_to(m)
381
+
382
+ # Process each location and store coordinates
383
+ for idx, row in result_df.iterrows():
384
+ if pd.isna(row[places_column]):
385
+ continue
386
+
387
+ location = str(row[places_column]).strip()
388
+
389
+ additional_info = ""
390
+ for col in result_df.columns:
391
+ if col != places_column and not pd.isna(row[col]):
392
+ additional_info += f"<br><b>{col}:</b> {row[col]}"
393
+
394
+ try:
395
+ locations = [loc.strip() for loc in location.split(',') if loc.strip()]
396
+ if not locations:
397
+ locations = [location]
398
+ except:
399
+ locations = [location]
400
+
401
+ for loc in locations:
402
+ point = geocoder.get_coords(loc)
403
+ if point:
404
+ # Store coordinates in the dataframe
405
+ result_df.at[idx, 'latitude'] = point[0]
406
+ result_df.at[idx, 'longitude'] = point[1]
407
+
408
+ # Add marker to map
409
+ popup_content = f"""
410
+ <div style="min-width: 200px; max-width: 300px">
411
+ <h4 style="font-family: 'Source Sans Pro', sans-serif; margin-bottom: 5px;">{loc}</h4>
412
+ <div style="font-family: 'Source Sans Pro', sans-serif; font-size: 14px;">
413
+ {additional_info}
414
+ </div>
415
+ </div>
416
+ """
417
+
418
+ folium.Marker(
419
+ location=point,
420
+ popup=folium.Popup(popup_content, max_width=300),
421
+ tooltip=loc,
422
+ icon=folium.Icon(color="blue", icon="info-sign")
423
+ ).add_to(marker_cluster)
424
+
425
+ coords.append(point)
426
+ processed_count += 1
427
+ break # Use first successful geocode
428
+
429
+ # Fit map to coordinates if any were found
430
+ if coords:
431
+ m.fit_bounds(coords)
432
+
433
+ # Add custom CSS to map
434
+ custom_css = """
435
+ <style>
436
+ @import url('https://fonts.googleapis.com/css2?family=Source+Sans+Pro:wght@400;600&display=swap');
437
+ .leaflet-container {
438
+ font-family: 'Source Sans Pro', sans-serif;
439
+ }
440
+ .leaflet-popup-content {
441
+ font-family: 'Source Sans Pro', sans-serif;
442
+ }
443
+ .leaflet-popup-content h4 {
444
+ font-weight: 600;
445
+ margin-bottom: 8px;
446
+ }
447
+ </style>
448
+ """
449
+ m.get_root().header.add_child(folium.Element(custom_css))
450
+
451
+ # Save the updated dataframe to Excel
452
  with tempfile.NamedTemporaryFile(suffix=".xlsx", delete=False) as tmp:
453
  processed_path = tmp.name
454
+ result_df.to_excel(processed_path, index=False)
455
 
456
+ total_locations = result_df[places_column].count()
457
  success_rate = (processed_count / total_locations * 100) if total_locations > 0 else 0
458
 
459
  stats = f"Gefunden: {processed_count} von {total_locations} Orten ({success_rate:.1f}%)"
460
 
461
+ # Print the dataframe to debug
462
+ print("DataFrame with coordinates:")
463
+ print(result_df.head())
464
+
465
+ return m._repr_html_(), stats, processed_path
466
  except Exception as e:
467
  import traceback
468
  trace = traceback.format_exc()