Skip to content

spatial arrays pick the first *truthy* Location, not the first *usable* one #6296

Description

@cody-seibert-gsa

Code repo: GSA/datagov-harvester

Problem

When a v3.0 spatial value is an array of Location objects, _unwrap_location() (general_utils.py:1250-1251) selects the first element that is truthy, not the first that is actually usable (i.e. has geometry/bbox/centroid):

if isinstance(input_value, list):
    input_value = next((item for item in input_value if item), None)

A non-empty dict with only prefLabel is truthy, so it wins over a later element that actually carries geometry:

Input Result
[{bbox: POLYGON(…)}, {prefLabel: "Alaska"}] ✅ Polygon
[{prefLabel: "Alaska"}, {bbox: POLYGON(…)}] NULL

Order in the source feed silently determines whether a dataset gets a translated_spatial at all.

Separately (lower priority, same function): even when selection works, only one of N array elements is ever kept, so a multi-region spatial silently drops every region but the first. spatial_shape is a geo_shape OpenSearch field, so combining into a MultiPolygon/GeometryCollection is possible if that's wanted — that part is a product call, not filed as a fix here.

Suggested fix

In _unwrap_location, iterate the list and return the first element that actually unwraps to something usable (has a resolvable geometry/bbox/centroid, or is itself a bare GeoJSON dict), rather than the first truthy one.

Acceptance criteria

  • [{no usable geometry}, {usable geometry}] resolves using the second element
  • [{usable geometry}, {no usable geometry}] is unchanged (still resolves using the first)
  • Existing test_translate_spatial_location_array_uses_first still passes
  • Unit test added for the reversed-order case — there is currently none

Context

Found in #6038 (SPIKE-3), finding 3.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions