Skip to content

Commit

Permalink
Fix for #155
Browse files Browse the repository at this point in the history
Changed the implementation of PositionSequence
getOrdinate() method in line with the JTS
implementation in PackedCoordinateSequence.

Also added implementations for the getMeasures(),
hasZ(), hasM(), getZ() and getM() methods that
have been added to the CoordinateSequence interface.
  • Loading branch information
maesenka committed Jan 28, 2023
1 parent 543eef5 commit 048a860
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 36 deletions.
51 changes: 37 additions & 14 deletions geom/src/main/java/org/geolatte/geom/AbstractPositionSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -88,6 +88,38 @@ public int getCoordinateDimension() {
@Override
public abstract PositionSequence<P> 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()];
Expand Down Expand Up @@ -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
Expand Down
34 changes: 20 additions & 14 deletions geom/src/test/java/org/geolatte/geom/PackedPointSequenceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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() {

Expand All @@ -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));


Expand Down
42 changes: 34 additions & 8 deletions geom/src/test/java/org/geolatte/geom/jts/ReverseTest.java
Original file line number Diff line number Diff line change
@@ -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;


/**
Expand All @@ -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<C2DM> ls =
linestring(crsM, cM(1, 1, 1), cM(2, 2, 2), cM(3, 3, 3));

ProjectedGeometryOperations ops = GeometryOperations.projectedGeometryOperations();
org.geolatte.geom.LineString<C2DM> 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);
}
}
}

0 comments on commit 048a860

Please sign in to comment.