Support FeatureCollection and MultiPolygon in geozones

We're reworking the format validation to correctly interpret feature
collection, feature, and geometry, according to RFC 7946 [1].

Since Leaflet interprets GeoJSON format, we're rendering the GeoJSON as
a layer instead of as a set of points. For that, we're normalizing the
GeoJSON to make sure it contains either a Feature or a
FeatureCollection. We're also adding the Leaflet images to the assets
path so the markers used for point geometries are rendered correctly.

Note we no longer allow a GeoJSON containing a geometry but not a
defined type. Since there might be invalid GeoJSON in existing Consul
Democracy databases, we're normalizing these existing geometry objects
to be part of a feature object.

We're also wrapping the outline points in a FeatureCollection object
because most of the large GIS systems eg ArcGIS, QGIS export geojson as
a complete FeatureCollection.

[1] https://datatracker.ietf.org/doc/html/rfc7946

Co-authored-by: Javi Martín <javim@elretirao.net>
This commit is contained in:
CoslaJohn
2024-07-19 12:13:43 +01:00
committed by Javi Martín
parent bf4e79d42b
commit 5dbe2cbf24
12 changed files with 635 additions and 112 deletions

View File

@@ -0,0 +1,329 @@
require "rails_helper"
describe GeojsonFormatValidator do
before do
dummy_model = Class.new do
include ActiveModel::Model
attr_accessor :geojson
validates :geojson, geojson_format: true
end
stub_const("DummyModel", dummy_model)
end
let(:record) { DummyModel.new }
it "is not valid with an empty hash" do
record.geojson = "{}"
expect(record).not_to be_valid
end
it "is not valid with arbitrary keys" do
record.geojson = '{ "invalid": "yes" }'
expect(record).not_to be_valid
end
it "is not valid without a type" do
record.geojson = '{ "coordinates": [1.23, 4.56] }'
expect(record).not_to be_valid
end
it "is not valid without a type but a geometry" do
record.geojson = '{ "geometry": { "type": "Point", "coordinates": [1.23, 4.56] } }'
expect(record).not_to be_valid
end
context "Point geometry" do
it "is not valid without coordinates" do
record.geojson = '{ "type": "Point" }'
expect(record).not_to be_valid
end
it "is not valid with only one the longitude" do
record.geojson = '{ "type": "Point", "coordinates": 1.23 }'
expect(record).not_to be_valid
end
it "is not valid with non-numerical coordinates" do
record.geojson = '{ "type": "Point", "coordinates": ["1.23", "4.56"] }'
expect(record).not_to be_valid
end
it "is not valid with 3-dimensional coordinates" do
record.geojson = '{ "type": "Point", "coordinates": [1.23, 4.56, 7.89] }'
expect(record).not_to be_valid
end
it "is not valid with multiple coordinates" do
record.geojson = '{ "type": "Point", "coordinates": [[1.23, 4.56], [7.89, 10.11]] }'
expect(record).not_to be_valid
end
it "is not valid with a longitude above 180" do
record.geojson = '{ "type": "Point", "coordinates": [180.01, 4.56] }'
expect(record).not_to be_valid
end
it "is not valid with a longitude below -180" do
record.geojson = '{ "type": "Point", "coordinates": [-180.01, 4.56] }'
expect(record).not_to be_valid
end
it "is not valid with a latitude above 90" do
record.geojson = '{ "type": "Point", "coordinates": [1.23, 90.01] }'
expect(record).not_to be_valid
end
it "is not valid with a latitude below -90" do
record.geojson = '{ "type": "Point", "coordinates": [1.23, -90.01] }'
expect(record).not_to be_valid
end
it "is valid with coordinates in the valid range" do
record.geojson = '{ "type": "Point", "coordinates": [1.23, 4.56] }'
expect(record).to be_valid
end
it "is valid with coordinates at the positive end of the range" do
record.geojson = '{ "type": "Point", "coordinates": [180.0, 90.0] }'
expect(record).to be_valid
end
it "is valid with coordinates at the negative end of the range" do
record.geojson = '{ "type": "Point", "coordinates": [-180.0, -90.0] }'
expect(record).to be_valid
end
end
context "LineString or MultiPoint geometry" do
it "is not valid with a one-dimensional array of coordinates" do
record.geojson = '{ "type": "LineString", "coordinates": [1.23, 4.56] }'
expect(record).not_to be_valid
record.geojson = '{ "type": "MultiPoint", "coordinates": [1.23, 4.56] }'
expect(record).not_to be_valid
end
it "is valid with a two-dimensional array including only one point" do
record.geojson = '{ "type": "LineString", "coordinates": [[1.23, 4.56]] }'
expect(record).to be_valid
record.geojson = '{ "type": "MultiPoint", "coordinates": [[1.23, 4.56]] }'
expect(record).to be_valid
end
it "is not valid when some coordinates are invalid" do
record.geojson = '{ "type": "LineString", "coordinates": [[1.23, 4.56], [180.01, 4.56]] }'
expect(record).not_to be_valid
record.geojson = '{ "type": "MultiPoint", "coordinates": [[1.23, 4.56], [180.01, 4.56]] }'
expect(record).not_to be_valid
end
it "is valid when all the coordinates are valid" do
record.geojson = '{ "type": "LineString", "coordinates": [[1.23, 4.56], [7.89, 4.56]] }'
expect(record).to be_valid
record.geojson = '{ "type": "MultiPoint", "coordinates": [[1.23, 4.56], [7.89, 4.56]] }'
expect(record).to be_valid
end
end
context "GeometryCollection" do
it "is not valid if it doesn't contain geometries" do
record.geojson = '{ "type": "GeometryCollection" }'
expect(record).not_to be_valid
end
it "is not valid if geometries is not an array" do
record.geojson = <<~JSON
{
"type": "GeometryCollection",
"geometries": { "type": "Point", "coordinates": [1.23, 4.56] }
}
JSON
expect(record).not_to be_valid
end
it "is valid if the array of geometries is empty" do
record.geojson = '{ "type": "GeometryCollection", "geometries": [] }'
expect(record).to be_valid
end
it "is valid if all geometries are valid" do
record.geojson = <<~JSON
{
"type": "GeometryCollection",
"geometries": [
{
"type": "Point",
"coordinates": [100.0, 0.0]
},
{
"type": "LineString",
"coordinates": [
[101.0, 0.0],
[102.0, 1.0]
]
}
]
}
JSON
expect(record).to be_valid
end
it "is not valid if some geometries are invalid" do
record.geojson = <<~JSON
{
"type": "GeometryCollection",
"geometries": [
{
"type": "Point",
"coordinates": [100.0, 0.0]
},
{
"type": "LineString",
"coordinates": [101.0, 0.0]
}
]
}
JSON
expect(record).not_to be_valid
end
end
context "Feature" do
it "is valid with a valid geometry" do
record.geojson = <<~JSON
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [1.23, 4.56]
}
}
JSON
expect(record).to be_valid
end
it "is not valid with a valid geometry" do
record.geojson = <<~JSON
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [1.23]
}
}
JSON
expect(record).not_to be_valid
end
end
context "FeatureCollection" do
it "is not valid without features" do
record.geojson = '{ "type": "FeatureCollection" }'
end
it "is not valid if features is not an array" do
record.geojson = <<~JSON
{
"type": "FeatureCollection",
"features": {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [1.23, 4.56]
}
}
}
JSON
end
it "is valid if the array of features is empty" do
record.geojson = '{ "type": "FeatureCollection", "features": [] }'
expect(record).to be_valid
end
it "is valid if all features are valid" do
record.geojson = <<~JSON
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [1.23, 4.56]
}
},
{
"type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [[101.0, 0.0], [102.0, 1.0]]
}
}
]
}
JSON
expect(record).to be_valid
end
it "is not valid if some features are invalid" do
record.geojson = <<~JSON
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [1.23, 4.56]
}
},
{
"type": "LineString",
"coordinates": [[101.0, 0.0], [102.0, 1.0]]
}
]
}
JSON
expect(record).not_to be_valid
end
end
end

