create Polygon from point collection with NetTopologySuite
Asked Answered
D

3

6

What is the best way to create a polygon from list of point?

I have an array of points, if points are at least 3 I would like to join to create a polygon

Dim ClickedPoint As New NetTopologySuite.Geometries.Point(coordinates)
ClickedPointArray.Add(ClickedPoint)

if   ClickedPointArray.Count > 2 then

   Polygonizer = New Polygonizer()
   Polygonizer.Add(ClickedPointArray)

end if        

return Polygonizer.GetPolygons

I think I'm very far from solution. Could you help me?

Dorthydortmund answered 29/9, 2015 at 7:58 Comment(1)
Like this? ` var сoordinates = new List<NetTopologySuite.Geometries.Coordinate>(); var polygon = new NetTopologySuite.Geometries.Polygon(new NetTopologySuite.Geometries.LinearRing(сoordinates.ToArray()));`Anschluss
R
4

You can create a Polygon with an array of coordinates using GeometryFactory like this:

Dim coordinatesArray as Coordinate[] = YourMethodToGetCoordinates
Dim geomFactory As New GeometryFactory
Dim poly As geomFactory.CreatePolygon(coordinatesArray) //this returns an IPolygon that you can cast to Polygon
Rowden answered 17/5, 2018 at 12:2 Comment(1)
(Maybe it's already late to answer this, but I was looking for the same(in C#) and when I solved it I thought answering this could be useful for the next one visiting)Rowden
Y
1

Here is the C#

    Coordinate[] imageOutlineCoordinates = new Coordinate[] 
    {
        new Coordinate(1, 1),
        new Coordinate(2, 1),
        new Coordinate(2, 2),
        new Coordinate(1, 1)
    };
    GeometryFactory geometryFactory = new GeometryFactory();
    Polygon poly = geometryFactory.CreatePolygon(imageOutlineCoordinates);
Yatzeck answered 9/9, 2021 at 17:31 Comment(0)
N
0

here is a solution for c# using nettopologysuite (https://github.com/NetTopologySuite/NetTopologySuite)

Polygon GetGeometry(Coordinate[] coordinates)
        {
            var geometryFactory = new GeometryFactory(new PrecisionModel(), 4326);
            var polygon = new Polygon(new LinearRing(coordinates), geometryFactory);
            return polygon;
        }

Note: here 4326 is srid of coordinate system. Without this geometry operations dont give correct results

--------------------------------- Usage of above method

var coordinates = new List<Coordinate>();


 for (var index = 0; index < doubleList.Count; index = index + 2)
    {
      var coordinate = new Coordinate(doubleList[index], doubleList[index + 1]);
        coordinates.Add(coordinate);
    }   


   coordinates.Add(new Coordinate(points[0], points[1]));
    
   GetGeometry(coordinates.ToArray())
Nietzsche answered 8/3, 2022 at 15:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.