3.4.9. Spatial functions

These functions are used to specify points in a 2D coordinate system and to calculate the geodesic distance between two points.

Two coordinate reference systems (CRS) are supported: (i) WGS 84 and (ii) cartesian 2D.

WGS 84 is specified with a map containing coordinate values for either longitude and latitude (this is the default), or x and y. Cartesian is specified with a map containing only x and y coordinate values.

Functions:

The following graph is used for some of the examples below.

Figure 3.24. Graph
alt

3.4.9.1. distance()

distance() returns a floating point number representing the geodesic distance between two points in the same CRS. If the points are in the cartesian CRS, then the units of the returned distance will be the same as the units of the points, calculated using Pythagoras' theorem. If the points are in the WGS-84 CRS, then the units of the returned distance will be meters, based on the haversine formula over a spherical earth approximation.

Syntax: distance(point1, point2)

Returns:

A Float.

Arguments:

Name Description

point1

A point in either the WGS 84 or cartesian CRS.

point2

A point in the same CRS as 'point1'.

Considerations:

distance(null, null), distance(null, point2) and distance(point1, null) all return null.

Query. 

WITH point({ x: 2.3, y: 4.5, crs: 'cartesian' }) AS p1, point({ x: 1.1, y: 5.4, crs: 'cartesian' }) AS p2
RETURN distance(p1,p2) AS dist

The distance between two points in the cartesian CRS is returned.

Table 3.193. Result
dist

1 row

1.5

Try this query live.  CREATE (copenhagen:TrainStation {longitude: 12.564590, latitude: 55.672874, city: 'Copenhagen'}), (malmo:Office {longitude: 12.994341, latitude: 55.611784, city: 'Malmo'}), (copenhagen)-[:TRAVEL_ROUTE]->(malmo) WITH point({x: 2.3, y: 4.5, crs: 'cartesian'}) as p1, point({x: 1.1, y: 5.4, crs: 'cartesian'}) as p2 RETURN distance(p1,p2) AS dist

Query. 

MATCH (t:TrainStation)-[:TRAVEL_ROUTE]->(o:Office)
WITH point({ longitude: t.longitude, latitude: t.latitude }) AS trainPoint, point({ longitude: o.longitude, latitude: o.latitude }) AS officePoint
RETURN round(distance(trainPoint, officePoint)) AS travelDistance

The distance between the train station in Copenhagen and the Neo4j office in Malmo is returned.

Table 3.194. Result
travelDistance

1 row

27842.0

Try this query live.  CREATE (copenhagen:TrainStation {longitude: 12.564590, latitude: 55.672874, city: 'Copenhagen'}), (malmo:Office {longitude: 12.994341, latitude: 55.611784, city: 'Malmo'}), (copenhagen)-[:TRAVEL_ROUTE]->(malmo) MATCH (t:TrainStation)-[:TRAVEL_ROUTE]->(o:Office) WITH point({longitude: t.longitude, latitude: t.latitude}) AS trainPoint, point({longitude: o.longitude, latitude: o.latitude}) AS officePoint RETURN round(distance(trainPoint, officePoint)) AS travelDistance

Query. 

RETURN distance(NULL , point({ longitude: 56.7, latitude: 12.78 })) AS d

If null is provided as one or both of the arguments, null is returned.

Table 3.195. Result
d

1 row

<null>

Try this query live.  CREATE (copenhagen:TrainStation {longitude: 12.564590, latitude: 55.672874, city: 'Copenhagen'}), (malmo:Office {longitude: 12.994341, latitude: 55.611784, city: 'Malmo'}), (copenhagen)-[:TRAVEL_ROUTE]->(malmo) RETURN distance(null, point({longitude: 56.7, latitude: 12.78})) AS d

3.4.9.2. point() - WGS 84

point() returns a point in the WGS 84 coordinate system corresponding to the given coordinate values.

Syntax: point({longitude | x, latitude | y [, crs]})

Returns:

A Point.

Arguments:

Name Description

A single map consisting of the following:

 

longitude/x

A numeric expression

latitude/y

A numeric expression

crs

