Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

UPDATE: 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:

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.

share|improve this question
up vote 2 down vote accepted

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)

share|improve this answer
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

I have been trying to figure out how to send a filter to geoserver using wfs.

The filter that I want to send is:

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

I have a WFS protocol and i would like to attach that filter with this request:

                wfsProtocol = new OpenLayers.Protocol.WFS({
                            url: "http://localhost:8080/geoserver/wfs",
                            featureType: ["tasmania_water_bodies"],
                            featureNS: ""

            wfsLayer = new OpenLayers.Layer.Vector("LinesWFS", {
                        strategies: [new OpenLayers.Strategy.Fixed()],
                        protocol: wfsProtocol

I have tried converting it to a filter using this:

            parser = new OpenLayers.Format.Filter.v1_1_0 ();
            xml = new OpenLayers.Format.XML();
            my_filter_xml = fil; //your filter string

            var x =
            var filter1 =;

That works for very basic xml filters, but not the one that I want to convert.

Am I doing it the wrong way?

share|improve this answer
I think you need to ask this as a separate question. From the outset, the filter is in GML/XML. So, you may need to convert your WFS request to GML/XML as well. Otherwise, convert the filter into a format compatible with the way your are constructing your WFS request. – okello Jul 22 '12 at 7:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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