How to connect certain points into a polylines?

493
0
10-01-2021 03:48 AM
DavidMrázek
Occasional Contributor II

Hello,

I have a question for you. How do I connect certain points to form a polylines?(point one with another, third with fourth, etc.)

The code I have thus forms a polyline from the first to the first point, from the first to the second, from the first to the third, etc.

private Task<bool> ConstructSamplePolylines()
{
var polylineLayer = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>()
.FirstOrDefault(f => f.ShapeType == esriGeometryType.esriGeometryPolyline);
var pointLayer = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>()
.FirstOrDefault(f => f.ShapeType == esriGeometryType.esriGeometryPoint);
// execute the fine grained API calls on the CIM main thread
return QueuedTask.Run(() =>
{
// get the underlying feature class for each layer
var polylineFeatureClass = polylineLayer.GetTable() as FeatureClass;
var pointFeatureClass = pointLayer.GetTable() as FeatureClass;

// retrieve the feature class schema information for the feature classes
var polylineDefinition = polylineFeatureClass.GetDefinition();


// construct a cursor for all point features, since we want all feature there is no
// QueryFilter required
var pointCursor = pointFeatureClass.Search(null, false);


// initialize a counter variable
int pointCounter = 0;
// initialize a list to hold 5 coordinates that are used as vertices for the polyline
var lineMapPoints = new List<MapPoint>();

// set up the edit operation for the feature creation
var createOperation = new EditOperation()
{
Name = "Tvorba Linií",
SelectNewFeatures = false
};

// loop through the point features
while (pointCursor.MoveNext())
{
pointCounter++;

var pointFeature = pointCursor.Current as Feature;
// add the feature point geometry as a coordinate into the vertex list of the line
// - ensure that the projection of the point geometry is converted to match the spatial reference of the line
lineMapPoints.Add(((MapPoint)GeometryEngine.Instance.Project(pointFeature.GetShape(), polylineDefinition.GetSpatialReference())));
var newPolyline = PolylineBuilder.CreatePolyline(lineMapPoints, polylineDefinition.GetSpatialReference());
// queue the create operation as part of the edit operation
createOperation.Create(polylineLayer, newPolyline);
}

// execute the edit (create) operation
return createOperation.ExecuteAsync();
});
}

0 Kudos
0 Replies