StartPoint of Polyline

185
2
Jump to solution
11-20-2017 07:08 AM
Highlighted
Regular Contributor

Hi,

I'm trying to get the StartPoint of a selected Polyline.  So far, this is what I have:

I use an Inspector to get each selected feature, like this:

var selection = currentLayer.GetSelection();
IReadOnlyList<long> selectedOIDs = selection.GetObjectIDs(); //save the selected OBJECTIDs to a list.

var insp = new ArcGIS.Desktop.Editing.Attributes.Inspector(); //create the Inspector

Then I loop through each selected OBJECTID:


foreach (var oid in selectedOIDs)
{
insp.Load(currentLayer, oid); //use the current OBJECTID as you go through the loop of selected features

Polyline theLine = insp.Shape as Polyline;  //This works

Segment theSegment = insp.Shape as Segment;  //This will not cast....not quite sure what to do here

MapPoint startPoint = theSegment.StartPoint;  //This is ultimately what I want to get at
}

Since a Segment has a .StartPoint property, I think I need to cast my Polyline into a Segment, I'm just not sure how to do it.  Of course, I could be totally wrong.

Any advice on how to do this would be appreciated.

Reply
0 Kudos
1 Solution

Accepted Solutions
Highlighted
Esri Regular Contributor

Brian,

A polyline is technically a multipart geometry so you cant directly cast to segment, you need to get part(n) of the polyline first. Take a look at the Multipart geometry class.

The other way to get the start point is through the first item in a point collection:

        //get the sketch as a point collection
        var pointCol = ((Multipart)sketchGeom).Points;
        var firstPoint = pointCol[0];

View solution in original post

2 Replies
Highlighted
Esri Regular Contributor

Brian,

A polyline is technically a multipart geometry so you cant directly cast to segment, you need to get part(n) of the polyline first. Take a look at the Multipart geometry class.

The other way to get the start point is through the first item in a point collection:

        //get the sketch as a point collection
        var pointCol = ((Multipart)sketchGeom).Points;
        var firstPoint = pointCol[0];

View solution in original post

Highlighted
Regular Contributor

Hi Sean,

Yes, I eventually found a sample that broke my line down into segments, but I like your solution much better.  Two lines of code is always better than 10, especially when all I need is the first point.

This is what I had:

Polyline theLine = insp.Shape as Polyline;
ReadOnlyPartCollection polylineParts = theLine.Parts;
IEnumerator<ReadOnlySegmentCollection> segments = polylineParts.GetEnumerator();
segments.MoveNext();
ReadOnlySegmentCollection seg = segments.Current;

foreach (Segment s in seg)
{
intersectPoint = s.StartPoint;
break; //to get just the first point
}

Thanks for your help!!  This is what I ultimately went with:

var pointCollection = ((Multipart)insp.Shape).Points;
MapPoint thePoint = pointCollection[0];

Reply
0 Kudos