c# Route layer to points

634
6
07-28-2011 09:23 AM
BaluRamki
New Contributor
Hey Fellows,
I have added stops by using code and I got the route and saved it to layer. Now I want to get points in the order of start to end from that layer...
I checked the generated route layer in arcmap looking for attribute table and it shows one row and shape field as polylineM ....
Please Guide me ....

Route from a-b . point at a , point next to a towards b along the generated route ++++ till , point at b....

I'm using 9.3
Tags (2)
0 Kudos
6 Replies
PatrickStevens
Esri Contributor
Each output route is determined internally by traversing edges, junctions, and turns from the starting point to the ending point.  The generated output is one polyline that represents the shape of the entire completed path.

You have a couple of options, depending on the reasons you have for wanting intermediate points.  If, for example, you just need all of the vertices of the route, in order to feed the points into another application, then see the following post:

Convert Lines to Points

There you get guidance on programmatically iterating over the vertices of a polyline.


If, however, you are interested in which network elements were traversed and the cost of travel during the routes at the point those elements were traversed, then you can generate the "traversal result" for the route.  We have an add-in that will automatically generate the traversal result and add it to the table of contents here:

Network analyst Traversal Result Add-In

The code behind that add-in is in the 10.0 SDK and can be downloaded here:

Zip download of traversal result add-in code

Or viewed online here:

C# code for traversal result add-in


I hope one of these options gives you what you were looking for.
0 Kudos
BaluRamki
New Contributor
Thank You Patrick,
My use or requirement is to generate steering angle for the robot , the robot gets current position from gps and I need to get heading in terms next point and provide angle input to robot. I have the route saved and displayed as layer ,now should i use that layer file or the result nalayer that is being saved as the result layer(.lyr)
By the way I use 9.3 but the c# codes would be helpful

B
0 Kudos
PatrickStevens
Esri Contributor
Your use case appears more suited for iterating over the vertices of the output route geometry. 

This is not my area of expertise, but I did find the IConstructAngle.ConstructThreePoint method.  You would input from, through, and to points, and get the angle value created by the three points.  As you iterate the vertices of the route geometry, you can calculate what the next angle is for input to your robot.

You also stated, "I have the route saved and displayed as layer ,now should i use that layer file or the result nalayer that is being saved as the result layer(.lyr)"

If you do elect to retrieve your route feature using the Routes class in the NALayer, the way I usually get to the associated feature classes is by first retrieving the NAContext from the NALayer via INALayer.Context.  Then use INAContext.NAClasses to get your Routes class, like this:

  INAContext naContext = naLayer.Context;   
  INAClass naClass = naContext.NAClasses.get_ItemByName("Routes") as INAClass;

Then you can cast your NAClass as a feature class and iterate over it with IFeatureClass.Search.
0 Kudos
BaluRamki
New Contributor
Your use case appears more suited for iterating over the vertices of the output route geometry. 

This is not my area of expertise, but I did find the IConstructAngle.ConstructThreePoint method.  You would input from, through, and to points, and get the angle value created by the three points.  As you iterate the vertices of the route geometry, you can calculate what the next angle is for input to your robot.

You also stated, "I have the route saved and displayed as layer ,now should i use that layer file or the result nalayer that is being saved as the result layer(.lyr)"

If you do elect to retrieve your route feature using the Routes class in the NALayer, the way I usually get to the associated feature classes is by first retrieving the NAContext from the NALayer via INALayer.Context.  Then use INAContext.NAClasses to get your Routes class, like this:

  INAContext naContext = naLayer.Context;   
  INAClass naClass = naContext.NAClasses.get_ItemByName("Routes") as INAClass;

Then you can cast your NAClass as a feature class and iterate over it with IFeatureClass.Search.


Thanks A Bunch Patrick,
Just one doubt should I use IFeatureClass.search for getting the points or should I use IEnumVertex2 for that , ...
And Thanks for pointing out to IConstructAngle.ConstructThreePoint, you just saved my life, only now getting the points is my concern, I was using this example ms-help://ESRI.EDNv9.3/esriGeometry/html/esriGeometry_IEnumVertex_Example.htm for the iteration got an error when i tried using nalayer (one getting saved as route.lyr) cast as ipoint collection instead of the CreateMultipartPolyline() in the example and the error was null exception @

//Get the vertex enumerator
  IEnumVertex2 enumVertex = pointCollection.EnumVertices as IEnumVertex2;
