Applied patch to tidy whitespace and add tests

This commit is contained in:
CoslaJohn
2024-03-07 12:08:17 +00:00
parent 81d768f1c0
commit 8b3ec8fc79
2 changed files with 74 additions and 3 deletions

View File

@@ -30,8 +30,7 @@ class Geozone < ApplicationRecord
if geojson.present? if geojson.present?
if geojson.match(/"coordinates"\s*:\s*\[\s*\[\s*\[\s*\[/) if geojson.match(/"coordinates"\s*:\s*\[\s*\[\s*\[\s*\[/)
coordinates.reduce([], :concat).reduce([], :concat) coordinates.reduce([], :concat).reduce([], :concat)
elsif geojson.match(/"coordinates"\s*:\s*\[\s*\[\s*\[/) elsif geojson.match(/"coordinates"\s*:\s*\[\s*\[\s*\[/)
coordinates.reduce([], :concat) coordinates.reduce([], :concat)
else else
coordinates coordinates
@@ -40,7 +39,7 @@ class Geozone < ApplicationRecord
[] []
end end
end end
def coordinates def coordinates
JSON.parse(geojson)["geometry"]["coordinates"] JSON.parse(geojson)["geometry"]["coordinates"]
end end

View File

@@ -76,5 +76,77 @@ describe Geozone do
[-3.9247799675785, 40.8789131852224]] [-3.9247799675785, 40.8789131852224]]
) )
end 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
end end