demo (#22) - Error 'Invalid OGC WKT (too short)' from 'An Introduction to pgRouting with OpenLayers?' (#251) - Message List

Error 'Invalid OGC WKT (too short)' from 'An Introduction to pgRouting with OpenLayers?'

Hi !!

First sorry for my poor English... My name is Poul, I am new in the hole pgrouting stuff, and I am just learning. I got some problem during the tutorial from page http://pgrouting.postlbs.org/wiki/WorkshopFOSS4G2007.

And i don't know what to do

My data base looks like this:  http://img139.imageshack.us/img139/5016/dbase.jpg

My php file looks like this:

<?php
  // Database connection settings
  define("PG_DB"  , "postgis");
  define("PG_HOST", "localhost");
  define("PG_USER", "postgres");
  define("PG_PORT", "5432");
  define("TABLE",   "proba");
  $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);
    $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;
  }
?>

And also I changed the html page from the tutorial. The line:

                OpenLayers.loadURL("rout.php",
                                   OpenLayers.Util.getParameterString(result),
                                   null,
                                   displayRoute);

to:

                OpenLayers.loadURL("rout.php?startpoint= +result.startpoint+&finalpoint=+result.finalpoint+",
                                   OpenLayers.Util.getParameterString(result),
                                   null,
                                   displayRoute);

This was only way it would start... I don't know if it means something but I posted it....

When I comment the function findNearestEdge everything is fine. Any suggestions? Please help me... I don't know what to do next, and I am so desperate to do that... Please any anybody... I would really appreciate...

