[postgis-users] use PostGIS ST_Union in OpenLayers

Roman Zoun zoony87 at googlemail.com
Thu Jan 27 05:02:55 PST 2011


Hello,

I would like to combine Polygons with ST_UNION function in my OpenLayers
client. The number of polygons is unknown.

The geometrie of the polygons is in polyArray and i get it from the
OpenLayers Layer.

First I build a request in OpenLayers.
___________________________________
var queryString = "?";
for (var i=0; i<polyArray.length; i++)
{
queryString += "polygon"+i + "=" + polyArray[i] + "&";
}
var bla = queryString.substring(0, queryString.length-1);
___________________________________

I'tested it with two polygons, the result is
"?polygon0=POLYGON((0 0,400 0,400 400,400 0,0 0))&polygon1=POLYGON((-600
200,-600 300,200 300,200 200,-600 200))"


Now i send this to my php file.

OpenLayers.loadURL("Test.php", query, executeUnion, executeUnion);

function executeUnion(response) {
             alert (response.responseText);


Now i read the Request and insert the polygons in an Array and build the
statement in php
___________________________________
$countInput=0;
while (true)
    {
        if($_GET['polygon'.$countInput]==null)
        {
            break;
        }else{
            $polygon[$countInput] = $_GET['polygon'.$countInput];
        }
        $countInput++;
    }

$statementString  = "SELECT ST_AsText(ST_Union(ARRAY[";
$countStatements=0;
while (true)
{
    if($polygon[$countStatements]==null)
        break;
    else
    {
        $statementString .= "ST_GeomFromText('$polygon[$countStatements]'),
";
    }
    $countStatements++;
}
$statementString[strlen($statementString)-2] = '';
$statementString .= "]))";
___________________________________
result ist "SELECT ST_AsText(ST_Union(ARRAY[ST_GeomFromText('POLYGON((0
0,400 0,400 400,400 0,0 0))'),
ST_GeomFromText('POLYGON((-600 200,-600 300,200 300,200 200,-600 200))'),
ST_GeomFromText('POLYGON((200 200,200 600,300 600,300 300,200 200))') ] )
)")



now connect to DB

$conn = pg_connect ("dbname=$dbname user=$user password=$password
                                                    port=$port host=$host");


the insert the statement
___________________________________
$result = pg_query($conn, $statementString);
$row = pg_fetch_array($result,0);
echo  $row[0];
___________________________________
nothing happend, i get an empty alert.

if i use the statement as a string and not as a variable
___________________________________
$result = pg_query($conn, "SELECT
ST_AsText(ST_Union(ARRAY[ST_GeomFromText('POLYGON((0 0,400 0,400 400,400 0,0
0))'),
ST_GeomFromText('POLYGON((-600 200,-600 300,200 300,200 200,-600 200))'),
ST_GeomFromText('POLYGON((200 200,200 600,300 600,300 300,200 200))') ] )
)");
___________________________________

i get the geometry of the Union of the polygons.

how i can build the statement dynamically?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20110127/3ad99dc9/attachment.html>


More information about the postgis-users mailing list