Skip to content

Commit 028a36a

Browse files
committed
locationtechGH-162 handle empty point / shape collection in toString
Signed-off-by: Jeen Broekstra <[email protected]>
1 parent 86f83bf commit 028a36a

File tree

2 files changed

+56
-8
lines changed

2 files changed

+56
-8
lines changed

src/main/java/org/locationtech/spatial4j/io/WKTWriter.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
package org.locationtech.spatial4j.io;
1010

11+
import java.io.IOException;
12+
import java.io.Writer;
13+
import java.math.RoundingMode;
14+
import java.text.NumberFormat;
15+
import java.util.Iterator;
1116
import org.locationtech.spatial4j.shape.Circle;
1217
import org.locationtech.spatial4j.shape.Point;
1318
import org.locationtech.spatial4j.shape.Rectangle;
@@ -16,12 +21,6 @@
1621
import org.locationtech.spatial4j.shape.impl.BufferedLine;
1722
import org.locationtech.spatial4j.shape.impl.BufferedLineString;
1823

19-
import java.io.IOException;
20-
import java.io.Writer;
21-
import java.math.RoundingMode;
22-
import java.text.NumberFormat;
23-
import java.util.Iterator;
24-
2524
public class WKTWriter implements ShapeWriter {
2625

2726
@Override
@@ -42,8 +41,13 @@ protected NumberFormat getNumberFormat() {
4241
public String toString(Shape shape) {
4342
NumberFormat nf = getNumberFormat();
4443
if (shape instanceof Point) {
44+
Point point = (Point)shape;
45+
if (point.isEmpty()) {
46+
return "POINT EMPTY";
47+
}
48+
4549
StringBuilder buffer = new StringBuilder();
46-
return append(buffer.append("POINT ("),(Point)shape,nf).append(")").toString();
50+
return append(buffer.append("POINT ("), point, nf).append(")").toString();
4751
}
4852
if (shape instanceof Rectangle) {
4953
NumberFormat nfMIN = nf;
@@ -103,10 +107,17 @@ public String toString(Shape shape) {
103107
return str.toString();
104108
}
105109
if(shape instanceof ShapeCollection) {
110+
@SuppressWarnings("unchecked")
111+
ShapeCollection<? extends Shape> collection = (ShapeCollection<? extends Shape>) shape;
112+
113+
if (collection.isEmpty()) {
114+
return "GEOMETRYCOLLECTION EMPTY";
115+
}
116+
106117
StringBuilder buffer = new StringBuilder();
107118
buffer.append("GEOMETRYCOLLECTION (");
108119
boolean first = true;
109-
for(Shape sub : ((ShapeCollection<? extends Shape>)shape).getShapes()) {
120+
for (Shape sub : collection.getShapes()) {
110121
if(!first) {
111122
buffer.append(",");
112123
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.locationtech.spatial4j.io;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import java.util.ArrayList;
5+
import org.junit.Test;
6+
import org.locationtech.spatial4j.context.SpatialContext;
7+
import org.locationtech.spatial4j.shape.Point;
8+
import org.locationtech.spatial4j.shape.ShapeCollection;
9+
10+
public class WKTWriterTest {
11+
12+
private SpatialContext ctx;
13+
14+
protected WKTWriterTest(SpatialContext ctx) {
15+
this.ctx = ctx;
16+
}
17+
18+
public WKTWriterTest() {
19+
this(SpatialContext.GEO);
20+
}
21+
22+
@Test
23+
public void testToStringOnEmptyPoint() throws Exception {
24+
ShapeWriter writer = ctx.getFormats().getWktWriter();
25+
Point emptyPoint = ctx.makePoint(Double.NaN, Double.NaN);
26+
27+
assertEquals("POINT EMPTY", writer.toString(emptyPoint));
28+
}
29+
30+
@Test
31+
public void testToStringOnEmptyShapeCollection() throws Exception {
32+
ShapeWriter writer = ctx.getFormats().getWktWriter();
33+
ShapeCollection<Point> emptyCollection = ctx.makeCollection(new ArrayList<Point>());
34+
35+
assertEquals("GEOMETRYCOLLECTION EMPTY", writer.toString(emptyCollection));
36+
}
37+
}

0 commit comments

Comments
 (0)