Skip to content

Commit

Permalink
Switch to iterative version of WKT format parser
Browse files Browse the repository at this point in the history
Signed-off-by: Heemin Kim <[email protected]>
  • Loading branch information
heemin32 committed Jun 8, 2024
1 parent fbe048f commit 9a429ef
Showing 1 changed file with 85 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Stack;

/**
* Utility class for converting to and from WKT
Expand Down Expand Up @@ -301,16 +302,95 @@ private Geometry parseGeometry(StreamTokenizer stream) throws IOException, Parse
throw new IllegalArgumentException("Unknown geometry type: " + type);
}

/**
* Iterative version of
* <!--
* ```java
* private GeometryCollection<Geometry> parseGeometryCollectionA(StreamTokenizer stream) throws IOException, ParseException {
* if (nextEmptyOrOpen(stream).equals(EMPTY)) {
* return GeometryCollection.EMPTY;
* }
* List<Geometry> shapes = new ArrayList<>();
* shapes.add(parseGeometry(stream));
* while (nextCloserOrComma(stream).equals(COMMA)) {
* shapes.add(parseGeometry(stream));
* }
* return new GeometryCollection<>(shapes);
* }
* -->
* to avoid StackOverflowError when there deeply nested structure of GeometryCollection.
*/
private GeometryCollection<Geometry> parseGeometryCollection(StreamTokenizer stream) throws IOException, ParseException {
if (nextEmptyOrOpen(stream).equals(EMPTY)) {
return GeometryCollection.EMPTY;
}
List<Geometry> shapes = new ArrayList<>();
shapes.add(parseGeometry(stream));
while (nextCloserOrComma(stream).equals(COMMA)) {
shapes.add(parseGeometry(stream));

List<Geometry> topLevelShapes = new ArrayList<>();
Stack<List<Geometry>> stack = new Stack<>();
stack.push(topLevelShapes);
boolean isFirstIteration = true;
List<Geometry> currentLevelShapes = null;
while (!stack.isEmpty()) {
List<Geometry> previousShapes = stack.pop();
if (currentLevelShapes != null) {
previousShapes.add(new GeometryCollection<>(currentLevelShapes));
}
currentLevelShapes = previousShapes;

if (isFirstIteration == true) {
isFirstIteration = false;
} else {
if (!nextCloserOrComma(stream).equals(COMMA)) {
continue;
}
}
while (true) {
final String type = nextWord(stream).toLowerCase(Locale.ROOT);
switch (type) {
case "point":
currentLevelShapes.add(parsePoint(stream));
break;
case "multipoint":
currentLevelShapes.add(parseMultiPoint(stream));
break;
case "linestring":
currentLevelShapes.add(parseLine(stream));
break;
case "multilinestring":
currentLevelShapes.add(parseMultiLine(stream));
break;
case "polygon":
currentLevelShapes.add(parsePolygon(stream));
break;
case "multipolygon":
currentLevelShapes.add(parseMultiPolygon(stream));
break;
case "bbox":
currentLevelShapes.add(parseBBox(stream));
break;
case "geometrycollection":
if (nextEmptyOrOpen(stream).equals(EMPTY)) {
currentLevelShapes.add(GeometryCollection.EMPTY);
break;
} else {
stack.push(currentLevelShapes);
currentLevelShapes = new ArrayList<>();
continue;
}
case "circle": // Not part of the standard, but we need it for internal serialization
currentLevelShapes.add(parseCircle(stream));
break;
default:
throw new IllegalArgumentException("Unknown geometry type: " + type);
}

if (!nextCloserOrComma(stream).equals(COMMA)) {
break;
}
}
}
return new GeometryCollection<>(shapes);

return new GeometryCollection<>(topLevelShapes);
}

private Point parsePoint(StreamTokenizer stream) throws IOException, ParseException {
Expand Down

0 comments on commit 9a429ef

Please sign in to comment.