The string 'WGS-84'

Considerations:

If any argument provided to point() is null, null will be returned.

Query. 

RETURN point({ longitude: 56.7, latitude: 12.78 }) AS point

A point with a longitude of 56.7 and a latitude of 12.78 in the WGS 84 CRS is returned.

Table 3.196. Result
point

1 row

GeographicPoint(56.7,12.78,CRS(WGS-84,4326,http://spatialreference.org/ref/epsg/4326/))

Try this query live.  CREATE (copenhagen:TrainStation {longitude: 12.564590, latitude: 55.672874, city: 'Copenhagen'}), (malmo:Office {longitude: 12.994341, latitude: 55.611784, city: 'Malmo'}), (copenhagen)-[:TRAVEL_ROUTE]->(malmo) RETURN point({longitude: 56.7, latitude: 12.78}) AS point

Query. 

RETURN point({ x: 2.3, y: 4.5, crs: 'WGS-84' }) AS point

x and y coordinates may be used in the WGS 84 CRS instead of longitude and latitude, respectively, providing crs is set to 'WGS-84'.

Table 3.197. Result
point

1 row

GeographicPoint(2.3,4.5,CRS(WGS-84,4326,http://spatialreference.org/ref/epsg/4326/))

Try this query live.  CREATE (copenhagen:TrainStation {longitude: 12.564590, latitude: 55.672874, city: 'Copenhagen'}), (malmo:Office {longitude: 12.994341, latitude: 55.611784, city: 'Malmo'}), (copenhagen)-[:TRAVEL_ROUTE]->(malmo) RETURN point({x: 2.3, y: 4.5, crs: 'WGS-84'}) AS point

Query. 

MATCH (p:Office)
RETURN point({ longitude: p.longitude, latitude: p.latitude }) AS officePoint

A point representing the coordinates of the city of Malmo in the WGS 84 CRS is returned.

Table 3.198. Result
officePoint

1 row

GeographicPoint(12.994341,55.611784,CRS(WGS-84,4326,http://spatialreference.org/ref/epsg/4326/))

Try this query live.  CREATE (copenhagen:TrainStation {longitude: 12.564590, latitude: 55.672874, city: 'Copenhagen'}), (malmo:Office {longitude: 12.994341, latitude: 55.611784, city: 'Malmo'}), (copenhagen)-[:TRAVEL_ROUTE]->(malmo) MATCH (p:Office) RETURN point({longitude: p.longitude, latitude: p.latitude}) AS officePoint

Query. 

RETURN point(NULL ) AS p

If null is provided as the argument, null is returned.

Table 3.199. Result
p

1 row

<null>

Try this query live.  CREATE (copenhagen:TrainStation {longitude: 12.564590, latitude: 55.672874, city: 'Copenhagen'}), (malmo:Office {longitude: 12.994341, latitude: 55.611784, city: 'Malmo'}), (copenhagen)-[:TRAVEL_ROUTE]->(malmo) RETURN point(null) AS p

3.4.9.3. point() - cartesian 2D

point() returns a point in the cartesian coordinate system corresponding to the given coordinate values.

Syntax: point({x, y [, crs]})

Returns:

A Point.

Arguments:

Name Description

A single map consisting of the following:

 

x

A numeric expression

y

A numeric expression

crs

The string 'cartesian'

Considerations:

If any argument provided to point() is null, null will be returned.

Query. 

RETURN point({ x: 2.3, y: 4.5 }) AS point

A point with an x coordinate of 2.3 and a y coordinate of 4.5 in the cartesian CRS is returned

Table 3.200. Result
point

1 row

CartesianPoint(2.3,4.5,CRS(cartesian,7203,http://spatialreference.org/ref/sr-org/7203/))

Try this query live.  CREATE (copenhagen:TrainStation {longitude: 12.564590, latitude: 55.672874, city: 'Copenhagen'}), (malmo:Office {longitude: 12.994341, latitude: 55.611784, city: 'Malmo'}), (copenhagen)-[:TRAVEL_ROUTE]->(malmo) RETURN point({x: 2.3, y: 4.5}) AS point