Skip to content

Commit

Permalink
fix: task id on task polygon and feature extracts (#976)
Browse files Browse the repository at this point in the history
* feat: added qr code in task list

* added qr code in taskout schema

* feat: added task id on task polygon file

* feat: added task_id on features extracted file

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* perf: prevent db call for each feat in task geojson

* refactor: typo task_id_mapping --> task_feature_mapping

---------

Co-authored-by: sujanadh <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: spwoodcock <[email protected]>
  • Loading branch information
4 people authored Nov 21, 2023
1 parent 517c3bb commit 7856df2
Showing 1 changed file with 24 additions and 9 deletions.
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

0 comments on commit 7856df2

Please sign in to comment.