1

I'm trying to add features to my OpenLayers map, by querying a publicly available WFS server which serves GML data.

// initalize the map
var map = new ol.Map({
    layers: [
        new ol.layer.Tile({
            // OpenLayers public map server
            source: new ol.source.OSM()
        }),
    ],
    target: 'map',
    view: new ol.View({
        // center on Murica
        center: [-10997148, 4569099],
        zoom: 4
    })
});

var xmlhttp = new XMLHttpRequest();

// execute this once the remote GML xml document has loaded
xmlhttp.onload = function() {

    console.log("GML XML document retrieved.  executing onload handler:");
    var format = new ol.format.GML3();

    var xmlDoc = xmlhttp.responseXML;

    console.log("you will see multiple features in the xml: ");
    console.log(xmlDoc);

    // Read and parse all features in XML document
    var features = format.readFeatures(xmlDoc, {
        featureProjection: 'EPSG:4326',
        dataProjection: 'EPSG:3857'
    });

    console.log("for some reason only a single feature will have been added: ")
    console.log(features);
    console.log("Why is this?");

    var vector = new ol.layer.Vector({
        source: new ol.source.Vector({
            format: format
        })
    });

    // Add features to the layer's source
    vector.getSource().addFeatures(features);

    map.addLayer(vector);
};

// configure a GET request
xmlhttp.open("GET", "http://geoint.nrlssc.navy.mil/dnc/wfs/DNC-WORLD/feature/merged?version=1.1.0&request=GetFeature&typename=DNC_APPROACH_LIBRARY_BOUNDARIES&srsname=3857",
   true);

// trigger the GET request
xmlhttp.send();

Here is a CodePen with the bug demonstrated.

http://codepen.io/anon/pen/yamOEK

Here you can download it packaged into a single HTML file:

https://drive.google.com/open?id=0B6L3fhx8G3H_cmp1d3hHOXNKNHM

I can successfully download an entire feature collection with multiple features into my variable xmlDoc, using a valid typename. However, when I use format.ReadFeatures(xmlDoc), the OpenLayers GML format parser appears to be only extracting a single feature from the feature collection, whereas it should be extracting many more.

It would be wonderful if someone could take a look and see if they can figure out if I'm doing something stupidly wrong or it's a legitimate bug in OpenLayers3.

  • As Shiko said, changing ol.format.GML3() to ol.format.GML2() results in all of the features being added, instead of just one. HOWEVER, each added feature has an undefined geometry, which is incorrect, as all of the geometries are defined in xmlDoc. I'm still looking for answers. Try: features[0].getGeometry() ---> results in undefined – Dominic Napoleon Nov 1 '16 at 3:10
  • Cross-posted as stackoverflow.com/q/40351960/820534 – PolyGeo Apr 14 '17 at 4:18
1

Change your GML to version 2 and it will work, as below:

 var format = new ol.format.GML2();
  • Wow. It works! Would you happen to have any idea why the GML formatter needs to be at version 2? The Capabilities file for this GML specifies that it is GML version 3.1.1, which corresponds with the OpenLayers GML3 formatter. geoint.nrlssc.navy.mil/dnc/wfs/DNC-WORLD/feature/… Doing a search for OutputFormat will reveal the GML 3.1.1 version. Thank you so much by the way. – Dominic Napoleon Nov 1 '16 at 2:20
  • your solution does not appear to work. All of the added features have undefined geometries, even though the geometries should be defined. Try: features[0].getGeometry() – Dominic Napoleon Nov 1 '16 at 2:45
  • I can see the geometry tag not starting with feature:. try to check why geometry tag is not like this '<feature:geometry>' – Shiko Nov 1 '16 at 3:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.