Spaces:
Runtime error
Runtime error
File size: 12,090 Bytes
363af29 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | import os
import json
import logging
from typing import List, Dict, Any, Optional
logger = logging.getLogger("spatial_analysis.interpolation")
# Helper to map intensity value (0 to 10) to a Hex Color for Leaflet rendering
def get_color_for_intensity(val: float) -> str:
# Blend from green (low risk) to yellow (medium) to red (high) to purple (critical)
if val < 2.0:
return "#2ecc71" # Green
elif val < 4.0:
return "#f1c40f" # Yellow
elif val < 7.0:
return "#e67e22" # Orange
elif val < 9.0:
return "#e74c3c" # Red
else:
return "#9b59b6" # Purple
class SpatialRiskInterpolator:
def __init__(self):
self.has_scipy = False
self.has_shapely = False
try:
from scipy.interpolate import Rbf
import numpy as np
self.has_scipy = True
except ImportError:
pass
try:
from shapely.geometry import Point, shape, LineString, Polygon
self.has_shapely = True
print("[+] Successfully initialized Shapely for Proximity Risk Queries.")
except ImportError:
print("[!] Shapely not found. Proximity risk calculation will use mathematical spherical approximation.")
def interpolate_idw_grid(self, incidents: List[Dict[str, Any]], grid_resolution: int = 25) -> Dict[str, Any]:
"""
Takes pollution incident reports, interpolates intensity across a bounding box grid,
and returns a Leaflet-friendly GeoJSON FeatureCollection of cell squares.
"""
if not incidents:
return {"type": "FeatureCollection", "features": []}
lats = [float(inc["latitude"]) for inc in incidents]
lons = [float(inc["longitude"]) for inc in incidents]
vals = [float(inc["intensity"]) for inc in incidents]
# Calculate bounding box with small buffer margin
lat_min, lat_max = min(lats) - 0.02, max(lats) + 0.02
lon_min, lon_max = min(lons) - 0.02, max(lons) + 0.02
# Handle single point or zero range
if lat_min == lat_max or lon_min == lon_max:
lat_min, lat_max = lat_min - 0.02, lat_max + 0.02
lon_min, lon_max = lon_min - 0.02, lon_max + 0.02
# Step 1: Interpolate grid values
import numpy as np
grid_lat = np.linspace(lat_min, lat_max, grid_resolution)
grid_lon = np.linspace(lon_min, lon_max, grid_resolution)
grid_lon_mesh, grid_lat_mesh = np.meshgrid(grid_lon, grid_lat)
if self.has_scipy:
try:
from scipy.interpolate import Rbf
# Radial Basis Function interpolation (multiquadric kernel)
# epsilon handles smoothing to prevent singularity spikes
rbf = Rbf(lons, lats, vals, function='linear', epsilon=0.01)
grid_vals = rbf(grid_lon_mesh, grid_lat_mesh)
except Exception as e:
logger.error(f"Scipy RBF interpolation failed: {e}. Falling back to manual IDW.")
grid_vals = self._manual_idw(lons, lats, vals, grid_lon_mesh, grid_lat_mesh)
else:
grid_vals = self._manual_idw(lons, lats, vals, grid_lon_mesh, grid_lat_mesh)
# Clip values to range [0, 10]
grid_vals = np.clip(grid_vals, 0.0, 10.0)
# Step 2: Convert grid to GeoJSON Feature Collection
features = []
for i in range(grid_resolution - 1):
for j in range(grid_resolution - 1):
# Calculate cell average value
c_val = (grid_vals[i, j] + grid_vals[i+1, j] + grid_vals[i+1, j+1] + grid_vals[i, j+1]) / 4.0
# Exclude ultra-low values to keep geojson light
if c_val < 0.2:
continue
# Cell corner coordinates
c_lons = [grid_lon[j], grid_lon[j+1], grid_lon[j+1], grid_lon[j], grid_lon[j]]
c_lats = [grid_lat[i], grid_lat[i], grid_lat[i+1], grid_lat[i+1], grid_lat[i]]
polygon_coords = [[ [lon, lat] for lon, lat in zip(c_lons, c_lats) ]]
features.append({
"type": "Feature",
"properties": {
"intensity": round(float(c_val), 2),
"color": get_color_for_intensity(c_val),
"fillOpacity": min(0.7, 0.1 + (c_val / 15.0))
},
"geometry": {
"type": "Polygon",
"coordinates": polygon_coords
}
})
return {
"type": "FeatureCollection",
"features": features
}
def _manual_idw(self, lons, lats, vals, grid_lon, grid_lat): # returns np.ndarray
"""Manual Inverse Distance Weighting interpolation implementation in NumPy."""
import numpy as np
grid_shape = grid_lon.shape
grid_vals = np.zeros(grid_shape)
for i in range(grid_shape[0]):
for j in range(grid_shape[1]):
glat = grid_lat[i, j]
glon = grid_lon[i, j]
# Compute distance to all points
dists = np.sqrt((np.array(lons) - glon)**2 + (np.array(lats) - glat)**2)
# If exact match
if np.any(dists < 0.0001):
grid_vals[i, j] = vals[np.argmin(dists)]
continue
# Weights are 1 / d^2
weights = 1.0 / (dists ** 2)
grid_vals[i, j] = np.sum(weights * np.array(vals)) / np.sum(weights)
return grid_vals
def compute_proximity_alerts(self, latitude: float, longitude: float, ecosystems_geojson: Optional[str] = None) -> List[Dict[str, Any]]:
"""
Uses Shapely to compute exact distance from an incident point to critical ecosystems
defined in a GeoJSON dataset. Falls back to mock coordinate math if Shapely isn't installed.
"""
alerts = []
point_lon, point_lat = longitude, latitude
# Default critical ecosystems for Indian region if no file is provided
default_ecosystems = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {"name": "Bhopal Upper Lake", "type": "Water Reservoir"},
"geometry": {
"type": "Polygon",
"coordinates": [[
[77.30, 23.23], [77.37, 23.23], [77.37, 23.28], [77.30, 23.28], [77.30, 23.23]
]]
}
},
{
"type": "Feature",
"properties": {"name": "Narmada River Tributary", "type": "River"},
"geometry": {
"type": "LineString",
"coordinates": [[77.20, 23.10], [77.40, 23.15], [77.60, 23.12]]
}
},
{
"type": "Feature",
"properties": {"name": "Van Vihar National Park Buffer", "type": "Protected Forest"},
"geometry": {
"type": "Polygon",
"coordinates": [[
[77.34, 23.20], [77.38, 23.20], [77.38, 23.24], [77.34, 23.24], [77.34, 23.20]
]]
}
}
]
}
# Load GeoJSON from file if provided and exists
ecosystems = default_ecosystems
if ecosystems_geojson and os.path.exists(ecosystems_geojson):
try:
with open(ecosystems_geojson, 'r') as f:
ecosystems = json.load(f)
except Exception as e:
logger.error(f"Failed to read ecosystems geojson from {ecosystems_geojson}: {e}")
if self.has_shapely:
try:
from shapely.geometry import Point, shape
point = Point(point_lon, point_lat)
for feature in ecosystems.get("features", []):
geom = shape(feature["geometry"])
properties = feature.get("properties", {})
name = properties.get("name", "Unnamed Ecosystem")
eco_type = properties.get("type", "Ecosystem")
# Calculate distance (degrees to meters approximation: 1 degree approx 111,320m)
dist_deg = point.distance(geom)
dist_meters = dist_deg * 111320.0
# Log alerts for ecosystems within 1.5 km (1500 meters)
if dist_meters <= 1500.0:
risk_status = "CRITICAL" if dist_meters <= 400.0 else "WARNING"
alerts.append({
"ecosystem_name": name,
"ecosystem_type": eco_type,
"distance_meters": round(dist_meters, 1),
"risk_status": risk_status,
"description": f"Incident is {round(dist_meters)}m away from {name}."
})
return alerts
except Exception as e:
logger.error(f"Shapely distance calculation failed: {e}. Falling back to coordinate math.")
return self._mock_proximity_alerts(point_lat, point_lon, ecosystems)
else:
return self._mock_proximity_alerts(point_lat, point_lon, ecosystems)
def _mock_proximity_alerts(self, lat: float, lon: float, ecosystems: Dict[str, Any]) -> List[Dict[str, Any]]:
"""Mock proximity calculator in pure python using Haversine formula approximation."""
import math
alerts = []
def get_dist_point_to_point(lat1, lon1, lat2, lon2):
# Haversine distance
R = 6371000 # Earth radius in meters
phi1 = math.radians(lat1)
phi2 = math.radians(lat2)
dphi = math.radians(lat2 - lat1)
dlambda = math.radians(lon2 - lon1)
a = math.sin(dphi/2)**2 + math.cos(phi1)*math.cos(phi2)*math.sin(dlambda/2)**2
return 2 * R * math.atan2(math.sqrt(a), math.sqrt(1-a))
for feature in ecosystems.get("features", []):
geom = feature["geometry"]
properties = feature.get("properties", {})
name = properties.get("name", "Unnamed Ecosystem")
eco_type = properties.get("type", "Ecosystem")
# Simple approximation by taking the average centroid coordinate of geometry
coords = []
if geom["type"] == "Point":
coords = [geom["coordinates"]]
elif geom["type"] == "LineString":
coords = geom["coordinates"]
elif geom["type"] == "Polygon":
coords = geom["coordinates"][0] # Exterior ring
if not coords:
continue
# Average coordinates
mean_lon = sum(c[0] for c in coords) / len(coords)
mean_lat = sum(c[1] for c in coords) / len(coords)
dist_meters = get_dist_point_to_point(lat, lon, mean_lat, mean_lon)
if dist_meters <= 1500.0:
risk_status = "CRITICAL" if dist_meters <= 400.0 else "WARNING"
alerts.append({
"ecosystem_name": name,
"ecosystem_type": eco_type,
"distance_meters": round(dist_meters, 1),
"risk_status": risk_status,
"description": f"Incident is approx {round(dist_meters)}m away from {name} center."
})
return alerts
|