I have list of geometry https://pastebin.ubuntu.com/p/zhgPJ4BkpK/ that I got from over pass turbo, Now I want to make polygon by .NET Core, in order to, after converting lat and long to coordinate Coordinate coordinate = new Coordinate(g.lon, g.lat);
I am trying to convert list to polygon:
var coordinates = new List<Coordinate>();
foreach (var geoElement in elements)
{
if (geoElement.type == "way")
{
foreach (var g in geoElement.geometry)
{
Coordinate coordinate = new Coordinate(g.lon, g.lat);
coordinates.Add(coordinate);
}
}
else
{
foreach (var m in geoElement.members)
{
if (m.type == "way")
{
foreach (var g in m.geometry)
{
Coordinate coordinate = new Coordinate(g.lon, g.lat);
coordinates.Add(coordinate);
}
}
}
}
}
var geometryFactory = new GeometryFactory(new PrecisionModel(), 4326);
var polygon = geometryFactory.CreatePolygon(coordinates.ToArray());
return polygon;
But I got this error:
Unhandled exception. System.ArgumentException: points must form a closed linestring
at NetTopologySuite.Geometries.LinearRing.ValidateConstruction()
at NetTopologySuite.Geometries.LinearRing..ctor(CoordinateSequence points, GeometryFactory factory)
at NetTopologySuite.Geometries.GeometryFactory.CreateLinearRing(CoordinateSequence coordinates)
at NetTopologySuite.Geometries.GeometryFactory.CreateLinearRing(Coordinate[] coordinates)
at NetTopologySuite.Geometries.GeometryFactory.CreatePolygon(Coordinate[] coordinates)
How can I fix this issue?
Add this line after the foreach
, so it will create a closed linestring, which can be used to create a polygon.
coordinates.Add(coordinates[0]);