From 2944eb807013300d2e2b552856160182afb81be3 Mon Sep 17 00:00:00 2001 From: Pradip-p Date: Wed, 11 Dec 2024 11:03:04 +0545 Subject: [PATCH] Added API to generate and download KMZ files --- src/backend/app/waypoints/waypoint_routes.py | 25 ++++++++++++++- src/backend/app/waypoints/waypoint_schemas.py | 32 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/backend/app/waypoints/waypoint_routes.py b/src/backend/app/waypoints/waypoint_routes.py index 4613e08c..f038f8cc 100644 --- a/src/backend/app/waypoints/waypoint_routes.py +++ b/src/backend/app/waypoints/waypoint_routes.py @@ -1,3 +1,4 @@ +import os import uuid import geojson import shutil @@ -147,7 +148,9 @@ async def get_task_waypoint( outfile = outfile = f"/tmp/{uuid.uuid4()}" kmz_file = wpml.create_wpml(placemarks, outfile) return FileResponse( - kmz_file, media_type="application/zip", filename="flight_plan.kmz" + kmz_file, + media_type="application/zip", + filename=f"{task_id}_flight_plan.kmz", ) flight_data = calculate_flight_time_from_placemarks(placemarks) return {"results": placemarks, "flight_data": flight_data} @@ -257,3 +260,23 @@ async def generate_kmz( return FileResponse( output_file, media_type="application/zip", filename="output.kmz" ) + + +@router.post("/{task_id}/generate-kmz/") +async def generate_kmz_with_placemarks( + task_id: uuid.UUID, data: waypoint_schemas.PlacemarksFeature +): + try: + outfile = f"/tmp/{task_id}_flight_plan.kmz" + + kmz_file = wpml.create_wpml(data.model_dump(), outfile) + if not os.path.exists(kmz_file): + raise HTTPException(status_code=500, detail="Failed to generate KMZ file.") + return FileResponse( + kmz_file, + media_type="application/zip", + filename=f"{task_id}_flight_plan.kmz", + ) + + except Exception as e: + raise HTTPException(status_code=500, detail=f"Error generating KMZ: {str(e)}") diff --git a/src/backend/app/waypoints/waypoint_schemas.py b/src/backend/app/waypoints/waypoint_schemas.py index e20fd87c..b9299b78 100644 --- a/src/backend/app/waypoints/waypoint_schemas.py +++ b/src/backend/app/waypoints/waypoint_schemas.py @@ -1,5 +1,7 @@ import json +from typing import List, Optional from pydantic import BaseModel, model_validator +from geojson_pydantic import FeatureCollection, Feature, Point class PointField(BaseModel): @@ -12,3 +14,33 @@ def validate_to_json(cls, value): if isinstance(value, str): return cls(**json.loads(value)) return value + + +class Properties(BaseModel): + altitude: float + gimbal_angle: str + heading: float + index: int + speed: float + take_photo: bool + elevation: Optional[float] = None + + +class Geometry(Point): + pass + + +class Feature(Feature): + geometry: Geometry + properties: Properties + + +class CRS(BaseModel): + properties: dict + type: str + + +class PlacemarksFeature(FeatureCollection): + type: str = "FeatureCollection" + crs: Optional[CRS] = None + features: List[Feature]