From 81d768f1c01e92eb5d6382222c62a3764f2c2b58 Mon Sep 17 00:00:00 2001 From: CoslaJohn Date: Fri, 9 Feb 2024 14:36:02 +0000 Subject: [PATCH 1/2] Refactor regex in normalized_coordinates method to allow for whitespace between square brackets --- app/models/geozone.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/models/geozone.rb b/app/models/geozone.rb index be2930ca8..eddf5a13f 100644 --- a/app/models/geozone.rb +++ b/app/models/geozone.rb @@ -28,9 +28,10 @@ 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 @@ -39,7 +40,7 @@ class Geozone < ApplicationRecord [] end end - + def coordinates JSON.parse(geojson)["geometry"]["coordinates"] end From 8b3ec8fc79a7eacd5f3adc8ce6b797c76e5af7a0 Mon Sep 17 00:00:00 2001 From: CoslaJohn Date: Thu, 7 Mar 2024 12:08:17 +0000 Subject: [PATCH 2/2] Applied patch to tidy whitespace and add tests --- app/models/geozone.rb | 5 ++- spec/models/geozone_spec.rb | 72 +++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 3 deletions(-) diff --git a/app/models/geozone.rb b/app/models/geozone.rb index eddf5a13f..d3fc484d5 100644 --- a/app/models/geozone.rb +++ b/app/models/geozone.rb @@ -30,8 +30,7 @@ class Geozone < ApplicationRecord if geojson.present? if geojson.match(/"coordinates"\s*:\s*\[\s*\[\s*\[\s*\[/) coordinates.reduce([], :concat).reduce([], :concat) - elsif geojson.match(/"coordinates"\s*:\s*\[\s*\[\s*\[/) - + elsif geojson.match(/"coordinates"\s*:\s*\[\s*\[\s*\[/) coordinates.reduce([], :concat) else coordinates @@ -40,7 +39,7 @@ class Geozone < ApplicationRecord [] end end - + def coordinates JSON.parse(geojson)["geometry"]["coordinates"] end 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