[pgrouting-users] Issues with pgrouting workshop code in postgis2.11/pgrouting 2.0

Emmanuel Adegboye eaadegboye at googlemail.com
Fri Jan 3 20:28:56 PST 2014


Regina.

It was from the foss4g 2007 workshop. The most recent worshops include
wrapper functions which I am not familiar with so I also get stuck at some
point.

What I am trying to do is to create a simple application that allows users
to set a start and end point and calculate the route using any one of the
major algorithms like the workshop describes.

So far, the user interface (based on openlayers 2.10) works well and
displays both points. I get an error in firebug when I click the 'calculate
route' button and the php script is called, and this was after I installed
legacy.sql. I get "ERROR: type "geoms" does not exist" when installing
pgrouting.sql.

Here's the error from firebug:

<br /><b>Warning</b>:  pg_query(): Query failed: ERROR:  parse error -
invalid geometryHINT:  "POINT( )" <-- parse error at
position 9 within geometry in
<b>C:\ms4w\Apache\htdocs\hgt\routing.php</b> on line <b>42</b><br
/><br /><b>Warning</b>:  pg_fetch_result() expects parameter 1 to be
resource, boolean given in
<b>C:\ms4w\Apache\htdocs\hgt\routing.php</b> on line <b>44</b><br
/><br /><b>Warning</b>:  pg_fetch_result() expects parameter 1 to be
resource, boolean given in
<b>C:\ms4w\Apache\htdocs\hgt\routing.php</b> on line <b>45</b><br
/><br /><b>Warning</b>:  pg_fetch_result() expects parameter 1 to be
resource, boolean given in
<b>C:\ms4w\Apache\htdocs\hgt\routing.php</b> on line <b>46</b><br
/><br /><b>Warning</b>:  pg_fetch_result() expects parameter 1 to be
resource, boolean given in
<b>C:\ms4w\Apache\htdocs\hgt\routing.php</b> on line <b>47</b><br
/><br /><b>Warning</b>:  pg_query(): Query failed: ERROR:  parse error
- invalid geometryHINT:  "POINT( )" <-- parse error at
position 9 within geometry in
<b>C:\ms4w\Apache\htdocs\hgt\routing.php</b> on line <b>42</b><br
/><br /><b>Warning</b>:  pg_fetch_result() expects parameter 1 to be
resource, boolean given in
<b>C:\ms4w\Apache\htdocs\hgt\routing.php</b> on line <b>44</b><br
/><br /><b>Warning</b>:  pg_fetch_result() expects parameter 1 to be
resource, boolean given in
<b>C:\ms4w\Apache\htdocs\hgt\routing.php</b> on line <b>45</b><br
/><br /><b>Warning</b>:  pg_fetch_result() expects parameter 1 to be
resource, boolean given in
<b>C:\ms4w\Apache\htdocs\hgt\routing.php</b> on line <b>46</b><br
/><br /><b>Warning</b>:  pg_fetch_result() expects parameter 1 to be
resource, boolean given in
<b>C:\ms4w\Apache\htdocs\hgt\routing.php</b> on line <b>47</b><br
/><br /><b>Warning</b>:  pg_query(): Query failed:  in
<b>C:\ms4w\Apache\htdocs\hgt\routing.php</b> on line <b>108</b><br
/><br /><b>Warning</b>:  pg_fetch_assoc() expects parameter 1 to be
resource, boolean given in
<b>C:\ms4w\Apache\htdocs\hgt\routing.php</b> on line <b>115</b><br
/><?xml version="1.0" encoding="UTF-8" standalone="yes"
?><route></route>



And the html document:


<!DOCTYPE html>

<html>

<head>

<title>pgRouting Demo</title>

<link rel="stylesheet" href="openlayers/theme/default/style.css"
type="text/css">

<style>

#map-id {

width: 100%;

height: 512px;

border: 1px solid black;

}

</style>

<script src="openlayers/OpenLayers.js"></script>

</head>

<body>

<div id="map-id"></div>

<script>

var SinglePoint = OpenLayers.Class.create();

SinglePoint.prototype = OpenLayers.Class.inherit(OpenLayers.Handler.Point, {

createFeature: function(evt) {

this.control.layer.removeFeatures(this.control.layer.features);

OpenLayers.Handler.Point.prototype.createFeature.apply(this, arguments);

}

});

var start_style = OpenLayers.Util.applyDefaults({

externalGraphic: "start.png",

graphicWidth: 18,

graphicHeight: 26,

graphicYOffset: -26,

graphicOpacity: 1

}, OpenLayers.Feature.Vector.style['default']);

