Skip to content

Commit d97fd5f

Browse files
authored
Allow WKTReader to read Inf ordinates (#1166)
1 parent a6ed79d commit d97fd5f

File tree

4 files changed

+24
-15
lines changed

4 files changed

+24
-15
lines changed

modules/core/src/main/java/org/locationtech/jts/geom/PrecisionModel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,15 +412,15 @@ public void toExternal(Coordinate internal, Coordinate external) {
412412
* uniform rounding behaviour no matter where the number is
413413
* on the number line.
414414
* <p>
415-
* This method has no effect on NaN values.
415+
* This method has no effect on NaN and infinite values.
416416
* <p>
417417
* <b>Note:</b> Java's <code>Math#rint</code> uses the "Banker's Rounding" algorithm,
418418
* which is not suitable for precision operations elsewhere in JTS.
419419
*/
420420
public double makePrecise(double val)
421421
{
422422
// don't change NaN values
423-
if (Double.isNaN(val)) return val;
423+
if (Double.isNaN(val) || Double.isInfinite(val)) return val;
424424

425425
if (modelType == FLOATING_SINGLE) {
426426
float floatSingleVal = (float) val;

modules/core/src/main/java/org/locationtech/jts/io/WKTReader.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@
8080
* <li>The reader uses <tt>Double.parseDouble</tt> to perform the conversion of ASCII
8181
* numbers to floating point. This means it supports the Java
8282
* syntax for floating point literals (including scientific notation).
83+
* <li><tt>NaN</tt> and <tt>Inf</tt> ordinate symbols are supported (case-insensitive),
84+
* which convert to the corresponding IEE-754 value
8385
* </ul>
8486
* <h3>Syntax</h3>
8587
* The following syntax specification describes the version of Well-Known Text
@@ -131,7 +133,7 @@
131133
* <i>Coordinate:
132134
* Number Number Number<sub>opt</sub> Number<sub>opt</sub></i>
133135
*
134-
* <i>Number:</i> A Java-style floating-point number (including <tt>NaN</tt>, with arbitrary case)
136+
* <i>Number:</i> A Java-style floating-point number (including <tt>NaN</tt> and <tt>Inf</tt>, with arbitrary case)
135137
*
136138
* <i>Dimension:</i>
137139
* <b>Z</b>|<b> Z</b>|<b>M</b>|<b> M</b>|<b>ZM</b>|<b> ZM</b>
@@ -159,6 +161,8 @@
159161
* POINT M (0 0 0)
160162
* POINTZM (0 0 0 0)
161163
* POINT ZM (0 0 0 0)
164+
*
165+
* POINT (Inf Nan)
162166
* </pre>
163167
*
164168
*@version 1.7
@@ -170,6 +174,7 @@ public class WKTReader
170174
private static final String L_PAREN = "(";
171175
private static final String R_PAREN = ")";
172176
private static final String NAN_SYMBOL = "NaN";
177+
private static final String INF_SYMBOL = "Inf";
173178

174179
private GeometryFactory geometryFactory;
175180
private CoordinateSequenceFactory csFactory;
@@ -517,6 +522,10 @@ private double getNextNumber(StreamTokenizer tokenizer) throws IOException,
517522
if (tokenizer.sval.equalsIgnoreCase(NAN_SYMBOL)) {
518523
return Double.NaN;
519524
}
525+
if (tokenizer.sval.equalsIgnoreCase(INF_SYMBOL)) {
526+
return Double.POSITIVE_INFINITY;
527+
}
528+
//TODO: handle -Inf ?
520529
else {
521530
try {
522531
return Double.parseDouble(tokenizer.sval);

modules/core/src/test/java/org/locationtech/jts/io/WKTReaderTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,13 @@ public void testNaN() throws Exception {
464464
assertTrue(isEqual(seq, pt3.getCoordinateSequence()));
465465
}
466466

467+
public void testInf() throws ParseException {
468+
Point pt = (Point) readerXY.read("POINT ( Inf INF )");
469+
CoordinateSequence cs = pt.getCoordinateSequence();
470+
assertEquals(Double.POSITIVE_INFINITY, cs.getOrdinate(0, Coordinate.X));
471+
assertEquals(Double.POSITIVE_INFINITY, cs.getOrdinate(0, Coordinate.Y));
472+
}
473+
467474
public void testLargeNumbers() throws Exception {
468475
PrecisionModel precisionModel = new PrecisionModel(1E9);
469476
GeometryFactory geometryFactory = new GeometryFactory(precisionModel, 0);

modules/core/src/test/java/org/locationtech/jts/operation/buffer/BufferTest.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -694,31 +694,24 @@ public void testInvalidCoordPoint() {
694694

695695
public void testInvalidCoordsLine() {
696696
// works for Inf ordinates as well
697-
Geometry geom = read("LINESTRING (NaN NaN, NaN NaN)");
697+
Geometry geom = read("LINESTRING (Inf Inf, NaN NaN)");
698698
checkBufferPolygonEmpty(geom, 1, true);
699699
}
700-
700+
701701
public void testInvalidCoordShell() {
702702
// using Inf ordinates creates a valid ring with equal endpoints
703-
// this would be simpler if JTS WKT supported Inf
704-
Geometry geom = getGeometryFactory().createPolygon( infCoords(5) );
703+
Geometry geom = (Polygon) read("POLYGON ((Inf Inf, Inf Inf, Inf Inf, Inf Inf, Inf Inf))");
705704
checkBufferPolygonEmpty(geom, 1, true);
706705
}
707706

708707
public void testInvalidCoordHole() {
709708
// using Inf ordinates creates a valid ring with equal endpoints
710-
// this would be simpler if JTS WKT supported Inf
711709
Polygon poly = (Polygon) read("POLYGON ((1 9, 9 9, 9 1, 1 1, 1 9), (3 7, 7 7, 7 3, 3 3, 3 7))");
712-
713-
LinearRing shell = poly.getExteriorRing();
714-
LinearRing hole = poly.getInteriorRingN(0);
715-
LinearRing infHole = getGeometryFactory().createLinearRing( infCoords(5) );
716-
Geometry polyInfHole = getGeometryFactory().createPolygon(
717-
shell, new LinearRing[] { hole, infHole } );
710+
Polygon polyInfHole = (Polygon) read("POLYGON ((1 9, 9 9, 9 1, 1 1, 1 9), (3 7, 7 7, 7 3, 3 3, 3 7), (Inf Inf, Inf Inf, Inf Inf, Inf Inf, Inf Inf))");
718711

719712
Geometry bufferOrig = poly.buffer(1);
720713
Geometry bufferInf = polyInfHole.buffer(1);
721-
// buffers should be same since inf hole is skipped
714+
// buffers should be same since inf hole is dropped
722715
checkEqual(bufferOrig, bufferInf);
723716
}
724717

0 commit comments

Comments
 (0)