private static Value calculateDistance( PointValue p1, PointValue p2 ) { if ( p1.getCoordinateReferenceSystem().equals( p2.getCoordinateReferenceSystem() ) ) { return doubleValue( p1.getCoordinateReferenceSystem().getCalculator().distance( p1, p2 ) ); } else { return NO_VALUE; } }
@Test void shouldCalculateCartesianDistance3D() { CoordinateReferenceSystem crs = CoordinateReferenceSystem.Cartesian_3D; assertThat( "", crs.getCalculator().distance( cart( 0.0, 0.0, 0.0 ), cart( 1.0, 0.0, 0.0 ) ), equalTo( 1.0 ) ); assertThat( "", crs.getCalculator().distance( cart( 0.0, 0.0, 0.0 ), cart( 0.0, 1.0, 0.0 ) ), equalTo( 1.0 ) ); assertThat( "", crs.getCalculator().distance( cart( 0.0, 0.0, 0.0 ), cart( 0.0, 0.0, 1.0 ) ), equalTo( 1.0 ) ); assertThat( "", crs.getCalculator().distance( cart( 0.0, 0.0, 0.0 ), cart( 0.0, 1.0, 1.0 ) ), closeTo( 1.41, 0.01 ) ); assertThat( "", crs.getCalculator().distance( cart( 0.0, 0.0, 0.0 ), cart( 1.0, 1.0, 1.0 ) ), closeTo( 1.73, 0.01 ) ); assertThat( "", crs.getCalculator().distance( cart( -1000000.0, -1000000.0, -1000000.0 ), cart( 1000000.0, 1000000.0, 1000000.0 ) ), closeTo( 3464102.0, 1.0 ) ); }
@Test void shouldCalculateCartesianDistance() { CoordinateReferenceSystem crs = CoordinateReferenceSystem.Cartesian; assertThat( "", crs.getCalculator().distance( cart( 0.0, 0.0 ), cart( 0.0, 1.0 ) ), equalTo( 1.0 ) ); assertThat( "", crs.getCalculator().distance( cart( 0.0, 0.0 ), cart( 1.0, 0.0 ) ), equalTo( 1.0 ) ); assertThat( "", crs.getCalculator().distance( cart( 0.0, 0.0 ), cart( 1.0, 1.0 ) ), closeTo( 1.4, 0.02 ) ); assertThat( "", crs.getCalculator().distance( cart( 0.0, 0.0 ), cart( 0.0, -1.0 ) ), equalTo( 1.0 ) ); assertThat( "", crs.getCalculator().distance( cart( 0.0, 0.0 ), cart( -1.0, 0.0 ) ), equalTo( 1.0 ) ); assertThat( "", crs.getCalculator().distance( cart( 0.0, 0.0 ), cart( -1.0, -1.0 ) ), closeTo( 1.4, 0.02 ) ); assertThat( "", crs.getCalculator().distance( cart( 1.0, 0.0 ), cart( 0.0, -1.0 ) ), closeTo( 1.4, 0.02 ) ); assertThat( "", crs.getCalculator().distance( cart( 1.0, 0.0 ), cart( -1.0, 0.0 ) ), equalTo( 2.0 ) ); assertThat( "", crs.getCalculator().distance( cart( 1.0, 0.0 ), cart( -1.0, -1.0 ) ), closeTo( 2.24, 0.01 ) ); assertThat( "", crs.getCalculator().distance( cart( -1000000.0, -1000000.0 ), cart( 1000000.0, 1000000.0 ) ), closeTo( 2828427.0, 1.0 ) ); }
@Test void shouldCalculateGeographicDistance3D() { CoordinateReferenceSystem crs = CoordinateReferenceSystem.WGS84_3D; //"distance function should measure distance from Copenhagen train station to Neo4j in Malmö" PointValue cph = geo( 12.564590, 55.672874, 0.0 ); PointValue cphHigh = geo( 12.564590, 55.672874, 1000.0 ); PointValue malmo = geo( 12.994341, 55.611784, 0.0 ); PointValue malmoHigh = geo( 12.994341, 55.611784, 1000.0 ); double expected = 27842.0; double expectedHigh = 27862.0; assertThat( "3D distance should match", crs.getCalculator().distance( cph, malmo ), closeTo( expected, 0.1 ) ); assertThat( "3D distance should match", crs.getCalculator().distance( cph, malmoHigh ), closeTo( expectedHigh, 0.2 ) ); assertThat( "3D distance should match", crs.getCalculator().distance( cphHigh, malmo ), closeTo( expectedHigh, 0.2 ) ); }
@Test void shouldCalculateGeographicDistance() { CoordinateReferenceSystem crs = CoordinateReferenceSystem.WGS84; assertThat( "2D distance should match", crs.getCalculator().distance( geo( 0.0, 0.0 ), geo( 0.0, 90.0 ) ), closeTo( 10000000.0, 20000.0 ) ); assertThat( "2D distance should match", crs.getCalculator().distance( geo( 0.0, 0.0 ), geo( 0.0, -90.0 ) ), closeTo( 10000000.0, 20000.0 ) ); assertThat( "2D distance should match", crs.getCalculator().distance( geo( 0.0, -45.0 ), geo( 0.0, 45.0 ) ), closeTo( 10000000.0, 20000.0 ) ); assertThat( "2D distance should match", crs.getCalculator().distance( geo( -45.0, 0.0 ), geo( 45.0, 0.0 ) ), closeTo( 10000000.0, 20000.0 ) ); assertThat( "2D distance should match", crs.getCalculator().distance( geo( -45.0, 0.0 ), geo( 45.0, 0.0 ) ), closeTo( 10000000.0, 20000.0 ) ); //"distance function should measure distance from Copenhagen train station to Neo4j in Malmö" PointValue cph = geo( 12.564590, 55.672874 ); PointValue malmo = geo( 12.994341, 55.611784 ); double expected = 27842.0; assertThat( "2D distance should match", crs.getCalculator().distance( cph, malmo ), closeTo( expected, 0.1 ) ); }
private static Value calculateDistance( PointValue p1, PointValue p2 ) { if ( p1.getCoordinateReferenceSystem().equals( p2.getCoordinateReferenceSystem() ) ) { return doubleValue( p1.getCoordinateReferenceSystem().getCalculator().distance( p1, p2 ) ); } else { return NO_VALUE; } }