Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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 @@ -82,7 +82,7 @@ class UpgradeCompatibilityTest {
+ "WITH toInteger(last(split(elementId(node), ':'))) AS id ORDER BY id RETURN collect(id) AS ids",
"5 withinDistance", "CALL spatial.withinDistance($l, {lon: 15.2, lat: 60.1}, 100) YIELD node, distance "
+ "RETURN toInteger(last(split(elementId(node), ':'))) AS id, round(distance, 4) AS d ORDER BY d, id",
"6 closest", "CALL spatial.closest($l, {lon: 15.2, lat: 60.1}, 1.0) YIELD node "
"6 closest", "CALL spatial.closest($l, {lon: 15.2, lat: 60.1}, 100) YIELD node "
+ "WITH toInteger(last(split(elementId(node), ':'))) AS id ORDER BY id RETURN collect(id) AS ids",
"7 cql", "CALL spatial.cql($l, 'BBOX(the_geom, 15, 60, 16, 61)') YIELD node RETURN count(node) AS n");

Expand Down
8 changes: 4 additions & 4 deletions compat-tests/src/test/resources/compat/2025.07/expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ native_places | withinDistance
id=254, d=6.2113
id=255, d=31.0377
native_places | closest
ids=[254, 255, 256]
ids=[254, 255]
native_places | cql
n=2
p_Geohash | layerMeta
Expand Down Expand Up @@ -348,7 +348,7 @@ p_WKB | withinDistance
id=118, d=6.2113
id=119, d=47.4494
p_WKB | closest
ids=[117, 118]
ids=[117, 118, 119]
p_WKB | cql
n=3
p_WKT | layerMeta
Expand All @@ -364,7 +364,7 @@ p_WKT | withinDistance
id=114, d=6.2113
id=115, d=47.4494
p_WKT | closest
ids=[113, 114]
ids=[113, 114, 115]
p_WKT | cql
n=3
p_ZOrder | layerMeta
Expand Down Expand Up @@ -395,7 +395,7 @@ places | withinDistance
id=251, d=6.2113
id=252, d=31.0377
places | closest
ids=[251, 252, 253]
ids=[251, 252]
places | cql
n=2
rtree20 | layerMeta
Expand Down
8 changes: 4 additions & 4 deletions compat-tests/src/test/resources/compat/2026.10.0/expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ native_places | withinDistance
id=254, d=6.2113
id=255, d=31.0377
native_places | closest
ids=[254, 255, 256]
ids=[254, 255]
native_places | cql
n=2
p_Geohash | layerMeta
Expand Down Expand Up @@ -348,7 +348,7 @@ p_WKB | withinDistance
id=118, d=6.2113
id=119, d=47.4494
p_WKB | closest
ids=[117, 118]
ids=[117, 118, 119]
p_WKB | cql
n=3
p_WKT | layerMeta
Expand All @@ -364,7 +364,7 @@ p_WKT | withinDistance
id=114, d=6.2113
id=115, d=47.4494
p_WKT | closest
ids=[113, 114]
ids=[113, 114, 115]
p_WKT | cql
n=3
p_ZOrder | layerMeta
Expand Down Expand Up @@ -395,7 +395,7 @@ places | withinDistance
id=251, d=6.2113
id=252, d=31.0377
places | closest
ids=[251, 252, 253]
ids=[251, 252]
places | cql
n=2
rtree20 | layerMeta
Expand Down
8 changes: 4 additions & 4 deletions compat-tests/src/test/resources/compat/5.19.0/expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ native_places | withinDistance
id=249, d=6.2113
id=250, d=31.0377
native_places | closest
ids=[249, 250, 251]
ids=[249, 250]
native_places | cql
n=2
p_Geohash | layerMeta
Expand Down Expand Up @@ -307,7 +307,7 @@ p_WKB | withinDistance
id=115, d=6.2113
id=116, d=47.4494
p_WKB | closest
ids=[114, 115]
ids=[114, 115, 116]
p_WKB | cql
n=3
p_WKT | layerMeta
Expand All @@ -323,7 +323,7 @@ p_WKT | withinDistance
id=111, d=6.2113
id=112, d=47.4494
p_WKT | closest
ids=[110, 111]
ids=[110, 111, 112]
p_WKT | cql
n=3
p_ZOrder | layerMeta
Expand Down Expand Up @@ -354,7 +354,7 @@ places | withinDistance
id=246, d=6.2113
id=247, d=31.0377
places | closest
ids=[246, 247, 248]
ids=[246, 247]
places | cql
n=2
rtree20 | layerMeta
Expand Down
8 changes: 4 additions & 4 deletions compat-tests/src/test/resources/compat/5.26.0/expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ native_places | withinDistance
id=254, d=6.2113
id=255, d=31.0377
native_places | closest
ids=[254, 255, 256]
ids=[254, 255]
native_places | cql
n=2
p_Geohash | layerMeta
Expand Down Expand Up @@ -328,7 +328,7 @@ p_WKB | withinDistance
id=118, d=6.2113
id=119, d=47.4494
p_WKB | closest
ids=[117, 118]
ids=[117, 118, 119]
p_WKB | cql
n=3
p_WKT | layerMeta
Expand All @@ -344,7 +344,7 @@ p_WKT | withinDistance
id=114, d=6.2113
id=115, d=47.4494
p_WKT | closest
ids=[113, 114]
ids=[113, 114, 115]
p_WKT | cql
n=3
p_ZOrder | layerMeta
Expand Down Expand Up @@ -375,7 +375,7 @@ places | withinDistance
id=251, d=6.2113
id=252, d=31.0377
places | closest
ids=[251, 252, 253]
ids=[251, 252]
places | cql
n=2
rtree20 | layerMeta
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ RETURN sum(count) AS count
.runCypher("CALL spatial.addLayer('geom','geohash','lon:lat')", ExampleCypher::skipResult)
.runCypher("CALL spatial.addWKTs('geom',$wkt)",
c -> c.setParams(Map.of("wkt", points)))
.runCypher("CALL spatial.closest('geom',{lon:15.0, lat:60.0}, 1.0)");
.runCypher("CALL spatial.closest('geom',{lon:15.0, lat:60.0}, 20.0)");

