Skip to content

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

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

WIP: add bbox endpoint for forms, dataviews, and merged datasets#3072
FrankApiyo wants to merge 6 commits into
mainfrom
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)

Replaces #3066 (rebased onto main, branch renamed from wip/bbox-endpoint to bbox-endpoint).

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