Skip to content

WIP: add bbox endpoint for forms, dataviews, and merged datasets#3066

Closed
FrankApiyo wants to merge 1 commit into
mainfrom
wip/bbox-endpoint
Closed

WIP: add bbox endpoint for forms, dataviews, and merged datasets#3066
FrankApiyo wants to merge 1 commit into
mainfrom
wip/bbox-endpoint

Conversation

@FrankApiyo
Copy link
Copy Markdown
Member

Summary

Adds GET /api/v1/forms/{id}/bbox, /api/v1/dataviews/{id}/bbox, and /api/v1/merged-datasets/{id}/bbox.

Each returns {"bbox": [min_lng, min_lat, max_lng, max_lat] | null} — the extent of geolocated submissions (null when the target has no geolocated rows, so callers can fall back to a default view cleanly).

Motivation

Zonkey's tile-based map view (Martin + form_tiles()) cannot compute bounds client-side since features are paged per MVT tile. A dedicated endpoint lets the frontend fit the viewport on first load without fetching the full submission set.

Design

  • onadata/libs/utils/bbox_tools.py — shared compute_instance_bbox(xform_ids, dataview=None) helper using Django GIS Extent aggregate
  • Filter shape mirrors the form_tiles() PostGIS function (non-deleted, geom non-null, optional dataview query filter) so the fit matches the data Martin serves
  • Three viewset @action methods (XFormViewSet, DataViewViewSet, MergedXFormViewSet) wrap the helper; permissions inherit from each viewset's existing class

Test plan

  • test_bbox_returns_extent_of_geolocated_submissions — XForm with the gps fixture returns a valid 4-tuple bbox
  • test_bbox_null_when_no_geolocated_submissions — returns {"bbox": null} for forms without geoms
  • DataView + MergedXForm bbox actions return the correct shape (null-case covered; the helper is exercised via the XForm extent test)
  • Run full onadata test suite in CI
  • Verify against a live form on stage once deployed

Consumers

  • Zonkey tile map view (TileMapView) → useFormBbox hook → fitBounds on map load. Work in progress on the martin branch of zonkey.

Why WIP

Marking WIP pending:

  • Discussion on whether the bbox_tools helper should live in libs/utils/ or somewhere closer to the logger models
  • Decision on whether to cache the endpoint (bbox changes slowly; 5-min Redis cache would be cheap)
  • Review of whether the null-return for empty datasets is preferable to 404 (I went with null to make the frontend branch simpler)

Adds GET /api/v1/forms/{id}/bbox, /dataviews/{id}/bbox, and
/merged-datasets/{id}/bbox returning {"bbox": [min_lng, min_lat, max_lng,
max_lat] | null} — the extent of geolocated submissions.

Motivation: Zonkey's tile-based map view (Martin + form_tiles()) cannot
compute bounds client-side since features are paged per MVT tile. A
dedicated endpoint lets the frontend fit the viewport on load without
fetching the full submission set.

Implementation:
- onadata/libs/utils/bbox_tools.py: shared `compute_instance_bbox` helper
  using Django GIS Extent aggregate. Mirrors the filter shape of the
  `form_tiles()` PostGIS function (non-deleted, geom non-null, optional
  dataview query filter) so the fit matches the data Martin serves.
- Three viewset @action methods (XForm/DataView/MergedXForm) wrapping the
  helper. Permissions inherit from the existing viewset class.
- Tests:
  * XForm: extent with the gps fixture, null when no geoms
  * DataView + MergedXForm: null-shape verification (helper itself is
    covered by the XForm extent test since all three viewset actions
    share it)

Returns null rather than 404 when a target has no geolocated rows so
callers can fall back to a default view cleanly.
@FrankApiyo
Copy link
Copy Markdown
Member Author

Superseded by #3072 — rebased onto main and renamed branch from wip/bbox-endpoint to bbox-endpoint.

@FrankApiyo FrankApiyo closed this Apr 22, 2026
@FrankApiyo FrankApiyo deleted the wip/bbox-endpoint branch April 22, 2026 11:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant