[pgrouting-users] Directions Start and End Points

This may be obvious, but for whatever reason, I’m not able to figure it out. When calculating driving directions with pgrouting, to find the starting and ending points for the path finding algorithm, I’m using the sample “findNearestEdge” function from pgrouting’s website, which looks like this:

function findNearestEdge($lonlat) {

$con = pg_connect(“dbname=”.PG_DB." host=“.PG_HOST.” user=".PG_USER);

$sql = “SELECT gid, source, target, the_geom,
distance(the_geom, GeometryFromText(
'POINT(”.$lonlat[0]." “.$lonlat[1].”)', 4326)) AS dist
FROM “.TABLE.”
WHERE the_geom && setsrid(
‘BOX3D(“.($lonlat[0]-0.1).”
“.($lonlat[1]-0.1).”,
“.($lonlat[0]+0.1).”
“.($lonlat[1]+0.1).”)’::box3d, 4326)
ORDER BY dist LIMIT 1";

$query = pg_query($con,$sql);

$edge[‘gid’] = pg_fetch_result($query, 0, 0);
$edge[‘source’] = pg_fetch_result($query, 0, 1);
$edge[‘target’] = pg_fetch_result($query, 0, 2);
$edge[‘the_geom’] = pg_fetch_result($query, 0, 3);

pg_close($con);

return $edge;
}

What I’d like to do is adjust the starting and ending points so they match up exactly with my lat/lon beginning/ending points. For example, the way it works now if my destination is mid-way down a particular street and I plot the route on a map, it may look like the route takes you to the end of the block, when it should have stopped earlier, or vice-versa. I hope that makes sense…

My lack of expertise with pgrouting keeps me from understanding some other posts I’ve seen about this topic. Could someone point me in the right direction… thanks a lot.

Hi,

When you run your routing query you unfortunately don’t know yet which direction it will take. So when you start from the nearest road link, it’s either “too much” or a small part is missing.
The workshop example is kept very simple. There are other PostGIS functions, that can help you to locate the nearest point on the road link and then return a substring:

So after you got your route you need to check which direction it went and which substring to add.

Daniel

On Tue, Nov 15, 2011 at 3:09 AM, John Williams <jdwilliams1982@gmail.com> wrote:

This may be obvious, but for whatever reason, I’m not able to figure it out. When calculating driving directions with pgrouting, to find the starting and ending points for the path finding algorithm, I’m using the sample “findNearestEdge” function from pgrouting’s website, which looks like this:

function findNearestEdge($lonlat) {

$con = pg_connect(“dbname=”.PG_DB." host=“.PG_HOST.” user=".PG_USER);

$sql = “SELECT gid, source, target, the_geom,
distance(the_geom, GeometryFromText(
'POINT(”.$lonlat[0]." “.$lonlat[1].”)', 4326)) AS dist
FROM “.TABLE.”
WHERE the_geom && setsrid(
‘BOX3D(“.($lonlat[0]-0.1).”
“.($lonlat[1]-0.1).”,
“.($lonlat[0]+0.1).”
“.($lonlat[1]+0.1).”)’::box3d, 4326)
ORDER BY dist LIMIT 1";

$query = pg_query($con,$sql);

$edge[‘gid’] = pg_fetch_result($query, 0, 0);
$edge[‘source’] = pg_fetch_result($query, 0, 1);
$edge[‘target’] = pg_fetch_result($query, 0, 2);
$edge[‘the_geom’] = pg_fetch_result($query, 0, 3);

pg_close($con);

return $edge;
}

What I’d like to do is adjust the starting and ending points so they match up exactly with my lat/lon beginning/ending points. For example, the way it works now if my destination is mid-way down a particular street and I plot the route on a map, it may look like the route takes you to the end of the block, when it should have stopped earlier, or vice-versa. I hope that makes sense…

My lack of expertise with pgrouting keeps me from understanding some other posts I’ve seen about this topic. Could someone point me in the right direction… thanks a lot.


Pgrouting-users mailing list
Pgrouting-users@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/pgrouting-users


Georepublic UG & Georepublic Japan
eMail: daniel.kastl@georepublic.de
Web: http://georepublic.de

Great, thanks Daniel. So, if the route returned from postgis doesn’t overlap the start/end points (ie, ST_Line_Locate_Point returns 0 or 1), is there an elegant way to add another linestring / multilinestring to the beginning or end of the route? I considered manually constructing a LINESTRING with my point and the first or last point of the route, but that wouldn’t include things like the street name, source and target from the ways table.

On Mon, Nov 14, 2011 at 6:32 PM, Daniel Kastl <daniel@georepublic.de> wrote:

Hi,

When you run your routing query you unfortunately don’t know yet which direction it will take. So when you start from the nearest road link, it’s either “too much” or a small part is missing.
The workshop example is kept very simple. There are other PostGIS functions, that can help you to locate the nearest point on the road link and then return a substring:

So after you got your route you need to check which direction it went and which substring to add.

Daniel

On Tue, Nov 15, 2011 at 3:09 AM, John Williams <jdwilliams1982@gmail.com> wrote:

This may be obvious, but for whatever reason, I’m not able to figure it out. When calculating driving directions with pgrouting, to find the starting and ending points for the path finding algorithm, I’m using the sample “findNearestEdge” function from pgrouting’s website, which looks like this:

function findNearestEdge($lonlat) {

$con = pg_connect(“dbname=”.PG_DB." host=“.PG_HOST.” user=".PG_USER);

$sql = “SELECT gid, source, target, the_geom,
distance(the_geom, GeometryFromText(
'POINT(”.$lonlat[0]." “.$lonlat[1].”)', 4326)) AS dist
FROM “.TABLE.”
WHERE the_geom && setsrid(
‘BOX3D(“.($lonlat[0]-0.1).”
“.($lonlat[1]-0.1).”,
“.($lonlat[0]+0.1).”
“.($lonlat[1]+0.1).”)’::box3d, 4326)
ORDER BY dist LIMIT 1";

$query = pg_query($con,$sql);

$edge[‘gid’] = pg_fetch_result($query, 0, 0);
$edge[‘source’] = pg_fetch_result($query, 0, 1);
$edge[‘target’] = pg_fetch_result($query, 0, 2);
$edge[‘the_geom’] = pg_fetch_result($query, 0, 3);

pg_close($con);

return $edge;
}

What I’d like to do is adjust the starting and ending points so they match up exactly with my lat/lon beginning/ending points. For example, the way it works now if my destination is mid-way down a particular street and I plot the route on a map, it may look like the route takes you to the end of the block, when it should have stopped earlier, or vice-versa. I hope that makes sense…

My lack of expertise with pgrouting keeps me from understanding some other posts I’ve seen about this topic. Could someone point me in the right direction… thanks a lot.


Pgrouting-users mailing list
Pgrouting-users@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/pgrouting-users


Georepublic UG & Georepublic Japan
eMail: daniel.kastl@georepublic.de
Web: http://georepublic.de


Pgrouting-users mailing list
Pgrouting-users@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/pgrouting-users