In the code below,

       <ogc:Function name="querySingle">
          <ogc:Literal>cat = 3</ogc:Literal>

I'd like to replace

   <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>' +

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?



So the third parameter would be something like:


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)

  • Thanks Andrea. I've tried your suggestion: I've transformed my projection and converted the polygon to wkt representation. Refer to the updated question above. But I get a ClassCastException when I send a request. Any idea on what I may be doing wrong? – okello Jul 16 '12 at 13:41
  • You added single quotes around the geometry WTK, there should be none. Not sure this is the reason for the issue though – Andrea Aime Jul 16 '12 at 21:28
  • Thanks for the response. Actually, its not a quote. Rather, I'm concatenating strings, and passing in the value of wktGeom. – okello Jul 17 '12 at 6:01
  • Its now working. I was using the inbuilt toWKT() function. It worked when I manually did the conversion to WKT. Thanks. – okello Jul 17 '12 at 8:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.