....
attached part of my codes
Any way waiting for your reply
Thanks A Billion again
B
0 Kudos
PatrickStevens
Esri Contributor
I didn't try running your code, but I think I see what your problem is.  In the method IEnumVertexExample, you take a NALayer (cast as ILayer) and try to cast it as a IPointCollection:

        public void IEnumVertexExample(ILayer nlyr)//(add nalayer(route) here)
        {
            IPointCollection pointCollection = nlyr as IPointCollection;

Here is a helpful hint in using the resource center to work with ArcObjects:  You can go to the link for an interface and see which CoClasses implement that interface.  If you go to IPointCollection, and scroll down to "CoClasses that implement IPointCollection", you won't see NALayer as one of those options.  You need one of the classes that is listed there.  In your case, you need a Polyline.

For the most part, a layer is just a reference to some data, for the purposes of displaying it in ArcMap or elsewhere.  Network Analyst Layers (NALayer), instead of referencing data in a geodatabase or shapefile, references some in-memory feature classes.  More specifically, the NALayer is a composite layer that holds sublayers that reference in-memory feature classes.  I know, it is a bit confusing.

For your scenario, you need to take your INALayer, get its Context, via INALayer.Context.  Then you use the Context to get to the subclass that interests you, "Routes".  That subclass holds your output route geometry.

Then you use a feature cursor to iterate over the rows of the class.  You should only have one row in your Routes class.  Get that row and get the polyline from it.  That polyline is your output route geometry.


INAContext naContext = naLayer.Context;
IFeatureClass routesClass = naContext.NAClasses.get_ItemByName("Routes") as IFeatureClass;
IFeatureCursor cursor = routesClass.Search(null, true);
IFeature feature = cursor.NextFeature();
IPolyline outputRoute = feature.Shape as IPolyline;

Then just pass your outputRoute to the method you made called "IEnumVertexExample", instead of a layer.  The outputRoute will not be null when cast as IPointCollection, because a Polyline supports that interface.

To tie up the resource center part of this forum post, if you go to the help page for IFeature.Shape, you see that the Shape is returned as an IGeometry.  If you follow the link to the IGeometry page, you see that one of the CoClasses that supports IGeometry is a Polyline.  And a Polyline is what you need to cast as an IPointCollection.  Bingo!  We came full circle.
0 Kudos
BaluRamki
New Contributor
I didn't try running your code, but I think I see what your problem is.  In the method IEnumVertexExample, you take a NALayer (cast as ILayer) and try to cast it as a IPointCollection:

        public void IEnumVertexExample(ILayer nlyr)//(add nalayer(route) here)
        {
            IPointCollection pointCollection = nlyr as IPointCollection;

Here is a helpful hint in using the resource center to work with ArcObjects:  You can go to the link for an interface and see which CoClasses implement that interface.  If you go to IPointCollection, and scroll down to "CoClasses that implement IPointCollection", you won't see NALayer as one of those options.  You need one of the classes that is listed there.  In your case, you need a Polyline.

For the most part, a layer is just a reference to some data, for the purposes of displaying it in ArcMap or elsewhere.  Network Analyst Layers (NALayer), instead of referencing data in a geodatabase or shapefile, references some in-memory feature classes.  More specifically, the NALayer is a composite layer that holds sublayers that reference in-memory feature classes.  I know, it is a bit confusing.

For your scenario, you need to take your INALayer, get its Context, via INALayer.Context.  Then you use the Context to get to the subclass that interests you, "Routes".  That subclass holds your output route geometry.

Then you use a feature cursor to iterate over the rows of the class.  You should only have one row in your Routes class.  Get that row and get the polyline from it.  That polyline is your output route geometry.


INAContext naContext = naLayer.Context;
IFeatureClass routesClass = naContext.NAClasses.get_ItemByName("Routes") as IFeatureClass;
IFeatureCursor cursor = routesClass.Search(null, true);
IFeature feature = cursor.NextFeature();
IPolyline outputRoute = feature.Shape as IPolyline;

Then just pass your outputRoute to the method you made called "IEnumVertexExample", instead of a layer.  The outputRoute will not be null when cast as IPointCollection, because a Polyline supports that interface.

To tie up the resource center part of this forum post, if you go to the help page for IFeature.Shape, you see that the Shape is returned as an IGeometry.  If you follow the link to the IGeometry page, you see that one of the CoClasses that supports IGeometry is a Polyline.  And a Polyline is what you need to cast as an IPointCollection.  Bingo!  We came full circle.

Hey Patrick,
I greatly appreciate your help ...
After posting the previous post( code.. error.. etc..) I was debugging line by line and found that I might need geometry as polyline(from actual example) now I have a better understanding ..
I should be able to finish it now

Thank You.
B
0 Kudos