[Mapserver-users] Classes based on Postgis && function

Benjamin Wragg bwragg at tpg.com.au
Fri Feb 21 08:55:00 EST 2003


This is a multi-part message in MIME format.

------=_NextPart_000_0001_01C2DA08.D6F047E0
Content-Type: text/plain;
	charset="us-ascii"
Content-Transfer-Encoding: 7bit

I've tried the code on another machine, which has the 3.6.4 release and
I get a slightly better error:
       
Warning: MapServer Error in prep_DB(): Error executing POSTGIS DECLARE
(the actual query) statement: 'DECLARE mycursor BINARY CURSOR FOR SELECT
sbinary(force_collection(force_2d(the_geom from(select sheet.the_geom as
the_geom, userpolygon.the_geom as polygeom, sheet.oid, case when
sheet.seriesid=1 and distance(sheet.the_geom, userpolygon.the_geom)=0
then 1 end as myclass)),'NDR'),OID::text from sheet,userpolygon where
sheet.the_geom && userpolygon.the_geom and sheet.seriesid=1 and
userpolygon.id=2) as foo using UNIQUE=sheet.oid WHERE the_geom
from(select sheet.the_geom as the_geom, userpolygon.the_geom as
polygeom, sheet.oid, case when sheet.seriesid=1 and
distance(sheet.the_geom,userpolygon.the_geom)=0 then 1 end as myclass &&
setSRID('BOX3D(-180 -135,180 135)'::BOX3D, -1 )'

Postgresql reports the error 'ERROR: parser: parse error at or near
"from" '

More Help:

Error with POSTGIS data variable. You specified '<check your .map
file>'.
Standard ways of specifiying are :

The 'from' part is the same error that the database gives back. I copied
the above sql and ran in over the database and it seems to be
complaining about the first 'from'.

Any suggestions?

Thanks,

Benjamin





-----Original Message-----
From: mapserver-users-admin at lists.gis.umn.edu [
<mailto:mapserver-users-admin at lists.gis.umn.edu>
mailto:mapserver-users-admin at lists.gis.umn.edu] On Behalf Of Jan
Hartmann
Sent: Thursday, 20 February 2003 10:43 PM
To: mapserver-users at lists.gis.umn.edu
Subject: Re: [Mapserver-users] Classes based on Postgis && function


I reformatted your select statement (always a good idea with SQL that
complex), and it looks OK.


select the_geom from
    (select sheet.the_geom as the_geom,
            userpolygon.the_geom as polygeom,
            sheet.oid,
            case when sheet.seriesid=1
                 and distance(sheet.the_geom,userpolygon.the_geom)=0
            then 1 end as myclass
     from sheet, userpolygon
     where sheet.the_geom && userpolygon.the_geom
       and sheet.seriesid=1
       and userpolygon.id=2
     ) as foo


If, as you say, PSQL accepts it, the problem must be with
MapServer/PostGIS. This could very well be the case, as you seem to be
using an old version of mappostgis.c, which had problems with subselects
from non-georeferenced data (SRID=-1). There have been quite a few
postings on the MapServer and PostGIS mailing lists on this. So perhaps
upgrading to the newest version will give you a map at last.

Jan


bwragg at tpg.com.au wrote:
> Thanks for pointing out the problems with my logic. I've decided to
> put the second class into
> its own layer. I've got the following sql working and returning the
right thing from psql:
>
> select the_geom from(select sheet.the_geom as the_geom,
> userpolygon.the_geom as
> polygeom, sheet.oid ,case when sheet.seriesid=1 and
distance(sheet.the_geom,
> userpolygon.the_geom)=0 then 1 end as myclass from sheet, userpolygon
where
> sheet.the_geom && userpolygon.the_geom and sheet.seriesid=1 and
userpolygon.id=2 ) as
> foo
>
> If I add this to the data property of a mapobj it crashes with the
> following error on the
> mapObj->draw() function.
>
> Warning: MapServer Error in msPOSTGISLayerWhichShapes(): prep_DB:Error
> executing
> POSTGIS DECLARE statement (0.6 failed - retried 0.5 and it failed
too). DECLARE mycursor
> BINARY CURSOR FOR SELECT asbinary(force_collection(force_2d(the_geom
from(select
> sheet.the_geom as the_geom, userpolygon.the_geom as polygeom,
sheet.oid ,case when
> sheet.seriesid=1 and distance(sheet.the_geom, userpolygon.the_geom)=0
then 1 end as
> myclass)),'NDR'),OID::text from sheet, userpolygon where
sheet.the_geom &&
> userpolygon.the_geom and sheet.seriesid=1 and userpolygon.id=2 ) as
foo WHERE the_geom
> from(select sheet.the_geom as the_geom, userpolygon.the_geom as
polygeom,
> sheet.oid ,case when sheet.seriesid=1 and distance(sheet.the_geom,
userpolygon.the_geom)
> =0 then 1 end as myclass && setSRID('BOX3D(-180 -135,180 135)'::BOX3D,
find_srid('','sheet,
> userpolygon where sheet.the_geom && userpolygon.the_geom and
sheet.seriesid=1 and
> userpolygon.id=2 ) as foo','the_geom from(select sheet.the_geom as
the_geom,
> userpolygon.the_geom as polygeom, she in...
>
> Postgres gives the following error at this same point:
>
> DEBUG:  query: DECLARE mycursor BINARY CURSOR FOR SELECT
> asbinary(force_collection (force_2d(the_geom from(select
> sheet.the_geom as the_geom, userpolygon.the_geom as polygeom,
> sheet.oid ,case when sheet.seriesid=1 and distance(sheet.the_geom,
> userpolygon.the_geom)=0 then 1 end as myclass)),'NDR'),OID::text from
> sheet, userpolygon where sheet.the_geom && userpolygon.the_geom and
> sheet.seriesid=1 and userpolygon.id=2 ) as foo WHERE the_geom
> from(select sheet.the_geom as the_geom, userpolygon.the_geom as
> polygeom, sheet.oid ,case when sheet.seriesid=1 and distance
> (sheet.the_geom, userpolygon.the_geom)=0 then 1 end as myclass &&
> setSRID('BOX3D(-180 - 135,180 135)'::BOX3D, find_srid('','sheet,
> userpolygon where sheet.the_geom && userpolygon.the_geom and
> sheet.seriesid=1 and userpolygon.id=2 ) as foo','the_geom from (select
> sheet.the_geom as the_geom, userpolygon.the_geom as polygeom,
sheet.oid ,case when sheet.seriesid=1 and distance(sheet.the_geom,
userpolygon.the_geom)=0 then 1 end as myclass') )
> ERROR:  parser: parse error at or near "from"
> DEBUG:  AbortCurrentTransaction
>
> I tried it with the "using unique=table1.oid using srid=-1" on the end
> but I get a similar error:
>
> Warning: MapServer Error in msPOSTGISLayerWhichShapes(): prep_DB:Error
> executing
> POSTGIS DECLARE statement (0.6 failed - retried 0.5 and it failed
too). DECLARE mycursor
> BINARY CURSOR FOR SELECT asbinary(force_collection(force_2d(the_geom
from(select
> sheet.the_geom as the_geom, userpolygon.the_geom as polygeom,
sheet.oid ,case when
> sheet.seriesid=1 and distance(sheet.the_geom, userpolygon.the_geom)=0
then 1 end as
> myclass)),'NDR'),OID::text from sheet, userpolygon where
sheet.the_geom &&
> userpolygon.the_geom and sheet.seriesid=1 and userpolygon.id=2 ) as
foo using
> unique=table1.oid using srid=-1 WHERE the_geom from(select
sheet.the_geom as the_geom,
> userpolygon.the_geom as polygeom, sheet.oid ,case when
sheet.seriesid=1 and distance
> (sheet.the_geom, userpolygon.the_geom)=0 then 1 end as myclass &&
setSRID('BOX3D(-180 -
> 135,180 135)'::BOX3D, find_srid('','sheet, userpolygon where
sheet.the_geom &&
> userpolygon.the_geom and sheet.seriesid=1 and userpolygon.id=2 ) as
foo using
> unique=table1.oid using srid=-1','the_geom f in ...
>
> And Postgres gives the following error:
>
> DEBUG:  query: DECLARE mycursor BINARY CURSOR FOR SELECT
> asbinary(force_collection (force_2d(the_geom from(select
> sheet.the_geom as the_geom, userpolygon.the_geom as polygeom,
> sheet.oid ,case when sheet.seriesid=1 and distance(sheet.the_geom,
> userpolygon.the_geom)=0 then 1 end as myclass)),'NDR'),OID::text from
> sheet, userpolygon where sheet.the_geom && userpolygon.the_geom and
> sheet.seriesid=1 and userpolygon.id=2 ) as foo using unique=table1.oid
> using srid=-1 WHERE the_geom from(select sheet.the_geom as the_geom,
> userpolygon.the_geom as polygeom, sheet.oid ,case when
> sheet.seriesid=1 and distance(sheet.the_geom, userpolygon.the_geom)=0
then 1 end as myclass && 'BOX3D(-180 -135,180 135)'::BOX3D
> ERROR:  parser: parse error at or near "from"
>
> I can't figure out what "from" its complaining about. Any ideas?
>
> Thanks
>
> Benjamin
>
>
> -----Original Message-----
> From: Jan Hartmann [ <mailto:jhart at frw.uva.nl>
mailto:jhart at frw.uva.nl]
> Sent: Thursday, 20 February 2003 12:10 AM
> To: bwragg at tpg.com.au
> Cc: mapserver-users at lists.gis.umn.edu
> Subject: Re: [Mapserver-users] Classes based on Postgis && function
>
>
> And of course the inevitable typo: there should be a , after
> table1.oid
> in line 2.
>
> The general syntax is:
>
> select var1,var2,var3 from table
>
> Each variable can be replaced by a named expression, e.g.:
>
> (var1+var2) as computedsum
>
> or:
>
> case when <expression1> then result1
>       when <expression2> then result2
> end as computedvar
>
> The whole "case ... end as <name>" has to be be handled as a single
> variable. This means it should be surrounded by semicolons if preceded
> or followed by another variable.
>
> This is reasonably difficult to find in the docs. It's in the (7.2)
> user's guide, chapter 4 (functions and operators), section 12
> (conditional expressions). The complete URL for this section is:
>
>
<http://www.postgresql.org/docs/view.php?version=7.2&idoc=0&file=functi>
http://www.postgresql.org/docs/view.php?version=7.2&idoc=0&file=functi
> ons-conditional.html
>
> For other versions, start at the docs homepage
> ( <http://www.postgresql.org/docs/> http://www.postgresql.org/docs/),
or download the PDFs.
>
>
> As for your refined example, if I understand it correctly, the second
> class selects items that are already in the first one. This won't
work,
> neither in MapServer nor in PostgreSQL. Once MapServer or PG have
> assigned an item to a class, they will proceed with the next item. So
> the second class will never be reached. You could revert the classes
> (put the most restricted one first), or exclude explicitly the items
for
> class 2 from class 1:
>
> select case when type=1 and distance(geom1,geom2) > 0 ... then 1
>       case when type=1 and distance(geom1,geom2) = 0 then 2
> end as myvar
>
> In the second case however, "distance()" is called twice, and this is
> horribly inefficient. To do this efficiently, you would probably have
to
> write a stored procedure, e.g. in PL/PGSQL.
>
>
> Good luck,
>
> Jan
>
>
> Benjamin Wragg wrote:
>
>>Hi Jan,
>>
>>Thanks for your response. I tried putting the sql you provided into my
>>php script but received some errors regarding the 'case' word. I set
>>it up as shown below:
>>
>>...
>>$layer->set("data","the_geom from (select table1.the_geom from
>>     ( select table1.the_geom,table2.the_geom, table1.oid
>>         case when table1.fieldX = 'x' then 1
>>              when table1.the_geom && table2.the_geom then 2
>>         end as myclass
>>         from table1,table2
>>         where table1.id = table2.id
>>     ) as foo using unique=table1.oid using srid=-1)");
>>...
>>$layer->set("classitem", "myclass"); $class=ms_newClassObj($layer);
>>$class->set("expression", "1"); $class->set("name", "All");
>>$colorId=$GLOBALS['map']->addColor(255,0,0);
>>$class->set("color", $colorId);
>>$class->set("size", 20);
>>$class->set("symbol", 2);
>>
>>$class=ms_newClassObj($layer);
>>$class->set("expression", "2");
>>$class->set("name", "Other");
>>$colorId=$GLOBALS['map']->addColor(255,0,0);
>>$class->set("color", $colorId);
>>$class->set("size", 20);
>>$class->set("symbol", 2);
>>
>>It complains about the 'case' word. I can't find the 'case' statement
>>in any psql docs. Is it a psql or a mapscript statement?
>>
>>I thought I should refine what I trying to do a little better.  I'm
>>trying to display, in one class, all the features in a table where a
>>field equals a certain value. This will return a couple hundred
>>features. Then in a second class I'm trying to display all the
>>features where the same field equals this same value but only features
>>that are intersected by a feature in another table.
>>
>>So I need to get the following sql statement translated into a
>>layer/class setup in MapScript:
>>
>>      SELECT sheet.thegeom
>>      FROM sheet, userdefinedshape
>>      WHERE sheet.the_geom && userdefinedshape.the_geom
>>              AND distance(sheet.the_geom,
>>userdefinedshape.the_geom)=0
>>              AND userdefinedshape.id='1'
>>              AND sheet.type='something';
>>
>>I played around with your example but couldn't get my problem to fit.
>>Any suggestions?
>>
>>Thanks,
>>
>>Benjamin
>>
>>-----Original Message-----
>>From: mapserver-users-admin at lists.gis.umn.edu
>>[ <mailto:mapserver-users-admin at lists.gis.umn.edu>
mailto:mapserver-users-admin at lists.gis.umn.edu] On Behalf Of Jan
>>Hartmann
>>Sent: Tuesday, 18 February 2003 11:39 PM
>>To: bwragg at tpg.com.au
>>Cc: mapserver-users at lists.gis.umn.edu
>>Subject: Re: [Mapserver-users] Classes based on Postgis && function
>>
>>
>>Benjamin,
>>
>>You can use named computed variables in PostgreSQL:
>>
>>select table1.the_geom from
>>     ( select table1.the_geom,table2.the_geom, table1.oid
>>         case when table1.fieldX = 'x' then 1
>>              when table1.the_geom && table2.the_geom then 2
>>         end as myclass
>>         from table1,table2
>>         where table1.id = table2.id
>>     ) as foo using unique=table1.oid using srid=...
>>
>>after which you can use the new computed variable "myclass" as a
>>regular
>>
>>MapServer CLASSITEM. The computed values of this variable go into the
>>EXPRESSION statement:
>>
>>CLASSITEM myclass
>>CLASS
>>    EXPRESSION 1
>>    COLOR ...
>>    ...
>>END
>>CLASS
>>    EXPRESSION 2
>>    COLOR ...
>>    ...
>>END
>>
>>Jan
>>
>>Benjamin Wragg wrote:
>>
>>
>>>Hi guys,
>>>
>>>Does anyone know if its possible to have a layer with two classes,
>>>one with a simple SQL expression and the other with a SQL statement
>>>that uses a PostGIS spatial function. Something like the following
>>>logic:
>>>
>>>class 1 = where table1.fieldX = 'x'
>>>class 2 = where table1.fieldX = 'x' and table1.the_geom &&
>>>table2.the_geom and table2.fieldY='Y'
>>>
>>>I can do it in two layers, but is it possible in one layer with two
>>>classes?
>>>
>>>Cheers,
>>>
>>>Benjamin
>>
>>
>>Jan Hartmann
>>Department of Geography
>>University of Amsterdam
>>jhart at frw.uva.nl
>>
>>_______________________________________________
>>Mapserver-users mailing list Mapserver-users at lists.gis.umn.edu
>> <http://lists.gis.umn.edu/mailman/listinfo/mapserver-users>
http://lists.gis.umn.edu/mailman/listinfo/mapserver-users
>>
>>
>
>
> Jan Hartmann
> Department of Geography
> University of Amsterdam
> jhart at frw.uva.nl
>
Jan Hartmann
Department of Geography
University of Amsterdam
jhart at frw.uva.nl

_______________________________________________
Mapserver-users mailing list
Mapserver-users at lists.gis.umn.edu
<http://lists.gis.umn.edu/mailman/listinfo/mapserver-users>
http://lists.gis.umn.edu/mailman/listinfo/mapserver-users



------=_NextPart_000_0001_01C2DA08.D6F047E0
Content-Type: text/html;
	charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3Dus-ascii">
<TITLE>Message</TITLE>

<META content=3D"MSHTML 6.00.2800.1106" name=3DGENERATOR></HEAD>
<BODY><!-- Converted from text/plain format -->
<P><FONT size=3D2><FONT face=3DTahoma><FONT face=3DArial>I've tried the =
code on=20
another machine, which has the 3.6.4 release and I get a slightly better =

error:</FONT><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR></FONT><FO=
NT=20
face=3DCourier>Warning: MapServer Error in prep_DB(): Error executing =
POSTGIS=20
DECLARE (the actual query) statement: 'DECLARE mycursor BINARY CURSOR =
FOR SELECT=20
sbinary(force_collection(force_2d(the_geom from(select sheet.the_geom as =

the_geom, userpolygon.the_geom as polygeom, sheet.oid, case when=20
sheet.seriesid=3D1 and distance(sheet.the_geom, =
userpolygon.the_geom)=3D0 then 1 end=20
as myclass)),'NDR'),OID::text from sheet,userpolygon where =
sheet.the_geom=20
&amp;&amp; userpolygon.the_geom and sheet.seriesid=3D1 and =
userpolygon.id=3D2) as=20
foo using UNIQUE=3Dsheet.oid WHERE the_geom from(select sheet.the_geom =
as=20
the_geom, userpolygon.the_geom as polygeom, sheet.oid, case when=20
sheet.seriesid=3D1 and distance(sheet.the_geom,userpolygon.the_geom)=3D0 =
then 1 end=20
as myclass &amp;&amp; setSRID('BOX3D(-180 -135,180 135)'::BOX3D, -1=20
)'<BR><BR>Postgresql reports the error 'ERROR: parser: parse error at or =
near=20
"from" '<BR><BR>More Help:<BR><BR>Error with POSTGIS data variable. You=20
specified '&lt;check your .map file&gt;'.<BR>Standard ways of =
specifiying are=20
:</FONT></FONT></P>
<P><FONT size=3D2><FONT face=3DArial>The 'from' part is the same error =
that the=20
database gives back. I copied the above sql and ran in over the database =
and it=20
seems to be&nbsp;complaining about the first 'from'.</FONT></FONT></P>
<P><FONT size=3D2><FONT face=3DArial color=3D#0000ff>Any=20
suggestions?</FONT></FONT></P>
<P><FONT size=3D2><FONT face=3DArial =
color=3D#0000ff>Thanks,</FONT></FONT></P>
<P><FONT size=3D2><FONT face=3DArial color=3D#0000ff>Benjamin</FONT></P>
<P><BR><BR><BR><BR>-----Original Message-----<BR>From:=20
mapserver-users-admin at lists.gis.umn.edu [</FONT><A=20
href=3D"mailto:mapserver-users-admin at lists.gis.umn.edu"><FONT=20
size=3D2>mailto:mapserver-users-admin at lists.gis.umn.edu</FONT></A><FONT =
size=3D2>]=20
On Behalf Of Jan Hartmann<BR>Sent: Thursday, 20 February 2003 10:43 =
PM<BR>To:=20
mapserver-users at lists.gis.umn.edu<BR>Subject: Re: [Mapserver-users] =
Classes=20
based on Postgis &amp;&amp; function<BR><BR><BR>I reformatted your =
select=20
statement (always a good idea with SQL that<BR>complex), and it looks=20
OK.<BR><BR><BR>select the_geom from<BR>&nbsp;&nbsp;&nbsp; (select =
sheet.the_geom=20
as=20
the_geom,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;=20
userpolygon.the_geom as=20
polygeom,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;=20
sheet.oid,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;=20
case when=20
sheet.seriesid=3D1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
and=20
distance(sheet.the_geom,userpolygon.the_geom)=3D0<BR>&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
then 1 end as myclass<BR>&nbsp;&nbsp;&nbsp;&nbsp; from sheet,=20
userpolygon<BR>&nbsp;&nbsp;&nbsp;&nbsp; where sheet.the_geom &amp;&amp;=20
userpolygon.the_geom<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; and=20
sheet.seriesid=3D1<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; and=20
userpolygon.id=3D2<BR>&nbsp;&nbsp;&nbsp;&nbsp; ) as foo<BR><BR><BR>If, =
as you say,=20
PSQL accepts it, the problem must be with<BR>MapServer/PostGIS. This =
could very=20
well be the case, as you seem to be<BR>using an old version of =
mappostgis.c,=20
which had problems with subselects<BR>from non-georeferenced data =
(SRID=3D-1).=20
There have been quite a few<BR>postings on the MapServer and PostGIS =
mailing=20
lists on this. So perhaps<BR>upgrading to the newest version will give =
you a map=20
at last.<BR><BR>Jan<BR><BR><BR>bwragg at tpg.com.au wrote:<BR>&gt; Thanks =
for=20
pointing out the problems with my logic. I've decided to<BR>&gt; put the =
second=20
class into<BR>&gt; its own layer. I've got the following sql working and =

returning the right thing from psql:<BR>&gt;<BR>&gt; select the_geom =
from(select=20
sheet.the_geom as the_geom,<BR>&gt; userpolygon.the_geom as<BR>&gt; =
polygeom,=20
sheet.oid ,case when sheet.seriesid=3D1 and =
distance(sheet.the_geom,<BR>&gt;=20
userpolygon.the_geom)=3D0 then 1 end as myclass from sheet, userpolygon=20
where<BR>&gt; sheet.the_geom &amp;&amp; userpolygon.the_geom and=20
sheet.seriesid=3D1 and userpolygon.id=3D2 ) as<BR>&gt; =
foo<BR>&gt;<BR>&gt; If I add=20
this to the data property of a mapobj it crashes with the<BR>&gt; =
following=20
error on the<BR>&gt; mapObj-&gt;draw() function.<BR>&gt;<BR>&gt; =
Warning:=20
MapServer Error in msPOSTGISLayerWhichShapes(): prep_DB:Error<BR>&gt;=20
executing<BR>&gt; POSTGIS DECLARE statement (0.6 failed - retried 0.5 =
and it=20
failed too). DECLARE mycursor<BR>&gt; BINARY CURSOR FOR SELECT=20
asbinary(force_collection(force_2d(the_geom from(select<BR>&gt; =
sheet.the_geom=20
as the_geom, userpolygon.the_geom as polygeom, sheet.oid ,case =
when<BR>&gt;=20
sheet.seriesid=3D1 and distance(sheet.the_geom, =
userpolygon.the_geom)=3D0 then 1 end=20
as<BR>&gt; myclass)),'NDR'),OID::text from sheet, userpolygon where=20
sheet.the_geom &amp;&amp;<BR>&gt; userpolygon.the_geom and =
sheet.seriesid=3D1 and=20
userpolygon.id=3D2 ) as foo WHERE the_geom<BR>&gt; from(select =
sheet.the_geom as=20
the_geom, userpolygon.the_geom as polygeom,<BR>&gt; sheet.oid ,case when =

sheet.seriesid=3D1 and distance(sheet.the_geom, =
userpolygon.the_geom)<BR>&gt; =3D0=20
then 1 end as myclass &amp;&amp; setSRID('BOX3D(-180 -135,180 =
135)'::BOX3D,=20
find_srid('','sheet,<BR>&gt; userpolygon where sheet.the_geom &amp;&amp; =

userpolygon.the_geom and sheet.seriesid=3D1 and<BR>&gt; =
userpolygon.id=3D2 ) as=20
foo','the_geom from(select sheet.the_geom as the_geom,<BR>&gt;=20
userpolygon.the_geom as polygeom, she in...<BR>&gt;<BR>&gt; Postgres =
gives the=20
following error at this same point:<BR>&gt;<BR>&gt; DEBUG:&nbsp; query: =
DECLARE=20
mycursor BINARY CURSOR FOR SELECT<BR>&gt; asbinary(force_collection=20
(force_2d(the_geom from(select<BR>&gt; sheet.the_geom as the_geom,=20
userpolygon.the_geom as polygeom,<BR>&gt; sheet.oid ,case when =
sheet.seriesid=3D1=20
and distance(sheet.the_geom,<BR>&gt; userpolygon.the_geom)=3D0 then 1 =
end as=20
myclass)),'NDR'),OID::text from<BR>&gt; sheet, userpolygon where =
sheet.the_geom=20
&amp;&amp; userpolygon.the_geom and<BR>&gt; sheet.seriesid=3D1 and=20
userpolygon.id=3D2 ) as foo WHERE the_geom<BR>&gt; from(select =
sheet.the_geom as=20
the_geom, userpolygon.the_geom as<BR>&gt; polygeom, sheet.oid ,case when =

sheet.seriesid=3D1 and distance<BR>&gt; (sheet.the_geom, =
userpolygon.the_geom)=3D0=20
then 1 end as myclass &amp;&amp;<BR>&gt; setSRID('BOX3D(-180 - 135,180=20
135)'::BOX3D, find_srid('','sheet,<BR>&gt; userpolygon where =
sheet.the_geom=20
&amp;&amp; userpolygon.the_geom and<BR>&gt; sheet.seriesid=3D1 and=20
userpolygon.id=3D2 ) as foo','the_geom from (select<BR>&gt; =
sheet.the_geom as=20
the_geom, userpolygon.the_geom as polygeom, sheet.oid ,case when=20
sheet.seriesid=3D1 and distance(sheet.the_geom, =
userpolygon.the_geom)=3D0 then 1 end=20
as myclass') )<BR>&gt; ERROR:&nbsp; parser: parse error at or near=20
"from"<BR>&gt; DEBUG:&nbsp; AbortCurrentTransaction<BR>&gt;<BR>&gt; I =
tried it=20
with the "using unique=3Dtable1.oid using srid=3D-1" on the end<BR>&gt; =
but I get a=20
similar error:<BR>&gt;<BR>&gt; Warning: MapServer Error in=20
msPOSTGISLayerWhichShapes(): prep_DB:Error<BR>&gt; executing<BR>&gt; =
POSTGIS=20
DECLARE statement (0.6 failed - retried 0.5 and it failed too). DECLARE=20
mycursor<BR>&gt; BINARY CURSOR FOR SELECT=20
asbinary(force_collection(force_2d(the_geom from(select<BR>&gt; =
sheet.the_geom=20
as the_geom, userpolygon.the_geom as polygeom, sheet.oid ,case =
when<BR>&gt;=20
sheet.seriesid=3D1 and distance(sheet.the_geom, =
userpolygon.the_geom)=3D0 then 1 end=20
as<BR>&gt; myclass)),'NDR'),OID::text from sheet, userpolygon where=20
sheet.the_geom &amp;&amp;<BR>&gt; userpolygon.the_geom and =
sheet.seriesid=3D1 and=20
userpolygon.id=3D2 ) as foo using<BR>&gt; unique=3Dtable1.oid using =
srid=3D-1 WHERE=20
the_geom from(select sheet.the_geom as the_geom,<BR>&gt; =
userpolygon.the_geom as=20
polygeom, sheet.oid ,case when sheet.seriesid=3D1 and distance<BR>&gt;=20
(sheet.the_geom, userpolygon.the_geom)=3D0 then 1 end as myclass =
&amp;&amp;=20
setSRID('BOX3D(-180 -<BR>&gt; 135,180 135)'::BOX3D, find_srid('','sheet, =

userpolygon where sheet.the_geom &amp;&amp;<BR>&gt; userpolygon.the_geom =
and=20
sheet.seriesid=3D1 and userpolygon.id=3D2 ) as foo using<BR>&gt; =
unique=3Dtable1.oid=20
using srid=3D-1','the_geom f in ...<BR>&gt;<BR>&gt; And Postgres gives =
the=20
following error:<BR>&gt;<BR>&gt; DEBUG:&nbsp; query: DECLARE mycursor =
BINARY=20
CURSOR FOR SELECT<BR>&gt; asbinary(force_collection (force_2d(the_geom=20
from(select<BR>&gt; sheet.the_geom as the_geom, userpolygon.the_geom as=20
polygeom,<BR>&gt; sheet.oid ,case when sheet.seriesid=3D1 and=20
distance(sheet.the_geom,<BR>&gt; userpolygon.the_geom)=3D0 then 1 end as =

myclass)),'NDR'),OID::text from<BR>&gt; sheet, userpolygon where =
sheet.the_geom=20
&amp;&amp; userpolygon.the_geom and<BR>&gt; sheet.seriesid=3D1 and=20
userpolygon.id=3D2 ) as foo using unique=3Dtable1.oid<BR>&gt; using =
srid=3D-1 WHERE=20
the_geom from(select sheet.the_geom as the_geom,<BR>&gt; =
userpolygon.the_geom as=20
polygeom, sheet.oid ,case when<BR>&gt; sheet.seriesid=3D1 and=20
distance(sheet.the_geom, userpolygon.the_geom)=3D0 then 1 end as myclass =

&amp;&amp; 'BOX3D(-180 -135,180 135)'::BOX3D<BR>&gt; ERROR:&nbsp; =
parser: parse=20
error at or near "from"<BR>&gt;<BR>&gt; I can't figure out what "from" =
its=20
complaining about. Any ideas?<BR>&gt;<BR>&gt; Thanks<BR>&gt;<BR>&gt;=20
Benjamin<BR>&gt;<BR>&gt;<BR>&gt; -----Original Message-----<BR>&gt; =
From: Jan=20
Hartmann [</FONT><A href=3D"mailto:jhart at frw.uva.nl"><FONT=20
size=3D2>mailto:jhart at frw.uva.nl</FONT></A><FONT size=3D2>]<BR>&gt; =
Sent: Thursday,=20
20 February 2003 12:10 AM<BR>&gt; To: bwragg at tpg.com.au<BR>&gt; Cc:=20
mapserver-users at lists.gis.umn.edu<BR>&gt; Subject: Re: [Mapserver-users] =
Classes=20
based on Postgis &amp;&amp; function<BR>&gt;<BR>&gt;<BR>&gt; And of =
course the=20
inevitable typo: there should be a , after<BR>&gt; table1.oid<BR>&gt; in =
line=20
2.<BR>&gt;<BR>&gt; The general syntax is:<BR>&gt;<BR>&gt; select =
var1,var2,var3=20
from table<BR>&gt;<BR>&gt; Each variable can be replaced by a named =
expression,=20
e.g.:<BR>&gt;<BR>&gt; (var1+var2) as computedsum<BR>&gt;<BR>&gt;=20
or:<BR>&gt;<BR>&gt; case when &lt;expression1&gt; then=20
result1<BR>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; when =
&lt;expression2&gt;=20
then result2<BR>&gt; end as computedvar<BR>&gt;<BR>&gt; The whole "case =
... end=20
as &lt;name&gt;" has to be be handled as a single<BR>&gt; variable. This =
means=20
it should be surrounded by semicolons if preceded<BR>&gt; or followed by =
another=20
variable.<BR>&gt;<BR>&gt; This is reasonably difficult to find in the =
docs. It's=20
in the (7.2)<BR>&gt; user's guide, chapter 4 (functions and operators), =
section=20
12<BR>&gt; (conditional expressions). The complete URL for this section=20
is:<BR>&gt;<BR>&gt; </FONT><A=20
href=3D"http://www.postgresql.org/docs/view.php?version=3D7.2&amp;idoc=3D=
0&amp;file=3Dfuncti"><FONT=20
size=3D2>http://www.postgresql.org/docs/view.php?version=3D7.2&amp;idoc=3D=
0&amp;file=3Dfuncti</FONT></A><BR><FONT=20
size=3D2>&gt; ons-conditional.html<BR>&gt;<BR>&gt; For other versions, =
start at=20
the docs homepage<BR>&gt; (</FONT><A=20
href=3D"http://www.postgresql.org/docs/"><FONT=20
size=3D2>http://www.postgresql.org/docs/</FONT></A><FONT size=3D2>), or =
download the=20
PDFs.<BR>&gt;<BR>&gt;<BR>&gt; As for your refined example, if I =
understand it=20
correctly, the second<BR>&gt; class selects items that are already in =
the first=20
one. This won't work,<BR>&gt; neither in MapServer nor in PostgreSQL. =
Once=20
MapServer or PG have<BR>&gt; assigned an item to a class, they will =
proceed with=20
the next item. So<BR>&gt; the second class will never be reached. You =
could=20
revert the classes<BR>&gt; (put the most restricted one first), or =
exclude=20
explicitly the items for<BR>&gt; class 2 from class 1:<BR>&gt;<BR>&gt; =
select=20
case when type=3D1 and distance(geom1,geom2) &gt; 0 ... then 1<BR>&gt;=20
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case when type=3D1 and =
distance(geom1,geom2) =3D 0=20
then 2<BR>&gt; end as myvar<BR>&gt;<BR>&gt; In the second case however,=20
"distance()" is called twice, and this is<BR>&gt; horribly inefficient. =
To do=20
this efficiently, you would probably have to<BR>&gt; write a stored =
procedure,=20
e.g. in PL/PGSQL.<BR>&gt;<BR>&gt;<BR>&gt; Good luck,<BR>&gt;<BR>&gt;=20
Jan<BR>&gt;<BR>&gt;<BR>&gt; Benjamin Wragg wrote:<BR>&gt;<BR>&gt;&gt;Hi=20
Jan,<BR>&gt;&gt;<BR>&gt;&gt;Thanks for your response. I tried putting =
the sql=20
you provided into my<BR>&gt;&gt;php script but received some errors =
regarding=20
the 'case' word. I set<BR>&gt;&gt;it up as shown=20
below:<BR>&gt;&gt;<BR>&gt;&gt;...<BR>&gt;&gt;$layer-&gt;set("data","the_g=
eom=20
from (select table1.the_geom from<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; ( =
select=20
table1.the_geom,table2.the_geom,=20
table1.oid<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
case when=20
table1.fieldX =3D 'x' then=20
1<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;=20
when table1.the_geom &amp;&amp; table2.the_geom then=20
2<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end as=20
myclass<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; from =

table1,table2<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
 where=20
table1.id =3D table2.id<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; ) as foo =
using=20
unique=3Dtable1.oid using=20
srid=3D-1)");<BR>&gt;&gt;...<BR>&gt;&gt;$layer-&gt;set("classitem", =
"myclass");=20
$class=3Dms_newClassObj($layer);<BR>&gt;&gt;$class-&gt;set("expression", =
"1");=20
$class-&gt;set("name",=20
"All");<BR>&gt;&gt;$colorId=3D$GLOBALS['map']-&gt;addColor(255,0,0);<BR>&=
gt;&gt;$class-&gt;set("color",=20
$colorId);<BR>&gt;&gt;$class-&gt;set("size",=20
20);<BR>&gt;&gt;$class-&gt;set("symbol",=20
2);<BR>&gt;&gt;<BR>&gt;&gt;$class=3Dms_newClassObj($layer);<BR>&gt;&gt;$c=
lass-&gt;set("expression",=20
"2");<BR>&gt;&gt;$class-&gt;set("name",=20
"Other");<BR>&gt;&gt;$colorId=3D$GLOBALS['map']-&gt;addColor(255,0,0);<BR=
>&gt;&gt;$class-&gt;set("color",=20
$colorId);<BR>&gt;&gt;$class-&gt;set("size",=20
20);<BR>&gt;&gt;$class-&gt;set("symbol", 2);<BR>&gt;&gt;<BR>&gt;&gt;It =
complains=20
about the 'case' word. I can't find the 'case' statement<BR>&gt;&gt;in =
any psql=20
docs. Is it a psql or a mapscript statement?<BR>&gt;&gt;<BR>&gt;&gt;I =
thought I=20
should refine what I trying to do a little better.&nbsp; =
I'm<BR>&gt;&gt;trying=20
to display, in one class, all the features in a table where =
a<BR>&gt;&gt;field=20
equals a certain value. This will return a couple =
hundred<BR>&gt;&gt;features.=20
Then in a second class I'm trying to display all the<BR>&gt;&gt;features =
where=20
the same field equals this same value but only features<BR>&gt;&gt;that =
are=20
intersected by a feature in another table.<BR>&gt;&gt;<BR>&gt;&gt;So I =
need to=20
get the following sql statement translated into a<BR>&gt;&gt;layer/class =
setup=20
in MapScript:<BR>&gt;&gt;<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
SELECT=20
sheet.thegeom<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FROM sheet,=20
userdefinedshape<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WHERE =
sheet.the_geom=20
&amp;&amp; =
userdefinedshape.the_geom<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AND=20
distance(sheet.the_geom,<BR>&gt;&gt;userdefinedshape.the_geom)=3D0<BR>&gt=
;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AND=20
userdefinedshape.id=3D'1'<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AND=20
sheet.type=3D'something';<BR>&gt;&gt;<BR>&gt;&gt;I played around with =
your example=20
but couldn't get my problem to fit.<BR>&gt;&gt;Any=20
suggestions?<BR>&gt;&gt;<BR>&gt;&gt;Thanks,<BR>&gt;&gt;<BR>&gt;&gt;Benjam=
in<BR>&gt;&gt;<BR>&gt;&gt;-----Original=20
Message-----<BR>&gt;&gt;From:=20
mapserver-users-admin at lists.gis.umn.edu<BR>&gt;&gt;[</FONT><A=20
href=3D"mailto:mapserver-users-admin at lists.gis.umn.edu"><FONT=20
size=3D2>mailto:mapserver-users-admin at lists.gis.umn.edu</FONT></A><FONT =
size=3D2>]=20
On Behalf Of Jan<BR>&gt;&gt;Hartmann<BR>&gt;&gt;Sent: Tuesday, 18 =
February 2003=20
11:39 PM<BR>&gt;&gt;To: bwragg at tpg.com.au<BR>&gt;&gt;Cc:=20
mapserver-users at lists.gis.umn.edu<BR>&gt;&gt;Subject: Re: =
[Mapserver-users]=20
Classes based on Postgis &amp;&amp;=20
function<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;Benjamin,<BR>&gt;&gt;<BR>&gt;=
&gt;You=20
can use named computed variables in =
PostgreSQL:<BR>&gt;&gt;<BR>&gt;&gt;select=20
table1.the_geom from<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; ( select=20
table1.the_geom,table2.the_geom,=20
table1.oid<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
case when=20
table1.fieldX =3D 'x' then=20
1<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;=20
when table1.the_geom &amp;&amp; table2.the_geom then=20
2<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end as=20
myclass<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; from =

table1,table2<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
 where=20
table1.id =3D table2.id<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;&nbsp; ) as foo =
using=20
unique=3Dtable1.oid using srid=3D...<BR>&gt;&gt;<BR>&gt;&gt;after which =
you can use=20
the new computed variable "myclass" as=20
a<BR>&gt;&gt;regular<BR>&gt;&gt;<BR>&gt;&gt;MapServer CLASSITEM. The =
computed=20
values of this variable go into the<BR>&gt;&gt;EXPRESSION=20
statement:<BR>&gt;&gt;<BR>&gt;&gt;CLASSITEM=20
myclass<BR>&gt;&gt;CLASS<BR>&gt;&gt;&nbsp;&nbsp;&nbsp; EXPRESSION=20
1<BR>&gt;&gt;&nbsp;&nbsp;&nbsp; COLOR ...<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;=20
...<BR>&gt;&gt;END<BR>&gt;&gt;CLASS<BR>&gt;&gt;&nbsp;&nbsp;&nbsp; =
EXPRESSION=20
2<BR>&gt;&gt;&nbsp;&nbsp;&nbsp; COLOR ...<BR>&gt;&gt;&nbsp;&nbsp;&nbsp;=20
...<BR>&gt;&gt;END<BR>&gt;&gt;<BR>&gt;&gt;Jan<BR>&gt;&gt;<BR>&gt;&gt;Benj=
amin=20
Wragg wrote:<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;&gt;Hi=20
guys,<BR>&gt;&gt;&gt;<BR>&gt;&gt;&gt;Does anyone know if its possible to =
have a=20
layer with two classes,<BR>&gt;&gt;&gt;one with a simple SQL expression =
and the=20
other with a SQL statement<BR>&gt;&gt;&gt;that uses a PostGIS spatial =
function.=20
Something like the=20
following<BR>&gt;&gt;&gt;logic:<BR>&gt;&gt;&gt;<BR>&gt;&gt;&gt;class 1 =
=3D where=20
table1.fieldX =3D 'x'<BR>&gt;&gt;&gt;class 2 =3D where table1.fieldX =3D =
'x' and=20
table1.the_geom &amp;&amp;<BR>&gt;&gt;&gt;table2.the_geom and=20
table2.fieldY=3D'Y'<BR>&gt;&gt;&gt;<BR>&gt;&gt;&gt;I can do it in two =
layers, but=20
is it possible in one layer with=20
two<BR>&gt;&gt;&gt;classes?<BR>&gt;&gt;&gt;<BR>&gt;&gt;&gt;Cheers,<BR>&gt=
;&gt;&gt;<BR>&gt;&gt;&gt;Benjamin<BR>&gt;&gt;<BR>&gt;&gt;<BR>&gt;&gt;Jan =

Hartmann<BR>&gt;&gt;Department of Geography<BR>&gt;&gt;University of=20
Amsterdam<BR>&gt;&gt;jhart at frw.uva.nl<BR>&gt;&gt;<BR>&gt;&gt;____________=
___________________________________<BR>&gt;&gt;Mapserver-users=20
mailing list Mapserver-users at lists.gis.umn.edu<BR>&gt;&gt;</FONT><A=20
href=3D"http://lists.gis.umn.edu/mailman/listinfo/mapserver-users"><FONT =

size=3D2>http://lists.gis.umn.edu/mailman/listinfo/mapserver-users</FONT>=
</A><BR><FONT=20
size=3D2>&gt;&gt;<BR>&gt;&gt;<BR>&gt;<BR>&gt;<BR>&gt; Jan =
Hartmann<BR>&gt;=20
Department of Geography<BR>&gt; University of Amsterdam<BR>&gt;=20
jhart at frw.uva.nl<BR>&gt;<BR>Jan Hartmann<BR>Department of=20
Geography<BR>University of=20
Amsterdam<BR>jhart at frw.uva.nl<BR><BR>____________________________________=
___________<BR>Mapserver-users=20
mailing list<BR>Mapserver-users at lists.gis.umn.edu </FONT><A=20
href=3D"http://lists.gis.umn.edu/mailman/listinfo/mapserver-users"><FONT =

size=3D2>http://lists.gis.umn.edu/mailman/listinfo/mapserver-users</FONT>=
</A><BR></P></BODY></HTML>

------=_NextPart_000_0001_01C2DA08.D6F047E0--




More information about the mapserver-users mailing list