5

I have found similar questions asked previously, but I still fail to get a simple WFS point layer to work using OpenLayers and Google Maps. The original code I used was based on WFS polygon layers I have done previously, I then re-created a page based on the OpenGeo Introduction and other sources, all without success.

The WFS side of things seems fine -- I can see the WFS traffic and it contains co-ordinates in the target space. But I can't see anything rendered at all. Even adding a feature via code doesn't get rendered, in the example code below the feature in the program does display if added to the "vectorLayer", but not if added to the "wfsLayer".

On the risk of appearing stupid: what is wrong with this code?

<html>
  <head>
    <title>OpenLayers: Google Layer Example</title>
    <link rel="stylesheet" href="http://openlayers.org/api/theme/default/style.css" type="text/css" />
    <link rel="stylesheet" href="http://openlayers.org/dev/examples/style.css" type="text/css" />
    <script src="http://maps.google.com/maps/api/js?v=3.3&amp;sensor=false" type="text/javascript"></script>
    <script src="http://openlayers.org/api/OpenLayers.js"></script>
    <script type="text/javascript">
      var map;

      function init() {
        map = new OpenLayers.Map("map");
        var gmap = new OpenLayers.Layer.Google(
            "Google Streets",
            {numZoomLevels: 20}
        );
        map.addLayer(gmap);

        var wfsLayer = new OpenLayers.Layer.Vector("WFS", {
            strategies: [new OpenLayers.Strategy.BBOX()],
            protocol: new OpenLayers.Protocol.WFS({
                        url:  "/geoserver/XXX/wfs",
                        featureType: "XXX",
                        featureNS: "XXX",
                        geometryName: "location",
                        srsName: "EPSG:900913",
                        version: "1.1.0"                    
                })
        });
        map.addLayer(wfsLayer);

        var vectorLayer = new OpenLayers.Layer.Vector("Overlay");
        var feature = new OpenLayers.Feature.Vector(
             new OpenLayers.Geometry.Point(12885085, -3806132),
             {some:'data'},
             {externalGraphic: 'https://upload.wikimedia.org/wikipedia/commons/thumb/0/07/Button_Icon_Red.svg/300px-Button_Icon_Red.svg.png', graphicHeight: 20, graphicWidth: 20});
        // wfsLayer.addFeatures(feature); // does not work
        vectorLayer.addFeatures(feature); // works
        map.addLayer(vectorLayer);<gml:Point srsDimension="2" srsName="http://www.opengis.net/gml/srs/epsg.xml#900913"><gml:pos>1.2884006544922689E7 -3801543.4695909135</gml:pos></gml:Point>

        map.setCenter(new OpenLayers.LonLat(12885085, -3806132), 12);         
      }
    </script>
  </head>

  <body onload="init()">
    <div id="map" class="smallmap"></div>
  </body>
</html>

Note that I replaced some values with "XXX" as to not disclose the client project. As I said: the WFS traffic looks fine, so I assume these values are not relevant for the question. Points in the WFS come through like this:

<gml:Point srsDimension="2" srsName="http://www.opengis.net/gml/srs/epsg.xml#900913">
    <gml:pos>1.2884006544922689E7 -3801543.4695909135</gml:pos>
</gml:Point>

which seems fine for the spherical Mercator projection around the target location.

2
  • 1
    Just curious about your geometryName. Did you change the Geoserver defaults?
    – R.K.
    Commented Feb 16, 2012 at 7:02
  • The underlying data structure is a PostGIS table where the geometry column has that name. It's actually not necessary here, I should have removed it. It's a habit from having the occasional table with more than one geometry. Removing it doesn't change behaviour, changing the value causes WFS exceptions (as expected). Commented Feb 16, 2012 at 20:56

3 Answers 3

1

I think the problem is in your map initialization code. It's for a single layer map. The multi-layer map initialization code in the OpenLayers example from the OpenGeo Stack intro looks like this:

var map;

// Avoid pink error tiles
OpenLayers.IMAGE_RELOAD_ATTEMPTS = 3;
OpenLayers.Util.onImageLoadErrorColor = "transparent";

