Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,24 @@ object FastDWithin {

override def evaluate(obj: AnyRef): Boolean = {
val geom = exp1.evaluate(obj).asInstanceOf[Geometry]
if (geom == null || envelope.distance(geom.getEnvelopeInternal) > maxDegrees) { false } else {

if (geom == null) {
false
} else {
val op = new DistanceOp(geometry, geom)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable names geometry and geom are too similar and thus confusing. I think we should change these to something more descriptive.

op.distance <= minDegrees || {
val Array(p1, p2) = op.nearestPoints()
val calculator = calculators.get
calculator.setStartingGeographicPoint(p1.x, p1.y)

calculator.setStartingGeographicPoint(p1.x - 360, p1.y)
calculator.setDestinationGeographicPoint(p2.x, p2.y)
calculator.getOrthodromicDistance <= meters
val dist1 = calculator.getOrthodromicDistance

calculator.setStartingGeographicPoint(p1.x + 360, p1.y)
calculator.setDestinationGeographicPoint(p2.x, p2.y)
val dist2 = calculator.getOrthodromicDistance
Copy link
Contributor

@elahrvivaz elahrvivaz Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as another optimization here, we could return early if dist1 <= meters and not calculate dist2. but we also need to calculate dist0, which would be the unmodified point. I'm a little surprised none of the existing unit tests failed with this change :( before changing anything, could you add a test for (0 0) to (1 0) (or something similar) and verify that it fails? possibly the calculator is accounting for the anti-meridian wrapping?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a unit test (db811e3), but it passes, which I'm not sure is the intended behavior.

However, I think the calculator computes the shortest distance between two points on the Earth's surface, so it's likely that it already accounts for the anti-meridian wrapping.


Math.min(dist1, dist2) <= meters
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,21 @@ class FastFilterFactoryTest extends Specification {
FastFilterFactory.toFilter(sft, "s < '100'").evaluate(sf) must beFalse
FastFilterFactory.toFilter(sft, "s <= '100'").evaluate(sf) must beFalse
}

"calculate the distance between points horizontally across the antimeridian" >> {
val sft = SimpleFeatureTypes.createType("test", "geom:Point:srid=4326")
val feature = ScalaSimpleFeature.create(sft, "1", "POINT (178 0)")
val ecql = "dwithin('POINT (-179 0)', geom, 2500, kilometers)"
val fast = FastFilterFactory.toFilter(sft, ecql)
fast.evaluate(feature) must beTrue
}

"calculate the distance between points diagonally across the antimeridian" >> {
val sft = SimpleFeatureTypes.createType("test", "geom:Point:srid=4326")
val feature = ScalaSimpleFeature.create(sft, "1", "POINT (178 3.1415)")
val ecql = "dwithin('POINT (-179 -2.7182)', geom, 2500, kilometers)"
val fast = FastFilterFactory.toFilter(sft, ecql)
fast.evaluate(feature) must beTrue
}
}
}