|
@@ -19,6 +19,9 @@ import io
|
|
|
|
|
|
from ..config.cd_prediction_config import cd_config
|
|
from ..config.cd_prediction_config import cd_config
|
|
from ..utils.cd_prediction_wrapper import CdPredictionWrapper
|
|
from ..utils.cd_prediction_wrapper import CdPredictionWrapper
|
|
|
|
+from ..database import SessionLocal
|
|
|
|
+from .admin_boundary_service import get_boundary_geojson_by_name
|
|
|
|
+import json
|
|
|
|
|
|
class CdPredictionService:
|
|
class CdPredictionService:
|
|
"""
|
|
"""
|
|
@@ -550,6 +553,25 @@ class CdPredictionService:
|
|
@returns {Dict[str, Any]} 预测结果信息
|
|
@returns {Dict[str, Any]} 预测结果信息
|
|
"""
|
|
"""
|
|
try:
|
|
try:
|
|
|
|
+ # 用数据库边界覆盖环境变量给集成系统
|
|
|
|
+ tmp_geojson = None
|
|
|
|
+ try:
|
|
|
|
+ db = SessionLocal()
|
|
|
|
+ feature = get_boundary_geojson_by_name(db, county_name, level="auto")
|
|
|
|
+ fc = {"type": "FeatureCollection", "features": [feature]}
|
|
|
|
+ tmp_dir = tempfile.mkdtemp()
|
|
|
|
+ tmp_geojson = os.path.join(tmp_dir, "boundary.geojson")
|
|
|
|
+ with open(tmp_geojson, 'w', encoding='utf-8') as f:
|
|
|
|
+ json.dump(fc, f, ensure_ascii=False)
|
|
|
|
+ os.environ['CD_BOUNDARY_FILE'] = tmp_geojson
|
|
|
|
+ except Exception as _e:
|
|
|
|
+ self.logger.warning(f"从数据库获取边界失败,回退到默认配置: {str(_e)}")
|
|
|
|
+ finally:
|
|
|
|
+ try:
|
|
|
|
+ db.close()
|
|
|
|
+ except Exception:
|
|
|
|
+ pass
|
|
|
|
+
|
|
# 运行作物Cd预测
|
|
# 运行作物Cd预测
|
|
self.logger.info(f"为{county_name}执行作物Cd预测")
|
|
self.logger.info(f"为{county_name}执行作物Cd预测")
|
|
prediction_result = self.wrapper.run_prediction_script("crop", raster_config_override)
|
|
prediction_result = self.wrapper.run_prediction_script("crop", raster_config_override)
|
|
@@ -565,7 +587,7 @@ class CdPredictionService:
|
|
# 清理旧文件
|
|
# 清理旧文件
|
|
self._cleanup_old_files(model_type)
|
|
self._cleanup_old_files(model_type)
|
|
|
|
|
|
- return {
|
|
|
|
|
|
+ result_obj = {
|
|
'map_path': copied_files.get('map_path'),
|
|
'map_path': copied_files.get('map_path'),
|
|
'histogram_path': copied_files.get('histogram_path'),
|
|
'histogram_path': copied_files.get('histogram_path'),
|
|
'raster_path': copied_files.get('raster_path'),
|
|
'raster_path': copied_files.get('raster_path'),
|
|
@@ -574,6 +596,16 @@ class CdPredictionService:
|
|
'timestamp': timestamp,
|
|
'timestamp': timestamp,
|
|
'stats': self._get_file_stats(copied_files.get('map_path'))
|
|
'stats': self._get_file_stats(copied_files.get('map_path'))
|
|
}
|
|
}
|
|
|
|
+ # 清理临时边界
|
|
|
|
+ try:
|
|
|
|
+ if tmp_geojson and os.path.exists(tmp_geojson):
|
|
|
|
+ import shutil
|
|
|
|
+ shutil.rmtree(os.path.dirname(tmp_geojson), ignore_errors=True)
|
|
|
|
+ if 'CD_BOUNDARY_FILE' in os.environ:
|
|
|
|
+ del os.environ['CD_BOUNDARY_FILE']
|
|
|
|
+ except Exception:
|
|
|
|
+ pass
|
|
|
|
+ return result_obj
|
|
|
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
self.logger.error(f"为{county_name}执行作物Cd预测失败: {str(e)}")
|
|
self.logger.error(f"为{county_name}执行作物Cd预测失败: {str(e)}")
|
|
@@ -590,6 +622,25 @@ class CdPredictionService:
|
|
@returns {Dict[str, Any]} 预测结果信息
|
|
@returns {Dict[str, Any]} 预测结果信息
|
|
"""
|
|
"""
|
|
try:
|
|
try:
|
|
|
|
+ # 用数据库边界覆盖环境变量给集成系统
|
|
|
|
+ tmp_geojson = None
|
|
|
|
+ try:
|
|
|
|
+ db = SessionLocal()
|
|
|
|
+ feature = get_boundary_geojson_by_name(db, county_name, level="auto")
|
|
|
|
+ fc = {"type": "FeatureCollection", "features": [feature]}
|
|
|
|
+ tmp_dir = tempfile.mkdtemp()
|
|
|
|
+ tmp_geojson = os.path.join(tmp_dir, "boundary.geojson")
|
|
|
|
+ with open(tmp_geojson, 'w', encoding='utf-8') as f:
|
|
|
|
+ json.dump(fc, f, ensure_ascii=False)
|
|
|
|
+ os.environ['CD_BOUNDARY_FILE'] = tmp_geojson
|
|
|
|
+ except Exception as _e:
|
|
|
|
+ self.logger.warning(f"从数据库获取边界失败,回退到默认配置: {str(_e)}")
|
|
|
|
+ finally:
|
|
|
|
+ try:
|
|
|
|
+ db.close()
|
|
|
|
+ except Exception:
|
|
|
|
+ pass
|
|
|
|
+
|
|
# 运行有效态Cd预测
|
|
# 运行有效态Cd预测
|
|
self.logger.info(f"为{county_name}执行有效态Cd预测")
|
|
self.logger.info(f"为{county_name}执行有效态Cd预测")
|
|
prediction_result = self.wrapper.run_prediction_script("effective", raster_config_override)
|
|
prediction_result = self.wrapper.run_prediction_script("effective", raster_config_override)
|
|
@@ -605,7 +656,7 @@ class CdPredictionService:
|
|
# 清理旧文件
|
|
# 清理旧文件
|
|
self._cleanup_old_files(model_type)
|
|
self._cleanup_old_files(model_type)
|
|
|
|
|
|
- return {
|
|
|
|
|
|
+ result_obj = {
|
|
'map_path': copied_files.get('map_path'),
|
|
'map_path': copied_files.get('map_path'),
|
|
'histogram_path': copied_files.get('histogram_path'),
|
|
'histogram_path': copied_files.get('histogram_path'),
|
|
'raster_path': copied_files.get('raster_path'),
|
|
'raster_path': copied_files.get('raster_path'),
|
|
@@ -614,6 +665,16 @@ class CdPredictionService:
|
|
'timestamp': timestamp,
|
|
'timestamp': timestamp,
|
|
'stats': self._get_file_stats(copied_files.get('map_path'))
|
|
'stats': self._get_file_stats(copied_files.get('map_path'))
|
|
}
|
|
}
|
|
|
|
+ # 清理临时边界
|
|
|
|
+ try:
|
|
|
|
+ if tmp_geojson and os.path.exists(tmp_geojson):
|
|
|
|
+ import shutil
|
|
|
|
+ shutil.rmtree(os.path.dirname(tmp_geojson), ignore_errors=True)
|
|
|
|
+ if 'CD_BOUNDARY_FILE' in os.environ:
|
|
|
|
+ del os.environ['CD_BOUNDARY_FILE']
|
|
|
|
+ except Exception:
|
|
|
|
+ pass
|
|
|
|
+ return result_obj
|
|
|
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
self.logger.error(f"为{county_name}执行有效态Cd预测失败: {str(e)}")
|
|
self.logger.error(f"为{county_name}执行有效态Cd预测失败: {str(e)}")
|