# [pgrouting-users] directions problem

Stephen Woodbridge woodbri at swoodbridge.com
Wed Apr 15 17:58:50 EDT 2009

```Valeria,

You need to think about the big picture of what you are trying to do here.

after you generate the route you have a list of segments. Each segment
can have some number of points. You also need to figure out if the
segments need to be flipped. That is did the route traverse the segment
from start to end or end to start?

you can only do this by comparing two segments at a time. Something like
this in pseudo code:

open cursor to results

first = true
last = fetch cursor

while (this = fetch cursor) {
if (first) {
check which end of last is connected to this
flip last if needed
trim the segment to the start point if needed
first = false
}
if ( this needs to be flipped ) {
flip this
}
compute azimuth of end of last and beginning of this
turn = the change in direction
...
if (last.name = this.name) {
join this to last to compress out an extraneous maneuver
}
}

this is the final segment in the route
trim it to the end point if needed.

close the cursor

You need to check for some degenerate cases like the whole route in on a
single segment, etc.

break it down into smaller tasks:

routing gives you list of segments

maneuvers transforms the segments into what happens between the segments
and might compress out extraneous steps

driving_directions transforms maneuvers into text

So you should go make a bunch of routes on google and look at the text,
it has a pattern. Each of the unique patterns is a maneuver. You want
look at your segments and transform then into maneuvers which you can
define any way that is convenient. Once you have a set of maneuvers, you
want to transform that into the text. So you identify the pattern
suggested by the maneuver, get a template string for the maneuver, and
substitute in the real values, like street names, turns, compass
directionals, etc.

-Steve

Valeria Muñoz wrote:
> some result give me 2,3 or 4  points. how can i define (1,2)(3,4) when i
> got 2 points?
>
> segment 4390:
> "MULTILINESTRING((-71.1958400125164 -30.591211719682,-71.1959567794677
> -30.5912308536826,-71.1968582353939 -30.5913171684662))"
>
> segment 7320:
> "MULTILINESTRING((-71.2063821613355 -30.5998726725634,-71.2070054367284
> -30.5991545647783))"
>
> i try with this query, but does not work:
>
> SELECT
> ST_Azimuth(ST_MakePoint(st_x(st_startpoint(the_geom)),st_x(st_pointn(the_geom,
> 2))), ST_MakePoint(st_x(st_pointn(the_geom,
> st_numpoints(the_geom)-1)),st_x(st_endpoint(the_geom))))/(2*pi())*360 as
> degAz,ST_Azimuth(ST_MakePoint(st_x(st_pointn(the_geom,
> st_numpoints(the_geom)-1)),st_x(st_endpoint(the_geom))),
> ST_MakePoint(st_x(st_startpoint(the_geom)),st_x(st_pointn(the_geom,
> 2))))/(2*pi())*360 As degAzrev from calle where gid=".\$id_gid."
>
> results
>
>       degAz                       degAzrev
> 187.380514396367 :: 7.38051439636702
>  4.88000936193493 :: 184.880009361935
>  3.80862917910364 :: 183.808629179104
>  4.56236891364906 :: 184.562368913649
>  6.13664883828968 :: 186.13664883829
>  84.2113342354141 :: 264.211334235414
>  83.7951033588638 :: 263.795103358864
>  83.8097651650778 :: 263.80976516507

So for the first row, you were going

>
> RG
>
>
> 2009/2/14 Stephen Woodbridge <woodbri at swoodbridge.com
> <mailto:woodbri at swoodbridge.com>>
>
>     Valeria,
>
>              C
>              ^
>              |
>              |
>     A-------->B
>
>     You need to look at the azimuth of the A->B and compare it to the
>     azimuth of B->C, if the difference is around 0.0 then you are
>     continuing straight ahead, if the difference is around +-90.0 you
>     are turn right or left.
>
>     You also have to be careful with segments that have a lot of
>     curvature, like exit ramps, because if you take the azimuth of the
>     start and end points it might not be what you want, you might want
>     to compute the azimuth points 1 and 2 for the start end and points
>     numpoints-1, numpoints for the end of the segment.
>
>     -Steve
>
>
>
>
>     Valeria Muñoz wrote:
>
>         hi daniel
>            i did try with Shooting*  and WORKS!!! .. thanks thanks!!...
>         now my next problem :) i am using azimuth() for
>         instructions(turn left right etc..) but i do not how use it, can
>          RG
>          Query :
>          SELECT azimuth(startpoint(the_geom),endpoint(the_geom)) as
>         azimuth from calle where gid = ".\$id_gid."
>          I read in a foro about azimuth() and this says that the result
>         i have to multiply by 57.29577951. The result is the angle. Now
>         How i know if the user have to turn left or rigth ..etc?, exist
>         some algoritm? or function?
>          RG
>
>         2009/2/12 Daniel Kastl <kastl at orkney.co.jp
>         <mailto:kastl at orkney.co.jp> <mailto:kastl at orkney.co.jp
>         <mailto:kastl at orkney.co.jp>>>
>
>
>

```