Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -80,46 +80,23 @@ def fetch_coordinates_batch(unique_locations):
|
|
| 80 |
if os.path.exists(json_file):
|
| 81 |
try:
|
| 82 |
with open(json_file, 'r') as f:
|
| 83 |
-
# Convert string keys "District
|
| 84 |
loaded_data = json.load(f)
|
| 85 |
for k, v in loaded_data.items():
|
| 86 |
-
|
| 87 |
-
|
|
|
|
| 88 |
except json.JSONDecodeError:
|
| 89 |
pass # File corrupted, start fresh
|
| 90 |
|
| 91 |
-
# 2.
|
| 92 |
-
hardcoded_map = {
|
| 93 |
-
('Gautam Buddha Nagar', 'Uttar Pradesh'): (28.39, 77.65),
|
| 94 |
-
('West Jaintia Hills', 'Meghalaya'): (25.55, 92.38),
|
| 95 |
-
('West Khasi Hills', 'Meghalaya'): (25.56, 91.29),
|
| 96 |
-
('Bijapur', 'Chhattisgarh'): (18.80, 80.82),
|
| 97 |
-
('Dhule', 'Maharashtra'): (20.90, 74.77),
|
| 98 |
-
('Dhamtari', 'Chhattisgarh'): (20.71, 81.55),
|
| 99 |
-
('Udupi', 'Karnataka'): (13.34, 74.75),
|
| 100 |
-
('Supaul', 'Bihar'): (26.29, 86.82),
|
| 101 |
-
('Puruliya', 'West Bengal'): (23.25, 86.50),
|
| 102 |
-
('Mumbai', 'Maharashtra'): (19.0760, 72.8777),
|
| 103 |
-
('Pune', 'Maharashtra'): (18.5204, 73.8567),
|
| 104 |
-
('Bangalore', 'Karnataka'): (12.9716, 77.5946),
|
| 105 |
-
('Bengaluru', 'Karnataka'): (12.9716, 77.5946),
|
| 106 |
-
('Chennai', 'Tamil Nadu'): (13.0827, 80.2707),
|
| 107 |
-
('Hyderabad', 'Telangana'): (17.3850, 78.4867),
|
| 108 |
-
('Kolkata', 'West Bengal'): (22.5726, 88.3639),
|
| 109 |
-
('Delhi', 'Delhi'): (28.7041, 77.1025),
|
| 110 |
-
('Shimla', 'Himachal Pradesh'): (31.1048, 77.1734)
|
| 111 |
-
}
|
| 112 |
-
# Update cache with hardcoded values (overrides JSON if conflict, usually better accuracy)
|
| 113 |
-
coords_map.update(hardcoded_map)
|
| 114 |
-
|
| 115 |
-
# 3. Identify missing locations
|
| 116 |
missing_locs = [loc for loc in unique_locations if loc not in coords_map]
|
| 117 |
|
| 118 |
if not missing_locs:
|
| 119 |
return coords_map
|
| 120 |
|
| 121 |
-
#
|
| 122 |
-
progress_text = "📡
|
| 123 |
my_bar = st.progress(0, text=progress_text)
|
| 124 |
|
| 125 |
headers = {'User-Agent': 'StarkDashboard/1.0 (Government Research Project)'}
|
|
@@ -142,8 +119,7 @@ def fetch_coordinates_batch(unique_locations):
|
|
| 142 |
coords_map[(district, state)] = (float(data['lat']), float(data['lon']))
|
| 143 |
updated = True
|
| 144 |
else:
|
| 145 |
-
#
|
| 146 |
-
pass
|
| 147 |
|
| 148 |
# Respect Rate Limiting (1 request per second)
|
| 149 |
time.sleep(1.1)
|
|
@@ -153,7 +129,7 @@ def fetch_coordinates_batch(unique_locations):
|
|
| 153 |
|
| 154 |
my_bar.empty()
|
| 155 |
|
| 156 |
-
#
|
| 157 |
if updated:
|
| 158 |
# Convert keys to string "District|State" for JSON compatibility
|
| 159 |
save_data = {f"{k[0]}|{k[1]}": v for k, v in coords_map.items()}
|
|
|
|
| 80 |
if os.path.exists(json_file):
|
| 81 |
try:
|
| 82 |
with open(json_file, 'r') as f:
|
| 83 |
+
# Convert string keys "District|State" back to tuple
|
| 84 |
loaded_data = json.load(f)
|
| 85 |
for k, v in loaded_data.items():
|
| 86 |
+
if "|" in k:
|
| 87 |
+
d, s = k.split("|")
|
| 88 |
+
coords_map[(d, s)] = tuple(v)
|
| 89 |
except json.JSONDecodeError:
|
| 90 |
pass # File corrupted, start fresh
|
| 91 |
|
| 92 |
+
# 2. Identify missing locations
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
missing_locs = [loc for loc in unique_locations if loc not in coords_map]
|
| 94 |
|
| 95 |
if not missing_locs:
|
| 96 |
return coords_map
|
| 97 |
|
| 98 |
+
# 3. Dynamic Fetching for missing
|
| 99 |
+
progress_text = "📡 New locations found. Fetching coordinates..."
|
| 100 |
my_bar = st.progress(0, text=progress_text)
|
| 101 |
|
| 102 |
headers = {'User-Agent': 'StarkDashboard/1.0 (Government Research Project)'}
|
|
|
|
| 119 |
coords_map[(district, state)] = (float(data['lat']), float(data['lon']))
|
| 120 |
updated = True
|
| 121 |
else:
|
| 122 |
+
pass # Fail silently, will fall back to state center logic later
|
|
|
|
| 123 |
|
| 124 |
# Respect Rate Limiting (1 request per second)
|
| 125 |
time.sleep(1.1)
|
|
|
|
| 129 |
|
| 130 |
my_bar.empty()
|
| 131 |
|
| 132 |
+
# 4. Save back to JSON if new data fetched
|
| 133 |
if updated:
|
| 134 |
# Convert keys to string "District|State" for JSON compatibility
|
| 135 |
save_data = {f"{k[0]}|{k[1]}": v for k, v in coords_map.items()}
|