# [postgis-users] Pgrouting directions

Stephen Woodbridge woodbri at swoodbridge.com
Mon Jan 5 06:45:32 PST 2009

```In the example I used below, the road segment would have had 4 points,
but regardless of the number of points, you would use:

pnt[1], pnt[2] for the calculations at the start of the segment.
pnt[numpoints-1], pnt[numpoints] for the calculations at the end of the
segment.

So in postgis you could use:

st_x(st_pointn(the_geom, 1))  or st_x(st_startpoint(the_geom))
st_y(st_pointn(the_geom, 1))  or st_y(st_startpoint(the_geom))

st_x(st_pointn(the_geom, 2))
st_y(st_pointn(the_geom, 2))

st_x(st_pointn(the_geom, st_numpoints(the_geom)-1))
st_y(st_pointn(the_geom, st_numpoints(the_geom)-1))

st_x(st_pointn(the_geom, st_numpoints(the_geom)))  or
st_x(st_endpoint(the_geom))
st_y(st_pointn(the_geom, st_numpoints(the_geom)))  or
st_y(st_endpoint(the_geom))

If you need to compute the bearing or Azimuth of the segment ends look
at this:
http://postgis.refractions.net/pipermail/postgis-users/2007-November/017768.html
http://postgis.refractions.net/documentation/manual-svn/ST_Azimuth.html

-Steve

> Thanx Steve, your information were helpful, but when I think of it it
> confuses me a lot, lets first start with my fields in DB.
>
> Columns are:
>   gid serial NOT NULL,
>   rod_name_a character varying(50),
>   label character varying(50),
>   rod_length integer,
>   rod_no integer,
>   rod_type character varying(1),
>   rod_class character varying(1),
>   rod_code character varying(10),
>   shape_leng numeric,
>   the_geom geometry,
>   source integer,
>   target integer,
>   length double precision,
>   x1 double precision,
>   y1 double precision,
>   x2 double precision,
>   y2 double precision,
>   to_cost double precision,
>   "rule" text,
>   cost_factor integer,
>   main_cost double precision,
>   reverse_cost double precision,
>   cost_reverse_factor integer DEFAULT 5,
>
> Now, I am confused, because I cannot calculate the heading in the first
> place, If I have
> B+---\
> x2,y2
>     |
> x1,y1
> A+---/
>
> Now I can calculate the heading simply by comparing x1 and y1 to x2 to
> y2 and which is larger (based on lonlat) so I can judge it, but some
> times I am having this:
> B+---\
> x1,y1
>     |
> x2,y2
> A+---/
>
> Now it is misleading, maybe thats why you told me not to use start and
> end point, but wait, what else to use ? According to my DB I cannot
> think of any other way to calculate headings and angles.
>
> Regards
>
> On Wed, Dec 31, 2008 at 7:45 AM, Stephen Woodbridge
> <woodbri at swoodbridge.com <mailto:woodbri at swoodbridge.com>> wrote:
>
>
>         Yes, that is something very similar to what I need to do, I
>         already wrote a php code to check angels and slopes, but it was
>         not accurate, it was more life "if else" code, I check the
>         longlat for current segment
>
>
>     When you check the angles do not use the end points because they are
>
>     B+---\
>         |
>     A+---/
>
>     If you have a segment from A to B like the above and you calculate
>     the heading or angles based on the vector AB then it will point
>     north and is should be pointing west at the end of the vector or
>     east at the start of the vector.
>
>     You also need to look at weather or not you need to flip segments
>     because you route might traverse them from start to end OR from end
>     to start.
>
>     When I compute angles I grid the angles into 8 pie pieces of 45
>     degrees each.
>
>     continue straight
>     turn slight right/left
>     turn right/left
>     turn sharp right/left
>     make a u-turn
>
>
>         with longlat for the previous one, I guess you didn't use the
>         database for direction calculation(except for routing) am I
>         right? Isn't steps 3 and 4 depends on your angel calculations?
>
>
>     I have done this in C in a prototype router I wrote years ago. I
>     have implemented it in PHP and in perl in various incarnations in
>     the past. The demo page has it written in pgpsql, but it is based on
>     my own heavily modified versions of the pgRouting stored procedures.
>
>     Hope the above helps with your problems. I find it easiest to work
>     out the math problems of computing the angles and headings first and
>     make sure they are correct. Then move on to the other issues. If
>     your math is wrong computing the headings and angles it will cloud
>     all the other results.
>
>     Best regards,
>      -Steve
>
>         I have the roads with two different languages, a road map like
>         anyother road map I guess.
>
>         On Tue, Dec 30, 2008 at 3:17 AM, Stephen Woodbridge
>         <woodbri at swoodbridge.com <mailto:woodbri at swoodbridge.com>
>         <mailto:woodbri at swoodbridge.com
>         <mailto:woodbri at swoodbridge.com>>> wrote:
>
>            bdair2002 wrote:
>
>                Hello,
>                I am developing an application using Pgrouting with Postgis
>                data, everything
>                is fine, but now I am looking  to do something like Google
>                directions guide,
>                where it tells you turn left or right, I am wondering if this
>                feature is a
>                built-in feature in Pgrouting or Postgis.
>
>                Regards
>
>
>            No this is not a feature that is part of either.
>
>            I have implemented a directions explicator, but it is somewhat
>            specific to the data that you have, as you need to know what
>            column(s) contain the road names, if you have signage information
>            then you need to know how to link to that. If you want to be
>         able to
>            explicate in multiple languages, then you probably need to
>         consider
>            supporting multiple languages each in a separate table the
>         you can
>            select from when you generate the languages.
>
>            The algorithm is pretty straight forward as the you currently
>         get a
>            list of segments that make up the route. Then you need to:
>
>            analyze each segment and
>            1) check to see if you want to join it with the previous segment
>            because the road name matches
>            2) check the angle that it makes with the last segment to
>         determine
>            right, left, straightness of turn
>            3) compute the compass heading for the segment
>            4) determine if you have signage
>            5) based on these, determine if you are read to explicate and
>         which
>            predefined instruction you need to explicate an instruction
>            6) loop through all segments
>
>            For a demo of this you can try:
>
>            Zoom into a city in one of the yellow areas, set a start and end
>            point and click [calculate route] button. The driving directions
>            should show below the button if it was successful, be
>         patience the
>            routing is running on a 333MHz box.
>
>            -Steve
>
>            _______________________________________________
>            postgis-users mailing list
>            postgis-users at postgis.refractions.net
>         <mailto:postgis-users at postgis.refractions.net>
>            <mailto:postgis-users at postgis.refractions.net
>         <mailto:postgis-users at postgis.refractions.net>>
>
>            http://postgis.refractions.net/mailman/listinfo/postgis-users
>
>
>
>         ------------------------------------------------------------------------
>
>
>         _______________________________________________
>         postgis-users mailing list
>         postgis-users at postgis.refractions.net
>         <mailto:postgis-users at postgis.refractions.net>
>         http://postgis.refractions.net/mailman/listinfo/postgis-users
>
>
>     _______________________________________________
>     postgis-users mailing list
>     postgis-users at postgis.refractions.net
>     <mailto:postgis-users at postgis.refractions.net>
>     http://postgis.refractions.net/mailman/listinfo/postgis-users
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> postgis-users mailing list
> postgis-users at postgis.refractions.net
> http://postgis.refractions.net/mailman/listinfo/postgis-users

```