cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Calculate center of bunch of (Spatial Point) 3D cartesian points

Hi,

I have bunch of cartesian-3D points. Is there a function/procedure to calculate center point, positioned after calculating distances from each node? I want to create a new node, that would represent the points it was calculated from.

Refer: https://neo4j.com/docs/cypher-manual/current/functions/spatial/#functions-point-cartesian-3d

Thanks!

4 REPLIES 4

mojo2go
Graph Buddy

There's no reason to calculate distances between points. Instead you can deconstruct each point and calculate the average of each individual dimension. Try out the code below. Put any number of points in there that you desire and it will provide you with the average of all 3 dimensions.

WITH [point({ x: 2.3, y: 4.5, z: 2 }), point({ x: 2.3, y: 4.5, z: 4 })] AS points
UNWIND points AS point
WITH avg(point.x) AS avgx, avg(point.y) AS avgy, avg(point.z) AS avgz
RETURN point({ x: avgx, y: avgy, z: avgz }) AS avgpoint;

That's doesn't work with spatial data near lat 180/0:
avg [179,-179] = 0 (the other side of the world).

@tony.chiboucas you are right that this does not work for geographic points, but it does work as @mojo2go said for 3D cartesian points.

For geographic data you would first need to define what is meant by the term 'center point', which could mean a point on the direct line through the earth, or a point on the surface of the earth. Two completely different definitions with two completely different mathematical solutiuons. For some cases the second definition leads to infinite solutions.

Thank you @craig.taverner, I ended up writing my own function which:

  1. Converts the points from latLong to cartesian3d.
  2. Averages all the cartesian3d.
  3. Convert back to latLong (projects the point back up to the surface).

This leaves an opportunity to use the distance from earth-center to average cart3d point as a measure of how widely distributed the set is.