20

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 you 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).

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
geo_james
  • 591
  • 1
  • 7
  • 23

5 Answers5

24

Take a look at my answer at OpenLayers WFS vector layer.

  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"
                })
            });
PolyGeo
  • 65,136
  • 29
  • 109
  • 338
CaptDragon
  • 13,313
  • 6
  • 55
  • 96
  • Thanks capdragon. I've taken all the steps but still no joy...my workspace is now called rpid and i've added a featurePrefix. I've tried featureType with and without appending the workspace. I've tried various namespaces including: "http://rpid.opengeo.com", "http://localhost:8081/rpid". I'm still confused about this property. I've inserted the projection and geometry name (GEOM). My map.html page lives here: file:///C:/Aptana/Workspace/GIS/map.html. Will this catch me out with the Same Origin Policy below? My Net tabe in Firebug shows a 'OPTIONS wfs' with no response – geo_james Jan 30 '12 at 14:33
  • 2
    The namespace of featureNS can be whatever you like so both of those will work. The important thing is that you have it exactly the same on your geoserver. Or else things don't get serialized and you get no response from the web service. So go into geoserver, click on your workspace (this will take you to the page Edit Workspace. the Name is what put as featurePrefix. And the Namespace URI is what you put as featureNS. Also, not sure if the file:///c: location will require you to use a proxy. But i would make the file live at http://localhost to test and make sure. – CaptDragon Jan 30 '12 at 14:43
  • OK I've now moved my code to localhost:8081/apps/index.html and changed my featureNS. I can now see 30 features coming back on the response...rpid:OBJECTID25</rpid:OBJECTID>rpid:FIELD_IDNS/86427/74196</rpid:‌​FIELD_ID>rpid:GROSS_AREA2.307</rpid:GROSS_AREA>. Progress! However I'm not seeing any of the features on my map. When added as WMS I can see them fine. The only other layer is a base WMS. My console is also riddled with errors such as: OpenLayers.Marker is undefined and Ext.preg is not a function. I'm importing all my libraries locally... – geo_james Jan 30 '12 at 18:48
  • When you say there are 30 features in the response. They are in xml format wrapped within a parent <wfs:FeatureCollection numberOfFeatures="30"... element right? – CaptDragon Jan 30 '12 at 18:59
  • I would say create a new very simple test page and add that WFS layer. It seems to me that those errors are comming from other parts of your code since OpenLayers.Marker is not involved with WFS. Maybe i'm wrong, but creating a simple test page always help determine if it's working or not. That's what i do anyway. – CaptDragon Jan 30 '12 at 19:04
  • CaptDragon. Thanks again - getting closer. My test page is very simple. I only add a baselayer (epsg 27700) and then my wfs layer (also epsg 27700). Yes the first 30 features are correctly wrapped...xml to follow – geo_james Jan 31 '12 at 12:41
  • Is there a reason why my srs is epsg 4326 (WGS84) even though I'm specifying 27700 (British national grid)...rpid:OBJECTID1</rpid:OBJECTID>rpid:FIELD_IDNS/89790/69204</rpid:FIELD_ID>rpid:GROSS_AREA2.755</rpid:GROSS_AREA>rpid:AREA0.0</rpid:AREA>rpid:LEN0.0</rpid:LEN>rpid:SHAPE_LENGTH738.39935148554</rpid:SHAPE_LENGTH>rpid:SHAPE_AREA27555.6610998946</rpid:SHAPE_AREA>rpid:GEOM<gml:Polygon srsDimension="2" srsName="http://www.opengis.net/gml/srs/epsg.xml#4326">gml:exteriorgml:LinearRinggml:posList-3.762929992067714 55.90273029047715 – geo_james Jan 31 '12 at 13:11
  • Are you sure you are specifying the correct projection in the WFS layer constructor as in my example? Edit your original question and add another code block of your simple example as it is now? Or a link to it would be best if that's possible. – CaptDragon Jan 31 '12 at 14:06
  • @CaptDragon do we need to add new workspace or can we use a previously created workspace?. – Sam007 May 14 '12 at 19:07
  • @Sam007 You should be able to use any workspace. – CaptDragon May 14 '12 at 19:23
  • I did, followed the entire process, the WMS files are coming great, but the overlay is not visible – Sam007 May 14 '12 at 19:36
  • @Sam007 : post a question and show your code. – CaptDragon May 14 '12 at 21:28
  • here is the question @CaptDragon http://gis.stackexchange.com/questions/25446/unable-to-create-wfs-successfully – Sam007 May 14 '12 at 22:34
3

First thing to check: is your server / test webpage on 'medford.opengeo.org'? If it isn't, you won't be able to access WFS because of the Same Origin Policy. You can technically use a proxy to fix this. (though in my opinion this is just a case of WFS being a poorly designed protocol)

tmcw
  • 4,286
  • 20
  • 22
  • 1
    Same Origin is a browser problem, WFS works exactly as designed. – Ian Turton Jan 20 '12 at 08:32
  • Yes and no: WFS is XML, so it necessitates an XML parser on every platform but Firefox (and 'the desktop'), and it also doesn't mix well with the only way we have to do cross-domain communication: JSONP. GeoJSON, on the other hand, is natively parsed in nearly every browser and is easily and commonly wrapped to be cross-domain compatible. Obviously not apples-to-oranges, and that's not the extent of my WFS critique (bloated, poorly extended, etc). – tmcw Jan 20 '12 at 23:16
  • You are assuming that browsers make good WFS clients. You hardly ever want to pull large amounts of geographic data into a web browser as vectors. – Ian Turton Jan 30 '12 at 14:37
  • Hi tmcw. No my test page is just a local file on my C drive. What should I set my Namespace URI to if i want to work with a local GeoServer on port 8081? – geo_james Jan 30 '12 at 14:40
2

Your problem is that you have the prefix on the feature name and a namespace this is confusing GeoServer (and/or OpenLayers).

Try:

 featureType : "parks",

I have some WFS examples at http://ian01.geog.psu.edu/geoserver/www/wfs/index.html with commented source code that you can study.

Ian Turton
  • 81,417
  • 6
  • 84
  • 185
0

just correct your featuretype from "medford:parks" to "parks"

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.0.0",
                    url: "http://localhost:8081/geoserver/wfs",
                    featureType: "parks", //geoserver Layer Name without workspace prefix
                    featureNS: "http://medford.opengeo.org/medford"

                })
            });

See jsfiddle as example: http://jsfiddle.net/expedio/ucrtthya/

Thomas B
  • 8,807
  • 1
  • 21
  • 62
-2

Your code is ok. You just need to change the browser address of your page. Point your browser page through "localhost:8080/geoserver/www/your_file_name.html" instead of "file:\c......"

Then the vector layer will render perfectly.

Fezter
  • 21,867
  • 11
  • 68
  • 123