[Routing] render the postgresql query into openlayers (GeoJSON output)

javad sadidi jsadidi at gmail.com
Wed Aug 10 19:41:32 BST 2011


hi
I think this the code. please see it and say if that is not right:

<?php

   // Database connection settings
   define("PG_DB"  , "routing");
   define("PG_HOST", "localhost");
   define("PG_USER", "postgres");
   define("PG_PORT", "5432");
   define("TABLE",   "ways");
?>
<?php
 $sql="SELECT ST_AsGeoJSON(the_geom) AS geojson FROM ways WHERE status
= 'closed'";
 $dbcon = pg_connect("dbname=".PG_DB." host=".PG_HOST." user=".PG_USER);
 $query = pg_query($dbcon,$sql);

?>

// for geojson output

<?php


$geojson = array(
      'type'      => 'FeatureCollection',
      'features'  => array()
   );

   // Add edges to GeoJSON array
   while($edge=pg_fetch_assoc($query)) {

      $feature = array(
         'type' => 'Feature',
         'geometry' => json_decode($edge['geojson'], true),
         'crs' => array(
            'type' => 'EPSG',
            'properties' => array('code' => '4326')
         ),
         'properties' => array(
            'id' => $edge['id'],
            'length' => $edge['length']
         )
      );

      // Add feature array to feature collection array
      array_push($geojson['features'], $feature);
   }

	
   // Close database connection
   pg_close($dbcon);

   // Return routing result
   header('Content-type: application/json',true);
   echo json_encode($geojson);
   ?>

.is it ok?

then to display it in html file with opn layers:  I have to the following
steps:
1-create the layer where the query result will be drown
2- add the layer to the map


thanks











On Wed, Aug 10, 2011 at 8:22 PM, Daniel Kastl <daniel at georepublic.de> wrote:

>
>
> On Thu, Aug 11, 2011 at 3:09 AM, javad sadidi <jsadidi at gmail.com> wrote:
>
>> it is very easy.
>> I am using pgrouting workshop2010 material and find the best route. I do
>> it.
>> but I also want to select special streets in pgsql and show them in red
>> color in the browser.
>> for this I have added a filed in the name of "Status" in the pgsql
>> database . then for some streets that I want to show them in the browser I
>> set the Status field to CLOSE:
>> then I want to show the records which the STATUS of them set to  CLOSE
>> with red line in the browser. I mean this query:select * from ways where
>> Status is CLOSE,
>> with this command I select the streets taht are CLOSE, then I want to show
>> them in the browser with red color.
>> I want to select the records that are CLOSE and bring and show them by
>> geoext format .
>> do you understand my mean?
>
>
> You need to do like
>
> SELECT ST_AsGeoJSON(the_geom) AS geojson FROM ways WHERE status = 'closed';
>
> Then you get a list of road segments which you wrap into a GeoJSON format
> that OpenLayers GeoJSON layer can read, like:
> http://workshop.pgrouting.org/chapters/php_server.html#geojson-output
>
> Daniel
>
>
> Daniel
>
>
>
>
>>
>>
>> On Wed, Aug 10, 2011 at 7:55 PM, Daniel Kastl <daniel at georepublic.de>wrote:
>>
>>>
>>>
>>> On Thu, Aug 11, 2011 at 2:43 AM, javad sadidi <jsadidi at gmail.com> wrote:
>>>
>>>> yes.but in your example Id dont know how can I replace the edge variable
>>>> $edge with my query result.
>>>> select * from ways where street name is somthing.
>>>
>>>
>>> How does your query result look like?
>>> I don't understand what you want to replace there?
>>>
>>> Your query "select * from ways where street name is somthing" needs to
>>> return at least the geometries of your route segments. And GeoJSON is a
>>> convenient format if you later want to use OpenLayers.
>>>
>>> Daniel
>>>
>>>
>>>
>>>
>>>>
>>>>
>>>>
>>>> On Wed, Aug 10, 2011 at 7:02 PM, Daniel Kastl <daniel at georepublic.de>wrote:
>>>>
>>>>> Hi Javad,
>>>>>
>>>>> hi to all.
>>>>>>  I am working on pgrouting project.I am using OSM data to find the
>>>>>> best route.
>>>>>> but I want to take a query into postgresql by php server side and
>>>>>> returen the result on the browser with GeoJSON format. for example I want
>>>>>> bring the result of this query as selected streets on the browser by
>>>>>> openlayers.
>>>>>> select * from table where streetname is x or y;
>>>>>>
>>>>>
>>>>> You mean this?
>>>>> http://workshop.pgrouting.org/chapters/php_server.html#geojson-output
>>>>>
>>>>>
>>>>>> then bring the result by Geojson output to the browser,I would like to
>>>>>> display these streets as red color in the browser for clients.
>>>>>> who can help me?
>>>>>>
>>>>>
>>>>> Here is an example with GeoExt:
>>>>>
>>>>> http://workshop.pgrouting.org/chapters/geoext_client.html#full-source-code
>>>>>
>>>>> Daniel
>>>>>
>>>>>
>>>>>> it the simplest example.
>>>>>> danke
>>>>>>
>>>>>>
>>>>>>
>>>>>> _______________________________________________
>>>>>> Routing mailing list
>>>>>> Routing at openstreetmap.org
>>>>>> http://lists.openstreetmap.org/listinfo/routing
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>> --
>>>>> Georepublic UG & Georepublic Japan
>>>>> eMail: daniel.kastl at georepublic.de
>>>>> Web: http://georepublic.de
>>>>>
>>>>> _______________________________________________
>>>>> Routing mailing list
>>>>> Routing at openstreetmap.org
>>>>> http://lists.openstreetmap.org/listinfo/routing
>>>>>
>>>>>
>>>>
>>>> _______________________________________________
>>>> Routing mailing list
>>>> Routing at openstreetmap.org
>>>> http://lists.openstreetmap.org/listinfo/routing
>>>>
>>>>
>>>
>>>
>>> --
>>> Georepublic UG & Georepublic Japan
>>> eMail: daniel.kastl at georepublic.de
>>> Web: http://georepublic.de
>>>
>>> _______________________________________________
>>> Routing mailing list
>>> Routing at openstreetmap.org
>>> http://lists.openstreetmap.org/listinfo/routing
>>>
>>>
>>
>> _______________________________________________
>> Routing mailing list
>> Routing at openstreetmap.org
>> http://lists.openstreetmap.org/listinfo/routing
>>
>>
>
>
> --
> Georepublic UG & Georepublic Japan
> eMail: daniel.kastl at georepublic.de
> Web: http://georepublic.de
>
> _______________________________________________
> Routing mailing list
> Routing at openstreetmap.org
> http://lists.openstreetmap.org/listinfo/routing
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openstreetmap.org/pipermail/routing/attachments/20110810/dd652ddc/attachment-0001.html>


More information about the Routing mailing list