From 12f8b1101a4f393e1cadb3aa3bdbc18d24fc782b Mon Sep 17 00:00:00 2001 From: Jeen Broekstra Date: Mon, 9 Mar 2020 13:32:49 +1100 Subject: [PATCH] GH-162 handle empty point / shape collection in toString Signed-off-by: Jeen Broekstra --- .../locationtech/spatial4j/io/WKTWriter.java | 27 +++++++++---- .../spatial4j/io/WKTWriterTest.java | 40 +++++++++++++++++++ 2 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 src/test/java/org/locationtech/spatial4j/io/WKTWriterTest.java diff --git a/src/main/java/org/locationtech/spatial4j/io/WKTWriter.java b/src/main/java/org/locationtech/spatial4j/io/WKTWriter.java index 38a49156a..a20afa768 100644 --- a/src/main/java/org/locationtech/spatial4j/io/WKTWriter.java +++ b/src/main/java/org/locationtech/spatial4j/io/WKTWriter.java @@ -8,6 +8,11 @@ package org.locationtech.spatial4j.io; +import java.io.IOException; +import java.io.Writer; +import java.math.RoundingMode; +import java.text.NumberFormat; +import java.util.Iterator; import org.locationtech.spatial4j.shape.Circle; import org.locationtech.spatial4j.shape.Point; import org.locationtech.spatial4j.shape.Rectangle; @@ -16,12 +21,6 @@ import org.locationtech.spatial4j.shape.impl.BufferedLine; import org.locationtech.spatial4j.shape.impl.BufferedLineString; -import java.io.IOException; -import java.io.Writer; -import java.math.RoundingMode; -import java.text.NumberFormat; -import java.util.Iterator; - public class WKTWriter implements ShapeWriter { @Override @@ -42,8 +41,13 @@ protected NumberFormat getNumberFormat() { public String toString(Shape shape) { NumberFormat nf = getNumberFormat(); if (shape instanceof Point) { + Point point = (Point)shape; + if (point.isEmpty()) { + return "POINT EMPTY"; + } StringBuilder buffer = new StringBuilder(); - return append(buffer.append("POINT ("),(Point)shape,nf).append(")").toString(); + + return append(buffer.append("POINT ("), point, nf).append(")").toString(); } if (shape instanceof Rectangle) { NumberFormat nfMIN = nf; @@ -103,10 +107,17 @@ public String toString(Shape shape) { return str.toString(); } if(shape instanceof ShapeCollection) { + @SuppressWarnings("unchecked") + ShapeCollection collection = (ShapeCollection) shape; + + if (collection.isEmpty()) { + return "GEOMETRYCOLLECTION EMPTY"; + } + StringBuilder buffer = new StringBuilder(); buffer.append("GEOMETRYCOLLECTION ("); boolean first = true; - for(Shape sub : ((ShapeCollection)shape).getShapes()) { + for (Shape sub : collection.getShapes()) { if(!first) { buffer.append(","); } diff --git a/src/test/java/org/locationtech/spatial4j/io/WKTWriterTest.java b/src/test/java/org/locationtech/spatial4j/io/WKTWriterTest.java new file mode 100644 index 000000000..fcf3ca40b --- /dev/null +++ b/src/test/java/org/locationtech/spatial4j/io/WKTWriterTest.java @@ -0,0 +1,40 @@ +package org.locationtech.spatial4j.io; + +import static org.junit.Assert.assertEquals; +import java.util.ArrayList; +import org.junit.Test; +import org.locationtech.spatial4j.context.SpatialContext; +import org.locationtech.spatial4j.shape.Point; +import org.locationtech.spatial4j.shape.ShapeCollection; + +public class WKTWriterTest { + + private SpatialContext ctx; + + protected WKTWriterTest(SpatialContext ctx) { + this.ctx = ctx; + } + + public WKTWriterTest() { + this(SpatialContext.GEO); + } + + @Test + public void testToStringOnEmptyPoint() throws Exception { + ShapeWriter writer = ctx.getFormats().getWktWriter(); + Point emptyPoint = ctx.makePoint(Double.NaN, Double.NaN); + + assertEquals("POINT EMPTY", writer.toString(emptyPoint)); + + } + + @Test + public void testToStringOnEmptyShapeCollection() throws Exception { + ShapeWriter writer = ctx.getFormats().getWktWriter(); + ShapeCollection emptyCollection = ctx.makeCollection(new ArrayList()); + + assertEquals("GEOMETRYCOLLECTION EMPTY", writer.toString(emptyCollection)); + + } + +}