<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
  <META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
  <META NAME="GENERATOR" CONTENT="GtkHTML/3.30.3">
</HEAD>
<BODY>
Hi Javad<BR>
<BR>
What geojson string is returned by the php? - is it correctly formatted?<BR>
<BR>
&gt;&gt;&nbsp;&nbsp; $sql = &quot;SELECT gid, ST_AsGeoJSON(the_geom) AS geojson, length(the_geom) AS length FROM &quot;.TABLE.&quot;&quot;<BR>
<BR>
I usually format my geojson with properties at this point already (in a plpgsql function)...<BR>
<BR>
the wrapper function I wrote for this:<BR>
 <BR>
CREATE OR REPLACE FUNCTION geojson_attrib(table_name character varying, gid character varying, cs character varying, ce character varying, schema_name character varying)<BR>
&nbsp; RETURNS SETOF character varying AS<BR>
$BODY$<BR>
 select 'SELECT ''{' || $3 || '&quot;geometry&quot;:'' || st_asgeojson(transform(the_geom, 900913),6) || <BR>
                '',&quot;type&quot;:&quot;Feature&quot;,' || $4 || '&quot;properties&quot;:{' <BR>
        || array_to_string(ARRAY(<BR>
        SELECT '&quot;' || c.column_name || '&quot;:&quot;''||coalesce(&quot;'|| c.column_name || '&quot;::varchar,''null'')||''&quot;'<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FROM information_schema.columns As c<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WHERE table_schema= $5 and table_name = $1 <BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AND&nbsp; c.column_name NOT IN('gid', 'the_geom')<BR>
&nbsp;&nbsp;&nbsp; ), ',') || '},&quot;gid&quot;:&quot;'' || gid || ''&quot;}'' as v FROM ' || $5 || '.' || $1 || ' where gid in(' || $2 || ')' as v;<BR>
$BODY$<BR>
&nbsp; LANGUAGE sql VOLATILE<BR>
<BR>
I use cs and ce to (comment start, comment end) remove geometries if I only want json, so you can set them to: ''<BR>
<BR>
<BR>
To display in Openlayers you need to add a geojson reader:<BR>
<BR>
//load the php result&nbsp; into variable str using an xmlhttprequest ( I use Ext to do this)<BR>
<BR>
//geojson reader<BR>
var geojson_format = new OpenLayers.Format.GeoJSON();<BR>
<BR>
//create&nbsp; a vector layer<BR>
var vector_sites = new OpenLayers.Layer.Vector(); <BR>
<BR>
//add the vector to the ol map<BR>
map.addLayer(vector_sites);<BR>
<BR>
// read the geojson string into the layer<BR>
vector_sites.addFeatures(geojson_format.read(str)); <BR>
<BR>
Alternatively you can use GeoExt and just load the php result into a FeatureStore that you point to a vector layer<BR>
<BR>
&nbsp;&nbsp;&nbsp; var&nbsp; store = new GeoExt.data.FeatureStore({<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; layer: vector_layer,<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fields:fields,<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; proxy: new GeoExt.data.ProtocolProxy({<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; protocol: new OpenLayers.Protocol.HTTP({<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; url: store_url,<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; format: new OpenLayers.Format.GeoJSON()<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; })<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }),<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; autoLoad: true<BR>
&nbsp;&nbsp;&nbsp; });<BR>
<BR>
Regards<BR>
<BR>
<TABLE CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR>
<TD>
<BR>
<B>Glenn Mullett</B><BR>
<I><FONT SIZE="1"><FONT COLOR="#333333">BTech(Nature Cons) PGDip(UNIGIS)</FONT></FONT></I><BR>
<FONT SIZE="2"><FONT COLOR="#333333">H.O.D. GIS</FONT></FONT><BR>
<FONT SIZE="2"><FONT COLOR="#333333">Lidwala Consulting Engineers</FONT></FONT><BR>
<FONT SIZE="2">email:<A HREF="mailto:gmullett@lidwala.com">gmullett@lidwala.com</A></FONT><BR>
<BR>
<FONT COLOR="#808080">___________________________________________________________________________________________________________________________________________________</FONT>
</TD>
</TR>
</TABLE>
On Wed, 2011-08-24 at 19:32 +0200, javad sadidi wrote:<BR>
<BLOCKQUOTE TYPE=CITE>
    hi <BR>
    I have a table in postgresql that it has topology and I want to display it as line in openlayers.<BR>
    I use php to connect and convert my data to Geojson. this the codes.but I can not&nbsp; get the result.who can help me.where is wrong in my codes,this is the code.<BR>
    <BR>
    <BR>
    &lt;?php<BR>
    <BR>
    &nbsp;&nbsp; // Database connection settings<BR>
    &nbsp;&nbsp; define(&quot;PG_DB&quot;&nbsp; , &quot;tehran&quot;);<BR>
    &nbsp;&nbsp; define(&quot;PG_HOST&quot;, &quot;localhost&quot;); <BR>
    &nbsp;&nbsp; define(&quot;PG_USER&quot;, &quot;postgres&quot;);<BR>
    &nbsp;&nbsp; define(&quot;PG_PORT&quot;, &quot;5432&quot;); <BR>
    &nbsp;&nbsp; define(&quot;TABLE&quot;, &quot;close&quot;);<BR>
    <BR>
    &nbsp; <BR>
    ?&gt;<BR>
    <BR>
    &lt;?php<BR>
    // Connect to database<BR>
    &nbsp;&nbsp; $dbcon = pg_connect(&quot;dbname=&quot;.PG_DB.&quot; host=&quot;.PG_HOST.&quot; user=&quot;.PG_USER);<BR>
    &nbsp;&nbsp; $sql = &quot;SELECT gid, ST_AsGeoJSON(the_geom) AS geojson, length(the_geom) AS length FROM &quot;.TABLE.&quot;&quot;<BR>
    <BR>
    &nbsp;&nbsp; // Perform database query<BR>
    &nbsp;&nbsp; $query = pg_query($dbcon,$sql); <BR>
    &nbsp;&nbsp; <BR>
    ?&gt;<BR>
    <BR>
    &lt;?php<BR>
    <BR>
    &nbsp;&nbsp; // Return route as GeoJSON<BR>
    &nbsp;&nbsp; $geojson = array(<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'type'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =&gt; 'FeatureCollection',<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'features'&nbsp; =&gt; array()<BR>
    &nbsp;&nbsp; ); <BR>
    &nbsp; <BR>
    &nbsp;&nbsp; // Add edges to GeoJSON array<BR>
    &nbsp;&nbsp; while($edge=pg_fetch_assoc($query)) {&nbsp; <BR>
    <BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $feature = array(<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'type' =&gt; 'Feature',<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'geometry' =&gt; json_decode($edge['geojson'], true),<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'crs' =&gt; array(<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'type' =&gt; 'EPSG',<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'properties' =&gt; array('code' =&gt; '4326')<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ),<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'properties' =&gt; array(<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'id' =&gt; $edge['id'],<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'length' =&gt; $edge['length']<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; );<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Add feature array to feature collection array<BR>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; array_push($geojson['features'], $feature);<BR>
    &nbsp;&nbsp; }<BR>
    <BR>
    &nbsp;&nbsp; // Close database connection<BR>
    &nbsp;&nbsp; pg_close($dbcon);<BR>
    <BR>
    &nbsp;&nbsp; // Return routing result<BR>
    &nbsp;&nbsp; header('Content-type: application/json',true);<BR>
    &nbsp;&nbsp; echo json_encode($geojson);<BR>
    &nbsp;&nbsp; <BR>
    ?&gt;<BR>
    <BR>
<PRE>
_______________________________________________
Users mailing list
<A HREF="mailto:Users@lists.osgeo.org">Users@lists.osgeo.org</A>
<A HREF="http://lists.osgeo.org/mailman/listinfo/openlayers-users">http://lists.osgeo.org/mailman/listinfo/openlayers-users</A>
</PRE>
</BLOCKQUOTE>
</BODY>
</HTML>