docExample("spatial.cql", "Find geometries using CQL")
.runCypher("CALL spatial.addWKTLayer('geom','wkt') YIELD node")
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/modules/ROOT/partials/generated/api/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Finds all geometry nodes in the given layer within the lower left and upper righ
|label:procedure[]
|xref:api/spatial/spatial.closest.adoc[spatial.closest icon:book[]]

Finds all geometry nodes in the layer within the distance to the given coordinate
Finds all geometry nodes in the layer within the distance to the given coordinate, closest first
|label:procedure[]
|xref:api/spatial/spatial.cql.adoc[spatial.cql icon:book[]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ a|

[source,cypher]
----
CALL spatial.closest('geom',{lon:15.0, lat:60.0}, 1.0)
CALL spatial.closest('geom',{lon:15.0, lat:60.0}, 20.0)
----

.Result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ a|

[source,cypher]
----
CALL spatial.closest('geom',{lon:15.0, lat:60.0}, 1.0)
CALL spatial.closest('geom',{lon:15.0, lat:60.0}, 20.0)
----

.Result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
label:procedure[]

[.emphasis]
Finds all geometry nodes in the layer within the distance to the given coordinate
Finds all geometry nodes in the layer within the distance to the given coordinate, closest first

== Signature

[source]
----
spatial.closest(layerName :: STRING, coordinate :: ANY, distanceInKm :: FLOAT) :: (node :: NODE)
spatial.closest(layerName :: STRING, coordinate :: ANY, distance :: FLOAT) :: (node :: NODE)
----

== Input parameters
Expand All @@ -33,8 +33,8 @@ a|A valid value for the coordinate is a:
* Node or Relationship with properties 'latitude' and 'longitude'
* Node or Relationship with properties 'lat' and 'lon'

|distanceInKm|FLOAT|null
a|The distance in kilometers within which to search for geometries
|distance|FLOAT|null
a|The distance within which to search for geometries: kilometers for layers in a geographic CRS (WGS84, also assumed for layers without a CRS), CRS units otherwise (projected CRS, Cartesian native points)
|===

== Output parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Returns all geometry nodes and their ordered distance in the layer within the di

[source]
----
spatial.withinDistance(layerName :: STRING, coordinate :: ANY, distanceInKm :: FLOAT) :: (node :: NODE, distance :: FLOAT)
spatial.withinDistance(layerName :: STRING, coordinate :: ANY, distance :: FLOAT) :: (node :: NODE, distance :: FLOAT)
----

== Input parameters
Expand All @@ -33,8 +33,8 @@ a|A valid value for the coordinate is a:
* Node or Relationship with properties 'latitude' and 'longitude'
* Node or Relationship with properties 'lat' and 'lon'

|distanceInKm|FLOAT|null
a|The distance in kilometers within which to search for geometries
|distance|FLOAT|null
a|The distance within which to search for geometries: kilometers for layers in a geographic CRS (WGS84, also assumed for layers without a CRS), CRS units otherwise (projected CRS, Cartesian native points)
|===

== Output parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ public interface Constants {
* Node or Relationship with properties 'latitude' and 'longitude'
* Node or Relationship with properties 'lat' and 'lon'
""";
String DOC_DISTANCE = "The distance within which to search for geometries: kilometers for layers in a geographic CRS"
+ " (WGS84, also assumed for layers without a CRS), CRS units otherwise (projected CRS, Cartesian native points)";
String DOC_URI = "The path of the file to import the data from";

String DOC_JTS_GEOMETRY = "A WKT string or a coordinate. " + DOC_COORDINATE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@
import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.linearref.LengthIndexedLine;
import org.locationtech.jts.linearref.LinearLocation;
import org.locationtech.jts.linearref.LocationIndexedLine;
import org.locationtech.jts.operation.distance.DistanceOp;
import org.neo4j.gis.spatial.filter.SearchIntersect;
import org.neo4j.graphdb.Transaction;
import org.neo4j.spatial.api.SpatialRecord;
Expand Down Expand Up @@ -114,26 +112,19 @@ public static List<PointResult> findClosestEdges(Transaction tx, Point point, La

/**
* Find geometries in the given layer that are closest to the given point while applying the filter
* currently only handles point and linestrings (projecting them to a point) TODO Craig for other geoms
*
* @return list of point results containing the matched point on the geometry, the spatial record and the distance each
* @return list of point results containing the nearest point on the geometry (the given point itself if the
* geometry contains it), the spatial record and the planar distance, closest first
*/
public static List<PointResult> findClosestEdges(Transaction tx, Point point, Layer layer, Geometry filter) {
ArrayList<PointResult> results = new ArrayList<>();

SpatialRecords records = layer.getIndex().search(tx, new SearchIntersect(layer, filter));
for (SpatialRecord record : records) {
Geometry geom = record.getGeometry();
if (geom instanceof LineString) {
LocationIndexedLine line = new LocationIndexedLine(geom);
LinearLocation here = line.project(point.getCoordinate());
Coordinate snap = line.extractPoint(here);
double distance = snap.distance(point.getCoordinate());
results.add(new PointResult(layer.getGeometryFactory()
.createPoint(snap), record, distance));
} else if (geom instanceof Point here) {
results.add(new PointResult(here, record, here.distance(point)));
}
Coordinate snap = DistanceOp.nearestPoints(geom, point)[0];
results.add(new PointResult(layer.getGeometryFactory().createPoint(snap), record,
snap.distance(point.getCoordinate())));
}
Collections.sort(results);
return results;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ private Neo4jCRS crs() {
return Neo4jCRS.findCRS(configuration.crs);
}

public boolean isGeographic() {
return crs().isGeographic();
}

@Override
public List<String> getIdentifiers() {
return List.of("NativePointEncoder", "org.neo4j.gis.spatial.encoders.NativePointEncoder");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ private Neo4jCRS crs() {
return Neo4jCRS.findCRS(configuration.crs);
}

public boolean isGeographic() {
return crs().isGeographic();
}

@Override
public List<String> getIdentifiers() {
return List.of("NativePointsEncoder", "org.neo4j.gis.spatial.encoders.NativePointsEncoder");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public String getHref() {
return crs.getHref();
}

public boolean isGeographic() {
return crs.isGeographic();
}

public int dimensions() {
return crs.getDimension();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,10 @@ public static GeoPipeline startNearestNeighborSearch(final Transaction tx, Layer
}

/**
* Calculates the distance between Layer items inside the given search window and the given point.
* Calculates the planar distance between Layer items inside the given search window and the given point.
*
* @param layer the layer to search
* @param point with latitude, longitude coordinates
* @param point in the layer's CRS
* @param searchWindow the search window
* @return geoPipeline
*/
Expand All @@ -341,11 +341,11 @@ public static GeoPipeline startNearestNeighborSearch(final Transaction tx, Layer
}

/**
* Extracts Layer items with a distance from the given point that is less than or equal the given distance.
* Extracts Layer items with a planar distance from the given point that is less than or equal the given distance.
*
* @param layer the layer to search
* @param point with latitude, longitude coordinates
* @param maxDistance the maximum distance in kilometers
* @param point in the layer's CRS
* @param maxDistance the maximum distance in the layer's CRS units
* @return geoPipeline
*/
public static GeoPipeline startNearestNeighborSearch(final Transaction tx, Layer layer, Coordinate point,
Expand All @@ -355,7 +355,7 @@ public static GeoPipeline startNearestNeighborSearch(final Transaction tx, Layer

return start(tx, layer, new SearchIntersectWindow(layer, extent))
.calculateDistance(layer.getGeometryFactory().createPoint(point))
.propertyFilter("Distance", maxDistance, FilterPipe.Filter.LESS_THAN_EQUAL);
.propertyFilter(Distance.DISTANCE, maxDistance, FilterPipe.Filter.LESS_THAN_EQUAL);
Comment thread
Andy2003 marked this conversation as resolved.
Resolved
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@
*/
public class Distance extends AbstractGeoPipe {

public static final String DISTANCE = "Distance";

private final Geometry reference;

public Distance(Geometry reference) {
this.reference = reference;
this(reference, DISTANCE);
}

/**
Expand Down
Loading
Loading