Skip to content

Add bbox endpoint for forms, dataviews, and merged datasets#3072

Open
FrankApiyo wants to merge 10 commits into
mainfrom
bbox-endpoint
Open

Add bbox endpoint for forms, dataviews, and merged datasets#3072
FrankApiyo wants to merge 10 commits into
mainfrom
bbox-endpoint

Conversation

@FrankApiyo
Copy link
Copy Markdown
Member

@FrankApiyo FrankApiyo commented Apr 22, 2026

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).

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

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 added 7 commits May 5, 2026 09:09
Move filter_to_field_lookup, get_field_lookup, get_filter_kwargs, and
apply_filters from dataview_viewset into onadata/libs/utils/dataview_filters
so bbox_tools can import them at module top level instead of lazily.

Resolves prospector cyclic-import and import-outside-toplevel findings
between dataview_viewset and bbox_tools.
…ER_AUTH

Pins ona-oidc to the HEAD of feat/per-provider-target-url-after-auth
(PR onaio/ona-oidc#122) so we can stage-test the per-provider
TARGET_URL_AFTER_AUTH fallback before the upstream PR merges. Will be
re-pinned to a release tag once #122 lands.
… proxy

Picks up onaio/ona-oidc#124 — adds proxy endpoints for Keycloak's
Account REST API (sessions, linked-accounts, credentials), plus a
``logout`` action that takes generic ``extra_params`` instead of the
per-param ``id_token_hint`` kwarg. Required for the zonkey SPA's
Account-tab work landing on ``feat/email-otp-auth-flows``.

Pinned to the branch tip (still open as PR #124 against ona-oidc
main); fold the pin to the merged main SHA once that lands.
Pulls in the fix that reshapes Keycloak's
``userCredentialMetadatas[].credential`` response into the flat
``credentials[{id,userLabel?,createdDate?}]`` array the zonkey SPA
reads — without it users with a TOTP credential saw "Authenticator
app — Not set up" because the SPA's lookup hit Keycloak's nested
wire shape.
These ona-oidc version bumps rode along on the branch but are not part of
the bbox endpoint work. Restore requirements/ to match main.
Cache the form, dataview, and merged-dataset bbox responses (1h safety TTL)
and invalidate them whenever a submission is created, edited, hard-deleted,
or soft-deleted via the Instance signals. invalidate_bbox_cache clears the
form's own key plus every DataView and MergedXForm whose extent includes the
affected xform, so the next map-fit request recomputes from current rows.
@FrankApiyo FrankApiyo changed the title WIP: add bbox endpoint for forms, dataviews, and merged datasets Add bbox endpoint for forms, dataviews, and merged datasets Jun 3, 2026
Replace the BBOX_CACHE_TTL constant with get_bbox_cache_ttl(), which reads
the BBOX_CACHE_TTL setting and falls back to BBOX_CACHE_TTL_DEFAULT (1h),
mirroring the existing get_enketo_urls_cache_ttl() pattern.
# Conflicts:
#	onadata/apps/api/viewsets/xform_viewset.py
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.

2 participants