diff --git a/geom/src/main/java/org/geolatte/geom/AbstractPositionSequence.java b/geom/src/main/java/org/geolatte/geom/AbstractPositionSequence.java index 99d9e943..cd0015c1 100644 --- a/geom/src/main/java/org/geolatte/geom/AbstractPositionSequence.java +++ b/geom/src/main/java/org/geolatte/geom/AbstractPositionSequence.java @@ -21,8 +21,8 @@ package org.geolatte.geom; -import org.locationtech.jts.geom.*; import org.locationtech.jts.geom.Envelope; +import org.locationtech.jts.geom.*; import java.io.Serializable; @@ -88,6 +88,38 @@ public int getCoordinateDimension() { @Override public abstract PositionSequence

clone(); + @Override + public int getMeasures() { + return factory.hasMComponent() ? 1 : 0; + } + + @Override + public boolean hasZ() { + return factory.hasZComponent(); + } + + @Override + public boolean hasM() { + return factory.hasMComponent(); + } + + @Override + public double getZ(int index) { + if (hasZ()) { + return getOrdinate(index, 2); + } else { + return Double.NaN; + } + } + + @Override + public double getM(int index) { + if (hasM()) { + return getOrdinate(index, factory.getMComponentIndex()); + } else { + return Double.NaN; + } + } public org.locationtech.jts.geom.Coordinate getCoordinate(int i) { double[] c = new double[getCoordinateDimension()]; @@ -165,20 +197,11 @@ public double getY(int index) { public double getOrdinate(int i, int ordinateIndex) { double[] c = new double[getCoordinateDimension()]; getCoordinates(i, c); - int idx = 0; - switch (ordinateIndex) { - case CoordinateSequence.X: - return c[0]; - case CoordinateSequence.Y: - return c[1]; - case CoordinateSequence.Z: - return factory.hasZComponent() ? - c[2] : Double.NaN; - case CoordinateSequence.M: - return factory.hasMComponent()? - c[factory.getMComponentIndex()] : Double.NaN; + if (ordinateIndex < getCoordinateDimension()) { + return c[ordinateIndex]; + } else { + throw new IllegalArgumentException("Ordinate index " + ordinateIndex + " is not supported."); } - throw new IllegalArgumentException("Ordinate index " + ordinateIndex + " is not supported."); } @Override diff --git a/geom/src/test/java/org/geolatte/geom/PackedPointSequenceTest.java b/geom/src/test/java/org/geolatte/geom/PackedPointSequenceTest.java index cbe96e87..a1e5ab52 100644 --- a/geom/src/test/java/org/geolatte/geom/PackedPointSequenceTest.java +++ b/geom/src/test/java/org/geolatte/geom/PackedPointSequenceTest.java @@ -275,12 +275,12 @@ public void testGetY() { @Test public void testGetZ() { - assertEquals(0d, testSeq3D.getPositionN(0).getZ(), Math.ulp(10d)); - assertEquals(1d, testSeq3D.getPositionN(1).getZ(), Math.ulp(10d)); - assertEquals(2d, testSeq3D.getPositionN(2).getZ(), Math.ulp(10d)); - assertEquals(0d, testSeq3DM.getPositionN(0).getZ(), Math.ulp(10d)); - assertEquals(1d, testSeq3DM.getPositionN(1).getZ(), Math.ulp(10d)); - assertEquals(2d, testSeq3DM.getPositionN(2).getZ(), Math.ulp(10d)); + assertEquals(0d, testSeq3D.getZ(0), Math.ulp(10d)); + assertEquals(1d, testSeq3D.getZ(1), Math.ulp(10d)); + assertEquals(2d, testSeq3D.getZ(2), Math.ulp(10d)); + assertEquals(0d, testSeq3DM.getZ(0), Math.ulp(10d)); + assertEquals(1d, testSeq3DM.getZ(1), Math.ulp(10d)); + assertEquals(2d, testSeq3DM.getZ(2), Math.ulp(10d)); try { testSeq3D.getPositionN(3); @@ -291,12 +291,12 @@ public void testGetZ() { @Test public void testGetM() { - assertEquals(0d, testSeq2DM.getPositionN(0).getM(), Math.ulp(10d)); - assertEquals(1d, testSeq2DM.getPositionN(1).getM(), Math.ulp(10d)); - assertEquals(2d, testSeq2DM.getPositionN(2).getM(), Math.ulp(10d)); - assertEquals(1d, testSeq3DM.getPositionN(0).getM(), Math.ulp(10d)); - assertEquals(2d, testSeq3DM.getPositionN(1).getM(), Math.ulp(10d)); - assertEquals(3d, testSeq3DM.getPositionN(2).getM(), Math.ulp(10d)); + assertEquals(0d, testSeq2DM.getM(0), Math.ulp(10d)); + assertEquals(1d, testSeq2DM.getM(1), Math.ulp(10d)); + assertEquals(2d, testSeq2DM.getM(2), Math.ulp(10d)); + assertEquals(1d, testSeq3DM.getM(0), Math.ulp(10d)); + assertEquals(2d, testSeq3DM.getM(1), Math.ulp(10d)); + assertEquals(3d, testSeq3DM.getM(2), Math.ulp(10d)); try { testSeq3D.getX(3); @@ -305,6 +305,14 @@ public void testGetM() { } } + @Test + public void testGetMeasures() { + assertEquals(0, testSeq2D.getMeasures()); + assertEquals(0, testSeq3D.getMeasures()); + assertEquals(1, testSeq2DM.getMeasures()); + assertEquals(1, testSeq3DM.getMeasures()); + } + @Test public void testGetOrdinate() { @@ -326,8 +334,6 @@ public void testGetOrdinate() { Assert.assertEquals(testSeq3DM.getPositionN(i).getZ(), testSeq3DM.getOrdinate(i, CoordinateSequence.Z), Math.ulp(10d)); //M - Assert.assertEquals(testSeq2DM.getPositionN(i).getM(), testSeq2DM.getOrdinate(i, CoordinateSequence.M), Math.ulp(10d)); - Assert.assertEquals(testSeq3DM.getPositionN(i).getM(), testSeq3DM.getOrdinate(i, CoordinateSequence.M), Math.ulp(10d)); diff --git a/geom/src/test/java/org/geolatte/geom/jts/ReverseTest.java b/geom/src/test/java/org/geolatte/geom/jts/ReverseTest.java index 170a224c..c65c8ab5 100644 --- a/geom/src/test/java/org/geolatte/geom/jts/ReverseTest.java +++ b/geom/src/test/java/org/geolatte/geom/jts/ReverseTest.java @@ -1,12 +1,16 @@ package org.geolatte.geom.jts; -import org.locationtech.jts.geom.LineString; +import org.geolatte.geom.C2DM; +import org.geolatte.geom.GeometryOperations; +import org.geolatte.geom.ProjectedGeometryOperations; +import org.geolatte.geom.codec.Wkt; import org.junit.Test; +import org.locationtech.jts.geom.Geometry; +import org.locationtech.jts.geom.LineString; import static org.geolatte.geom.CrsMock.*; import static org.geolatte.geom.builder.DSL.*; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; /** @@ -15,18 +19,40 @@ public class ReverseTest { @Test - public void testReverseLineString2D(){ - LineString ls = (LineString) JTS.to(linestring(crs, c(1, 2), c(2, 3), c(4, 5))); - LineString expected = (LineString) JTS.to(linestring(crs, c(4, 5), c(2, 3), c(1, 2))); + public void testReverseLineString2D() { + LineString ls = JTS.to(linestring(crs, c(1, 2), c(2, 3), c(4, 5))); + LineString expected = JTS.to(linestring(crs, c(4, 5), c(2, 3), c(1, 2))); assertEquals(expected, ls.reverse()); } @Test - public void testReverseLineString3D(){ - LineString ls = (LineString) JTS.to(linestring(crsZ, c(1, 2, 3), c(2, 3, 4), c(4, 5, 6))); - LineString expected = (LineString) JTS.to(linestring(crs, c(4, 5, 6), c(2, 3, 4), c(1, 2, 3))); + public void testReverseLineString3D() { + LineString ls = JTS.to(linestring(crsZ, c(1, 2, 3), c(2, 3, 4), c(4, 5, 6))); + LineString expected = JTS.to(linestring(crs, c(4, 5, 6), c(2, 3, 4), c(1, 2, 3))); assertEquals(expected, ls.reverse()); } + @Test + public void testReverse2DM() { + org.geolatte.geom.LineString ls = + linestring(crsM, cM(1, 1, 1), cM(2, 2, 2), cM(3, 3, 3)); + + ProjectedGeometryOperations ops = GeometryOperations.projectedGeometryOperations(); + org.geolatte.geom.LineString reversed = ops.reverse(ls); + assertEquals(linestring(crsM, cM(3, 3, 3), cM(2, 2, 2), cM(1, 1, 1)), + reversed); + } + + @Test + public void testReverse2DMJts() { + Geometry geometry = JTS.to(Wkt.fromWkt("LINESTRING M (1 1 1, 2 2 2, 3 3 3)")); + Geometry reversed = geometry.reverse(); + + //note: we can't test with assertEquals() because JTS only checks equality in 2D! + for (int i = 0; i < geometry.getNumPoints(); i++) { + assertEquals(geometry.getCoordinates()[i].getOrdinate(2), + reversed.getCoordinates()[geometry.getNumPoints() - 1 - i].getOrdinate(2), 0.001); + } + } }