CoordinatesUtils { public static double length3D(CoordinateSequence pts) { int n = pts.size(); if (n <= 1) { return 0.0; } double len = 0.0; Coordinate p = new Coordinate(); pts.getCoordinate(0, p); double x0 = p.x; double y0 = p.y; double z0 = p.z; if (Double.isNaN(z0)) { return 0.0; } for (int i = 1; i < n; i++) { pts.getCoordinate(i, p); double x1 = p.x; double y1 = p.y; double z1 = p.z; if (Double.isNaN(z1)) { return 0.0; } double dx = x1 - x0; double dy = y1 - y0; double dz = z1 - z0; len += Math.sqrt(dx * dx + dy * dy + dz * dz); x0 = x1; y0 = y1; z0 = z1; } return len; } private  CoordinatesUtils(); static double interpolate(Coordinate firstCoordinate, Coordinate lastCoordinate, Coordinate toBeInterpolated); static boolean contains(Coordinate[] coords, Coordinate coord); static boolean contains2D(Coordinate[] coords, Coordinate coord); static boolean contains3D(Coordinate[] coords, Coordinate coord); static double[] zMinMax(final Coordinate[] cs); static Coordinate[] getFurthestCoordinate(Coordinate base, Coordinate[] coords); static double length3D(CoordinateSequence pts); static double length3D(Geometry geom); static double length3D(LineString lineString); static double length3D(Polygon polygon);  }
@Test public void testLength3D() throws Exception { LineString geom = (LineString) wKTReader.read("LINESTRING(0 1 0, 5 1 0, 10 1 0)"); double length = CoordinatesUtils.length3D(geom.getCoordinateSequence()); double expectedLength = geom.getLength(); assertEquals(expectedLength, length, 10E-11); }
@Test public void testLength3DNaNZ() throws Exception { LineString geom = (LineString) wKTReader.read("LINESTRING(0 1, 5 1 0, 10 1 0)"); double length = CoordinatesUtils.length3D(geom.getCoordinateSequence()); assertEquals(0.0, length, 10E-11); }
@Test public void testLength3D1() throws Exception { LineString geom = (LineString) wKTReader.read("LINESTRING(0 1 10, 10 1 0)"); double length = CoordinatesUtils.length3D(geom.getCoordinateSequence()); double expectedLength = 14.1421356237309; assertEquals(expectedLength, length, 10E-11); }
@Test public void testLength3D2() throws Exception { Geometry geom = wKTReader.read("LINESTRING(0 1 10, 10 1 0)"); double length = CoordinatesUtils.length3D(geom); double expectedLength = 14.1421356237309; assertEquals(expectedLength, length, 10E-11); }
@Test public void testLength3D3() throws Exception { Geometry geom = wKTReader.read("MULTILINESTRING((0 1 10, 10 1 0),(0 1 10, 10 1 0))"); double length = CoordinatesUtils.length3D(geom); double expectedLength = 14.1421356237309; assertEquals(expectedLength * 2, length, 10E-11); }
@Test public void testLength3D4() throws Exception { Geometry geom = wKTReader.read("MULTILINESTRING((0 1 10, 10 1 0),(0 1 , 10 1 0))"); double length = CoordinatesUtils.length3D(geom); double expectedLength = 14.1421356237309; assertEquals(expectedLength, length, 10E-11); }
@Test public void testLength3D5() throws Exception { Geometry geom = wKTReader.read("GEOMETRYCOLLECTION(LINESTRING(0 1 10, 10 1 0),POINT(0 1))"); double length = CoordinatesUtils.length3D(geom); double expectedLength = 14.1421356237309; assertEquals(expectedLength, length, 10E-11); }
@Test public void testLength3D6() throws Exception { Geometry geom = wKTReader.read("LINESTRING(0 0 10, 0 10 10)"); double length = CoordinatesUtils.length3D(geom); double expectedLength = geom.getLength(); assertEquals(expectedLength, length, 10E-11); }