diff --git a/app/models/geozone.rb b/app/models/geozone.rb index be2930ca8..d3fc484d5 100644 --- a/app/models/geozone.rb +++ b/app/models/geozone.rb @@ -28,9 +28,9 @@ class Geozone < ApplicationRecord def normalized_coordinates if geojson.present? - if geojson.match(/"coordinates"\s*:\s*\[{4}/) + if geojson.match(/"coordinates"\s*:\s*\[\s*\[\s*\[\s*\[/) coordinates.reduce([], :concat).reduce([], :concat) - elsif geojson.match(/"coordinates"\s*:\s*\[{3}/) + elsif geojson.match(/"coordinates"\s*:\s*\[\s*\[\s*\[/) coordinates.reduce([], :concat) else coordinates diff --git a/spec/models/geozone_spec.rb b/spec/models/geozone_spec.rb index c2b441f10..973e61139 100644 --- a/spec/models/geozone_spec.rb +++ b/spec/models/geozone_spec.rb @@ -76,5 +76,77 @@ describe Geozone do [-3.9247799675785, 40.8789131852224]] ) end + + it "handles coordinates with three-dimensional arrays" do + geozone = build(:geozone, geojson: '{ + "geometry": { + "type": "Polygon", + "coordinates": [[[40.8792937308316, -3.9259027239257], + [40.8788966596619, -3.9249047078766], + [40.8789131852224, -3.9247799675785]]] + } + }') + + expect(geozone.outline_points).to eq( + [[-3.9259027239257, 40.8792937308316], + [-3.9249047078766, 40.8788966596619], + [-3.9247799675785, 40.8789131852224]] + ) + end + + it "handles coordinates with three-dimensional arrays with spaces between brackets" do + geozone = build(:geozone, geojson: '{ + "geometry": { + "type": "Polygon", + "coordinates": [[ + [40.8792937308316, -3.9259027239257], + [40.8788966596619, -3.9249047078766], + [40.8789131852224, -3.9247799675785] + ]] + } + }') + + expect(geozone.outline_points).to eq( + [[-3.9259027239257, 40.8792937308316], + [-3.9249047078766, 40.8788966596619], + [-3.9247799675785, 40.8789131852224]] + ) + end + + it "handles coordinates with four-dimensional arrays" do + geozone = build(:geozone, geojson: '{ + "geometry": { + "type": "Polygon", + "coordinates": [[[[40.8792937308316, -3.9259027239257], + [40.8788966596619, -3.9249047078766], + [40.8789131852224, -3.9247799675785]]]] + } + }') + + expect(geozone.outline_points).to eq( + [[-3.9259027239257, 40.8792937308316], + [-3.9249047078766, 40.8788966596619], + [-3.9247799675785, 40.8789131852224]] + ) + end + + it "handles coordinates with four-dimensional arrays with spaces between brackets" do + geozone = build(:geozone, geojson: '{ + "geometry": { + "type": "Polygon", + "coordinates": [[[ + [40.8792937308316, -3.9259027239257], + [40.8788966596619, -3.9249047078766], + [40.8789131852224, -3.9247799675785] + ]]] + } + }') + + expect(geozone.outline_points).to eq( + [[-3.9259027239257, 40.8792937308316], + [-3.9249047078766, 40.8788966596619], + [-3.9247799675785, 40.8789131852224]] + ) + end end end