Skip to content

Commit

Permalink
Handle empty geoshapes and geotraces in data
Browse files Browse the repository at this point in the history
Signed-off-by: Kipchirchir Sigei <[email protected]>
  • Loading branch information
KipSigei committed Oct 12, 2023
1 parent 296ced4 commit 935f086
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
3 changes: 2 additions & 1 deletion onadata/libs/serializers/geojson_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,10 @@ def to_representation(self, instance):
points = instance.json.get(geo_field)
if geo_field in geotrace_xpaths or geo_field in polygon_xpaths:
value = get_values_matching_key(instance.json, geo_field)
# handle empty geoms
try:
points = next(value)
except TypeError:
except StopIteration:
points = None
geometry = (
geometry_from_string(points, simple_style)
Expand Down
10 changes: 8 additions & 2 deletions onadata/libs/utils/dict_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ def _get_values(doc, key):
yield item
elif isinstance(v, list):
for i in v:
for j in _get_values(i, key):
yield j
if isinstance(i, (dict, list)):
try:
for j in _get_values(i, key):
yield j
except StopIteration:
continue
elif i == key:
yield i

return _get_values(doc, key)

Expand Down

0 comments on commit 935f086

Please sign in to comment.