CreatePolygon(IEnumerable<Segment>,SpatialReference) Method
Convenience method to create a new instance of the
Polygon class.
Parameters
- segments
- Segments to create the polyline. The spatial references of the segments are checked for compatibility.
- spatialReference
- (Optional) The SpatialReference. The default value is null. If spatialReference is null, then the spatial reference will be inherited from which requires traversing through the enumeration of segments. For improved performance or if you want to set these properties yourself, use CreatePolygon(IEnumerable<Segment>,AttributeFlags,SpatialReference).
Return Value
Get the segments of a Polygon
List<Segment> segmentList = new List<Segment>(30);
ICollection<Segment> collection = segmentList;
polygon.GetAllSegments(ref collection);
// segmentList.Count = 4
// segmentList.Capacity = 30
// use the segments to build another polygon
Polygon polygonFromSegments = PolygonBuilderEx.CreatePolygon(collection);
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;
}
Create_polygon_poly
//Create a polygon graphic with simple line and fill styles.
//Construct on the worker thread
await QueuedTask.Run(() =>
{
//Build geometry
List<Coordinate2D> plyCoords = new List<Coordinate2D>();
plyCoords.Add(new Coordinate2D(1, 7));
plyCoords.Add(new Coordinate2D(2, 7));
plyCoords.Add(new Coordinate2D(2, 6.7));
plyCoords.Add(new Coordinate2D(3, 6.7));
plyCoords.Add(new Coordinate2D(3, 6.1));
plyCoords.Add(new Coordinate2D(1, 6.1));
//At 2.x - Polygon poly = PolygonBuilder.CreatePolygon(plyCoords);
Polygon poly = PolygonBuilderEx.CreatePolygon(plyCoords);
//Set symbolology, create and add element to layout
CIMStroke outline = SymbolFactory.Instance.ConstructStroke(ColorFactory.Instance.BlueRGB, 2.0, SimpleLineStyle.DashDotDot);
CIMPolygonSymbol polySym = SymbolFactory.Instance.ConstructPolygonSymbol(ColorFactory.Instance.RedRGB, SimpleFillStyle.ForwardDiagonal, outline);
//At 2.x - GraphicElement polyElm = LayoutElementFactory.Instance.CreatePolygonGraphicElement(layout, poly, polySym);
// polyElm.SetName("New Polygon");
GraphicElement polyElm = ElementFactory.Instance.CreateGraphicElement(layout, poly, polySym, "New Polygon");
});
Create_lasso
//Create a graphic lasso element with simple line and fill styles.
//Construct on the worker thread
await QueuedTask.Run(() =>
{
//Build geometry
List<Coordinate2D> plyCoords = new List<Coordinate2D>();
plyCoords.Add(new Coordinate2D(1, 1));
plyCoords.Add(new Coordinate2D(1.25, 2));
plyCoords.Add(new Coordinate2D(1.5, 1.1));
plyCoords.Add(new Coordinate2D(1.75, 2));
plyCoords.Add(new Coordinate2D(2, 1.1));
plyCoords.Add(new Coordinate2D(2.25, 2));
plyCoords.Add(new Coordinate2D(2.5, 1.1));
plyCoords.Add(new Coordinate2D(2.75, 2));
plyCoords.Add(new Coordinate2D(3, 1));
//At 2.x - Polygon poly = PolygonBuilder.CreatePolygon(plyCoords);
Polygon poly = PolygonBuilderEx.CreatePolygon(plyCoords);
//Set symbolology, create and add element to layout
CIMStroke outline = SymbolFactory.Instance.ConstructStroke(ColorFactory.Instance.BlackRGB, 2.0, SimpleLineStyle.Solid);
CIMPolygonSymbol polySym = SymbolFactory.Instance.ConstructPolygonSymbol(ColorFactory.Instance.RedRGB, SimpleFillStyle.ForwardDiagonal, outline);
//At 2.x - GraphicElement polyElm = LayoutElementFactory.Instance.CreatePolygonGraphicElement(layout, poly, polySym);
// polyElm.SetName("New Lasso");
GraphicElement polyElm = ElementFactory.Instance.CreateGraphicElement(layout, poly, polySym, "New Lasso");
});
Create Lasso Polygon, Freehand Element
//Must be on QueuedTask.Run(() => { ...
List<Coordinate2D> plyCoords = new List<Coordinate2D>();
plyCoords.Add(new Coordinate2D(1, 1));
plyCoords.Add(new Coordinate2D(1.25, 2));
plyCoords.Add(new Coordinate2D(1.5, 1.1));
plyCoords.Add(new Coordinate2D(1.75, 2));
plyCoords.Add(new Coordinate2D(2, 1.1));
plyCoords.Add(new Coordinate2D(2.25, 2));
plyCoords.Add(new Coordinate2D(2.5, 1.1));
plyCoords.Add(new Coordinate2D(2.75, 2));
plyCoords.Add(new Coordinate2D(3, 1));
Polygon poly = PolygonBuilderEx.CreatePolygon(plyCoords);
//Set symbology, create and add element to layout
CIMStroke outline = SymbolFactory.Instance.ConstructStroke(
ColorFactory.Instance.BlackRGB, 2.0, SimpleLineStyle.Solid);
CIMPolygonSymbol polySym = SymbolFactory.Instance.ConstructPolygonSymbol(
ColorFactory.Instance.RedRGB, SimpleFillStyle.ForwardDiagonal, outline);
ElementFactory.Instance.CreateGraphicElement(
container, poly, polySym, "New Lasso");
Create Polygon Element
//Must be on QueuedTask.Run(() => { ...
//Build geometry
List<Coordinate2D> plyCoords = new List<Coordinate2D>();
plyCoords.Add(new Coordinate2D(1, 7));
plyCoords.Add(new Coordinate2D(2, 7));
plyCoords.Add(new Coordinate2D(2, 6.7));
plyCoords.Add(new Coordinate2D(3, 6.7));
plyCoords.Add(new Coordinate2D(3, 6.1));
plyCoords.Add(new Coordinate2D(1, 6.1));
Polygon poly = PolygonBuilderEx.CreatePolygon(plyCoords);
//Set symbology, create and add element to layout
CIMStroke outline = SymbolFactory.Instance.ConstructStroke(
ColorFactory.Instance.BlueRGB, 2.0, SimpleLineStyle.DashDotDot);
CIMPolygonSymbol polySym = SymbolFactory.Instance.ConstructPolygonSymbol(
ColorFactory.Instance.RedRGB, SimpleFillStyle.ForwardDiagonal, outline);
ElementFactory.Instance.CreateGraphicElement(
container, poly, polySym, "New Polygon", false);
Target Platforms: Windows 11, Windows 10
ArcGIS Pro version: 3 or higher.