[pgrouting-users] Question about driving directions

Stephen Woodbridge woodbri at swoodbridge.com
Wed Oct 19 14:06:06 EDT 2011


On 10/19/2011 11:50 AM, John Williams wrote:
> I'm trying to implement text-based driving directions in PHP and I've
> run into a little issue. I've read online that you have to check each
> segment as you loop through them and make sure the endpoints are lined
> up with the direction that you're traveling, but I'm still confused on
> how to do this. Right now I'm pulling source and target from the ways
> table. Then, if the current segment's target does not equal the next
> segment's source I flip the lat/lon values. Can anyone give me a
> simplified explanation of how to go about doing this? Thanks...

The key to this is to get the first segment correct. So in pseudocode:

## check first segment
if (seg[0][source] == seg[1][source] or
     seg[0][source] == seg[1][target]) ) then
   seg[0] = flipsegment(seg[0]);
end if

## check the rest of the segments
for (i=1; i<count(seg); i++) {
   if (seg[i-1][target] == seg[i][target]) then
     seg[i] = flipsegment(seg[i]);
   end if
end for

so flipsegment needs to swap source and target AND needs to reverse the 
order of the points in the linestring.

-Steve


More information about the Pgrouting-users mailing list