Skip to content

Commit

Permalink
Merge pull request #781 from hotosm/feat-ConfigFile
Browse files Browse the repository at this point in the history
api to download data extracts
  • Loading branch information
nrjadkry authored Aug 29, 2023
2 parents 4167e0b + 1709c73 commit c414da7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/backend/app/projects/project_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 25 additions & 1 deletion src/backend/app/projects/project_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit c414da7

Please sign in to comment.