How to extract polyline vertices from IFeature ?

2534
4
Jump to solution
10-15-2014 01:59 PM
Highlighted
New Contributor III

I am trying to get to the vertices coordinates of a polylines feature layer in C#.

I can get each "IFeature" object in the feature layer (in a similar way as can be done for points feature layers Getting raster cell indexes from point coordinates), but I do not know how to get the polyline vertices from there.

Reply
0 Kudos
1 Solution

Accepted Solutions
Highlighted
New Contributor II

Hi Marcelo,

As Owen mentioned, the IPointCollection is an interface of the Shape.  Rather than casting your feature from IFeature to IPointCollection, you should get the shape from the feature using the Shape property, then cast that to IPointCollection.

var myPolyline = myFeature.Shape;

var myPointCollection = (IPointCollection)myPolyline;

As Owen also mentioned, the Shape is only guaranteed to be a type of IGeometry, so you don't know that the concrete type is definitely a PolylineClass, so you may need to use the GeometryType property of IGeometry to ensure you are dealing with polyline shapes.

Cheers,

Todd

View solution in original post

Reply
0 Kudos
4 Replies
Highlighted
Regular Contributor

For a polyline feature class each shape can be cast to IPointCollection.

You can then iterate through each point in the point collection using EnumVertices to access the vertices. Alternatively, you can access each point using the IPointCollection.Point property and the PointCount. Something like:

for (int i = 0; i < pointCollection.PointCount - 1 ; i++)

{

  // access the point from the pointCollection here

}

Note that the first Point in the PointCollection has index 0, and the last Point has index equal to PointCount - 1.

Also, the Feature.Shape property is an IGeometry. You can use IGeometry.GeometryType to ensure that the shape is a Polyline before accessing the vertices.

I don't have a full code example but have done similar things in VB.Net/ArcObjects previously.

Reply
0 Kudos
Highlighted
New Contributor III

Thank you Owen, but when I try to cast "IPointCollection" on "IFeature" I get an exception. I have tried to cast without success other objects on "IFeature", such as  "IPolylineArray" and "IPolyline". Please let me know if you find a way to get an "IPointCollection" from an "IFeature".

Reply
0 Kudos
Highlighted
New Contributor II

Hi Marcelo,

As Owen mentioned, the IPointCollection is an interface of the Shape.  Rather than casting your feature from IFeature to IPointCollection, you should get the shape from the feature using the Shape property, then cast that to IPointCollection.

var myPolyline = myFeature.Shape;

var myPointCollection = (IPointCollection)myPolyline;

As Owen also mentioned, the Shape is only guaranteed to be a type of IGeometry, so you don't know that the concrete type is definitely a PolylineClass, so you may need to use the GeometryType property of IGeometry to ensure you are dealing with polyline shapes.

Cheers,

Todd

View solution in original post

Reply
0 Kudos
Highlighted
New Contributor III

Thank you Owen and Todd for your help!

Reply
0 Kudos