3

I want to be able to create a layer on init, then load features from a WFS protocol afterward. I'm having trouble doing this.

Using WFS protocol in vector constructor works fine and is not my problem: http://dev.openlayers.org/releases/OpenLayers-2.10/examples/wfs-filter.js

What i want to do is Add a layer like such:

var layer = new OpenLayers.Layer.Vector("Results", {
    projection: new OpenLayers.Projection("EPSG:4326")
});

Then Query it using the WFS Protocol.... this is what i can't get to work... what i have so far is:

var protocol = new OpenLayers.Protocol.WFS({
    version: "1.1.0",
    url: MapBase.Configuration.GeoServer.WFS_Service.EndPointURL,
    featurePrefix: MapBase.Configuration.GeoServer.WFS_Service.featurePrefix,
    featureType: MapBase.Configuration.GeoServer.WFS_Service.featureType,
    featureNS: MapBase.Configuration.GeoServer.WFS_Service.featureNS,
    geometryName: MapBase.Configuration.GeoServer.WFS_Service.geometryName
});


var response = protocol.read({
    maxFeatures: Search.Properties.maxResults,
    callback: function (resp) {
        console.log(resp);
        layer.addFeatures(resp.features);
    }
});

resp.features gets populated fine. My main problem is I can't find where to specify my filter and projection as i can in the Vector constructor. Even though i specified the projection in the vector constructor, when i addFeatures from my WFS response, they come back in a different projection because they end up really small spooning the west coast of Africa. Same thing with the filter. I don't see that WFS protocol alone supports applying a filter? It only seems to work in conjunction with a vector layer constructor.

This seems wrong though because the WFS protocol as described by the OGC support the filter and when openlayers submits it's WFS request via the vector constructor, it does indeed include it just fine. There should be a way to specify projection and filter in the WFS protocol alone?

EDIT #1:

What i have now:

var protocol = new OpenLayers.Protocol.WFS({
    version: "1.1.0",
    url: MapBase.Configuration.GeoServer.WFS_Service.EndPointURL,
    featurePrefix: MapBase.Configuration.GeoServer.WFS_Service.featurePrefix,
    featureType: MapBase.Configuration.GeoServer.WFS_Service.featureType,
    featureNS: MapBase.Configuration.GeoServer.WFS_Service.featureNS,
    geometryName: MapBase.Configuration.GeoServer.WFS_Service.geometryName,
    srsName: "EPSG:4326"
//    ,
//    defaultFilter: myFilter
});

protocol.mergeWithDefaultFilter(Search.GetWFSSpatialFilters());

1 Answer 1

4

You should be able to set a defaultFilter, or once the protocol is created you can use mergeWithDefaultFilter.

var protocol = new OpenLayers.Protocol.WFS({
    version: "1.1.0",
    url: MapBase.Configuration.GeoServer.WFS_Service.EndPointURL,
    featurePrefix: MapBase.Configuration.GeoServer.WFS_Service.featurePrefix,
    featureType: MapBase.Configuration.GeoServer.WFS_Service.featureType,
    featureNS: MapBase.Configuration.GeoServer.WFS_Service.featureNS,
    geometryName: MapBase.Configuration.GeoServer.WFS_Service.geometryName,
    srsName: "EPSG:4326",
    defaultFilter: myFilter
});

protocol.mergeWithDefaultFilter(myNewFilter);

For the projection you can add in the following for WFS 1.1.0:

srsName: "EPSG:4326"

A few more details here.

3
  • I've added an Edit to my question. Though the results still end up off the coast of Africa. I've found a format property that takes an internal and external projection but still am trying to get it to work.
    – CaptDragon
    Commented Jul 18, 2011 at 15:35
  • 2
    Sorry this worked : srsName: new OpenLayers.Projection("EPSG:4326")
    – CaptDragon
    Commented Jul 18, 2011 at 15:37
  • 1
    You should be able to see the requests in FireBug. Check the srsName is being set, and then look at the coordinates to see they come back in this projection. Commented Jul 18, 2011 at 15:38

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.