Poul

  • Message #887

    By the way I am working on windows XP, with the Openlayer 2.7, pgRouting-1.02_pg-8.3.3, postgis_1_3_5_pg83 and PostgreSql?_8.3.

    • Message #888

      The hole error is :

      Błąd: not understood series of signs after element of duciment
      Source code: http://localhost/buslines/Projekt1/Projekt1/rout.php?startpoint=%20+result.startpoint+&finalpoint=+result.finalpoint+
      Row: 2, Column: 1
      Source code:
      <b>Warning</b>:  pg_query() [<a href='function.pg-query'>function.pg-query</a>]: Query failed: ERROR:  Invalid OGC WKT (too short) in <b>C:\Program Files\Apache Group\Apache2\htdocs\buslines\Projekt1\Projekt1\rout.php</b> on line <b>45</b><br />
      

      The 45 line of the rout.php is :

       $query = pg_query($con,$sql);
      
      • Message #891

        Could you send the $sql string that will do the database query for both "closest edge" as well as for "shortest path" function?

        PS: if you put code in

        {{{ ... }}}
        

        it's better readable .

  • Message #892

    OK, I edited your posts to make it a bit easier to read and a problem could be with you projection:

    This part could make troubles (I didn't test it though):

                WHERE the_geom && setsrid(
                      'BOX3D(".($lonlat[0]-200)."
                             ".($lonlat[1]-200).",
                             ".($lonlat[0]+200)."
                             ".($lonlat[1]+200).")'::box3d, 4326)
    

    In our workshop SRID is 54004, which is a projection in "meter". You use WGS84, which is a projection in "decimal degrees".

    Now you create a bounding box around your point adding "200" to the top, right, left and bottom to later select all distances from the points inside this box to your point and get the one which is the nearest.

    "200" is in the same unit as your projection is and 400 degree is definitely more than the earth can provide ;-)
    Try it with 0.1 (or even less) for example.

    • Message #893

      Hi !

      Thanks a lot for a reply. I was thinking that I was left alone with this problem :) I tried your idea, but unfortunately I have still this error. I try somethink like that:

                    WHERE the_geom && setsrid(
                        'BOX3D(".($lonlat[0]-0.001)."
                               ".($lonlat[1]-0.001).",
                               ".($lonlat[0]+0.001)."
                               ".($lonlat[1]+0.001).")'::box3d, 4326)
                    ORDER BY dist LIMIT 1";
      

      I also try with bigger value (0.1) like you said, but still this same.

      I was thinking that something was wrong with projection. I start from the begininig of my journey

      - convert shp file to postgrsql data:

      shp2pgsql -w -s 4326 -i -I zielona_gora.shp proba > zielona_gora.sql
      

      I am not sure that I was suppose to the '-w', and I used as you can see the 4326 srid

      - definition of map

      		 map = new OpenLayers.Map('map', {
                               projection: "EPSG:4326",
                               units: "m",
                               maxResolution: 156543.0339,
      			 controls:[],
                               maxExtent: new OpenLayers.Bounds(-20037508,-20037508,
                                                       20037508, 20037508)
                             });
      

      as you can see I used 4326

      - definition of layer

                  var gmap = new OpenLayers.Layer.Google(
                      "Google Streets",
                      {'sphericalMercator': true,
                        srs:'ESPG:900913'
                        }
                  );
      

      As you can see I used 900913 in this case.

      I am a little bit confused with this projection stuff. I don't know if what I write may help you understand better my problem, but I hope :). One more thing as you can see my data in databse is  http://img139.imageshack.us/img139/5016/dbase.jpg for example

      MULTILINESTRING((15.50853 51939308,15.508331 51939308))
      

      But on the map I have different values to get for example the startpoint and finalpoint are:

      1725288.829070158 6788009.4051772505
      

      you also can see this screen  http://img27.imageshack.us/img27/340/pagegya.jpg

      Thanks a lot for your help, if you have any ideas... Please write :)

      Regards, paweluz

      • Message #894

        Hi Paweluz

        Thanks a lot for a reply. I was thinking that I was left alone with this problem :) I tried your idea, but unfortunately I have still this error.

        Because the people a usually answering in this forum are located in Japan we have a time offset. If you need a faster response you could also try the mailing list ;-)

        Regarding your problem:

        I think you have a problem with your projections:

        • 4326 ... are you sure this is correct? Then your unit is "degree"
        • 54004 ... this is World Mercator projection in "meter" (used in the 2007 tutorial)
        • 900913 ... this is the projection that you have to use when you want to use a Google basemap in OpenLayers? (in "meter"

        So if you get a high number like 1725288.829070158 of course you won't find a nearby point when your database keeps the geometry in degree where it would be maybe 15.50853. You kno what I mean?

        So what you have to do is transforming your data in the required projection, and because you can't change OpenLayers? when you want to use Google basemap, you need to reproject all to 900913 (look for this in  http://spatialreference.org to load it to PostgreSQL).

        You can keep your data in 4326, but you need to transform the points to 4326 then, which come in "meter" unit from OpenLayers?.

        • Message #895

          Hi!

          Thanks for the reply so soon :) I was thinking that I got something wrong with projection. Actually I was afraid of this. Because I don't know anything about this, I am a programmer, and actually I do not know much about projection. So I am really thankful for you help...

          I understand that I got points in different projection and I can't compare them, because as you said they are COMPLITLY DIFFERENT. Let me get it straight. - I change the the projection in my map from 4326 to 900913 (In the geoserver example in file ol-demo.html is this same) - the google layer is ok. Al I have to do is to change the projection of my geometry in my database from 4326 to 900913 right. I am going to find some information trough the Internet - thanks for the link. Do I figure it out right ?? Is my shp2pgsql convection is correct ?

          Regards Poul

          • Message #896

            I was thinking that I got something wrong with projection. Actually I was afraid of this. Because I don't know anything about this, I am a programmer, and actually I do not know much about projection.

            Take it as a different coordinate system.

            - I change the the projection in my map from 4326 to 900913 (In the geoserver example in file ol-demo.html is this same)

            You need 900913 if you want to use Google with OpenLayers?.

            Al I have to do is to change the projection of my geometry in my database from 4326 to 900913 right.

            postgres=# SELECT AsText(transform(GeometryFromText('POINT(90 45)',4326),900913));
                              astext
            ------------------------------------------
             POINT(10018754.1713946 5621521.48619142)
            (1 row)
            postgres=#
            

            If this works you can be sure that your database supports 900913 projection and it gives you a couple of hints to useful PostGIS functions.

            Is my shp2pgsql convection is correct ?

            I can't tell you the projection is correct there because this depends on your data.

            • Message #897

              Hi!

              Thanks for the quick reply, again :) Now I am at work, so I can't check anything. I would be at home in about 2,3 hours. I am guessing you would be a sleep :), you live in USA, am I right? I am going to write whether I did it or not. I am really,really, really thankful for your help. Thanks a lot :)

              Poul

              • Message #898

                Hi Paul, somehow we will get it:-) One more tutorium is going to be written and it will be based on OL2.7 (some things in the code changed there) and indeed the projection-thing might be a problem.

                http://pgrouting.postlbs.org/wiki/WorkshopOL2.7andOSM

                If you want you can send me your data and I will do a quick test. Kai-Behncke@…

            • Message #899

              Hi!

              I manage to deal with the projection stuff :) Now everything in my database is all right. here is screen:

               http://img22.imageshack.us/img22/2484/dbase900913.jpg

              I also had some other problems with php, but finally a manage I handled with them. But unfortunately now I have other problem. The rout that is drawing not exacly correct. here is some examples.

               http://img509.imageshack.us/img509/9250/routing.jpg

              In the php file the method findingnearles looks like that:

                function findNearestEdge($lonlat) {
                	    // Connect to database
                  $con = pg_connect("dbname=".PG_DB." host=".PG_HOST." user=".PG_USER);
              	$lonlat1=$lonlat[0]-200;
              	$lonlat2=$lonlat[1]-200;
              	$lonlat3=$lonlat[0]+200;
              	$lonlat4=$lonlat[1]+200;
                  $sql = "SELECT gid, source, target, the_geom,
              			 distance(the_geom, GeometryFromText(
                                'POINT(".$lonlat[0]." ".$lonlat[1].")', 900913)) AS dist
                          FROM ".TABLE."
                          WHERE the_geom && setsrid(
                                'BOX3D(".$lonlat1."
                                       ".$lonlat2.",
                                       ".$lonlat3."
                                       ".$lonlat4.")'::box3d, 900913)
                          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;
                }
              

              Are there any parameters that I can change , that could help me ? Or maybe it is because of the shp that I have at the begging. I am not sure if for example road looks like this:

              - road
              * node
              = the vector layer, found path
              ^ start point
              
              *-------*---------------------------* ...end
              

              and some click in the middle of the second road, like this

              *-------*------------^--------------* ...end
              

              then the roud on the map would looks like this

              *-------*------------^==============* ...end
              

              or maybe

              *-------*============^==============* ...end
              

              I hope you understand what I mean... Please help me understand all this stuff. I was hoping that it would be working like on the las example, for example something like that:

               http://demo.postlbs.org/openrouting/web/geobase.html

              or

               http://demo.postlbs.org/openrouting/web/suuchi.html

              And I am also trying to figure it out how come in the pages that I mentioned before when I click and hold the left mouse button I can move the map, but in may page there is an option 'navigate', and when it is checked I can move my page, I just input start or end point. Any ideas how can I get rid of the 'navigation' option an just have start and end points. Thanks a lot for your help...

              Regards Paul

              • Message #900

                Cool that it works now!

                Actually I would like to move this thread to a new topic from here because the question changed. Anyway ...

                What you see now is not a bug. It's just because the workshop is the most simple PHP script.

                What you did now is
                1. Select closest edges to start/end point
                2. Select either start or end of this link as start/end vertex ID
                3. Get the route between these two IDs

                If you take Shooting Star algorithm you take link IDs as start and end.

                In any case you're missing partial geometries, because shortest path algorithms don't split edges for you. You have to do this yourself and extend the PHP script or write a wrapper or whatever.

                You can use two PostGIS functions for this as far as I remember: line_locate_point(LineString?, Point) and line_substring(linestring, start, end) to get partial geometries.
                This way you can split your closest edge for start and end in two parts each.

                You actually have a few cases then and need to add the correct line_substring to the start and end of your routing result (or you have to remove it).

                • Message #901

                  Hi!

                  I was thinking that this is not bug, but this is just the way it works. Ok, I am going to move the subject to another topic, like you said. Thanks a lot for your help. I am going to check the line_locate_point, or line_substring functions... Maybe someone has done it to, who knows.... I am so happy that this is working :) You have no idea ... Again thanks a lot for all the responds, and all your time :) If you have any other idea, pleas don't hesitate to write to me :)

                  Regards, Paul