ArcGIS Pro 3.8 API Reference Guide
ArcGIS.Core.Geometry Namespace / PolygonBuilderEx Class / ToGeometry() Method
Example

In This Topic
    ToGeometry() Method
    In This Topic
    Returns a Polygon instance representing the current state of the builder.
    Syntax
    public new Polygon ToGeometry()
    Public Shadows Function ToGeometry() As Polygon

    Return Value

    Example
    Construct a Polygon - from an enumeration of MapPoints
    {
      // Use a builderEx convenience method or use a builderEx constructor.
    
      MapPoint pt1 = MapPointBuilderEx.CreateMapPoint(1.0, 1.0);
      MapPoint pt2 = MapPointBuilderEx.CreateMapPoint(1.0, 2.0);
      MapPoint pt3 = MapPointBuilderEx.CreateMapPoint(2.0, 2.0);
      MapPoint pt4 = MapPointBuilderEx.CreateMapPoint(2.0, 1.0);
    
      List<MapPoint> list = new List<MapPoint>() { pt1, pt2, pt3, pt4 };
    
      Polygon polygonFromPoints = PolygonBuilderEx.CreatePolygon(list, SpatialReferences.WGS84);
      // polygon.HasZ will be false - it is determined by the HasZ flag of the points in the list
    
      // or specifically use AttributeFlags.NoAttributes
      polygonFromPoints = PolygonBuilderEx.CreatePolygon(list, AttributeFlags.None);
    
      // use AttributeFlags.None as we have 2D points
      PolygonBuilderEx polygonBuilder = new PolygonBuilderEx(list, AttributeFlags.None);
      polygonBuilder.SpatialReference = SpatialReferences.WGS84;
      polygonFromPoints = polygonBuilder.ToGeometry();
    }
    Construct a Polygon - from an Envelope
    {
      // Use a builderEx convenience method or use a builderEx constructor.
    
      MapPoint minPt = MapPointBuilderEx.CreateMapPoint(1.0, 1.0);
      MapPoint maxPt = MapPointBuilderEx.CreateMapPoint(2.0, 2.0);
    
      // Create an envelope
      Envelope env = EnvelopeBuilderEx.CreateEnvelope(minPt, maxPt);
    
      Polygon polygonFromEnv = PolygonBuilderEx.CreatePolygon(env);
    
      PolygonBuilderEx polygonBuilderEx = new PolygonBuilderEx(env);
      polygonBuilderEx.SpatialReference = SpatialReferences.WGS84;
      polygonFromEnv = polygonBuilderEx.ToGeometry() as Polygon;
    }
    Build a donut polygon
    {
      List<Coordinate2D> outerCoordinates = new List<Coordinate2D>();
      outerCoordinates.Add(new Coordinate2D(10.0, 10.0));
      outerCoordinates.Add(new Coordinate2D(10.0, 20.0));
      outerCoordinates.Add(new Coordinate2D(20.0, 20.0));
      outerCoordinates.Add(new Coordinate2D(20.0, 10.0));
    
      // define the inner polygon as anti-clockwise
      List<Coordinate2D> innerCoordinates = new List<Coordinate2D>();
      innerCoordinates.Add(new Coordinate2D(13.0, 13.0));
      innerCoordinates.Add(new Coordinate2D(17.0, 13.0));
      innerCoordinates.Add(new Coordinate2D(17.0, 17.0));
      innerCoordinates.Add(new Coordinate2D(13.0, 17.0));
    
      PolygonBuilderEx pbEx = new PolygonBuilderEx(outerCoordinates);
      Polygon donutEx = pbEx.ToGeometry() as Polygon;
      double areaEx = donutEx.Area;       // area = 100
    
      pbEx.AddPart(innerCoordinates);
      donutEx = pbEx.ToGeometry() as Polygon;
    
      areaEx = donutEx.Area;    // area = 84.0
    
      areaEx = GeometryEngine.Instance.Area(donutEx);    // area = 84.0
    }
    Get the outermost rings of a polygon
    {
      List<Polygon> internalRings = new List<Polygon>();
    
      // explode the parts of the polygon into a list of individual geometries
      // see the "Get the individual parts of a multipart feature"
      // snippet for MultipartToSinglePart
      var parts = MultipartToSinglePart(polygon); //any polygon
    
      // get an enumeration of clockwise geometries (area > 0) ordered by the area
      var clockwiseParts = parts.Where(geom => ((Polygon)geom).Area > 0)
                            .OrderByDescending(geom => ((Polygon)geom).Area);
    
      // for each of the exterior rings
      foreach (var part in clockwiseParts)
      {
        // add the first (the largest) ring into the internal collection
        if (internalRings.Count == 0)
        {
          internalRings.Add(part as Polygon);
        }
    
        // use flag to indicate if current part is within the already selection polygons
        bool isWithin = false;
    
        foreach (var item in internalRings)
        {
          if (GeometryEngine.Instance.Within(part, item))
          {
            isWithin = true;
          }
        }
    
        // if the current polygon is not within any polygon of the internal collection
        // then it is disjoint and needs to be added to
        if (isWithin == false)
        {
          internalRings.Add(part as Polygon);
        }
      }
    
      PolygonBuilderEx outerRings = new PolygonBuilderEx();
      // now assemble a new polygon geometry based on the internal polygon collection
      foreach (var ring in internalRings)
      {
        outerRings.AddParts(ring.Parts);
      }
    
      // the final geometry of the outer rings
      var outerRingsResult = outerRings.ToGeometry();
    }
    Determine the boundary of a multi-part Polygon
    {
      // create a donut polygon.  Must use the PolygonBuilderEx object
    
      List<Coordinate2D> outerPts =
      [
        new Coordinate2D(10.0, 10.0),
        new Coordinate2D(10.0, 20.0),
        new Coordinate2D(20.0, 20.0),
        new Coordinate2D(20.0, 10.0),
      ];
    
      List<Coordinate2D> innerPts =
      [
        new Coordinate2D(13.0, 13.0),
        new Coordinate2D(17.0, 13.0),
        new Coordinate2D(17.0, 17.0),
        new Coordinate2D(13.0, 17.0),
      ];
    
      Polygon donut = null;
    
      // add the outer points
      PolygonBuilderEx pb = new(outerPts);
      // add the inner points (note they are defined anticlockwise)
      pb.AddPart(innerPts);
      // get the polygon
      donut = pb.ToGeometry();
    
      // get the boundary
      Geometry g = GeometryEngine.Instance.Boundary(donut);
      Polyline boundary = g as Polyline;
    
      // use the boundary...
    }
    Split multipart at point
    {
      // define a polyline
      MapPoint startPointZ = MapPointBuilderEx.CreateMapPoint(1, 1, 5);
      MapPoint endPointZ = MapPointBuilderEx.CreateMapPoint(20, 1, 5);
    
      Polyline polylineZ = PolylineBuilderEx.CreatePolyline([startPointZ, endPointZ]);
    
      // define a split point
      MapPoint splitPointAboveLine = MapPointBuilderEx.CreateMapPoint(10, 10, 10);
    
      // split the polyline at the point. Don't project the split point onto the line, don't create a new part
      var splitPolyline = GeometryEngine.Instance.SplitAtPoint(polylineZ, splitPointAboveLine, false, false, out bool splitOccurred, out int partIndex, out int segmentIndex);
    
      // splitOccurred = true
      // partIndex = 0
      // segmentIndex = 1
      // splitPolyline.PointCount = 3
      // splitPolyline.PartCount = 1
      // splitPolyline coordinates are (1, 1, 5), (10, 10, 10), (20, 1, 5)
    
      // split the polyline at the point.  dont project the split point onto the line, do create a new part
      splitPolyline = GeometryEngine.Instance.SplitAtPoint(polylineZ, splitPointAboveLine, false, false, out splitOccurred, out partIndex, out segmentIndex);
    
      // splitOccurred = true
      // partIndex = 1
      // segmentIndex = 0
      // splitPolyline.PointCount = 4
      // splitPolyline.PartCount = 2
      // splitPolyline first part coordinates are (1, 1, 5), (10, 10, 10)
      // splitPolyline second part coordinates are (10, 10, 10), (20, 1, 5)
    
      // split the polyline at the point.  do project the split point onto the line, don't create a new part
      splitPolyline = GeometryEngine.Instance.SplitAtPoint(polylineZ, splitPointAboveLine, false, false, out splitOccurred, out partIndex, out segmentIndex);
    
      // splitOccurred = true
      // partIndex = 0
      // segmentIndex = 1
      // splitPolyline.PointCount = 3
      // splitPolyline.PartCount = 1
      // splitPolyline coordinates are (1, 1, 5), (10, 10, 5), (20, 1, 5)
    
      // split the polyline at the point.  do project the split point onto the line, do create a new part
      splitPolyline = GeometryEngine.Instance.SplitAtPoint(polylineZ, splitPointAboveLine, false, false, out splitOccurred, out partIndex, out segmentIndex);
    
      // splitOccurred = true
      // partIndex = 1
      // segmentIndex = 0
      // splitPolyline.PointCount = 4
      // splitPolyline.PartCount = 2
      // splitPolyline first part coordinates are (1, 1, 5), (10, 10, 5)
      // splitPolyline second part coordinates are (10, 10, 5), (20, 1, 5)
    
      //
      // try to split with a point that won't split the line  - pt extends beyond the line
      //
    
      var pointAfterLine = MapPointBuilderEx.CreateMapPoint(50, 1, 10);
      splitPolyline = GeometryEngine.Instance.SplitAtPoint(polylineZ, pointAfterLine, false, false, out splitOccurred, out partIndex, out segmentIndex);
    
      // splitOccurred = false
      // ignore partIndex, sgementIndex
      // splitPolyline is the same as polylineZ
    
      ///
      ///  multipart polygon
      ///
      List<Coordinate3D> coordsZ =
      [
        new Coordinate3D(10,10,5),
      new Coordinate3D(10,20,5),
      new Coordinate3D(20,20,5),
      new Coordinate3D(20,10,5)
      ];
    
      List<Coordinate3D> coordsZ_2ndPart =
      [
        new Coordinate3D(30,20,10),
      new Coordinate3D(30,30,10),
      new Coordinate3D(35,28,10),
      new Coordinate3D(40,30,10),
      new Coordinate3D(40,20,10)
      ];
    
      var builder = new PolygonBuilderEx
      {
        HasZ = true
      };
      builder.AddPart(coordsZ);
      builder.AddPart(coordsZ_2ndPart);
    
      Polygon multipart = builder.ToGeometry();
    
      // pointA is closer to the first part of the multipart - the split occurs in the first part
      var pointA = MapPointBuilderEx.CreateMapPoint(22, 18, 7);
      var splitPolygon = GeometryEngine.Instance.SplitAtPoint(multipart, pointA, false, false, out splitOccurred, out partIndex, out segmentIndex);
    
      // splitPolygon.PointCount = 12
      // splitPolygon.PartCount = 2
      // splitPolygon first part coordinates  (10, 10, 5), (10, 20, 5), (20, 20, 5), (22, 18, 7), (20, 10, 5), (10, 10, 5)
    
      // pointB is midPoint between the 2 parts - no split will occur
      var pointB = MapPointBuilderEx.CreateMapPoint(25, 20, 7);
      splitPolygon = GeometryEngine.Instance.SplitAtPoint(multipart, pointB, true, false, out splitOccurred, out partIndex, out segmentIndex);
    
      // splitOccurred = false
      // ignore partIndex, sgementIndex
      // splitPolyline is the same as polylineZ
    }
    Requirements

    Target Platforms: Windows 11 Home, Pro, Enterprise (64 bit)

    ArcGIS Pro version: 3.0 or higher.
    See Also