使用 NetTopologySuite 从点集合创建多边形

create Polygon from point collection with NetTopologySuite

从点列表创建多边形的最佳方法是什么?

我有一个点数组,如果点数至少为 3 我想加入以创建一个多边形

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

我认为我离解决方案还很远。 你能帮帮我吗?

您可以像这样使用 GeometryFactory 创建一个包含坐标数组的多边形:

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

这是 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);

这里是使用 nettopologysuite 的 c# 解决方案 (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;
        }

注意:这里的4326是坐标系的srid。没有这个几何操作就不会给出正确的结果

-------------------------------- 以上方法的用法

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())