Spaces:
Sleeping
Sleeping
Upload realtime_pipeline.py
Browse files- realtime_pipeline.py +35 -42
realtime_pipeline.py
CHANGED
|
@@ -369,55 +369,48 @@ class RealTimeFeatureExtractor:
|
|
| 369 |
return 16 # Default
|
| 370 |
|
| 371 |
def get_ndvi_data(self, latitude, longitude):
|
| 372 |
-
"""Get NDVI with
|
| 373 |
try:
|
| 374 |
point = ee.Geometry.Point([longitude, latitude])
|
| 375 |
end_date = ee.Date(datetime.now())
|
| 376 |
|
| 377 |
-
#
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
(
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 393 |
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
).get('NDVI').getInfo()
|
| 405 |
-
|
| 406 |
-
if ndvi_value is not None:
|
| 407 |
-
ndvi_date = ee.Date(ndvi_latest.get('system:time_start')).format('YYYY-MM-dd').getInfo()
|
| 408 |
-
self.logger.info(f"✅ NDVI: {ndvi_value:.4f} (from {ndvi_date}, scale: {scale}m)")
|
| 409 |
-
return {'ndvi': ndvi_value, 'ndvi_date': ndvi_date}
|
| 410 |
-
except Exception as e:
|
| 411 |
-
continue
|
| 412 |
-
except Exception as e:
|
| 413 |
-
continue
|
| 414 |
|
| 415 |
-
self.logger.warning("⚠️ No NDVI data available even with fallbacks")
|
| 416 |
-
return {'ndvi': 0.1, 'ndvi_date': None} # Return default value instead of null
|
| 417 |
except Exception as e:
|
| 418 |
-
self.logger.error(f"❌ NDVI failed: {e}")
|
| 419 |
-
return {'ndvi':
|
| 420 |
-
|
| 421 |
def extract_features(self, latitude: float, longitude: float) -> Dict:
|
| 422 |
self.logger.info(f"🚀 Extracting features for ({latitude:.4f}, {longitude:.4f})")
|
| 423 |
|
|
|
|
| 369 |
return 16 # Default
|
| 370 |
|
| 371 |
def get_ndvi_data(self, latitude, longitude):
|
| 372 |
+
"""Get NDVI with extended date range"""
|
| 373 |
try:
|
| 374 |
point = ee.Geometry.Point([longitude, latitude])
|
| 375 |
end_date = ee.Date(datetime.now())
|
| 376 |
|
| 377 |
+
# EXTEND THE DATE RANGE SIGNIFICANTLY
|
| 378 |
+
start_date = end_date.advance(-180, 'day') # 6 months instead of 60 days
|
| 379 |
+
|
| 380 |
+
ndvi_col = ee.ImageCollection('MODIS/061/MOD13A2').select('NDVI') \
|
| 381 |
+
.filterDate(start_date, end_date).filterBounds(point)
|
| 382 |
+
|
| 383 |
+
if ndvi_col.size().getInfo() > 0:
|
| 384 |
+
ndvi_latest = ndvi_col.sort('system:time_start', False).first()
|
| 385 |
+
|
| 386 |
+
# Try multiple scales
|
| 387 |
+
scales = [500, 1000, 2000, 5000]
|
| 388 |
+
for scale in scales:
|
| 389 |
+
try:
|
| 390 |
+
ndvi_value = ndvi_latest.multiply(0.0001).reduceRegion(
|
| 391 |
+
reducer=ee.Reducer.mean(),
|
| 392 |
+
geometry=point.buffer(1000), # Add small buffer
|
| 393 |
+
scale=scale,
|
| 394 |
+
bestEffort=True,
|
| 395 |
+
maxPixels=1e9,
|
| 396 |
+
tileScale=4 # Help with computation
|
| 397 |
+
).get('NDVI').getInfo()
|
| 398 |
|
| 399 |
+
if ndvi_value is not None:
|
| 400 |
+
ndvi_date = ee.Date(ndvi_latest.get('system:time_start')).format('YYYY-MM-dd').getInfo()
|
| 401 |
+
self.logger.info(f"✅ NDVI: {ndvi_value:.4f} (scale: {scale}m)")
|
| 402 |
+
return {'ndvi': ndvi_value, 'ndvi_date': ndvi_date}
|
| 403 |
+
except Exception as scale_error:
|
| 404 |
+
continue
|
| 405 |
+
|
| 406 |
+
self.logger.warning("⚠️ No NDVI data even with extended range")
|
| 407 |
+
# Return minimal default ONLY if absolutely necessary
|
| 408 |
+
return {'ndvi': None, 'ndvi_date': None} # Better to return null than wrong value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 409 |
|
|
|
|
|
|
|
| 410 |
except Exception as e:
|
| 411 |
+
self.logger.error(f"❌ NDVI extraction failed: {e}")
|
| 412 |
+
return {'ndvi': None, 'ndvi_date': None}
|
| 413 |
+
|
| 414 |
def extract_features(self, latitude: float, longitude: float) -> Dict:
|
| 415 |
self.logger.info(f"🚀 Extracting features for ({latitude:.4f}, {longitude:.4f})")
|
| 416 |
|