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

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

    Return Value

    Example
    Construct GeometryBag
    MapPoint point = MapPointBuilderEx.CreateMapPoint(1, 2, SpatialReferences.WebMercator);
    
    List<Coordinate2D> coords2D = new List<Coordinate2D>()
    {
      new Coordinate2D(0, 0),
      new Coordinate2D(0, 1),
      new Coordinate2D(1, 1),
      new Coordinate2D(1, 0)
    };
    
    Multipoint multipoint = MultipointBuilderEx.CreateMultipoint(coords2D, SpatialReferences.WGS84);
    Polyline polyline = PolylineBuilderEx.CreatePolyline(coords2D, SpatialReferences.WebMercator);
    
    GeometryBagBuilderEx builder = new GeometryBagBuilderEx(SpatialReferences.WGS84);
    
    GeometryBag emptyBag = builder.ToGeometry();
    // emptyBag.IsEmpty = true
    
    builder.AddGeometry(point);
    // builder.GeometryCount = 1
    
    GeometryBag geometryBag = builder.ToGeometry();
    // geometryBag.PartCount = 1
    // geometryBag.PointCount = 1
    // geometryBag.IsEmpty = false
    
    IReadOnlyList<Geometry> geometries = geometryBag.Geometries;
    // geometries.Count = 1
    // geometries[0] is MapPoint with a sr of WGS84
    
    bool isEqual = geometryBag.IsEqual(emptyBag);   // isEqual = false
    
    builder.InsertGeometry(0, multipoint);
    geometryBag = builder.ToGeometry();
    // geometryBag.PartCount = 2
    
    geometries = geometryBag.Geometries;
    // geometries.Count = 2
    // geometries[0] is Multipoint
    // geometries[1] is MapPoint
    
    builder.AddGeometry(polyline);
    builder.RemoveGeometry(1);
    geometryBag = builder.ToGeometry();
    // geometryBag.PartCount = 2
    
    geometries = geometryBag.Geometries;
    // geometries.Count = 2
    // geometries[0] is Multipoint
    // geometries[1] is Polyline          
    
    Construct GeometryBag - from an enumeration of geometries
    // Use a builder convenience method or use a builder constructor.
    
    MapPoint point = MapPointBuilderEx.CreateMapPoint(10, 20);
    List<Coordinate2D> coords = new List<Coordinate2D>() { new Coordinate2D(50, 60), new Coordinate2D(-120, -70), new Coordinate2D(40, 60) };
    Multipoint multipoint = MultipointBuilderEx.CreateMultipoint(coords, SpatialReferences.WebMercator);
    Polyline polyline = PolylineBuilderEx.CreatePolyline(coords);
    
    string json = "{\"rings\":[[[0,0],[0,1],[1,1],[1,0],[0,0]],[[3,0],[3,1],[4,1],[4,0],[3,0]]]}";
    Polygon polygon = PolygonBuilderEx.FromJson(json);
    
    var geometries = new List<Geometry>() { point, multipoint, polyline, polygon };
    
    // Builder convenience methods don't need to run on the MCT.
    //At 2.x - GeometryBag bag = GeometryBagBuilder.CreateGeometryBag(geometries, SpatialReferences.WGS84);
    var bag = GeometryBagBuilderEx.CreateGeometryBag(geometries, SpatialReferences.WGS84);
    
    //At 2.x - using (var builder = new GeometryBagBuilder(geometries, SpatialReferences.WGS84)) 
    var builder = new GeometryBagBuilderEx(geometries, SpatialReferences.WGS84);
    // do something with the builder
    bag = builder.ToGeometry();
    
    Construct GeometryBag - adding or inserting an enumeration of geometries
    MapPoint point = MapPointBuilderEx.CreateMapPoint(10, 20);
    List<Coordinate2D> coords = new List<Coordinate2D>() { new Coordinate2D(50, 60), new Coordinate2D(-120, -70), new Coordinate2D(40, 60) };
    Multipoint multipoint = MultipointBuilderEx.CreateMultipoint(coords, SpatialReferences.WebMercator);
    Polyline polyline = PolylineBuilderEx.CreatePolyline(coords);
    
    string json = "{\"rings\":[[[0,0],[0,1],[1,1],[1,0],[0,0]],[[3,0],[3,1],[4,1],[4,0],[3,0]]]}";
    Polygon polygon = PolygonBuilderEx.FromJson(json);
    
    var geometries = new List<Geometry>() { point, multipoint, polyline, polygon };
    
    //At 2.x - using (var builder = new GeometryBagBuilder(SpatialReferences.WGS84))
    var builder = new GeometryBagBuilderEx(SpatialReferences.WGS84);
    builder.AddGeometries(geometries);
    
    GeometryBag geomBag = builder.ToGeometry();
    // geomBag.PartCount == 4    (point, multipoint, polyline, polygon)
    
    geometries = new List<Geometry>() { point, polyline };
    builder.InsertGeometries(1, geometries);
    // builder.GeometryCount == 6
    geomBag = builder.ToGeometry();
    // geomBag.PartCount == 6    (point, point, polyline, multipoint, polyline, polygon)
    
    Requirements

    Target Platforms: Windows 11, Windows 10

    ArcGIS Pro version: 3 or higher.
    See Also