[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 = &quot;?&quot;;</div><div><span class="Apple-tab-span" \
style="white-space:pre">	</span>for (var i=0; i&lt;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 += \
&quot;polygon&quot;+i + &quot;=&quot; + polyArray[i] + &quot;&amp;&quot;;</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&#39;tested \
it with two polygons, the result is </div> <div>&quot;?polygon0=POLYGON((0 0,400 \
0,400 400,400 0,0 0))&amp;polygon1=POLYGON((-600 200,-600 300,200 300,200 200,-600 \
200))&quot;</div><div><br></div><div><br></div><div>Now i send this to my php \
file.</div><div><br></div> <div><div>OpenLayers.loadURL(&quot;Test.php&quot;, 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[&#39;polygon&#39;.$countInput]==null)</div><div>        {</div><div>         \
break;</div><div>        }else{</div><div>            $polygon[$countInput] = \
$_GET[&#39;polygon&#39;.$countInput];</div> <div>        }</div><div>        \
$countInput++;</div><div>    }</div></div><div><br></div><div><div>$statementString  \
= &quot;SELECT ST_AsText(ST_Union(ARRAY[&quot;;</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 .= \
&quot;ST_GeomFromText(&#39;$polygon[$countStatements]&#39;), &quot;;</div> <div>    \
}</div><div>    $countStatements++;</div><div>}</div><div>$statementString[strlen($statementString)-2] \
= &#39;&#39;;</div><div>$statementString .= \
&quot;]))&quot;;</div></div><div>___________________________________</div> \
<div>result ist &quot;SELECT ST_AsText(ST_Union(ARRAY[ST_GeomFromText(&#39;POLYGON((0 \
0,400 0,400 400,400 0,0 0))&#39;),</div><div><span class="Apple-tab-span" \
style="white-space:pre">	</span>ST_GeomFromText(&#39;POLYGON((-600 200,-600 300,200 \
300,200 200,-600 200))&#39;),</div> <div><span class="Apple-tab-span" \
style="white-space:pre">	</span>ST_GeomFromText(&#39;POLYGON((200 200,200 600,300 \
600,300 300,200 200))&#39;) ] ) \
)&quot;)</div><div><br></div><div><br></div><div><br></div><div>now connect to \
DB</div> <div><br></div><div><div>$conn = pg_connect (&quot;dbname=$dbname user=$user \
password=$password </div><div>                                                    \
port=$port host=$host&quot;);</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, &quot;SELECT ST_AsText(ST_Union(ARRAY[ST_GeomFromText(&#39;POLYGON((0 \
0,400 0,400 400,400 0,0 0))&#39;),</div><div><span class="Apple-tab-span" \
style="white-space:pre">	</span>ST_GeomFromText(&#39;POLYGON((-600 200,-600 300,200 \
300,200 200,-600 200))&#39;),</div> <div><span class="Apple-tab-span" \
style="white-space:pre">	</span>ST_GeomFromText(&#39;POLYGON((200 200,200 600,300 \
600,300 300,200 200))&#39;) ] ) \
)&quot;);</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