A class representing a polygon.
Public NotInheritable Class Polygon
Inherits Multipart
Get the outermost rings of a polygon
/// <summary>
/// The methods retrieves the outer ring(s) of the input polygon.
/// </summary>
/// <param name="inputPolygon">Input Polygon.</param>
/// <returns>The outer most (exterior, clockwise) ring(s) of the polygon. If the input is null or empty, a null pointer is returned.</returns>
public Polygon GetOutermostRings(Polygon inputPolygon)
{
if (inputPolygon == null || inputPolygon.IsEmpty)
return null;
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(inputPolygon);
// 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);
}
// return the final geometry of the outer rings
return outerRings.ToGeometry();
}
Retrieve Geometry from Geodatabase
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
try
{
// open a gdb
using (ArcGIS.Core.Data.Geodatabase gdb =
new ArcGIS.Core.Data.Geodatabase(
new FileGeodatabaseConnectionPath(new Uri(@"c:\Temp\MyDatabase.gdb"))))
{
//Open a featureClass
using (ArcGIS.Core.Data.FeatureClass featureClass =
gdb.OpenDataset<ArcGIS.Core.Data.FeatureClass>("Polygon"))
{
ArcGIS.Core.Data.QueryFilter filter =
new ArcGIS.Core.Data.QueryFilter()
{
WhereClause = "OBJECTID = 6"
};
// get the row
using (ArcGIS.Core.Data.RowCursor rowCursor =
featureClass.Search(filter, false))
{
while (rowCursor.MoveNext())
{
using (var row = rowCursor.Current)
{
long oid = row.GetObjectID();
// get the shape from the row
ArcGIS.Core.Data.Feature feature = row as ArcGIS.Core.Data.Feature;
Polygon polygon = feature.GetShape() as Polygon;
// do something here
}
}
}
}
}
}
catch (Exception ex)
{
// error - handle appropriately
}
});
Target Platforms: Windows 11, Windows 10
ArcGIS Pro version: 3 or higher.