[prev in list] [next in list] [prev in thread] [next in thread]
List: postgis-users
Subject: [postgis-users] use PostGIS ST_Union in OpenLayers
From: Roman Zoun <zoony87 () googlemail ! com>
Date: 2011-01-27 13:02:55
Message-ID: AANLkTimm0kKjy86pBegS=2hdRNTYmXiMK-2d1bBsK2aN () mail ! gmail ! com
[Download RAW message or body]
[Attachment #2 (multipart/alternative)]
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?
[Attachment #5 (text/html)]
Hello,<div><br></div><div>I would like to combine Polygons with ST_UNION function in \
my OpenLayers client. The number of polygons is unknown. \
</div><div><br></div><div>The geometrie of the polygons is in polyArray and i get it \
from the OpenLayers Layer.</div> <div><br></div><div>First I build a request in \
OpenLayers.</div><div><div>___________________________________</div><div>var \
queryString = "?";</div><div><span class="Apple-tab-span" \
style="white-space:pre"> </span>for (var i=0; i<polyArray.length; i++)</div> \
<div><span class="Apple-tab-span" style="white-space:pre"> </span>{</div><div><span \
class="Apple-tab-span" style="white-space:pre"> </span>queryString += \
"polygon"+i + "=" + polyArray[i] + "&";</div> \
<div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div><div>var bla \
= queryString.substring(0, \
queryString.length-1);</div></div><div>___________________________________</div><div><br></div><div>I'tested \
it with two polygons, the result is </div> <div>"?polygon0=POLYGON((0 0,400 \
0,400 400,400 0,0 0))&polygon1=POLYGON((-600 200,-600 300,200 300,200 200,-600 \
200))"</div><div><br></div><div><br></div><div>Now i send this to my php \
file.</div><div><br></div> <div><div>OpenLayers.loadURL("Test.php", query, \
executeUnion, executeUnion);</div></div><div><br></div><div><div>function \
executeUnion(response) {</div><div> <span class="Apple-tab-span" \
style="white-space:pre"> </span>alert (response.responseText);</div> \
</div><div><br></div><div><br></div><div>Now i read the Request and insert the \
polygons in an Array and build the statement in \
php</div><div>___________________________________</div><div>$countInput=0;</div><div><div>while \
(true)</div> <div> {</div><div> \
if($_GET['polygon'.$countInput]==null)</div><div> {</div><div> \
break;</div><div> }else{</div><div> $polygon[$countInput] = \
$_GET['polygon'.$countInput];</div> <div> }</div><div> \
$countInput++;</div><div> }</div></div><div><br></div><div><div>$statementString \
= "SELECT ST_AsText(ST_Union(ARRAY[";</div><div>$countStatements=0;</div><div>while \
(true)</div> <div>{</div><div> if($polygon[$countStatements]==null)</div><div> \
break;</div><div> else</div><div> {</div><div> $statementString .= \
"ST_GeomFromText('$polygon[$countStatements]'), ";</div> <div> \
}</div><div> $countStatements++;</div><div>}</div><div>$statementString[strlen($statementString)-2] \
= '';</div><div>$statementString .= \
"]))";</div></div><div>___________________________________</div> \
<div>result ist "SELECT ST_AsText(ST_Union(ARRAY[ST_GeomFromText('POLYGON((0 \
0,400 0,400 400,400 0,0 0))'),</div><div><span class="Apple-tab-span" \
style="white-space:pre"> </span>ST_GeomFromText('POLYGON((-600 200,-600 300,200 \
300,200 200,-600 200))'),</div> <div><span class="Apple-tab-span" \
style="white-space:pre"> </span>ST_GeomFromText('POLYGON((200 200,200 600,300 \
600,300 300,200 200))') ] ) \
)")</div><div><br></div><div><br></div><div><br></div><div>now connect to \
DB</div> <div><br></div><div><div>$conn = pg_connect ("dbname=$dbname user=$user \
password=$password </div><div> \
port=$port host=$host");</div></div><div><br></div><div><br> </div><div>the \
insert the statement</div><div>___________________________________</div><div>$result \
= pg_query($conn, $statementString);</div><div><div>$row = \
pg_fetch_array($result,0);</div><div>echo $row[0];</div></div> \
<div>___________________________________</div><div>nothing happend, i get an empty \
alert.</div><div><br></div><div>if i use the statement as a string and not as a \
variable </div><div>___________________________________</div> <div><div>$result = \
pg_query($conn, "SELECT ST_AsText(ST_Union(ARRAY[ST_GeomFromText('POLYGON((0 \
0,400 0,400 400,400 0,0 0))'),</div><div><span class="Apple-tab-span" \
style="white-space:pre"> </span>ST_GeomFromText('POLYGON((-600 200,-600 300,200 \
300,200 200,-600 200))'),</div> <div><span class="Apple-tab-span" \
style="white-space:pre"> </span>ST_GeomFromText('POLYGON((200 200,200 600,300 \
600,300 300,200 200))') ] ) \
)");</div></div><div>___________________________________</div><div><br> \
</div><div>i get the geometry of the Union of the \
polygons.</div><div><br></div><div><span class="Apple-style-span" style="font-size: \
large;">how i can build the statement \
dynamically?</span></div><div><br></div><div><br> </div>
_______________________________________________
postgis-users mailing list
postgis-users@postgis.refractions.net
http://postgis.refractions.net/mailman/listinfo/postgis-users
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic