This project is read-only.

How to determine endpoints of multi-part polyline

Nov 7, 2013 at 9:34 PM
I am trying to find the endpoints of a multi-part polyline using get_Part and get_EndOfPart while iterating through the parts in the shape via Shape.NumParts. However, the sequence of parts returned is not the same as the sequence in which the parts physically appear in the polyline. As a result I cannot tell which end(s) of which part(s) are actually the ends of the polyline.

For example, in the shape below, how can I determine that 16 and 30 are the two ends of the polyline?

Part # get_Part get_EndOfPart
0 0 15
1 16 23 //16 is one end
2 24 28
3 29 30 //30 is the other
4 31 40

Many thanks for your kind assistance!

Nov 14, 2013 at 11:52 PM
I haven't had any responses so thought I'd give a simpler explanation. I've got a multipart polyline shape composed of contiguous segments. I'm trying to figure out how I can determine what the endpoints of the polyline are, since iterating over the points via get_Point(x) does not return the points in the order in which they appear on the map. As a result, get_Point(0) and get_Point(Numpoints-1) do not return the endpoints. I am quite stuck here so any input will be greatly appreciated.

Thank you,

Nov 15, 2013 at 10:24 AM
Sorry Don,

I don't know the answer. I will ask around.

Nov 15, 2013 at 11:45 AM
Paul, thank you very much for taking a look at this. The project I've been working on for many months depends on finding a solution.

I do have one idea that may simplify things. What created the multipart polyline in the first place was using the Dissolve function to aggregate several single part polyline shapes, which has the unfortunate side effect of turning these multiple single part polylines into a single multipart polyline. I could perhaps write my own Dissolve code (unfortunately would have to be in C#, not C++) that would create only a single part. This might make the problem of finding endpoints easier to solve.

The ideal solution would be an "Adjacent" function that would perform similar to Touches, except that it would return true if two points were adjacent to each other in a polyline. Then we could just look for point(s) that are adjacent to no more than one other point in the polyline. But unfortunately I'm not aware of any existing MW functionality that could be used to accomplish this.

Thanks again, I know there must be a solution for this and hopefully we will be able to find it. Have a great weekend!

Nov 15, 2013 at 12:25 PM
This maybe a topological problem which is not so easy to solve , Maurits

Sent from my iPhone

Nov 15, 2013 at 12:37 PM
Uh-oh!...but thanks for considering the problem, Maurits. --Don
Dec 10, 2013 at 10:13 PM
Hi Don, if I understood it correctly, the problem is specific to your data. There is hardly any ready to use solution, as parts of multipart polyline aren't necessarily adjacent. Therefore there is no such thing as specific 2 ends of multipart polyline from the MW's point of view.
But your approach with Adjacent function should work. Just:
  • take the end point of particular segment;
  • build rectangle around it (which represent the tolerance; something like Rectangle(x - tol, y - tol, x+ tol, y + tol));
  • loop through the endpoints of all other parts (both get_Part and get_EndPart) and count the number of those which are within the rectangle;
  • if there is one neighbor - it's in the middle, if there is none - it's the end point;
    The trick is to correctly set the tolerance. Then, given that the parts are indeed adjacent, it should work.
Dec 12, 2013 at 2:12 PM
Hi Sleschinski, Thanks much for your response! You are absolutely correct that this issue is a function of the data I'm working with, certainly not anything the software should/could be able to do for me. Interestingly, I stumbled into a way to accomplish this that pretty much mirrors the algorithm you proposed, and I'm off and running again. In case anyone else encounters the same challenge--again specific to my data but maybe of interest to someone down the road....

--find a polyline with an endpoint that is not coincident (within tolerance) with an endpoint of any other linear shape.
--recursively walk through the other shapes looking for a shape with an endpoint that is coincident with the endpoint previously found
--when done matching up endpoints for all contiguous shapes, "walk" them in order, writing point shapes to a new linear shape that will essentially become a dissolved version of the original shapes.

BTW, I see from your profile that you are one of the developers, and I'd like to say thank you and congratulations on a terrific product. Powerful, yet easy to learn and use. And great documentation, too!

Dec 12, 2013 at 5:12 PM
I'm glad that it worked out for you and you enjoy the project. Thanks.
Mar 11, 2015 at 2:13 PM
Edited Mar 11, 2015 at 2:17 PM

I got a similar problem and did a function like you did, it seemed to work when I discovered that some of my shapes, which are "cables" have parts which are sometimes exactly coincindent, representing a cable going one way and then coming back on the same path ( something like part 1, 2, 3 4... 2 and 4 being one on top of the other). I was wondering if shape.get_partisclokwise could help wihtout understanding exactly what "clockwise" means for a shape which could have an "S" shape


Just writing this, I realized that choosing one segment or the other is not a problem as long as you don't take the same twice...