var stop_style = OpenLayers.Util.applyDefaults({

externalGraphic: "stop.png",

graphicWidth: 18,

graphicHeight: 26,

graphicYOffset: -26,

graphicOpacity: 1

}, OpenLayers.Feature.Vector.style['default']);

var result_style = OpenLayers.Util.applyDefaults({

strokeWidth: 3,

strokeColor: "#ff0000",

fillOpacity: 0

}, OpenLayers.Feature.Vector.style['default']);

// global variables

var map, parser, start, stop, result, controls;

var format = 'image/png';

var bounds = new OpenLayers.Bounds(

                    -0.178166372987693, 5.62323476776518,

                    -0.140817506885068, 5.64840001174401

                );

var options = {

                    controls: [],

                    maxExtent: bounds,

                    maxResolution: 0.0001458940082134,

                    projection: "EPSG:4326",

                    units: 'degrees'

};

map = new OpenLayers.Map("map-id", options);

boundary = new OpenLayers.Layer.WMS(

                    "East Legon - Boundary", "
http://localhost:8080/geoserver/pgrouting/wms",

                    {

                        LAYERS: 'pgrouting:eastlegonboundary',

                        STYLES: '',

                        format: format,

                        tiled: true,

                        tilesOrigin : map.maxExtent.left + ',' +
map.maxExtent.bottom

                    },

                    {

                        buffer: 0,

                        displayOutsideMaxExtent: true,

                        isBaseLayer: true,

                        yx : {'EPSG:4326' : true}

                    }

                );

eastlegon = new OpenLayers.Layer.WMS(

                    "East Legon", "
http://localhost:8080/geoserver/pgrouting/wms",

                    {

                        LAYERS: 'pgrouting:eastlegon',

                        STYLES: '',

                        format: format,

transparent: true,

                        tiled: true,

                        tilesOrigin : map.maxExtent.left + ',' +
map.maxExtent.bottom

                    },

                    {

                        buffer: 0,

                        displayOutsideMaxExtent: true,

                        isBaseLayer: false,

                        yx : {'EPSG:4326' : true}

                    }

                );

start = new OpenLayers.Layer.Vector("Start Point", {style: start_style});

stop = new OpenLayers.Layer.Vector("End Point", {style: stop_style});

result = new OpenLayers.Layer.Vector("Routing Results",

{style: result_style});

map.addLayers([boundary, eastlegon, start, stop, result]);

map.addControl(new OpenLayers.Control.LayerSwitcher());

map.addControl(new OpenLayers.Control.PanPanel());

map.addControl(new OpenLayers.Control.ZoomPanel());

map.addControl(new OpenLayers.Control.ScaleLine());

map.addControl(new OpenLayers.Control.Navigation());

map.addControl(new OpenLayers.Control.MousePosition());

map.zoomToExtent(bounds);

// controls

controls = {

start: new OpenLayers.Control.DrawFeature(start, SinglePoint),

stop: new OpenLayers.Control.DrawFeature(stop, SinglePoint)

}

for (var key in controls) {

map.addControl(controls[key]);

}

function toggleControl(element) {

for (key in controls) {

if (element.value == key && element.checked) {

controls[key].activate();

} else {

controls[key].deactivate();

}

}

}

function compute() {

            var startPoint = start.features[0];

            var stopPoint = stop.features[0];


            if (startPoint && stopPoint) {

                var result = {

                    startpoint: startPoint.geometry.x + ' ' +
startPoint.geometry.y,

                    finalpoint: stopPoint.geometry.x + ' ' +
stopPoint.geometry.y,

                    method: OpenLayers.Util.getElement('method').value,

                    region: "eastlegon",

                    srid: "4326"

                };

                OpenLayers.loadURL("./routing.php",


 OpenLayers.Util.getParameterString(result),

                                   null,

                                   displayRoute);

            }

        }


        function displayRoute(response) {

            if (response && response.responseXML) {

                // erase the previous results

                result.removeFeatures(result.features);


                // parse the features

                var edges =
response.responseXML.getElementsByTagName('edge');

                var features = [];

                for (var i = 0; i < edges.length; i++) {

                    var g =
parser.read(edges[i].getElementsByTagName('wkt')[0].textContent);

                    features.push(new OpenLayers.Feature.Vector(g));

                }

                result.addFeatures(features);

            }

        }

</script>

<ul>

<li>

<input type="radio" name="control" id="noneToggle"

onclick="toggleControl(this);" checked="checked" />

<label for="noneToggle">navigate</label>

</li>

<li>

<input type="radio" name="control" value="start" id="startToggle"

onclick="toggleControl(this);" />

<label for="startToggle">set start point</label>

</li>

<li>

<input type="radio" name="control" value="stop" id="stopToggle"

onclick="toggleControl(this);" />

<label for="stopToggle">set stop point</label>

