Skip to content

Commit

Permalink
GH-162 handle empty point / shape collection in toString
Browse files Browse the repository at this point in the history
Signed-off-by: Jeen Broekstra <[email protected]>
  • Loading branch information
abrokenjester committed Mar 9, 2020
1 parent 86f83bf commit 12f8b11
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/main/java/org/locationtech/spatial4j/io/WKTWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -103,10 +107,17 @@ public String toString(Shape shape) {
return str.toString();
}
if(shape instanceof ShapeCollection) {
@SuppressWarnings("unchecked")
ShapeCollection<? extends Shape> collection = (ShapeCollection<? extends Shape>) shape;

if (collection.isEmpty()) {
return "GEOMETRYCOLLECTION EMPTY";
}

StringBuilder buffer = new StringBuilder();
buffer.append("GEOMETRYCOLLECTION (");
boolean first = true;
for(Shape sub : ((ShapeCollection<? extends Shape>)shape).getShapes()) {
for (Shape sub : collection.getShapes()) {
if(!first) {
buffer.append(",");
}
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/org/locationtech/spatial4j/io/WKTWriterTest.java
Original file line number Diff line number Diff line change
@@ -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<Point> emptyCollection = ctx.makeCollection(new ArrayList<Point>());

assertEquals("GEOMETRYCOLLECTION EMPTY", writer.toString(emptyCollection));

}

}

0 comments on commit 12f8b11

Please sign in to comment.