ArcGIS Pro 3.3 API Reference Guide
ArcGIS.Core.Geometry Namespace / Geometry Class / Clone Method
Example

In This Topic
    Clone Method (Geometry)
    In This Topic
    Clones this geometry instance. As geometry objects are immutable and hence never change, the clone returned will be this object rather than a copy of this instance.
    Syntax
    public Geometry Clone()
    Public Function Clone() As Geometry

    Return Value

    A geometry
    Remarks

    For Internal Use Only:
    In some geodatabase operations it may make sense for a geometry to be set to be recycling for performance or internal resource allocation reasons. In this scenario the geometry's content may change via some external code (i.e. GDB NextFeature). Consequently, the Clone method will return a new geometry with the copied contents of this instance rather than the original object. This cloned geometry should be passed to the code requiring the geometry object.

    Example
    Construct a MapPoint
    // Use a builder convenience method or use a builder constructor.
    
    // Create a 2D point without a spatial reference
    MapPoint point2D = MapPointBuilderEx.CreateMapPoint(1, 2);
    SpatialReference sr = point2D.SpatialReference; // sr = null
    bool hasZ = point2D.HasZ;   // hasZ = false
    bool hasM = point2D.HasM;   // hasM = false
    bool hasID = point2D.HasID; // hasID = false
    double x = point2D.X;   // x = 1
    double y = point2D.Y;   // y = 2
    double z = point2D.Z;   // z = 0 default value
    double m = point2D.M;   // m is NaN default value
    double id = point2D.ID; // id = 0 default value
    
    // Or use a builderEx which doesn't need to run on the MCT. 
    MapPointBuilderEx builderEx = new MapPointBuilderEx(1, 2);
    
    // do something with the builder
    builderEx.Y = 3;
    point2D = builderEx.ToGeometry();
    sr = point2D.SpatialReference; // sr = null
    hasZ = point2D.HasZ;   // hasZ = false
    hasM = point2D.HasM;   // hasM = false
    hasID = point2D.HasID; // hasID = false
    x = point2D.X;   // x = 1
    y = point2D.Y;   // y = 3
    z = point2D.Z;   // z = 0 default value
    m = point2D.M;   // m is NaN default value
    id = point2D.ID; // id = 0 default value
    
    // Create a 2D point with a spatial reference
    SpatialReference spatialReference = SpatialReferenceBuilder.CreateSpatialReference(4269);
    point2D = MapPointBuilderEx.CreateMapPoint(1, 2, spatialReference);
    sr = point2D.SpatialReference; // sr != null
    int wkid = sr.Wkid; // wkid = 4269
    
    // Or use a builder
    builderEx = new MapPointBuilderEx(1, 2, spatialReference);
    
    // Do something with the builder
    builderEx.SetValues(3, 5);
    point2D = builderEx.ToGeometry();
    sr = point2D.SpatialReference; // sr != null
    wkid = sr.Wkid; // wkid = 4269
    x = point2D.X; // x = 3
    y = point2D.Y; // y = 5
    
    // Change the spatial reference of the builder
    builderEx.SpatialReference = SpatialReferences.WGS84;
    point2D = builderEx.ToGeometry();
    sr = point2D.SpatialReference; // sr != null
    wkid = sr.Wkid; // wkid = 4326
    x = point2D.X; // x = 3
    y = point2D.Y; // y = 5
    
    // Create a 3D point with M
    MapPoint pointZM = MapPointBuilderEx.CreateMapPoint(1, 2, 3, 4);
    sr = pointZM.SpatialReference; // sr = null
    hasZ = pointZM.HasZ;   // hasZ = true
    hasM = pointZM.HasM;   // hasM = true
    hasID = pointZM.HasID; // hasID = false
    x = pointZM.X;   // x = 1
    y = pointZM.Y;   // y = 2
    z = pointZM.Z;   // z = 3
    m = pointZM.M;   // m = 4
    id = pointZM.ID; // id = 0 default value
    
    // Create a 3D point with M and a spatial reference
    pointZM = MapPointBuilderEx.CreateMapPoint(1, 2, 3, 4, spatialReference);
    sr = pointZM.SpatialReference; // sr != null
    wkid = sr.Wkid; // wkid = 4269
    
    // Create a point from another point in three ways
    MapPoint clone = pointZM.Clone() as MapPoint; // Has the same values including the spatial reference as pointZM
    MapPoint anotherZM = MapPointBuilderEx.CreateMapPoint(pointZM);  // Has the same values including the spatial reference as pointZM
            
    builderEx = new MapPointBuilderEx(pointZM); // Has the same values including the spatial reference as pointZM
    // Do something with the builder
    builderEx.HasM = false;
    builderEx.ID = 7; // Setting the id also sets HasID = true
    MapPoint pointZId = builderEx.ToGeometry();
    sr = pointZId.SpatialReference; // sr != null
    wkid = sr.Wkid; // wkid = 4269
    hasZ = pointZId.HasZ;   // hasZ = true
    hasM = pointZId.HasM;   // hasM = false
    hasID = pointZId.HasID; // hasID = true
    x = pointZId.X;   // x = 1
    y = pointZId.Y;   // y = 2
    z = pointZId.Z;   // z = 3
    m = pointZId.M;   // m is NaN, default value
    id = pointZId.ID; // id = 7
    
    // Create a point with Z, M, and ID-values
    MapPoint pointZMId = MapPointBuilderEx.CreateMapPoint(1, 2, 3, 4, 5, spatialReference);
    sr = pointZMId.SpatialReference; // sr != null
    wkid = sr.Wkid; // wkid = 4269
    hasZ = pointZMId.HasZ;   // hasZ = true
    hasM = pointZMId.HasM;   // hasM = true
    hasID = pointZMId.HasID; // hasID = true
    x = pointZMId.X;   // x = 1
    y = pointZMId.Y;   // y = 2
    z = pointZMId.Z;   // z = 3
    m = pointZMId.M;   // m = 4
    id = pointZMId.ID; // id = 5
    
    // Pick and choose which attributes to include
    MapPoint point = MapPointBuilderEx.CreateMapPoint(1, 2, false, 3, true, 4, true, 5);
    sr = point.SpatialReference; // sr = null
    hasZ = point.HasZ;   // hasZ = false
    hasM = point.HasM;   // hasM = true
    hasID = point.HasID; // hasID = true
    x = point.X;   // x = 1
    y = point.Y;   // y = 2
    z = point.Z;   // z = 0, default value
    m = point.M;   // m = 4
    id = point.ID; // id = 5
    
    // Or use a builder
    builderEx = new(1, 2, true, 3, false, 4, true, 5);
    // Do something with the builder
    builderEx.ID = 7;
    builderEx.SpatialReference = SpatialReferences.WGS84;
    point = builderEx.ToGeometry();
    sr = point.SpatialReference; // sr != null
    wkid = sr.Wkid; // wkid = 4326
    hasZ = point.HasZ;   // hasZ = true
    hasM = point.HasM;   // hasM = false
    hasID = point.HasID; // hasID = true
    x = point.X;   // x = 1
    y = point.Y;   // y = 2
    z = point.Z;   // z = 0, default value
    m = point.M;   // m is NaN, default value
    id = point.ID; // id = 7
    
    Get the individual parts of a multipart feature
    /// <summary>
    /// This method takes an input multi part geometry and breaks the parts into individual standalone geometries.
    /// </summary>
    /// <param name="inputGeometry">The geometry to be exploded into the individual parts.</param>
    /// <returns>An enumeration of individual parts as standalone geometries. The type of geometry is maintained, i.e.
    /// if the input geometry is of type Polyline then each geometry in the return is of type Polyline as well.
    /// If the input geometry is of type Unknown then an empty list is returned.</returns>
    public IEnumerable<Geometry> MultipartToSinglePart(Geometry inputGeometry)
    {
      // list holding the part(s) of the input geometry
      List<Geometry> singleParts = new List<Geometry>();
    
      // check if the input is a null pointer or if the geometry is empty
      if (inputGeometry == null || inputGeometry.IsEmpty)
        return singleParts;
    
      // based on the type of geometry, take the parts/points and add them individually into a list
      switch (inputGeometry.GeometryType)
      {
        case GeometryType.Envelope:
          singleParts.Add(inputGeometry.Clone() as Envelope);
          break;
        case GeometryType.Multipatch:
          singleParts.Add(inputGeometry.Clone() as Multipatch);
          break;
        case GeometryType.Multipoint:
          var multiPoint = inputGeometry as Multipoint;
    
          foreach (var point in multiPoint.Points)
          {
            // add each point of collection as a standalone point into the list
            singleParts.Add(point);
          }
          break;
        case GeometryType.Point:
          singleParts.Add(inputGeometry.Clone() as MapPoint);
          break;
        case GeometryType.Polygon:
          var polygon = inputGeometry as Polygon;
    
          foreach (var polygonPart in polygon.Parts)
          {
            // use the PolygonBuilderEx turning the segments into a standalone 
            // polygon instance
            singleParts.Add(PolygonBuilderEx.CreatePolygon(polygonPart));
          }
          break;
        case GeometryType.Polyline:
          var polyline = inputGeometry as Polyline;
    
          foreach (var polylinePart in polyline.Parts)
          {
            // use the PolylineBuilderEx turning the segments into a standalone
            // polyline instance
            singleParts.Add(PolylineBuilderEx.CreatePolyline(polylinePart));
          }
          break;
        case GeometryType.Unknown:
          break;
        default:
          break;
      }
    
      return singleParts;
    }
    
    Requirements

    Target Platforms: Windows 11, Windows 10

    ArcGIS Pro version: 3 or higher.
    See Also