1

I'm quite new to ogc webservices and I need some guidance. I have a shapefile in geoserver lmes.shp (WMS, WCS and WFS are activated), with attribute LME_ID for each polygon of lmes.shp. I need to write a function that will query the layer lmes for a given LME_ID and get back the polygon bounding box. Please note I'm not looking for an event-handler (I don't want to select the polygon by clicking on it, I just need to pass LME_ID to my function).

Which web service should I use (WFS I suppose) and what openlayers function is the most appropriate? An example would help!

1
  • Just some hints: you can use WFS service, and add filtering condition to your query. Since you're using Geoserver, you can use CQL_FILTER, something like CQL_FILTER=LME_ID=123 Commented Oct 2, 2013 at 14:25

1 Answer 1

2

You do need to use WFS in order to get the feature matching a given LME_ID value. Most WFS implementations would require you to specify an ogc:Filter, however as user1702401 points out GeoServer allows you to specify a CQL_FILTER parameter on the WFS request which simplifies things considerably.

For this to work then your function would need to do the following things:

  1. Issue a WFS GetFeature request using a CQL_FILTER parameter
  2. Read the response using an OpenLayers.Format, most likely OpenLayers.Format.GML
  3. Get the geometry of the returned feature and get its bounding box
  4. Return the bounding box

Here is a function that might work, feel free to tweak it for your own needs.

function getBoundingBox(lmeID) {
    // Create an appropriate request url, change to your own server and set the namespace:featuretype to the correct value for your lmes feature type
    var wfsBaseUrl = 'http://your_server.com:your_port/geoserver/wfs?service=wfs&version=1.0.0&request=GetFeature&typename=namespace:featuretype&outputFormat=GML2&cql_filter=LME_ID=';

    // Make the request using OpenLayers Request object, this could also be done with something like jQuery
    OpenLayers.Request.GET({
        url: wfsUrl + lmeID,
        success: function(response) {
            // Use a format to read the response, set any properties you need
            var format = new OpenLayers.Format.GML();

            // Read the response from the server
            var features = format.read(response.responseText);

            // Get the first item (we should only expect one but you should do some error trapping here) and return its bounding box
            return features[0].geometry.getBounds();
        },
        failure: function() {
            // Do something on failure, return null perhaps
            return null;
        }
    });
}

You will need to adapt this and put a lot more error trapping in there to make sure you handle failed responses correctly, or responses where you get more than one feature back. In this case the return will be an OpenLayers.Bounds object which you can then work with further, however you could return something else by using one of the methods on the OpenLayers.Bounds object.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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