function init(){
  // Map is in mercator this time, so over-ride the default
  // options that assume lat/lon.
  var options = {
      projection: new OpenLayers.Projection("EPSG:900913"),
      displayProjection: new OpenLayers.Projection("EPSG:4326"),
      units: "m",
      numZoomLevels: 20,
      maxResolution: 156543.0339,
      maxExtent: new OpenLayers.Bounds(-20037508, -20037508,
                                              20037508, 20037508.34)
  };
  // Create the map object
  map = new OpenLayers.Map('map', options);

Another possible problem would be the featurePrefix. You might need to set it according to the value you set in:

url:  "/geoserver/XXX/wfs",

It should be the value of XXX if I'm not mistaken. Another possibility is that the value in geometryName might be set incorrectly. It is "the_geom" in Geoserver by default, unless of course, you changed it.

1
  • Sorry, but this doesn't change a thing. My original map initialization was actually much more complex, also specifying the resolutions to avoid alignment issues. But it doesn't seem to affect anything -- copying your code into mine still leads to the same result.Thanks anyway. Commented Feb 16, 2012 at 3:51
1

Use map.addLayers instead of map.addLayer.

addLayers([layer1, layer2])

1
  • No change. I can see the layers in the layer switcher either way, but nothing is rendered. Commented Feb 16, 2012 at 12:38
1

Are you sure you have all the openlayers WFS protocol properties matched up correctly to their corresponding GeoServer properties ?

nine times out of ten, this is why the WFS layer does not show up.

Take a look at my answers here and here.


Update #1

Also, Try not to use the relative URL use the full url with protocol:

...
url:  "http://localhost:8080/geoserver/wfs",
...

Update #2

There are three more things i would like you to try.

  1. Specify a featurePrefix which i don't see in your code. It tells openlayers what your GeoServer catalog is.
  2. Use srsName: new OpenLayers.Projection("EPSG:900913") instead of EPSG:900913
  3. Use new OpenLayers.Strategy.Fixed() instead of new OpenLayers.Strategy.BBOX() just for testing purposes.
var wfsLayer = new OpenLayers.Layer.Vector("WFS", {
    strategies: [new OpenLayers.Strategy.Fixed()],
    protocol: new OpenLayers.Protocol.WFS({
                url:  "/geoserver/XXX/wfs",
                featurePrefix: "XXXX" //<-- GeoServer workspace name
                featureType: "XXX", //<-- layer name
                featureNS: "XXX", //<-- Edit Workspace Namespace URI
                geometryName: "location", //<-- geometry field
                //srsName: "EPSG:900913",
                srsName: new OpenLayers.Projection("EPSG:900913"),
                version: "1.1.0"                    
        })
});
7
  • I had seen your previous answers. I am pretty sure that the WFS part is correct. I have successfully used polygon layers before and I can see the WFS traffic coming through as expected (e.g. in the network tab of the Chrome developer tools). If I understand correctly the issues you describe would show up with issues in the HTTP response, which looks just fine to me. Commented Feb 16, 2012 at 20:52
  • Depends, the issues i'm describing (properties on Geoserver not matching properties on OpenLayers) can manifest themselves as the symptoms you're describing. For instance if your featureNS on OpenLayers does not match your Workspace Namespace URI on GeoServer then you would get a WFS response but OpenLayers would not be able to deserialize it and render it on the map. They have to match. On the otherhand, if the featureType on OL does not match your geoserver Layer Name, then you're right, you will not get a response at all.
    – CaptDragon
    Commented Feb 16, 2012 at 21:05
  • Thanks for the clarification -- I wasn't aware of that. But the URIs match. Do you know how I debug issues like this? Right now it seems I need to learn a lot about the OpenLayers internals to fix this, which is something I was hoping to avoid. Commented Feb 19, 2012 at 22:59
  • Unfortunately i don't know of a good way to debug this. Please try my suggestions in my Update #2.
    – CaptDragon
    Commented Feb 20, 2012 at 15:08
  • A million thanks to your suggestions stated in update #2, CaptDragon, they were of great help to me. I had trouble getting the right parameters across, but your explanation nailed it. Thanks!
    – user16871
    Commented Apr 5, 2013 at 16:23

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.