From 81148e44fe8c47e53c7c00fdc0a738ccd30fb7b4 Mon Sep 17 00:00:00 2001 From: Niraj Adhikari Date: Tue, 29 Aug 2023 10:22:50 +0545 Subject: [PATCH 1/2] api to download data extracts --- src/backend/app/projects/project_crud.py | 24 ++++++++++++++++++++++ src/backend/app/projects/project_routes.py | 24 ++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/src/backend/app/projects/project_crud.py b/src/backend/app/projects/project_crud.py index 267ca7cda4..0b1dd45c48 100644 --- a/src/backend/app/projects/project_crud.py +++ b/src/backend/app/projects/project_crud.py @@ -1496,6 +1496,30 @@ def get_task_geometry(db: Session, project_id: int): return json.dumps(feature_collection) +async def get_project_features_geojson(db:Session, project_id:int): + + # Get the geojson of those features for this task. + query = f"""SELECT jsonb_build_object( + 'type', 'FeatureCollection', + 'features', jsonb_agg(feature) + ) + FROM ( + SELECT jsonb_build_object( + 'type', 'Feature', + 'id', id, + 'geometry', ST_AsGeoJSON(geometry)::jsonb, + 'properties', properties + ) AS feature + FROM features + WHERE project_id={project_id} + ) features; + """ + + result = db.execute(query) + features = result.fetchone()[0] + return features + + def create_task_grid(db: Session, project_id: int, delta: int): try: # Query DB for project AOI diff --git a/src/backend/app/projects/project_routes.py b/src/backend/app/projects/project_routes.py index 2e18e8444c..791b5d66d0 100644 --- a/src/backend/app/projects/project_routes.py +++ b/src/backend/app/projects/project_routes.py @@ -920,6 +920,30 @@ async def download_task_boundaries( return Response(content=out, headers=headers) +@router.get("/features/download/") +async def download_features( + project_id: int, + db: Session = Depends(database.get_db) +): + """Downloads the features of a project as a GeoJSON file. + + Args: + project_id (int): The id of the project. + + Returns: + Response: The HTTP response object containing the downloaded file. + """ + + out = await project_crud.get_project_features_geojson(db, project_id) + + headers = { + "Content-Disposition": "attachment; filename=project_features.geojson", + "Content-Type": "application/media", + } + + return Response(content=json.dumps(out), headers=headers) + + @router.get("/tiles/{project_id}") async def generate_project_tiles( background_tasks: BackgroundTasks, From 1709c738969cdc5fc27dfe08d239f7624b79c870 Mon Sep 17 00:00:00 2001 From: Niraj Adhikari Date: Tue, 29 Aug 2023 15:25:28 +0545 Subject: [PATCH 2/2] update project details endpoint --- src/backend/app/projects/project_routes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend/app/projects/project_routes.py b/src/backend/app/projects/project_routes.py index 791b5d66d0..831186bc18 100644 --- a/src/backend/app/projects/project_routes.py +++ b/src/backend/app/projects/project_routes.py @@ -64,7 +64,7 @@ async def read_projects( return projects -@router.get("/{project_id}/") +@router.get("/project_details/{project_id}/") async def get_projet_details(project_id: int, db: Session = Depends(database.get_db)): """Returns the project details.