oberbics commited on
Commit
b6a1665
·
verified ·
1 Parent(s): 44e10c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -18
app.py CHANGED
@@ -18,9 +18,17 @@ warnings.filterwarnings("ignore")
18
 
19
  # Map Tile Providers with reliable sources
20
  MAP_TILES = {
21
- "Toner": {
22
- "url": "https://tiles.stadiamaps.com/tiles/stamen_toner/{z}/{x}/{y}.png",
23
- "attr": "Stadia Maps"
 
 
 
 
 
 
 
 
24
  }
25
  }
26
 
@@ -119,18 +127,28 @@ def extract_info(template, text):
119
  return f"❌ Error: {str(e)}", "{}"
120
 
121
  def create_map(df, location_col):
122
- # Initialize map with Toner style
123
  m = folium.Map(location=[20, 0], zoom_start=2, control_scale=True)
124
 
125
- # Add the single tile layer without controls
126
  folium.TileLayer(
127
- tiles=MAP_TILES["Toner"]["url"],
128
- attr=MAP_TILES["Toner"]["attr"],
129
- name="Toner",
130
  overlay=False,
131
  control=False
132
  ).add_to(m)
133
 
 
 
 
 
 
 
 
 
 
 
134
  # Add plugins
135
  Fullscreen().add_to(m)
136
  MeasureControl(position='topright', primary_length_unit='kilometers').add_to(m)
@@ -188,16 +206,49 @@ def create_map(df, location_col):
188
  if coords:
189
  m.fit_bounds(coords)
190
 
191
- # Add custom font CSS
192
- custom_css = """
193
- <style>
194
- @import url('https://fonts.googleapis.com/css2?family=Source+Sans+Pro:wght@400;600&display=swap');
195
- .leaflet-container {
196
- font-family: 'Source Sans Pro', sans-serif;
197
- }
198
- </style>
199
- """
200
- m.get_root().header.add_child(folium.Element(custom_css))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
201
 
202
  return m._repr_html_(), processed_count
203
 
 
18
 
19
  # Map Tile Providers with reliable sources
20
  MAP_TILES = {
21
+ "Terrain": {
22
+ "url": "https://server.arcgisonline.com/ArcGIS/rest/services/World_Terrain_Base/MapServer/tile/{z}/{y}/{x}",
23
+ "attr": "Esri"
24
+ },
25
+ "OpenTopoMap": {
26
+ "url": "https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png",
27
+ "attr": "OpenTopoMap"
28
+ },
29
+ "OpenStreetMap": {
30
+ "url": "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
31
+ "attr": "OpenStreetMap contributors"
32
  }
33
  }
34
 
 
127
  return f"❌ Error: {str(e)}", "{}"
128
 
129
  def create_map(df, location_col):
130
+ # Initialize map with Terrain style
131
  m = folium.Map(location=[20, 0], zoom_start=2, control_scale=True)
132
 
133
+ # Add the terrain tile layer
134
  folium.TileLayer(
135
+ tiles=MAP_TILES["Terrain"]["url"],
136
+ attr=MAP_TILES["Terrain"]["attr"],
137
+ name="Terrain",
138
  overlay=False,
139
  control=False
140
  ).add_to(m)
141
 
142
+ # Add OpenTopoMap as backup in case Esri terrain fails
143
+ folium.TileLayer(
144
+ tiles=MAP_TILES["OpenTopoMap"]["url"],
145
+ attr=MAP_TILES["OpenTopoMap"]["attr"],
146
+ name="OpenTopoMap",
147
+ overlay=False,
148
+ control=False,
149
+ show=False
150
+ ).add_to(m)
151
+
152
  # Add plugins
153
  Fullscreen().add_to(m)
154
  MeasureControl(position='topright', primary_length_unit='kilometers').add_to(m)
 
206
  if coords:
207
  m.fit_bounds(coords)
208
 
209
+ # Add better tile error handling with JavaScript
210
+ m.get_root().html.add_child(folium.Element("""
211
+ <script>
212
+ // Wait for the map to be fully loaded
213
+ document.addEventListener('DOMContentLoaded', function() {
214
+ setTimeout(function() {
215
+ // Get the map instance
216
+ var maps = document.querySelectorAll('.leaflet-container');
217
+ if (maps.length > 0) {
218
+ var map = maps[0];
219
+
220
+ // Add error handler for tiles
221
+ var layers = map.querySelectorAll('.leaflet-tile-pane .leaflet-layer');
222
+ var mainLayerLoaded = false;
223
+
224
+ for (var i = 0; i < layers.length; i++) {
225
+ var layer = layers[i];
226
+ var tiles = layer.querySelectorAll('.leaflet-tile');
227
+ var loadedTiles = layer.querySelectorAll('.leaflet-tile-loaded');
228
+
229
+ // Check if primary layer has loaded tiles
230
+ if (i === 0 && tiles.length > 0 && loadedTiles.length > 0) {
231
+ mainLayerLoaded = true;
232
+ }
233
+ }
234
+
235
+ // If main layer didn't load, try to activate backup layer
236
+ if (!mainLayerLoaded && layers.length > 1) {
237
+ // This is a workaround to switch to the backup layer
238
+ // by manipulating the DOM directly since we don't have
239
+ // direct access to the layer controls
240
+ var backupLayer = layers[1];
241
+ if (backupLayer) {
242
+ backupLayer.style.zIndex = "1";
243
+ backupLayer.style.opacity = "1";
244
+ console.log("Switched to backup terrain layer");
245
+ }
246
+ }
247
+ }
248
+ }, 3000); // Wait 3 seconds for tiles to load
249
+ });
250
+ </script>
251
+ """))
252
 
253
  return m._repr_html_(), processed_count
254