[Routing] render the postgresql query into openlayers (GeoJSON output)
javad sadidi
jsadidi at gmail.com
Thu Aug 11 13:43:55 BST 2011
hi daniel
excuse me for too much questions.
I just add the following commands to the workshop codes and also have added
a php file to address it for the query:
// create the layer where the Closed streets will be drawn
var closed_streets = new OpenLayers.Layer.Vector("streets", {
styleMap: new OpenLayers.StyleMap(new OpenLayers.Style({
strokeColor: "#F00000",
strokeWidth: 6
}))
});
and then:
// add the layers to the map
map.addLayers([points_layer, route_layer, closed_streets]);
and also:
// create the new store to query the web service for dispalying the CLOSED
streets
var store1 = new GeoExt.data.FeatureStore({
layer: closed_streets,
fields: [
{name: "length"}
],
proxy: new GeoExt.data.ProtocolProxy({
protocol: new OpenLayers.Protocol.HTTP({
url: "./php/closedstreets.php",
format: new OpenLayers.Format.GeoJSON({
internalProjection: epsg_900913,
externalProjection: epsg_4326
})
})
})
});
and then I created the new php file (closedstreets.php):
<?php
// Database connection settings
define("PG_DB" , "routing");
define("PG_HOST", "localhost");
define("PG_USER", "postgres");
define("PG_PORT", "5432");
define("TABLE", "ways");
?>
<?php
$dbcon = pg_connect("dbname=".PG_DB." host=".PG_HOST." user=".PG_USER);
$sql="SELECT ST_AsGeoJSON(the_geom) AS geojson FROM ways WHERE status =
'closed'";
$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);
?>
you said me that I have to delete the Properties but I dont know exactly
which lines. please delete the Property in my php code and copy the correct
file for me. and also look to added line in the lines that I added to
display the result of PHP file in the browser. for example if you look to my
added lines you see that I created a new seore(var store1) . is that right?
why the codes doest work? what was the problem?
thanks
On Thu, Aug 11, 2011 at 4:01 AM, Daniel Kastl <daniel at georepublic.de> wrote:
>
>
>> .is it ok?
>>
>
> More or less, but you need to to try it yourself. (Remove the "properties"
> from the GeoJSON, because you don't query for them, or add them to the
> query)
>
>
>>
>> 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
>>
>
> Yes, it's all described in the workshop. You just need one more layer.
>
> Daniel
>
>
>
>
>>
>>
>> thanks
>>
>>
>>
>>
> --
> 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/20110811/9b1c3c4b/attachment.html>
More information about the Routing
mailing list