Skip to content

Commit

Permalink
Merge branch 'feat/entity-based-odk' of https://github.com/hotosm/fmtm
Browse files Browse the repository at this point in the history
…into feat/entity-based-odk
  • Loading branch information
spwoodcock committed Mar 29, 2024
2 parents 0497fdc + 1b01f09 commit c107e1c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 34 deletions.
4 changes: 3 additions & 1 deletion src/backend/app/db/db_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,9 @@ class DbXForm(Base):

__tablename__ = "xforms"
id = cast(int, Column(Integer, primary_key=True, autoincrement=True))
project_id = cast(int, Column(Integer))
project_id = cast(
int, Column(Integer, ForeignKey("projects.id"), name="fk_projects", index=True)
)
form_id = cast(str, Column(String))
category = cast(str, Column(String))

Expand Down
33 changes: 16 additions & 17 deletions src/backend/app/db/postgis_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,8 +549,9 @@ def get_address_from_lat_lon(latitude, longitude):

country = address.get("country", "")
city = address.get("city", "")
state = address.get("state", "")

address_str = f"{city},{country}"
address_str = f"{city},{country}" if city else f"{state},{country}"

if not address_str or address_str == ",":
log.error("Getting address string failed")
Expand Down Expand Up @@ -581,23 +582,22 @@ async def geojson_to_javarosa_geom(geojson_geometry: dict) -> str:
Returns:
str: A string representing the geometry in JavaRosa format.
"""
# Ensure the GeoJSON geometry is of type Polygon
# FIXME support other geom types
if geojson_geometry["type"] != "Polygon":
raise ValueError("Input must be a GeoJSON Polygon")

coordinates = geojson_geometry["coordinates"][
0
] # Extract the coordinates of the Polygon
javarosa_geometry = []

for coordinate in coordinates:
lon, lat = coordinate[:2]
javarosa_geometry.append(f"{lat} {lon} 0.0 0.0")
coordinates = []
if geojson_geometry["type"] in ["Point", "LineString", "MultiPoint"]:
coordinates = [geojson_geometry.get("coordinates")]
elif geojson_geometry["type"] in ["Polygon", "MultiLineString"]:
coordinates = geojson_geometry.get("coordinates")
elif geojson_geometry["type"] == "MultiPolygon":
coordinates = sum(geojson_geometry.get("coordinates", []), [])
else:
raise ValueError("Unsupported GeoJSON geometry type")

javarosa_geometry_string = ";".join(javarosa_geometry)
javarosa_geometry = [
f"{lat} {lon} 0.0 0.0"
for lon, lat in (coordinate[:2] for coordinate in coordinates)
]

return javarosa_geometry_string
return ";".join(javarosa_geometry)


async def get_entity_dicts_from_task_geojson(
Expand All @@ -607,7 +607,6 @@ async def get_entity_dicts_from_task_geojson(
) -> dict:
"""Get a dictionary of Entity info mapped from task geojsons."""
id_properties_dict = {}

features = task_data_extract.get("features", [])
for feature in features:
geometry = feature.get("geometry")
Expand Down
25 changes: 9 additions & 16 deletions src/backend/app/tasks/tasks_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,22 +170,15 @@ async def task_features_count(
log.warning(msg)
raise HTTPException(status_code=404, detail=msg)

feature_count_task_dict = {f"{record[0]}": record[1] for record in feature_counts}

project_name_prefix = project.project_name_prefix

for x in odk_details:
# Strip everything except task id from xmlFormId
task_id = f"{x['xmlFormId']}".strip(f"{project_name_prefix}_task_")

data.append(
{
"task_id": task_id,
"submission_count": x["submissions"],
"last_submission": x["lastSubmission"],
"feature_count": feature_count_task_dict[task_id],
}
)
data.extend(
{
"task_id": record[0],
"submission_count": odk_details[0]["submissions"],
"last_submission": odk_details[0]["lastSubmission"],
"feature_count": record[1],
}
for record in feature_counts
)

return data

Expand Down
4 changes: 4 additions & 0 deletions src/backend/migrations/002-add-xforms-table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ ALTER SEQUENCE public.xforms_id_seq OWNED BY public.xforms.id;
ALTER TABLE ONLY public.xforms ALTER COLUMN id SET DEFAULT nextval('public.xforms_id_seq'::regclass);
ALTER TABLE ONLY public.xforms
ADD CONSTRAINT xforms_pkey PRIMARY KEY (id);
ALTER TABLE public.xforms
ADD CONSTRAINT fk_projects
FOREIGN KEY (project_id)
REFERENCES projects(id);

-- Commit the transaction
COMMIT;

0 comments on commit c107e1c

Please sign in to comment.