Skip to content

Commit

Permalink
Fix KMLReader to read coordinates with whitespace (#1062)
Browse files Browse the repository at this point in the history
  • Loading branch information
tohidemyname authored Jul 4, 2024
1 parent 8c71ae7 commit 11813d3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Constructs a {@link Geometry} object from the OGC KML representation.
Expand All @@ -44,6 +46,8 @@ public class KMLReader {
private final GeometryFactory geometryFactory;
private final Set<String> attributeNames;

private final Pattern whitespaceRegex = Pattern.compile("\\s+");

private static final String POINT = "Point";
private static final String LINESTRING = "LineString";
private static final String POLYGON = "Polygon";
Expand Down Expand Up @@ -119,6 +123,8 @@ private Coordinate[] parseKMLCoordinates(XMLStreamReader xmlStreamReader) throws
if (coordinates.isEmpty()) {
raiseParseError("Empty coordinates");
}
Matcher matcher= whitespaceRegex.matcher(coordinates.trim());
coordinates = matcher.replaceAll(" ");

double[] parsedOrdinates = {Double.NaN, Double.NaN, Double.NaN};
List<Coordinate> coordinateList = new ArrayList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
import org.locationtech.jts.geom.PrecisionModel;
import org.locationtech.jts.io.ParseException;

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.*;

public class KMLReaderTest extends TestCase {
public static void main(String args[]) {
Expand Down Expand Up @@ -131,6 +129,17 @@ public void testMultiGeometryWithAllPolygons() {
);
}

public void testCoordinatesWithWhitespace()
{
checkParsingResult(
"<LineString>" +
" <coordinates> 1.0,2.0" +
" -1.0,-2.0 </coordinates>" +
"</LineString>",
"LINESTRING (1 2, -1 -2)",
new Map[]{null, null}
);
}
public void testZ() {
String kml = "<Point><coordinates>1.0,1.0,50.0</coordinates></Point>";
KMLReader kmlReader = new KMLReader();
Expand All @@ -155,6 +164,7 @@ public void testPrecisionAndSRID() {
}
}


public void testCoordinatesErrors() {
checkExceptionThrown("<Point></Point>", "No element coordinates found in Point");
checkExceptionThrown("<Point><coordinates></coordinates></Point>", "Empty coordinates");
Expand Down

0 comments on commit 11813d3

Please sign in to comment.