-
-
Notifications
You must be signed in to change notification settings - Fork 97
Description
While trying to integrate the spatial index and Near Filter into my application based on the published documentation, I was unable to produce the expected results. The filter is returning many records far outside the specified radius.
The documentation shows a sample query that takes lat/long coordinate on Earth as the center point and floating point quantity in meters as the distance argument. However, the two unit tests covering NearFilter
use a location value of "POINT (490 490)"
, which is outside the possible range that can correspond to a lat/long pair on a globe.
Summary
The NearFilter behavior for a range of inputs is consistent with what one would expect if neither the code in Nitrite nor the code in the JTS library are doing anything to (1) convert meters to/from degrees of latitude/longitude; or (2) account for the curvature of the Earth.
I found this root cause hypothesis was supported by examination of the existing unit tests in nitrite-spatial/src/test/
, the implementation in nitrite-spatial/src/main/
, and the JTS library's code and documentation.
Repro steps
I have opened a PR on my own personal fork of nitrite where you can see simple unit tests cases based on real-world lat-long data designed to check whether the Spatial distance search is properly. (See this map for an illustration of the unit test data.)
Here is a simplified repro at the equator:
- Choose a center point
$P_C$ at$( 0 \degree, 0 \degree )$ in the Atlantic Ocean. - Let
$P_{E1}$ be a point approximately 111km due east of$P_C$ , at$( 0 \degree, +1 \degree )$ - Let
$P_{E2}$ be a point approximately 1km due east of$P_C$ , at$( 0 \degree , +0.01 \degree)$ - Use
NearFilter
to find the subset of$\{ P_{E1} , P_{E2} \}$ within 2km and within 20cm of$P_C$ .within2kmFilter = where("geometry").near(centerPoint, 2000.0 /* meters */); within20cmFilter = where("geometry").near(centerPoint, 0.02 /* meters */);
Observed vs. Expected results
Observed | Expected | |
---|---|---|
within2kmFilter |
returns |
returns |
within20cmFilter |
returns |
empty result set |