Skip to content

Commit

Permalink
Fix KMLReader to read coordinates with whitespace (#611)
Browse files Browse the repository at this point in the history
  • Loading branch information
FAlooC committed Jun 9, 2022
1 parent e4e6f3f commit e281524
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/NetTopologySuite/IO/KML/KMLReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;
using NetTopologySuite.Geometries;
using NetTopologySuite.Utilities;
Expand All @@ -17,6 +18,7 @@ public class KMLReader
//private final XMLInputFactory inputFactory = XMLInputFactory.newInstance();
private readonly GeometryFactory geometryFactory;
private readonly ISet<string> attributeNames;
private readonly Regex whitespaceRegex = new Regex(@"\s+");

private const string POINT = "Point";
private const string LINESTRING = "LineString";
Expand Down Expand Up @@ -113,6 +115,8 @@ private Coordinate[] ParseKMLCoordinates(XmlReader xmlStreamReader)
string coordinates = xmlStreamReader.ReadElementString();
if (string.IsNullOrWhiteSpace(coordinates))
RaiseParseError("Empty coordinates");

coordinates = whitespaceRegex.Replace(coordinates.Trim(), " ");

double[] parsedOrdinates = {double.NaN, double.NaN, double.NaN};
var coordinateList = new List<Coordinate>();
Expand Down
13 changes: 13 additions & 0 deletions test/NetTopologySuite.Tests.NUnit/IO/KML/KMLReaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,19 @@ public void TestPolygonIssue594()
new IDictionary<string, string>[] { null }
);
}

[Test]
public void TestCoordinatesWithWhitespace()
{
CheckParsingResult(new KMLReader(),
"<LineString>" +
" <coordinates> 1.0,2.0" +
" -1.0,-2.0 </coordinates>" +
"</LineString>",
"LINESTRING (1 2, -1 -2)",
new IDictionary<string, string>[]{ null, null }
);
}

private void CheckExceptionThrown(string kmlString, string expectedError)
{
Expand Down

0 comments on commit e281524

Please sign in to comment.