[GIS] GeoServer Web Feature Service (WFS) in OpenLayers problem

geoserveropenlayers-2wfs

I am running GeoServer 2.1.1, and have a datastore from ArcSDE populated with features projected in EPSG:26918. I can successfully add a WMS to an OpenLayers map, but am having issues with WFS. I have the map set to EPSG:900913 in preparation for a mashup with Google base layers.

I am under the impression that vector layers are reprojected on the fly, and that absent of any user specification, features will be rendered using the default style.

Here is the main chunk of my script:

    map = new OpenLayers.Map('map', {
        maxExtent: new OpenLayers.Bounds(
            -128 * 156543.0339,
            -128 * 156543.0339,
            128 * 156543.0339,
            128 * 156543.0339),
        maxResolution: 156543.0039,
        projection: "EPSG:900913",
        units: "m"
    });

    var bridgeLayer = new OpenLayers.Layer.WMS(
        "Bridge",
        "http://localhost/geoserver/geodatabase/wms", {
        layers: "geodatabase:Bridge",
        format: "image/png",
        transparent: true
    },{
        isBaseLayer: true
    });

    var wfsLayer = new OpenLayers.Layer.Vector(
        "Photo Points", {
            strategies: [new OpenLayers.Strategy.Fixed()],
            protocol: new OpenLayers.Protocol.WFS({
                version: "1.0.0",
                url: "http://localhost/geoserver/wfs",
                featureType: "Photo Point",
                srsName:"EPSG:26918",
                featureNS: "http://dev.geodatabase.org",
                geometryName: "SHAPE"
            }),
            visibility: true
    });

    map.addLayers([bridgeLayer, wfsLayer]);

And so, the WMS displays correctly (and when I add a Google base layer, it lines up correctly). However, the WFS layer fails to appear.

According to Firebug, however, my POST request is successful, and I receive an response. Furthermore, the layer object I defined looks to be a fully formed layer, although it does not appear to have any features in its "features" array. Not sure what that means. Screenshots appear below.

Something also that I don't understand, when I switch versions to "1.1.0", my POST request fails with a "NoApplicableCode" exception.

Additionally, I have tried reprojecting the data in GeoServer to spherical mercator EPSG:900918, and also redefining the srsName parameter, but both experiments have failed.


Best Answer

If you use WFS 1.0.0 the srsName parameter is not supported, so you can’t use OpenLayers to decide in which projection you want your features returned. Therefore you'll need 1.1.0

Also you need to request the WFS in the same SRS as your basemap otherwise no features will appear as they will be outside the map's bounding box. So the srsName should be set to EPSG:900913 (not EPSG:900918 as in your question - a typo maybe?).

Finally you need to make sure that Geoserver can reproject the WFS into EPSG:900913. You can check this by issuing a request similar to:

http://localhost/geoserver/wfs?service=WFS&request=GetCapabilities&version=1.1.0

I had similar issues with MapServer rather than GeoServer, but the same principles probably apply:

http://geographika.co.uk/mapserver-openlayers-and-the-wfs-maze

You can reproject features on the client side, but this may take longer than doing it on the server. You will also need to include the Proj4JS library in your web application and the EPSG:26918 definition in your code to take this approach. You'll also need to set the layers's projection:

Proj4js.defs["EPSG:26918"] = "+proj=utm +zone=18 +ellps=GRS80 +datum=NAD83 +units=m +no_defs";
var myproj =new OpenLayers.Projection("EPSG:26918");
var wfsLayer = new OpenLayers.Layer.Vector(
    "Photo Points", {
        strategies: [new OpenLayers.Strategy.Fixed()],
        protocol: new OpenLayers.Protocol.WFS({
            version: "1.0.0",
            url: "http://localhost/geoserver/wfs",
            featureType: "Photo Point",
            srsName:"EPSG:26918",
            featureNS: "http://dev.geodatabase.org",
            geometryName: "SHAPE"
        }),
        visibility: true,
        projection: myproj 
});

Have a look at the source in this example - http://openlayers.org/dev/examples/wfs-reprojection.html for more details. Also you may want to change the strategy to BBox or you will load all the features in in one go.

strategies: [new OpenLayers.Strategy.BBOX()]