View File

@@ -18,10 +18,20 @@ describe Geozone do
end
it "is not valid with invalid geojson file format" do
geozone.geojson = '{"geo\":{"type":"Incorrect key","coordinates": [
[40.8792937308316, -3.9259027239257],
[40.8788966596619, -3.9249047078766],
[40.8789131852224, -3.9247799675785]]}}'
geozone.geojson = <<~JSON
{
"type": "Feature",
"geometry": {
"type": "Incorrect",
"coordinates": [
[40.8792937308316, -3.9259027239257],
[40.8788966596619, -3.9249047078766],
[40.8789131852224, -3.9247799675785]
]
}
}
JSON
expect(geozone).not_to be_valid
end
@@ -54,99 +64,149 @@ describe Geozone do
end
describe "#outline_points" do
it "returns empty array when geojson is nil" do
expect(geozone.outline_points).to eq([])
it "returns nil when geojson is nil" do
geozone.geojson = nil
expect(geozone.outline_points).to be nil
end
it "returns coordinates array when geojson is not nil" do
geozone = build(:geozone, geojson: '{
"geometry": {
"type": "Polygon",
"coordinates": [
[40.8792937308316, -3.9259027239257],
[40.8788966596619, -3.9249047078766],
[40.8789131852224, -3.9247799675785]
]
it "returns normalized feature collection when geojson is a valid FeatureCollection" do
geozone.geojson = <<~JSON
{
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [[
[-3.9259027239257, 40.8792937308316],
[-3.9249047078766, 40.8788966596619],
[-3.9247799675785, 40.8789131852224],
[-3.9259027239257, 40.8792937308316]
]]
}
}]
}
}')
JSON
expect(geozone.outline_points).to eq(
[[-3.9259027239257, 40.8792937308316],
[-3.9249047078766, 40.8788966596619],
[-3.9247799675785, 40.8789131852224]]
)
expected = {
type: "FeatureCollection",
features: [{
type: "Feature",
geometry: {
type: "Polygon",
coordinates: [[
[-3.9259027239257, 40.8792937308316],
[-3.9249047078766, 40.8788966596619],
[-3.9247799675785, 40.8789131852224],
[-3.9259027239257, 40.8792937308316]
]]
}
}]
}
expect(geozone.outline_points).to eq expected.to_json
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]]]
it "returns normalized feature collection when geojson is a valid Feature" do
geozone.geojson = <<~JSON
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [[
[-3.9259027239257, 40.8792937308316],
[-3.9249047078766, 40.8788966596619],
[-3.9247799675785, 40.8789131852224],
[-3.9259027239257, 40.8792937308316]
]]
}
}
}')
JSON
expect(geozone.outline_points).to eq(
[[-3.9259027239257, 40.8792937308316],
[-3.9249047078766, 40.8788966596619],
[-3.9247799675785, 40.8789131852224]]
)
expected = {
type: "FeatureCollection",
features: [{
type: "Feature",
geometry: {
type: "Polygon",
coordinates: [[
[-3.9259027239257, 40.8792937308316],
[-3.9249047078766, 40.8788966596619],
[-3.9247799675785, 40.8789131852224],
[-3.9259027239257, 40.8792937308316]
]]
}
}]
}
expect(geozone.outline_points).to eq expected.to_json
end
it "handles coordinates with three-dimensional arrays with spaces between brackets" do
geozone = build(:geozone, geojson: '{
"geometry": {
it "returns normalized feature collection when geojson is a valid Geometry object" do
geozone.geojson = <<~JSON
{
"geometry": {
"type": "Polygon",
"coordinates": [[
[-3.9259027239257, 40.8792937308316],
[-3.9249047078766, 40.8788966596619],
[-3.9247799675785, 40.8789131852224],
[-3.9259027239257, 40.8792937308316]
]]
}
}
JSON
expected = {
type: "FeatureCollection",
features: [{
type: "Feature",
geometry: {
type: "Polygon",
coordinates: [[
[-3.9259027239257, 40.8792937308316],
[-3.9249047078766, 40.8788966596619],
[-3.9247799675785, 40.8789131852224],
[-3.9259027239257, 40.8792937308316]
]]
}
}]
}
expect(geozone.outline_points).to eq expected.to_json
end
it "returns normalized feature collection when geojson is a valid top-level Geometry object" do
geozone.geojson = <<~JSON
{
"type": "Polygon",
"coordinates": [[
[40.8792937308316, -3.9259027239257],
[40.8788966596619, -3.9249047078766],
[40.8789131852224, -3.9247799675785]
[-3.9259027239257, 40.8792937308316],
[-3.9249047078766, 40.8788966596619],
[-3.9247799675785, 40.8789131852224],
[-3.9259027239257, 40.8792937308316]
]]
}
}')
JSON
expect(geozone.outline_points).to eq(
[[-3.9259027239257, 40.8792937308316],
[-3.9249047078766, 40.8788966596619],
[-3.9247799675785, 40.8789131852224]]
)
end
expected = {
type: "FeatureCollection",
features: [{
type: "Feature",
geometry: {
type: "Polygon",
coordinates: [[
[-3.9259027239257, 40.8792937308316],
[-3.9249047078766, 40.8788966596619],
[-3.9247799675785, 40.8789131852224],
[-3.9259027239257, 40.8792937308316]
]]
}
}]
}
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]]
)
expect(geozone.outline_points).to eq expected.to_json
end
end
end