OpenLayers – Adding Simple WFS Layer from GeoServer to OpenLayers Map

geoserveropenlayers-2wfswfs-t

I'm trying to add one of the included datasets with GeoServer to an OpenLayers map. I've followed the OpenGeo tutorials, seen similar thread on here, but I can't seem to crack it. Can somebody take a glance at my code and GeoServer setup and tell me where I'm going wrong?

Here's my code:

//WMS map
world = new OpenLayers.Layer.WMS("Global Imagery", "http://maps.opengeo.org/geowebcache/service/wms", {
    layers : "openstreetmap",
    format : "image/png"
});
map.addLayer(world);

//WFS
parks = new OpenLayers.Layer.Vector("WFS", {
    strategies : [new OpenLayers.Strategy.BBOX()],
    protocol : new OpenLayers.Protocol.WFS({
        url : "http://localhost:8081/geoserver/wfs",
        featureType : "medford:parks",
        featureNS : "http://medford.opengeo.org/medford"
    })
});

map.addLayer(parks);

map.zoomToMaxExtent();

GeoServer details:
workspace name = medford, Namespace URI = http://medford.opengeo.org
For the feature type I have tried "parks" and "medford:parks"

The layer is enabled, advertised and has a 30 feature return limit.

I'm struggling to put a simple map together.

Updated Solution. Working Now.

wfs = new OpenLayers.Layer.Vector("Fields_WFS", {
        strategies : [new OpenLayers.Strategy.Fixed()],
        protocol : new OpenLayers.Protocol.WFS({
        version : "1.1.0",
        url : GEOSERVER_HOST + ":" + GEOSERVER_PORT + "/geoserver/wfs", 
        featurePrefix : "rpid",
        featureType : "FIELDS_SUBSET_BNG_OSGB36_1",
        featureNS : "<namespace>", 
        //geometryName : "GEOM", type "Geometry"
        srsName : "EPSG:27700"
}),
    renderers : renderer
})

Does not need to specify the geometry Name in Layer definition.
Since Native SRS was null in our dataset, if we keep geometryName than it takes default, but without it, it uses map projection(27700- in our case).

Best Answer

Take a look at my answer on this post.

  1. Make sure your featureNS value is in the "Edit Workspace Page under Namespace URI" in your GEOSERVER. Do not use something as generic as "http://postgis.org". Use something like "http://yourdomain/application/catalogLayer" or something unique like that. You can make this URI up, just make it unique.
  2. You have new BBOX() as a strategy but what do that box have as dimensions? I would change it to [new OpenLayers.Strategy.Fixed()] for testing.
  3. featureType is not actually the type of geometry, it's your "Layer Name" in GEOSERVER (i know, misleading).
  4. geometryName is the name of the field in your postgis database that has the Geometry data type. In GEOSERVER, Under "Edit Layer" you can see at the bottom "Feature Type details". There you will see a field with type "Geometry". Use the value under the property column

I would specify featurePrefix and version. Try something like this:

var wfs = new OpenLayers.Layer.Vector(
            "Stavros Features",
            {
                strategies: [new OpenLayers.Strategy.Fixed()]
                , projection: new OpenLayers.Projection("EPSG:4326")
                , protocol: new OpenLayers.Protocol.WFS({
                    version: "1.1.0",
                    url: "http://localhost:8081/geoserver/wfs",
                    featurePrefix: 'myWorkspace', //geoserver worspace name
                    featureType: "medford:parks", //geoserver Layer Name
                    featureNS: "http://medford.opengeo.org/medford", // Edit Workspace Namespace URI
                    geometryName: "bounds" // field in Feature Type details with type "Geometry"
                })
            });