WFS Requests – How to Pass Selected Polygon to INTERSECTS Filter

filtergeoserveropenlayers-2wfs

In the code below,

   <ogc:Filter>
     <ogc:Intersects>
       <ogc:PropertyName>the_geom</ogc:PropertyName>
       <ogc:Function name="querySingle">
          <ogc:Literal>sf:restricted</ogc:Literal>
          <ogc:Literal>the_geom</ogc:Literal>
          <ogc:Literal>cat = 3</ogc:Literal>
       </ogc:Function>
     </ogc:Intersects>
   </ogc:Filter>

I'd like to replace

   <ogc:Literal>sf:restricted</ogc:Literal>
   <ogc:Literal>the_geom</ogc:Literal>
   <ogc:Literal>cat = 3</ogc:Literal>

with a polygon selected by a user. I get the polygon from a selection event. I've tried a number of options, but none is working, yet. Any assistance will be highly appreciated.

My environment: GeoServer 2.1.3, OpenLayers 2.11.


I've transformed my geometry to EPSG:4326, the one used by my layers in GeoServer:

var geometry = event.feature.geometry.transform(new OpenLayers.Projection("900913"), new OpenLayers.Projection("EPSG:4326"));

The event.feature.geometry represents a polygon selected by a user.
I've also converted the geometry to WKT format as shown below:

var wktParser = new OpenLayers.Format.WKT();
var feature = new OpenLayers.Feature.Vector();
feature.geometry = geometry;

var wktGeom = wktParser.write(feature);

I've then passed the geometry in WKT format to the filter as shown below:

'<ogc:Filter>' +
'<ogc:Intersects>' +
    '<ogc:PropertyName>the_geom</ogc:PropertyName>' +
    '<ogc:Function name="querySingle">' +
        '<ogc:Literal>ws:layer_2</ogc:Literal>' +
        '<ogc:Literal>the_geom</ogc:Literal>' +
        '<ogc:Literal>INTERSECTS(the_geom,' + wktGeom + ')</ogc:Literal>' +
    '</ogc:Function>' +
'</ogc:Intersects>' +
'</ogc:Filter>';

But when I send a request, the following error is returned:

<ows:ExceptionText>Process failed during execution
java.lang.ClassCastException: org.geotools.filter.AttributeExpressionImpl cannot be cast to org.opengis.filter.Filter
org.geotools.filter.AttributeExpressionImpl cannot be cast to org.opengis.filter.Filter</ows:ExceptionText>
.

Any idea on what I may be doing wrong?

Best Answer

http://docs.geotools.org/latest/userguide/library/cql/cql.html

So the third parameter would be something like:

INTERSECTS(ATTR1, POLYGON((... )) )

The polygon has to be expressed in the WKT syntax and be in the same SRS as the layer being queried. Either that, or get a recent GeoServer version and call setSRS(POLYGON(...), 'EPSG:xywz') around the WKT (that function has been added May 27 so it should be in 2.1.4 but not earlier versions)

Related Question