</li>

</ul>

<select id="method">

<option value="SPD">Shortest Path Dijkstra - undirected (BBox)</option>

<option value="SPA">Shortest Path A Star - undirected</option>

<option value="SPS">Shortest Path Shooting Star</option>

</select>

<button onclick="compute()">Calculate Route</button>

</body>

</html>



On Jan 4, 2014 3:45 AM, "Paragon Corporation" <lr at pcorp.us> wrote:

>  Emmanuel,
>
> That PHP code looks like it was written for pgRouting 1.0.  Which workshop
> did you get that from?
>
> Most of the pgRouting 2.0 functions are prefixed with pgr_. There should
> be a file in your install called
>  In C:/Program Files/PostgreSQL/9.2/share/extensions/pgrouting_legacy.sql
> which installs the 1.0 function names.  You could try using that though I
> haven't personally had a need to.
>
> One other thing I did notice about this code which I think is a bug is
> that although PG_PORT is defined, it doesn't seem to be used for
> pg_connect, so will not work without modification if you are runnning
> postgres on non-default port (5432).
>
> Hope that helps,
> Regina
> http://www.postgis.us
> http://www.bostongis.com
> http://www.paragoncorporation.com
>
>
>
>  ------------------------------
> *From:* pgrouting-users-bounces at lists.osgeo.org [mailto:
> pgrouting-users-bounces at lists.osgeo.org] *On Behalf Of *Emmanuel Adegboye
> *Sent:* Friday, January 03, 2014 4:42 PM
> *To:* pgrouting-users at lists.osgeo.org
> *Subject:* [pgrouting-users] Issues with pgrouting workshop code in
> postgis2.11/pgrouting 2.0
>
>   I'm having issues getting the pgrouting workshop to work on Postgresql
> 9.2/PostGIS 2.11 and pgrouting 2.0 on windows.
>
> How can I rewrite the following php/sql code and make it compatible with
> my version:
>
> <?php
>
>   // Database connection settings
>   define("PG_DB"  , "routing");
>   define("PG_HOST", "localhost");
>   define("PG_USER", "postgres");
>   define("PG_PORT", "5432");
>    define("PG_PASSWD",   "*******");
>   define("TABLE",   "eastlegon");
>
>   $counter = $pathlength = 0;
>
>   // Retrieve start point
>   $start = split(' ',$_REQUEST['startpoint']);
>   $startPoint = array($start[0], $start[1]);
>
>   // Retrieve end point
>   $end = split(' ',$_REQUEST['finalpoint']);
>   $endPoint = array($end[0], $end[1]);
>
>   // Find the nearest edge
>   $startEdge = findNearestEdge($startPoint);
>   $endEdge   = findNearestEdge($endPoint);
>
>   // FUNCTION findNearestEdge
>   function findNearestEdge($lonlat) {
>
>     // Connect to database
>     $con = pg_connect("dbname=".PG_DB." host=".PG_HOST." user=".PG_USER." password=".PG_PASSWD);
>
>     $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]-200)."
>                          ".($lonlat[1]-200).",
>                          ".($lonlat[0]+200)."
>                          ".($lonlat[1]+200).")'::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);
>
>     // Close database connection
>     pg_close($con);
>
>     return $edge;
>   }
>
>   // Select the routing algorithm
>   switch($_REQUEST['method']) {
>
>     case 'SPD' : // Shortest Path Dijkstra
>
>       $sql = "SELECT rt.gid, AsText(rt.the_geom) AS wkt,
>                    length(rt.the_geom) AS length, ".TABLE.".id
>                 FROM ".TABLE.",
>                     (SELECT gid, the_geom
>                         FROM dijkstra_sp_delta(
>                             '".TABLE."',
>                             ".$startEdge['source'].",
>                             ".$endEdge['target'].",
>                             3000)
>                      ) as rt
>                 WHERE ".TABLE.".gid=rt.gid;";
>       break;
>
>     case 'SPA' : // Shortest Path A*
>
>       $sql = "SELECT rt.gid, AsText(rt.the_geom) AS wkt,
>                      length(rt.the_geom) AS length, ".TABLE.".id
>                   FROM ".TABLE.",
>                       (SELECT gid, the_geom
>                           FROM astar_sp_delta(
>                               '".TABLE."',
>                               ".$startEdge['source'].",
>                               ".$endEdge['target'].",
>                               3000)
>                        ) as rt
>                   WHERE ".TABLE.".gid=rt.gid;";
>
>
> _______________________________________________
> Pgrouting-users mailing list
> Pgrouting-users at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/pgrouting-users
> ...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/pgrouting-users/attachments/20140104/8d721590/attachment-0001.html>


More information about the Pgrouting-users mailing list