Skip to content

Commit 43e14cc

Browse files
committed
Handle some edge cases
- bbox is sometimes in a different field (ticket opened to understand why) - bbox sometimes has spaces and sometimes not so we handle both - convert strings to floats during initial string extraction rather than during coords array creation - if bbox_array is not 4 elements, don't use it - if we don't have a record at all, don't try to use it
1 parent 791e78a commit 43e14cc

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

app/controllers/record_controller.rb

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,34 @@ def view
2121

2222
private
2323

24+
# Converts a bounding box into a top left, bottom right set of coordinates
2425
def bounding_box_to_coords
26+
return unless @record.present?
2527
return unless geospatial_coordinates?(@record['locations'])
2628

29+
# Our preference is to use the `Bounding Box` kind
2730
raw_bbox = @record['locations'].select { |l| l if l['kind'] == 'Bounding Box' }.first
31+
32+
# If we had no `Bounding Box` kind, see if we have a `Geometry kind`
33+
if raw_bbox.blank?
34+
raw_bbox = @record['locations'].select { |l| l if l['kind'] == 'Geometry' }.first
35+
end
36+
37+
return unless raw_bbox.present?
38+
39+
# extract just the geo coordinates and remove the extra syntax
2840
bbox = raw_bbox['geoshape'].sub('BBOX (', '').sub(')', '')
29-
bbox_array = bbox.split(', ')
30-
coords = [[bbox_array[2].to_f, bbox_array[0].to_f], [bbox_array[3].to_f, bbox_array[1].to_f]]
41+
42+
# conver the string into an array of floats
43+
bbox_array = bbox.split(',').map!(&:strip).map!(&:to_f)
44+
45+
# Protect against unexpected data
46+
if bbox_array.count != 4
47+
Rails.logger.info("Unexpected Bounding Box: #{raw_bbox}")
48+
return
49+
end
50+
51+
coords = [[bbox_array[2], bbox_array[0]], [bbox_array[3], bbox_array[1]]]
3152
Rails.logger.info("Raw BBox: #{raw_bbox}")
3253
Rails.logger.info("Rectangle: #{coords}")
3354
coords

0 commit comments

Comments
 (0)