From fd265a893eef1a3448c3fc10deed635a57f4cdd8 Mon Sep 17 00:00:00 2001 From: chengkml Date: Sun, 3 May 2026 00:12:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=AB=98=E7=A8=8B=E5=88=86?= =?UTF-8?q?=E6=9E=90=20rasterio=20=E7=BC=BA=E5=A4=B1=E6=8A=A5=E9=94=99?= =?UTF-8?q?=E5=B9=B6=E5=A2=9E=E5=8A=A0=E6=A0=85=E6=A0=BC=E8=83=BD=E5=8A=9B?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: multica-agent --- api/Dockerfile | 1 + api/app/services/elevation_service.py | 21 ++++++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/api/Dockerfile b/api/Dockerfile index 903f85b..a98ca33 100644 --- a/api/Dockerfile +++ b/api/Dockerfile @@ -15,6 +15,7 @@ ENV PIP_RETRIES=${PIP_RETRIES} COPY requirements.txt ./ RUN pip install --no-cache-dir --retries "${PIP_RETRIES}" --timeout "${PIP_DEFAULT_TIMEOUT}" -i "${PIP_INDEX_URL}" -r requirements.txt +RUN python -c "import rasterio; print('rasterio ready:', rasterio.__version__)" COPY app ./app diff --git a/api/app/services/elevation_service.py b/api/app/services/elevation_service.py index 46af754..e3a4c7d 100644 --- a/api/app/services/elevation_service.py +++ b/api/app/services/elevation_service.py @@ -189,6 +189,8 @@ def create_dataset( normalized_file_path = payload.file_path.strip() file_format = _detect_file_format(normalized_file_path) + if file_format in RASTER_FILE_FORMATS: + _require_rasterio_available() _ensure_dataset_file_exists(db, mount_code=payload.mount_code, file_path=normalized_file_path) now = utcnow() @@ -710,6 +712,17 @@ def _resolve_dataset_file_format(dataset: ElevationDataset) -> str: return detected +def _require_rasterio_available() -> Any: + try: + import rasterio + except Exception as exc: + raise HTTPException( + status_code=status.HTTP_503_SERVICE_UNAVAILABLE, + detail="当前服务未启用 rasterio 栅格能力,请联系管理员重建 API 镜像后重试 IMG/TIF 分析与回填", + ) from exc + return rasterio + + def _open_raster_dataset( db: Session, dataset: ElevationDataset, @@ -721,13 +734,7 @@ def _open_raster_dataset( detail=f"当前文件不是栅格高程文件: {dataset.file_path}", ) - try: - import rasterio - except ImportError as exc: - raise HTTPException( - status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, - detail="服务未安装 rasterio,暂不支持 IMG/TIF 高程文件", - ) from exc + rasterio = _require_rasterio_available() mount = _require_mount(db, dataset.mount_code) driver = _build_driver_or_400(mount)