Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

task id on task polygon and feature extracts #976

Merged
merged 7 commits into from
Nov 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions src/backend/app/projects/project_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import shapely.wkb as wkblib
import sqlalchemy
from fastapi import File, HTTPException, UploadFile
from geoalchemy2.shape import from_shape
from geoalchemy2.shape import from_shape, to_shape
from geojson import dump
from loguru import logger as log
from osm_fieldwork.basemapper import create_basemap_file
Expand Down Expand Up @@ -1673,22 +1673,29 @@ def get_task_geometry(db: Session, project_id: int):
Returns:
str: A geojson of the task boundaries
"""
tasks = table("tasks", column("outline"), column("project_id"), column("id"))
where = f"project_id={project_id}"
sql = select(geoalchemy2.functions.ST_AsGeoJSON(tasks.c.outline)).where(text(where))
result = db.execute(sql)

db_tasks = tasks_crud.get_tasks(db, project_id, None)
features = []
for row in result:
geometry = json.loads(row[0])
feature = {"type": "Feature", "geometry": geometry, "properties": {}}
for task in db_tasks:
geom = to_shape(task.outline)
# Convert the shapely geometry object to GeoJSON
geometry = geom.__geo_interface__
properties = {
"task_id": task.id,
}
feature = {"type": "Feature", "geometry": geometry, "properties": properties}
features.append(feature)

feature_collection = {"type": "FeatureCollection", "features": features}
return json.dumps(feature_collection)


async def get_project_features_geojson(db: Session, project_id: int):
db_features = (
db.query(db_models.DbFeatures)
.filter(db_models.DbFeatures.project_id == project_id)
.all()
)

"""Get a geojson of all features for a task."""
query = text(
f"""SELECT jsonb_build_object(
Expand All @@ -1710,6 +1717,14 @@ async def get_project_features_geojson(db: Session, project_id: int):

result = db.execute(query)
features = result.fetchone()[0]

# Create mapping feat_id:task_id
task_feature_mapping = {feat.id: feat.task_id for feat in db_features}

for feature in features["features"]:
if (feat_id := feature["id"]) in task_feature_mapping:
feature["properties"]["task_id"] = task_feature_mapping[feat_id]

return features


Expand Down