LovnishVerma commited on
Commit
305506e
·
verified ·
1 Parent(s): 6bd048f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -28
app.py CHANGED
@@ -163,34 +163,54 @@ def load_data():
163
  'West Bengal': (22.9868, 87.8550)
164
  }
165
 
166
- def get_coords(row):
167
- state = row.get('state', 'Delhi')
168
- district = str(row.get('district', 'Unknown'))
169
-
170
- # 1. Get State Base Coordinates
171
- base_lat, base_lon = state_centers.get(state, (20.5937, 78.9629)) # Default to India Center
172
-
173
- # 2. DETERMINISTIC HASHING FOR DISTRICT
174
- # This ensures "District A" is ALWAYS in the same spot relative to the State Center
175
- # Creates distinct clusters instead of random noise
176
- district_hash = hash(state + district)
177
- np.random.seed(district_hash % 2**32)
178
-
179
- # Offset the district center by up to 1.5 degrees (~150km) from state center
180
- dist_lat_offset = np.random.uniform(-1.5, 1.5)
181
- dist_lon_offset = np.random.uniform(-1.5, 1.5)
182
-
183
- # 3. INDIVIDUAL CENTER JITTER
184
- # Add tiny random noise (~4km) so points don't stack perfectly
185
- # We re-seed with None to get true randomness for the jitter
186
- np.random.seed(None)
187
- noise_lat = np.random.normal(0, 0.04)
188
- noise_lon = np.random.normal(0, 0.04)
189
-
190
- return pd.Series({
191
- 'lat': base_lat + dist_lat_offset + noise_lat,
192
- 'lon': base_lon + dist_lon_offset + noise_lon
193
- })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
 
195
  # Apply coordinates
196
  coords = df.apply(get_coords, axis=1)
 
163
  'West Bengal': (22.9868, 87.8550)
164
  }
165
 
166
+ def get_coords(row):
167
+ state = row.get('state', 'Delhi')
168
+ district = str(row.get('district', 'Unknown'))
169
+
170
+ # 1. Get State Base Coordinates (Use your updated list)
171
+ base_lat, base_lon = state_centers.get(state, (20.5937, 78.9629))
172
+
173
+ # 2. DEFINE STATE RADIUS SCALER (In Degrees)
174
+ # Default is 0.5 (~55km) which is safer than 1.5
175
+ default_radius = 0.5
176
+
177
+ # Tighter constraints for small States/UTs
178
+ radius_map = {
179
+ 'Chandigarh': 0.04,
180
+ 'Delhi': 0.15,
181
+ 'Goa': 0.15,
182
+ 'Puducherry': 0.1,
183
+ 'Lakshadweep': 0.05,
184
+ 'Daman and Diu': 0.05,
185
+ 'Dadra and Nagar Haveli': 0.05,
186
+ 'Kerala': 0.3, # Narrow state
187
+ 'Haryana': 0.4,
188
+ 'Punjab': 0.4,
189
+ 'Tripura': 0.3,
190
+ 'Sikkim': 0.15,
191
+ 'Andaman and Nicobar Islands': 1.0 # Long archipelago
192
+ }
193
+
194
+ # Get the specific radius for this state
195
+ radius = radius_map.get(state, default_radius)
196
+
197
+ # 3. DETERMINISTIC HASHING
198
+ district_hash = hash(state + district)
199
+ np.random.seed(district_hash % 2**32)
200
+
201
+ # Offset using the specific radius
202
+ dist_lat_offset = np.random.uniform(-radius, radius)
203
+ dist_lon_offset = np.random.uniform(-radius, radius)
204
+
205
+ # 4. MICRO JITTER (Random noise for individual points)
206
+ np.random.seed(None)
207
+ noise_lat = np.random.normal(0, 0.02 * radius) # Scale noise relative to state size
208
+ noise_lon = np.random.normal(0, 0.02 * radius)
209
+
210
+ return pd.Series({
211
+ 'lat': base_lat + dist_lat_offset + noise_lat,
212
+ 'lon': base_lon + dist_lon_offset + noise_lon
213
+ })
214
 
215
  # Apply coordinates
216
  coords = df.apply(get_coords, axis=1)