[OpenLayers-Users] postgresql table to Geojson and diplay it in
openlayers
javad sadidi
jsadidi at gmail.com
Wed Aug 24 13:32:34 EDT 2011
hi
I have a table in postgresql that it has topology and I want to display it
as line in openlayers.
I use php to connect and convert my data to Geojson. this the codes.but I
can not get the result.who can help me.where is wrong in my codes,this is
the code.
<?php
// Database connection settings
define("PG_DB" , "tehran");
define("PG_HOST", "localhost");
define("PG_USER", "postgres");
define("PG_PORT", "5432");
define("TABLE", "close");
?>
<?php
// Connect to database
$dbcon = pg_connect("dbname=".PG_DB." host=".PG_HOST." user=".PG_USER);
$sql = "SELECT gid, ST_AsGeoJSON(the_geom) AS geojson, length(the_geom)
AS length FROM ".TABLE.""
// Perform database query
$query = pg_query($dbcon,$sql);
?>
<?php
// Return route as GeoJSON
$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);
?>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-users/attachments/20110824/8ba64070/attachment.html
More information about the Users
mailing list