3

I have some troubles displaying a WFS layer with OpenLayers2 and hope someone here can give me a hint. I have already checked other posts, like Adding GeoServer WFS layer using OpenLayers and Adding simple WFS layer from GeoServer to OpenLayers map but, for any reason, it hasn't solved my problem yet. The WFS layer appears in the layer switcher, but not at all on the map.

Here is the configuration of my data:

  • Workspace: luma_project (this is the default workspace)
  • Workspace URI: http://localhost:8080/luma
  • URL for WFS: http://localhost:8080/geoserver/wfs
  • Layer name: adresspt
  • SRS: 4326

and here is my OpenLayers code:

var adresspt_wfs = new OpenLayers.Layer.Vector("Adress points WFS", {
  strategies: [new OpenLayers.Strategy.BBOX()],
    protocol: new OpenLayers.Protocol.WFS({
    url: "http://localhost:8080/geoserver/wfs",
    featurePrefix:"luma_project",
    featureType: "adresspt",
    featureNS: "http://localhost:8080/luma",
    srsName: "EPSG:4326",
    geometryName: "the_geom",
    version: "1.0.0"
  })
});
map.addLayer(adresspt_wfs);

Do you see anything missing here?

As my data is stored in a (local) PostGIS database, should I refer to it as workspace:store:layer? or workspace:layer?

Also, I have no trouble displaying this layer as WMS in OpenLayers and I can even display it as WFS in QGIS. The problem thus doesn't come from the data or the GeoServer, but really from my OpenLayers code.

Here is the full code:

var map;

function init() {
var bounds = new OpenLayers.Bounds(
    13, 55.67, 13.5, 55.8
  );
  
 var options = {
    projection: new OpenLayers.Projection("EPSG:4326"),
    displayProjection: new OpenLayers.Projection("EPSG:4326"),
    maxExtent: bounds,
    units: 'degrees'
  };

  map = new OpenLayers.Map('map');

  var transportation = new OpenLayers.Layer.WMS(
    "Lund transportation network", "http://localhost:8080/geoserver/wms",
    { layers: 'lu_transportation',
      format: 'image/png',
      srs:'EPSG:4326',
      transparent:'true',
      zoomOffset: 11, 
   },
   {isBaseLayer: true, opacity: 0.4});
    
  var adresspt_wfs = new OpenLayers.Layer.Vector("Adress points WFS", {
    strategies: [new OpenLayers.Strategy.BBOX()],
    protocol: new OpenLayers.Protocol.WFS({
      url: "http://localhost:8080/geoserver/wfs",
      //featurePrefix:"luma_project",
      featureType: "luma_project:adresspt",
      featureNS: "http://localhost:8080/luma",
      srsName: "EPSG:4326",
      geometryName: "the_geom",
      version: "1.0.0"
    })
  });
         
    map.addLayer(transportation);
    map.addLayer(adresspt_wfs);
    
  map.addControl(new OpenLayers.Control.Navigation());
  map.addControl(new OpenLayers.Control.LayerSwitcher());
  map.addControl(new OpenLayers.Control.Scale($('scale')));
  map.addControl(new OpenLayers.Control.MousePosition({element: $('location')}));
            
  map.zoomToExtent(bounds);

}

And then the HTML code:

  <body onload="init()">
  <div id="wrapper">
    Coordinates of the mouse: <div id="location">...</div>
    <br>
    <div id="scale"></div>
  </div>
    
  <div id="map"></div>

  </body>

Just to try out, I have added another WFS layer, from a remote server, and still nothing displays... I took the exact code of this page, which is:

var test_wfs = new OpenLayers.Layer.Vector("WFS", {
  strategies: [new OpenLayers.Strategy.BBOX()],
  protocol: new OpenLayers.Protocol.WFS({
    url: "http://demo.opengeo.org/geoserver/wfs",
    featureType: "tasmania_roads",
    featureNS: "http://www.openplans.org/topp"
  })
});

I really don't understand... this layer displays perfectly on this page, but not on mine... with the same code...

0

2 Answers 2

2

All your code looks good - can you see if there are features being returned in Firebug?

If there are the next thing to check is if those points are in lat/lon or lon/lat order. If they are not in the order you are expecting then read http://geoserver.org/display/GEOS/Unambiguous+Communication+of+CRS on how to deal with this.

10
  • Thanks for this, but can you tell me what I have to check in Firebug? Under Network / all, I have lots of GET for the WMS, but not for the WFS. I guess it means that nothing is being returned? I will also check your link, but I assume that the coordinates are in the correct order, as they are stored as Geometry in PostGIS and as I have no problem to display them as WMS (or even as WFS in Quantum GIS) Commented Feb 17, 2012 at 12:02
  • If there are no WFS requests in the net tab then something is wrong with the OpenLayers code. I can't see anything may be others can
    – Ian Turton
    Commented Feb 17, 2012 at 12:19
  • This is what I think as well... also, I have tried with the sample data provided by Geoserver (tiger, topp...) and I have the exact same problem. So, a priori not a problem with the datasets. If someone else has an idea? Commented Feb 17, 2012 at 12:59
  • Still googling around for my problem and found some references about needing to setup a cgi script... Is that really necessary, knowing that, for now, I run the app exclusively on localhost? Also, I do not have Apache installed, and Geoserver runs on Jetty, not Tomcat (the "easy and straight-forward" installation), on Windows 7 Commented Feb 17, 2012 at 13:55
  • 2
    are you loading your HTML page from the same port as your GeoServer is running on? if not you will need a proxy or to move your html to the www directory of GeoServer
    – Ian Turton
    Commented Feb 17, 2012 at 15:06
2

With help from @iant I have solved my problem. What I didn't know (and could also be useful for others new to GeoServer like me) is that the html files must be in the GeoServer web directory (for the default installation on windows:

C:/Program Files/GeoServerx.x.x/data_dir/www/something) 

and must be accessed by the url. If on localhost:

http://localhost:8080/geoserver/web/something 

For any reason, "double-clicking" on the html file to display it in your browser will work for WMS feeds but not for WFS (there must be a rationale explanation, but I don't know it).

I still need to find out how to display the WFS on top of OpenStreetMap and Google tiles, as there is apparently a new problem with the projection. I already solved it for the WMS layers, but the WFS doesn't appear yet when the projection is EPSG:900913. I'll investigate.


For my 2nd problem, the solution is very easy: in the parameters of the WFS layer, it is necessary to define the srs of the map, not the one of the layer itself. So, instead of:

   var adresspt_wfs = new OpenLayers.Layer.Vector("Adress points WFS", {
    strategies: [new OpenLayers.Strategy.BBOX()],
    protocol: new OpenLayers.Protocol.WFS({
      url: "http://localhost:8080/geoserver/wfs",
      featurePrefix:"luma_project",
      featureType: "adresspt",
      featureNS: "http://localhost:8080/luma_project",
      srsName: "EPSG:4326",
      geometryName: "the_geom",
      version: "1.1.0"
    })
  });

you should write:

   var adresspt_wfs = new OpenLayers.Layer.Vector("Adress points WFS", {
    strategies: [new OpenLayers.Strategy.BBOX()],
    protocol: new OpenLayers.Protocol.WFS({
      url: 'http://localhost:8080/geoserver/wfs',
      featurePrefix:"luma_project",
      featureType: "adresspt",
      featureNS: "http://localhost:8080/luma_project",
      srsName: "EPSG:900913",
      geometryName: "the_geom",
      version: "1.1.0"
    })
  });
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.