From 11813d36c31bfc388ea173dcc10c469060129863 Mon Sep 17 00:00:00 2001 From: tohidemyname <42423544+tohidemyname@users.noreply.github.com> Date: Thu, 4 Jul 2024 10:00:09 +0800 Subject: [PATCH] Fix KMLReader to read coordinates with whitespace (#1062) --- .../org/locationtech/jts/io/kml/KMLReader.java | 6 ++++++ .../locationtech/jts/io/kml/KMLReaderTest.java | 16 +++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/modules/core/src/main/java/org/locationtech/jts/io/kml/KMLReader.java b/modules/core/src/main/java/org/locationtech/jts/io/kml/KMLReader.java index f1aabdc15f..bf00a0e203 100644 --- a/modules/core/src/main/java/org/locationtech/jts/io/kml/KMLReader.java +++ b/modules/core/src/main/java/org/locationtech/jts/io/kml/KMLReader.java @@ -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. @@ -44,6 +46,8 @@ public class KMLReader { private final GeometryFactory geometryFactory; private final Set 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"; @@ -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 coordinateList = new ArrayList(); diff --git a/modules/core/src/test/java/org/locationtech/jts/io/kml/KMLReaderTest.java b/modules/core/src/test/java/org/locationtech/jts/io/kml/KMLReaderTest.java index eda1d31839..742ca4351b 100644 --- a/modules/core/src/test/java/org/locationtech/jts/io/kml/KMLReaderTest.java +++ b/modules/core/src/test/java/org/locationtech/jts/io/kml/KMLReaderTest.java @@ -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[]) { @@ -131,6 +129,17 @@ public void testMultiGeometryWithAllPolygons() { ); } + public void testCoordinatesWithWhitespace() + { + checkParsingResult( + "" + + " 1.0,2.0" + + " -1.0,-2.0 " + + "", + "LINESTRING (1 2, -1 -2)", + new Map[]{null, null} + ); + } public void testZ() { String kml = "1.0,1.0,50.0"; KMLReader kmlReader = new KMLReader(); @@ -155,6 +164,7 @@ public void testPrecisionAndSRID() { } } + public void testCoordinatesErrors() { checkExceptionThrown("", "No element coordinates found in Point"); checkExceptionThrown("", "Empty coordinates");