Skip to content

Commit 4c83087

Browse files
authored
Add WKTReader support for -Inf (#1167)
1 parent 6721f0e commit 4c83087

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
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),
83+
* <li><tt>NaN</tt>, <tt>Inf</tt> and <tt>-Inf</tt> ordinate symbols are supported (case-insensitive),
8484
* which convert to the corresponding IEE-754 value
8585
* </ul>
8686
* <h3>Syntax</h3>
@@ -133,7 +133,7 @@
133133
* <i>Coordinate:
134134
* Number Number Number<sub>opt</sub> Number<sub>opt</sub></i>
135135
*
136-
* <i>Number:</i> A Java-style floating-point number (including <tt>NaN</tt> and <tt>Inf</tt>, with arbitrary case)
136+
* <i>Number:</i> A Java-style floating-point number (including <tt>NaN</tt>, <tt>Inf</tt> and <tt>-Inf</tt>, case-independent)
137137
*
138138
* <i>Dimension:</i>
139139
* <b>Z</b>|<b> Z</b>|<b>M</b>|<b> M</b>|<b>ZM</b>|<b> ZM</b>
@@ -163,6 +163,7 @@
163163
* POINT ZM (0 0 0 0)
164164
*
165165
* POINT (Inf Nan)
166+
* POINT (Inf -Inf)
166167
* </pre>
167168
*
168169
*@version 1.7
@@ -175,6 +176,7 @@ public class WKTReader
175176
private static final String R_PAREN = ")";
176177
private static final String NAN_SYMBOL = "NaN";
177178
private static final String INF_SYMBOL = "Inf";
179+
private static final String NEG_INF_SYMBOL = "-Inf";
178180

179181
private GeometryFactory geometryFactory;
180182
private CoordinateSequenceFactory csFactory;
@@ -525,6 +527,9 @@ private double getNextNumber(StreamTokenizer tokenizer) throws IOException,
525527
if (tokenizer.sval.equalsIgnoreCase(INF_SYMBOL)) {
526528
return Double.POSITIVE_INFINITY;
527529
}
530+
if (tokenizer.sval.equalsIgnoreCase(NEG_INF_SYMBOL)) {
531+
return Double.NEGATIVE_INFINITY;
532+
}
528533
//TODO: handle -Inf ?
529534
else {
530535
try {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public void testReadNaN() throws Exception {
3838
assertEquals(pt, writer.write(reader.read("POINT (10 10 NAN)")));
3939
}
4040

41+
public void testReadInf() throws Exception {
42+
final String pt = "POINT (Inf -Inf)";
43+
assertEquals(pt, writer.write(reader.read("POINT (Inf -Inf)")));
44+
}
45+
4146
public void testReadPoint() throws Exception {
4247
assertEquals("POINT (10 10)", writer.write(reader.read("POINT (10 10)")));
4348
assertEquals("POINT EMPTY", writer.write(reader.read("POINT EMPTY")));

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,10 +465,12 @@ public void testNaN() throws Exception {
465465
}
466466

467467
public void testInf() throws ParseException {
468-
Point pt = (Point) readerXY.read("POINT ( Inf INF )");
468+
LineString pt = (LineString) readerXY.read("LINESTRING ( Inf -INF, -Inf inf )");
469469
CoordinateSequence cs = pt.getCoordinateSequence();
470470
assertEquals(Double.POSITIVE_INFINITY, cs.getOrdinate(0, Coordinate.X));
471-
assertEquals(Double.POSITIVE_INFINITY, cs.getOrdinate(0, Coordinate.Y));
471+
assertEquals(Double.NEGATIVE_INFINITY, cs.getOrdinate(0, Coordinate.Y));
472+
assertEquals(Double.NEGATIVE_INFINITY, cs.getOrdinate(1, Coordinate.X));
473+
assertEquals(Double.POSITIVE_INFINITY, cs.getOrdinate(1, Coordinate.Y));
472474
}
473475

474476
public void testLargeNumbers() throws Exception {

0 commit comments

Comments
 (0)