Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix silent dimension addition of WKTReader when allowing old syntax #941

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions modules/core/src/main/java/org/locationtech/jts/io/WKTReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,50 @@ private Coordinate getCoordinate(StreamTokenizer tokenizer, EnumSet<Ordinate> or
tokenizer.nextToken();
opened = true;
}


// Read x- and y- ordinates
double x = precisionModel.makePrecise(getNextNumber(tokenizer));
double y = precisionModel.makePrecise(getNextNumber(tokenizer));

// Coordinate type
EnumSet<Ordinate> readOrdinates = Ordinate.createXY();

// additionally read other vertices
double z = Coordinate.NULL_ORDINATE, m = 0d;
if (ordinateFlags.contains(Ordinate.Z))
{
z = getNextNumber(tokenizer);
readOrdinates.add(Ordinate.Z);
}

if (ordinateFlags.contains(Ordinate.M))
{
m = getNextNumber(tokenizer);
readOrdinates.add(Ordinate.M);
}

if (ordinateFlags.size() == 2 && isAllowOldJtsCoordinateSyntax && isNumberNext(tokenizer))
{
z = getNextNumber(tokenizer);
readOrdinates.add(Ordinate.Z);
}

Coordinate coord;

if(readOrdinates.size() == 2) {
coord = new CoordinateXY(x, y);
} else if(readOrdinates.size() == 4) {
coord = new CoordinateXYZM(x, y, z, m);
} else if(readOrdinates.size() == 3){
if(readOrdinates.contains(Ordinate.Z))
coord = new Coordinate(x, y, z);
else
coord = new CoordinateXYM(x, y, m);
} else{
throw new ParseException("Unknown ordinate read: {readOrdinates}");
}

/*
// create a sequence for one coordinate
int offsetM = ordinateFlags.contains(Ordinate.Z) ? 1 : 0;
Coordinate coord = createCoordinate(ordinateFlags);
Expand All @@ -323,7 +366,8 @@ private Coordinate getCoordinate(StreamTokenizer tokenizer, EnumSet<Ordinate> or
if (ordinateFlags.size() == 2 && this.isAllowOldJtsCoordinateSyntax && isNumberNext(tokenizer)) {
coord.setOrdinate(CoordinateSequence.Z, getNextNumber(tokenizer));
}

*/

// read close token if it was opened here
if (opened) {
getNextCloser(tokenizer);
Expand All @@ -339,7 +383,7 @@ private Coordinate createCoordinate(EnumSet<Ordinate> ordinateFlags) {
return new CoordinateXYZM();
if (hasM)
return new CoordinateXYM();
if (hasZ || this.isAllowOldJtsCoordinateSyntax)
if (hasZ)
return new Coordinate();
return new CoordinateXY();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,27 @@ public void testNaN() throws Exception {
assertTrue(isEqual(seq, pt3.getCoordinateSequence()));
}

public void testDimensionConsistence() throws Exception {
//test 2-d point with XY coordinate
CoordinateSequence seq1 = createSequence(Ordinate.createXY(), new double[] {10, 10});
Point pt1 = (Point)readerXYOld.read("POINT (10 10)");
assertTrue(isEqual(seq1, pt1.getCoordinateSequence()));

//test 2-d point with XYZ coordinate
CoordinateSequence seq2 = createSequence(Ordinate.createXYZ(), new double[] { 10, 10 });
seq2.setOrdinate(0, 2, Double.NaN);
Point pt2 = (Point)this.readerXYOld.read("POINT (10 10 NaN)");
assertTrue(isEqual(seq2, pt2.getCoordinateSequence()));

//test points sequence
CoordinateSequence seq3 = createSequence(Ordinate.createXYZ(), new double[] { 10, 10, 20, 20, 30, 30 });
seq3.setOrdinate(0, 2, Double.NaN);
seq3.setOrdinate(1, 2, 25);
seq3.setOrdinate(2, 2, Double.NaN);
LineString ls = (LineString)this.readerXYOld.read("LINESTRING (10 10 NaN, 20 20 25, 30 30 NaN)");
assertTrue(isEqual(seq3, ls.getCoordinateSequence()));
}

public void testLargeNumbers() throws Exception {
PrecisionModel precisionModel = new PrecisionModel(1E9);
GeometryFactory geometryFactory = new GeometryFactory(precisionModel, 0);
Expand Down