private Task<bool> ConstructSamplePolygon(FeatureLayer polygonLayer, FeatureLayer lineLayer)
{
// execute the fine grained API calls on the CIM main thread
return QueuedTask.Run(() =>
{
// get the underlying feature class for each layer
var polygonFeatureClass = polygonLayer.GetTable() as FeatureClass;
var polygonDefinition = polygonFeatureClass.GetDefinition() as FeatureClassDefinition;
var lineFeatureClass = lineLayer.GetTable() as FeatureClass;
// construct a cursor to retrieve the line features
var lineCursor = lineFeatureClass.Search(null, false);
// set up the edit operation for the feature creation
var createOperation = new EditOperation()
{
Name = "Create polygons",
SelectNewFeatures = false
};
PolylineBuilder polylineBuilder = new PolylineBuilder(polygonDefinition.GetSpatialReference());
while (lineCursor.MoveNext())
{
// retrieve the first feature
using (var lineFeature = lineCursor.Current as Feature)
{
// add the coordinate collection of the current geometry into our overall list of collections
var polylineGeometry = lineFeature.GetShape() as Polyline;
polylineBuilder.AddParts(polylineGeometry.Parts);
}
}
// use the ConvexHull method from the GeometryEngine to construct the polygon geometry
var newPolygon = GeometryEngine.Instance.ConvexHull(polylineBuilder.ToGeometry()) as Polygon;
// queue the polygon creation
createOperation.Create(polygonLayer, newPolygon);
// execute the edit (polygon create) operation
return createOperation.ExecuteAsync();
});
}
protected async override void OnClick()
{
// to work in the context of the active display retrieve the current map
Map activeMap = MapView.Active.Map;
// retrieve the first line layer in the map
var lineFeatureLayer = activeMap.GetLayersAsFlattenedList().OfType().Where(
lyr => lyr.ShapeType == ArcGIS.Core.CIM.esriGeometryType.esriGeometryPolyline).FirstOrDefault();
if (lineFeatureLayer == null)
return;
// retrieve the first polygon feature layer in the map
var polygonFeatureLayer = activeMap.GetLayersAsFlattenedList().OfType().Where(
lyr => lyr.ShapeType == ArcGIS.Core.CIM.esriGeometryType.esriGeometryPolygon).FirstOrDefault();
if (polygonFeatureLayer == null)
return;
//construct the polyline based of the convex hull of all polylines
await ConstructSamplePolygon(polygonFeatureLayer, lineFeatureLayer);
}