ArcGIS Pro 3.7 API Reference Guide
ArcGIS.Core.Geometry Namespace / GeometryEngine Class / Centroid Method
The input geometry.
Example

In This Topic
    Centroid Method (GeometryEngine)
    In This Topic
    Gets the centroid (center of gravity) of the geometry.
    Syntax
    Public Function Centroid( _
       ByVal geometry As Geometry _
    ) As MapPoint
    public MapPoint Centroid( 
       Geometry geometry
    )

    Parameters

    geometry
    The input geometry.

    Return Value

    Returns the centroid of the geometry.
    Exceptions
    ExceptionDescription
    Geometry is null or empty.
    The method is not implemented for GeometryBag.
    Remarks
    If the geometry is a MapPoint, then the centroid returned is the same as the source; including Z and M-values (if any exist on the source geometry). The centroid of any other geometry type is a point with HasZ = false, HasM = false.
    Example
    Find the centroid of geometries
    {
      // simple polygon
      List<Coordinate2D> list2D =
      [
        new Coordinate2D(0, 0),
        new Coordinate2D(0, 2),
        new Coordinate2D(2, 2),
        new Coordinate2D(2, 0),
      ];
    
      polygon = PolygonBuilderEx.CreatePolygon(list2D, SpatialReferences.WGS84);
    
      // verify it is simple
      bool isSimple = GeometryEngine.Instance.IsSimpleAsFeature(polygon);
      // find the centroid
      MapPoint centroid = GeometryEngine.Instance.Centroid(polygon);
      // centroid.X = 1
      // centroid.Y = 1
    
      // map Point
      MapPoint pt1 = MapPointBuilderEx.CreateMapPoint(1, 2, 3, 4, SpatialReferences.WGS84);
      MapPoint pt2 = MapPointBuilderEx.CreateMapPoint(5, 2, double.NaN, 7);
    
      // pt1.HasZ = true
      // pt1.HasM = true
      centroid = GeometryEngine.Instance.Centroid(pt1);
      // centroid.HasZ = true
      // centroid.HasM = true
      // pt1.IsEqual(centroid) = true
    
      // multipoint
      List<MapPoint> list = [pt1, pt2];
      Multipoint multipoint = MultipointBuilderEx.CreateMultipoint(list);
      // multipoint.HasZ = true
      // multipoint.HasM = true
    
      centroid = GeometryEngine.Instance.Centroid(multipoint);
      // centroid.X = 3
      // centroid.Y = 2
      // centroid.HasZ = false
      // centroid.HasM = false
    }
    Rotate or Move the Annotation
    {
      var oid = 1; //ObjectID of the annotation feature to modify
                   // Note: QueuedTask is required to access the AnnotationFeature
      {
        //Don't use 'Shape'....Shape is the bounding box of the annotation text. This is NOT what you want...
        //
        //var insp = new Inspector();
        //insp.Load(annotationLayer, oid);
        //var shape = insp["SHAPE"] as Polygon;
        //...wrong shape...
    
        //Instead, we must get the TextGraphic from the anno feature.
        //The TextGraphic shape will be the anno baseline...
        QueryFilter qf = new QueryFilter()
        {
          WhereClause = "OBJECTID = 1"
        };
    
        //annotationLayer is ~your~ Annotation layer
    
        using var rowCursor = annotationLayer.Search(qf);
        if (rowCursor.MoveNext())
        {
          using (var annoFeature = rowCursor.Current as
          ArcGIS.Core.Data.Mapping.AnnotationFeature)
          {
            var graphic = annoFeature.GetGraphic();
            var textGraphic = graphic as CIMTextGraphic;
            var textLine = textGraphic.Shape as Polyline;
            // rotate the shape 90 degrees
            var origin = GeometryEngine.Instance.Centroid(textLine);
            Geometry rotatedPolyline = GeometryEngine.Instance.Rotate(textLine, origin, System.Math.PI / 2);
            //Move the line 5 "units" in the x and y direction
            //GeometryEngine.Instance.Move(textLine, 5, 5);
    
            EditOperation op = new EditOperation();
            op.Name = "Change annotation angle";
            op.Modify(annotationLayer, oid, rotatedPolyline);
            op.Execute();
          }
        }
      }
    }
    Requirements

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

    ArcGIS Pro version: 3.0 or higher.
    See Also