Skip to content

Commit

Permalink
Added API to generate and download KMZ files
Browse files Browse the repository at this point in the history
  • Loading branch information
Pradip-p committed Dec 11, 2024
1 parent 858af11 commit 2944eb8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/backend/app/waypoints/waypoint_routes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import uuid
import geojson
import shutil
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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)}")
32 changes: 32 additions & 0 deletions src/backend/app/waypoints/waypoint_schemas.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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]

0 comments on commit 2944eb8

Please sign in to comment.