[OpenLayers-Users] how to add php/postgresql query result as
Geojson layer to openlayers
javad sadidi
jsadidi at gmail.com
Mon Oct 10 09:11:35 EDT 2011
hi
I have a databse in postgresql and want to add it to open layer as lines in
Geojson format.for this I write php codes . my file is close.php 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
$sql = "SELECT gid, ST_AsGeoJSON(the_geom) AS geojson,
length(the_geom) AS length FROM .TABLE " ;
// Connect to database
$dbcon = pg_connect("dbname=".PG_DB." host=".PG_HOST." user=".PG_USER);
// Perform database query
$query = pg_query($dbcon,$sql);
?>
<?php
// Return streets 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 result
header('Content-type: application/json',true);
echo json_encode($geojson);
?>
then I add a layers to openlayers:this is the code:
json_layer = new OpenLayers.Layer.Vector("GeoJSON", {
strategies: [new OpenLayers.Strategy.Fixed()],
protocol: new OpenLayers.Protocol.HTTP({
url: "./php/test.php",
format: new OpenLayers.Format.GeoJSON()
})
});
// Add our vector layer to the map
map.addLayer(json_layer);
but I dont see any line in the result of openlayers in the
browser.projection of my database is4326 and for openlayers is 900913.
I have added the following lines to the openlayers codes but it doesnt works
yet:
internalProjection: epsg_900913,
externalProjection: epsg_4326
what is wrong in the php or openlayers code?
who can help me?
I have confused with this problem for some weeks.
thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-users/attachments/20111010/154d7d7f/attachment.html
More information about the Users
mailing list