Skip to content

Commit

Permalink
Geoms: invalidate coplanar geoms
Browse files Browse the repository at this point in the history
Fixes #103
  • Loading branch information
ewlarson committed Oct 25, 2024
1 parent 3e7016e commit 7aa05e4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
12 changes: 9 additions & 3 deletions app/models/document/geom_validator.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require "rgeo"
require 'rgeo/wkrep/wkt_parser'
require "rgeo/wkrep/wkt_parser"

# GEOM Validation
#
Expand Down Expand Up @@ -128,7 +128,13 @@ def coplanar_points?(geom)
# Parse the WKT to extract points
factory = RGeo::Cartesian.factory
wkt_parser = RGeo::WKRep::WKTParser.new(factory)
parsed_geom = wkt_parser.parse(geom)

# Parse the WKT
begin
parsed_geom = wkt_parser.parse(geom)
rescue => e
return false
end

# Ensure the geometry is a polygon
return false unless parsed_geom.is_a?(RGeo::Feature::Polygon)
Expand All @@ -152,7 +158,7 @@ def collinear?(points)
initial_slope = slope(x0, y0, x1, y1)

# Check if all subsequent points have the same slope with the first point
points[2..-1].all? do |point|
points[2..].all? do |point|
x, y = point.x, point.y
slope(x0, y0, x, y) == initial_slope
end
Expand Down
10 changes: 10 additions & 0 deletions test/models/document/geom_validator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,14 @@ def setup
assert_not valid, "Expected invalid POLYGON to be rejected"
assert_includes @document.errors["locn_geometry"].join, "Invalid geometry"
end

test "rejects coplanar points" do
@document.locn_geometry = "POLYGON((-180 90, 180 90, 180 -90, -180 -90, -180 90))"

validator = Document::GeomValidator.new
valid = validator.validate(@document)

assert_not valid, "Expected coplanar points to be rejected"
assert_includes @document.errors["locn_geometry"].join, "Invalid polygon: all points are coplanar input, Solr will not index"
end
end

0 comments on commit 7aa05e4

Please sign in to comment.