2

I am trying to get attribute information from features in a WMS Shapefile layer being served by MS4W and viewed in OpenLayers3.

Is there a way to get multiple feature information in one command like you can with the vector source method below?

vectorSource.forEachFeatureIntersectingExtent(extent, function(feature) {
          selectedFeatures.push(feature);
          info.push(feature.get('name'));
Red
  • 35
  • 3

2 Answers2

1

For WMS layers served by any wms/wfs server you may execute a wms get feature request using something like the following:

         var url = myWMSLayer
            .getSource()
            .getGetFeatureInfoUrl(
                evt.coordinate,
                map.getView().getResolution(),
                map.getView().getProjection(),
                {
                    'INFO_FORMAT': 'application/json',
                    'propertyName': 'ATTR1,ATTR2,ATTR3'
                }
            );

This should give you any feature exist within the event.coordinate passed. So you may get back all features exist within the point given. I think this is your only option if you have access to just WMS requests on the server.

But if your server supports WFS requests and you have access on them you may execute a wfs request to get the features you want. Something like following:

  //here is the rectangle to search for fetaures
  var extent [-8876804.07807116, 5368955.976007851, -8866790.827365803, 5374688.75312924];
  $.ajax('http://demo.opengeo.org/geoserver/wfs', {
        type: 'GET',
        data: {
            service: 'WFS',
            version: '1.1.0',
            request: 'GetFeature',
            typename: 'mylayer',
            srsname: 'EPSG:3857',
            bbox: extent.join(',') + ',EPSG:3857'
        }
    }).done(function(resp){
    //you may parse the responce back here 
    var formatWFS = new ol.format.WFS();
    var feats = formatWFS.readFeatures(resp);
    //now you can iterate through your features and get the attrs
    for (var i=0;i<feats.length;i++){
    console.log(feats[i].get('ATTR1'));
    }    
    }).fail(function () {
        alert("fail loading features");
    });
pavlos
  • 2,691
  • 14
  • 20
0

For WMS map layer we can use the function below for getting the feature while clicking on map having WMS layer:

map.forEachFeatureAtPixel(evt.pixel, function(feature, layer)
{
//Do your logic 
}

Note: evt = Event of click.

craigcaulfield
  • 2,976
  • 10
  • 26
  • 33