123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- """
- Cd预测模型API接口
- @description: 提供作物Cd和有效态Cd的预测与可视化功能
- """
- from fastapi import APIRouter, HTTPException, BackgroundTasks
- from fastapi.responses import FileResponse
- from typing import Dict, Any
- import os
- import logging
- from ..services.cd_prediction_service import CdPredictionService
- router = APIRouter()
- # 设置日志
- logger = logging.getLogger(__name__)
- @router.post("/crop-cd/generate-and-get-map",
- summary="一键生成并获取作物Cd预测地图",
- description="执行作物Cd模型预测,生成可视化地图后直接返回图片文件")
- async def generate_and_get_crop_cd_map():
- """
- 一键生成并获取作物Cd预测地图
-
- @returns {FileResponse} 作物Cd预测地图文件
- @throws {HTTPException} 当预测过程发生错误时抛出500错误
- @example
- >>> response = await generate_and_get_crop_cd_map()
- >>> # 直接返回图片文件,可在浏览器中查看或下载
- """
- try:
- logger.info("开始一键生成作物Cd预测地图")
-
- service = CdPredictionService()
- result = await service.generate_crop_cd_prediction()
-
- if not result['map_path'] or not os.path.exists(result['map_path']):
- raise HTTPException(
- status_code=500,
- detail="地图文件生成失败"
- )
-
- logger.info(f"作物Cd预测地图生成成功,直接返回文件: {result['map_path']}")
-
- return FileResponse(
- path=result['map_path'],
- filename="crop_cd_prediction_map.jpg",
- media_type="image/jpeg"
- )
-
- except Exception as e:
- logger.error(f"一键生成作物Cd预测地图失败: {str(e)}")
- raise HTTPException(
- status_code=500,
- detail=f"一键生成作物Cd预测地图失败: {str(e)}"
- )
- @router.post("/effective-cd/generate-and-get-map",
- summary="一键生成并获取有效态Cd预测地图",
- description="执行有效态Cd模型预测,生成可视化地图后直接返回图片文件")
- async def generate_and_get_effective_cd_map():
- """
- 一键生成并获取有效态Cd预测地图
-
- @returns {FileResponse} 有效态Cd预测地图文件
- @throws {HTTPException} 当预测过程发生错误时抛出500错误
- @example
- >>> response = await generate_and_get_effective_cd_map()
- >>> # 直接返回图片文件,可在浏览器中查看或下载
- """
- try:
- logger.info("开始一键生成有效态Cd预测地图")
-
- service = CdPredictionService()
- result = await service.generate_effective_cd_prediction()
-
- if not result['map_path'] or not os.path.exists(result['map_path']):
- raise HTTPException(
- status_code=500,
- detail="地图文件生成失败"
- )
-
- logger.info(f"有效态Cd预测地图生成成功,直接返回文件: {result['map_path']}")
-
- return FileResponse(
- path=result['map_path'],
- filename="effective_cd_prediction_map.jpg",
- media_type="image/jpeg"
- )
-
- except Exception as e:
- logger.error(f"一键生成有效态Cd预测地图失败: {str(e)}")
- raise HTTPException(
- status_code=500,
- detail=f"一键生成有效态Cd预测地图失败: {str(e)}"
- )
- @router.post("/crop-cd/generate-and-get-histogram",
- summary="一键生成并获取作物Cd预测直方图",
- description="执行作物Cd模型预测,生成预测值分布直方图后直接返回图片文件")
- async def generate_and_get_crop_cd_histogram():
- """
- 一键生成并获取作物Cd预测直方图
-
- @returns {FileResponse} 作物Cd预测直方图文件
- @throws {HTTPException} 当预测过程发生错误时抛出500错误
- @example
- >>> response = await generate_and_get_crop_cd_histogram()
- >>> # 直接返回直方图文件,可在浏览器中查看或下载
- """
- try:
- logger.info("开始一键生成作物Cd预测直方图")
-
- service = CdPredictionService()
- result = await service.generate_crop_cd_prediction()
-
- if not result['histogram_path'] or not os.path.exists(result['histogram_path']):
- raise HTTPException(
- status_code=500,
- detail="直方图文件生成失败"
- )
-
- logger.info(f"作物Cd预测直方图生成成功,直接返回文件: {result['histogram_path']}")
-
- return FileResponse(
- path=result['histogram_path'],
- filename="crop_cd_prediction_histogram.jpg",
- media_type="image/jpeg"
- )
-
- except Exception as e:
- logger.error(f"一键生成作物Cd预测直方图失败: {str(e)}")
- raise HTTPException(
- status_code=500,
- detail=f"一键生成作物Cd预测直方图失败: {str(e)}"
- )
- @router.post("/effective-cd/generate-and-get-histogram",
- summary="一键生成并获取有效态Cd预测直方图",
- description="执行有效态Cd模型预测,生成预测值分布直方图后直接返回图片文件")
- async def generate_and_get_effective_cd_histogram():
- """
- 一键生成并获取有效态Cd预测直方图
-
- @returns {FileResponse} 有效态Cd预测直方图文件
- @throws {HTTPException} 当预测过程发生错误时抛出500错误
- @example
- >>> response = await generate_and_get_effective_cd_histogram()
- >>> # 直接返回直方图文件,可在浏览器中查看或下载
- """
- try:
- logger.info("开始一键生成有效态Cd预测直方图")
-
- service = CdPredictionService()
- result = await service.generate_effective_cd_prediction()
-
- if not result['histogram_path'] or not os.path.exists(result['histogram_path']):
- raise HTTPException(
- status_code=500,
- detail="直方图文件生成失败"
- )
-
- logger.info(f"有效态Cd预测直方图生成成功,直接返回文件: {result['histogram_path']}")
-
- return FileResponse(
- path=result['histogram_path'],
- filename="effective_cd_prediction_histogram.jpg",
- media_type="image/jpeg"
- )
-
- except Exception as e:
- logger.error(f"一键生成有效态Cd预测直方图失败: {str(e)}")
- raise HTTPException(
- status_code=500,
- detail=f"一键生成有效态Cd预测直方图失败: {str(e)}"
- )
- # 分步式接口 - 先生成后下载
- @router.post("/crop-cd/generate-map",
- summary="生成作物Cd预测地图",
- description="执行作物Cd模型预测并生成可视化地图")
- async def generate_crop_cd_map() -> Dict[str, Any]:
- """
- 生成作物Cd预测地图
-
- @returns {Dict[str, Any]} 包含地图文件路径和预测统计信息的响应
- @throws {HTTPException} 当预测过程发生错误时抛出500错误
- @example
- >>> response = await generate_crop_cd_map()
- >>> print(response['data']['map_path'])
- """
- try:
- logger.info("开始生成作物Cd预测地图")
-
- service = CdPredictionService()
- result = await service.generate_crop_cd_prediction()
-
- logger.info(f"作物Cd预测地图生成成功: {result['map_path']}")
-
- return {
- "success": True,
- "message": "作物Cd预测地图生成成功",
- "data": {
- "map_path": result['map_path'],
- "histogram_path": result['histogram_path'],
- "raster_path": result['raster_path'],
- "prediction_stats": result.get('stats', {})
- },
- "error": None
- }
-
- except Exception as e:
- logger.error(f"生成作物Cd预测地图失败: {str(e)}")
- raise HTTPException(
- status_code=500,
- detail=f"生成作物Cd预测地图失败: {str(e)}"
- )
- @router.post("/effective-cd/generate-map",
- summary="生成有效态Cd预测地图",
- description="执行有效态Cd模型预测并生成可视化地图")
- async def generate_effective_cd_map() -> Dict[str, Any]:
- """
- 生成有效态Cd预测地图
-
- @returns {Dict[str, Any]} 包含地图文件路径和预测统计信息的响应
- @throws {HTTPException} 当预测过程发生错误时抛出500错误
- @example
- >>> response = await generate_effective_cd_map()
- >>> print(response['data']['map_path'])
- """
- try:
- logger.info("开始生成有效态Cd预测地图")
-
- service = CdPredictionService()
- result = await service.generate_effective_cd_prediction()
-
- logger.info(f"有效态Cd预测地图生成成功: {result['map_path']}")
-
- return {
- "success": True,
- "message": "有效态Cd预测地图生成成功",
- "data": {
- "map_path": result['map_path'],
- "histogram_path": result['histogram_path'],
- "raster_path": result['raster_path'],
- "prediction_stats": result.get('stats', {})
- },
- "error": None
- }
-
- except Exception as e:
- logger.error(f"生成有效态Cd预测地图失败: {str(e)}")
- raise HTTPException(
- status_code=500,
- detail=f"生成有效态Cd预测地图失败: {str(e)}"
- )
- @router.get("/crop-cd/download-map",
- summary="下载作物Cd预测地图",
- description="下载最新生成的作物Cd预测地图文件")
- async def download_crop_cd_map():
- """
- 下载作物Cd预测地图文件
-
- @returns {FileResponse} 作物Cd预测地图文件
- @throws {HTTPException} 当地图文件不存在时抛出404错误
- """
- try:
- service = CdPredictionService()
- map_path = service.get_latest_crop_cd_map()
-
- if not map_path or not os.path.exists(map_path):
- raise HTTPException(
- status_code=404,
- detail="作物Cd预测地图文件不存在,请先生成地图"
- )
-
- return FileResponse(
- path=map_path,
- filename="crop_cd_prediction_map.jpg",
- media_type="image/jpeg"
- )
-
- except Exception as e:
- logger.error(f"下载作物Cd预测地图失败: {str(e)}")
- raise HTTPException(
- status_code=500,
- detail=f"下载作物Cd预测地图失败: {str(e)}"
- )
- @router.get("/effective-cd/download-map",
- summary="下载有效态Cd预测地图",
- description="下载最新生成的有效态Cd预测地图文件")
- async def download_effective_cd_map():
- """
- 下载有效态Cd预测地图文件
-
- @returns {FileResponse} 有效态Cd预测地图文件
- @throws {HTTPException} 当地图文件不存在时抛出404错误
- """
- try:
- service = CdPredictionService()
- map_path = service.get_latest_effective_cd_map()
-
- if not map_path or not os.path.exists(map_path):
- raise HTTPException(
- status_code=404,
- detail="有效态Cd预测地图文件不存在,请先生成地图"
- )
-
- return FileResponse(
- path=map_path,
- filename="effective_cd_prediction_map.jpg",
- media_type="image/jpeg"
- )
-
- except Exception as e:
- logger.error(f"下载有效态Cd预测地图失败: {str(e)}")
- raise HTTPException(
- status_code=500,
- detail=f"下载有效态Cd预测地图失败: {str(e)}"
- )
- @router.get("/crop-cd/download-histogram",
- summary="下载作物Cd预测直方图",
- description="下载最新生成的作物Cd预测值分布直方图")
- async def download_crop_cd_histogram():
- """
- 下载作物Cd预测直方图文件
-
- @returns {FileResponse} 作物Cd预测直方图文件
- @throws {HTTPException} 当直方图文件不存在时抛出404错误
- """
- try:
- service = CdPredictionService()
- histogram_path = service.get_latest_crop_cd_histogram()
-
- if not histogram_path or not os.path.exists(histogram_path):
- raise HTTPException(
- status_code=404,
- detail="作物Cd预测直方图文件不存在,请先生成图表"
- )
-
- return FileResponse(
- path=histogram_path,
- filename="crop_cd_prediction_histogram.jpg",
- media_type="image/jpeg"
- )
-
- except Exception as e:
- logger.error(f"下载作物Cd预测直方图失败: {str(e)}")
- raise HTTPException(
- status_code=500,
- detail=f"下载作物Cd预测直方图失败: {str(e)}"
- )
- @router.get("/effective-cd/download-histogram",
- summary="下载有效态Cd预测直方图",
- description="下载最新生成的有效态Cd预测值分布直方图")
- async def download_effective_cd_histogram():
- """
- 下载有效态Cd预测直方图文件
-
- @returns {FileResponse} 有效态Cd预测直方图文件
- @throws {HTTPException} 当直方图文件不存在时抛出404错误
- """
- try:
- service = CdPredictionService()
- histogram_path = service.get_latest_effective_cd_histogram()
-
- if not histogram_path or not os.path.exists(histogram_path):
- raise HTTPException(
- status_code=404,
- detail="有效态Cd预测直方图文件不存在,请先生成图表"
- )
-
- return FileResponse(
- path=histogram_path,
- filename="effective_cd_prediction_histogram.jpg",
- media_type="image/jpeg"
- )
-
- except Exception as e:
- logger.error(f"下载有效态Cd预测直方图失败: {str(e)}")
- raise HTTPException(
- status_code=500,
- detail=f"下载有效态Cd预测直方图失败: {str(e)}"
- )
|