Skip to content

Commit

Permalink
Empty WKT Strings (#1162) (#1237)
Browse files Browse the repository at this point in the history
* read ahead and check if empty string, if so, return empty geom

* specifically force toStrings to return geom with EMPTY string

* Tests for empty WKT parsing

* Check if bbox exists before reaching into it

* WKT writer specifically write EMPTY string

* isEmpty for Geometry interface, do not simplify if empty
  • Loading branch information
alexgao1 authored Jul 24, 2024
1 parent d3a7871 commit 2baff93
Show file tree
Hide file tree
Showing 13 changed files with 321 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public GeometrySimplificationReport simplifyGeometry(Geometry geometry) throws E

if( geometry instanceof Point ){
// Do not simplify points
}
else if (geometry.isEmpty()) {
// Don't simplify what doesn't exist
} else {
Geometry lastGeometry = null;
for(Double resolution : resolutions){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public interface Geometry {
BoundingBox getBoundingBox();

void extendBoundingBox(BoundingBox boundingBox);

boolean isEmpty();

/**
* Unravels all the geometry collections and saves the basic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public void addGeometry(Geometry geometry){
public String toString(){
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);

if (isEmpty()) {
pw.print("GEOMETRYCOLLECTION EMPTY");
pw.flush();
return sw.toString();
}

pw.print("GEOMETRYCOLLECTION(");

Expand Down Expand Up @@ -68,4 +74,9 @@ public void accumulateBasicGeometries(Collection<Geometry> geometries) {
geometry.accumulateBasicGeometries(geometries);
}
}

@Override
public boolean isEmpty() {
return (getGeometries().size() == 0);
}
}
11 changes: 11 additions & 0 deletions nunaliit2-geom/src/main/java/ca/carleton/gcrc/geom/LineString.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public void addPoint(Point point){
public String toString(){
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);

if (isEmpty()) {
pw.print("LINESTRING EMPTY");
pw.flush();
return sw.toString();
}

pw.print("LINESTRING(");

Expand Down Expand Up @@ -69,4 +75,9 @@ public void extendBoundingBox(BoundingBox boundingBox) {
public void accumulateBasicGeometries(Collection<Geometry> geometries) {
geometries.add(this);
}

@Override
public boolean isEmpty() {
return (getPoints().size() == 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public void addLineString(LineString lineString){
public String toString(){
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);

if (isEmpty()) {
pw.print("MULTILINESTRING EMPTY");
pw.flush();
return sw.toString();
}

pw.print("MULTILINESTRING(");

Expand Down Expand Up @@ -94,4 +100,9 @@ public void accumulateBasicGeometries(Collection<Geometry> geometries) {
lineString.accumulateBasicGeometries(geometries);
}
}

@Override
public boolean isEmpty() {
return (getLineStrings().size() == 0);
}
}
11 changes: 11 additions & 0 deletions nunaliit2-geom/src/main/java/ca/carleton/gcrc/geom/MultiPoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public void addPoint(Point point){
public String toString(){
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);

if (isEmpty()) {
pw.print("MULTIPOINT EMPTY");
pw.flush();
return sw.toString();
}

pw.print("MULTIPOINT(");

Expand Down Expand Up @@ -85,4 +91,9 @@ public void accumulateBasicGeometries(Collection<Geometry> geometries) {
point.accumulateBasicGeometries(geometries);
}
}

@Override
public boolean isEmpty() {
return (getPoints().size() == 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public void addPolygon(Polygon polygon){
public String toString(){
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);

if (isEmpty()) {
pw.print("MULTIPOLYGON EMPTY");
pw.flush();
return sw.toString();
}

pw.print("MULTIPOLYGON(");

Expand Down Expand Up @@ -107,4 +113,9 @@ public void accumulateBasicGeometries(Collection<Geometry> geometries) {
polygon.accumulateBasicGeometries(geometries);
}
}

@Override
public boolean isEmpty() {
return (getPolygons().size() == 0);
}
}
11 changes: 11 additions & 0 deletions nunaliit2-geom/src/main/java/ca/carleton/gcrc/geom/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ public Double getZ(){
public String toString(){
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);

if (isEmpty()) {
pw.print("POINT EMPTY");
pw.flush();
return sw.toString();
}

pw.print("POINT(");

Expand Down Expand Up @@ -98,4 +104,9 @@ public void extendBoundingBox(BoundingBox boundingBox) {
public void accumulateBasicGeometries(Collection<Geometry> geometries) {
geometries.add(this);
}

@Override
public boolean isEmpty() {
return (getPositions().size() == 0);
}
}
11 changes: 11 additions & 0 deletions nunaliit2-geom/src/main/java/ca/carleton/gcrc/geom/Polygon.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public void addLinearRing(LineString linearRing){
public String toString(){
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);

if (isEmpty()) {
pw.print("POLYGON EMPTY");
pw.flush();
return sw.toString();
}

pw.print("POLYGON(");

Expand Down Expand Up @@ -80,4 +86,9 @@ public void extendBoundingBox(BoundingBox boundingBox) {
public void accumulateBasicGeometries(Collection<Geometry> geometries) {
geometries.add(this);
}

@Override
public boolean isEmpty() {
return (getLinearRings().size() == 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,18 @@ public Geometry parseWkt(Reader reader) throws Exception {
BufferedReader bufReader = new BufferedReader(reader);
return parseGeometry(bufReader);
}

private boolean isWKTEmpty(BufferedReader br) throws Exception {
String geomData = readIdentifier(br);
return geomData.equalsIgnoreCase("empty");
}

private Geometry parseGeometry(BufferedReader bufReader) throws Exception {

Geometry geometry = null;
try {
skipWhiteSpaces(bufReader);
String identifier = readIdentifier(bufReader);

if( "point".equalsIgnoreCase(identifier) ) {
geometry = parsePoint(bufReader);

Expand Down Expand Up @@ -281,6 +285,11 @@ private List<Number> parsePositions(BufferedReader br) throws Exception {
private Point parsePoint(BufferedReader br) throws Exception {

skipWhiteSpaces(br);

if (isWKTEmpty(br)) {
return new Point();
}

popLeftParen(br);

List<Number> positions = parsePositions(br);
Expand All @@ -299,6 +308,11 @@ private LineString parseLineString(BufferedReader br) throws Exception {
LineString lineString = new LineString();

skipWhiteSpaces(br);

if (isWKTEmpty(br)) {
return lineString;
}

popLeftParen(br);

// Accumulate points
Expand Down Expand Up @@ -329,6 +343,11 @@ private Polygon parsePolygon(BufferedReader br) throws Exception {
Polygon polygon = new Polygon();

skipWhiteSpaces(br);

if (isWKTEmpty(br)) {
return polygon;
}

popLeftParen(br);

LineString ls = parseLineString(br);
Expand All @@ -351,6 +370,11 @@ private MultiPoint parseMultiPoint(BufferedReader br) throws Exception {
MultiPoint multiPoint = new MultiPoint();

skipWhiteSpaces(br);

if (isWKTEmpty(br)) {
return multiPoint;
}

popLeftParen(br);

boolean done = false;
Expand Down Expand Up @@ -386,6 +410,11 @@ private MultiLineString parseMultiLineString(BufferedReader br) throws Exception
MultiLineString multiLineString = new MultiLineString();

skipWhiteSpaces(br);

if (isWKTEmpty(br)) {
return multiLineString;
}

popLeftParen(br);

boolean done = false;
Expand All @@ -412,6 +441,11 @@ private MultiPolygon parseMultiPolygon(BufferedReader br) throws Exception {
MultiPolygon multiPolygon = new MultiPolygon();

skipWhiteSpaces(br);

if (isWKTEmpty(br)) {
return multiPolygon;
}

popLeftParen(br);

boolean done = false;
Expand All @@ -438,6 +472,11 @@ private GeometryCollection parseGeometryCollection(BufferedReader br) throws Exc
GeometryCollection geometryCollection = new GeometryCollection();

skipWhiteSpaces(br);

if (isWKTEmpty(br)) {
return geometryCollection;
}

popLeftParen(br);

boolean done = false;
Expand Down
Loading

0 comments on commit 2baff93

Please sign in to comment.