3

In OL2 I think that it was done by using something like this:

the_control = new OpenLayers.Control.GetFeature({
   protocol: OpenLayers.Protocol.WFS.fromWMSLayer(mymap, {
      geometryName: 'geom',
      featureType: 'feature'
   }),
   box: false,
   hover: false
});

which sent XML request to mapserver which returned GML response with the geometry.

What is the alternative in OL3? I just want to 'highlight' the the feature on WMS layer. How to setup the select interaction with the WMS layer OL3, send information to mapserver and highlight the feature according to the response? And how to configure layer in mymap.map file?

I get response from mapserver like this:

<?xml version="1.0" encoding="UTF-8"?>

<msGMLOutput 
     xmlns:gml="http://www.opengis.net/gml"
     xmlns:xlink="http://www.w3.org/1999/xlink"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <tudu_linestring_layer>
    <gml:name>TODO 3 (WMS)</gml:name>
        <tudu_linestring_feature>
            <gml:boundedBy>
                <gml:Box srsName="EPSG:102067">
                    <gml:coordinates>-687941.344000,-1099495.708000 -678154.701000,-1093996.637000</gml:coordinates>
                </gml:Box>
            </gml:boundedBy>
            <fid>8394</fid>
            <color>#00C800</color>
            <layer>0</layer>
            <version>20.3</version>
            <idgr>173314038596</idgr>
            <width>1</width>
            <linetype>solid</linetype>
            <todo>173314</todo>         

        </todo_linestring_feature>
    </todo_linestring_layer>
</msGMLOutput>

The style is set up like this:

new ol.style.Style({
  stroke: new ol.style.Stroke({
     color: '#007FFF',
     width: 5
  })
}),
1
  • Trying to do the same with ol3 + GeoServer. No luck so far. Do Geoserver layers require any configuration as well?
    – user50142
    Commented Apr 7, 2015 at 13:49

1 Answer 1

6

The easiest way to achieve the same in OpenLayers 3 is to use WMS GetFeatureInfo. Your code could look something like this (assuming mymap is your layer with a WMS source, and map is your ol.Map instance):

var parser = new ol.format.WMSGetFeatureInfo();
var highlightOverlay = new ol.Layer.Vector({
  // style: (customize your highlight style here),
  source: new ol.source.Vector(),
  map: map
});
map.on('singleclick', function(evt) {
  var view = map.getView();
  var url = mymap.getSource().getFeatureInfoUrl(evt.coordinate,
      view.getResolution(), view.getProjection(),
      {'INFO_FORMAT': 'application/vnd.ogc.gml'});
  $.ajax(url).then(function(response) {
    var features = parser.readFeatures(response);
    highlightOverlay.getSource().clear();
    highlightOverlay.getSource().addFeatures(features);
  });
});

In your MapServer mapfile, you will have to set the layer TEMPLATE parameter for the layer to be queryable by GetFeatureInfo requests. You also need to configure some METADATA to include geometries in the response:

LAYER
  TYPE LINE
  ...
  METADATA
    "gml_geometries" "mygeom"
    "gml_mygeom_type" "line"
  END
  ...
END

The above assumes that your layer is a line layer, and that the geometry column in your data is 'mygeom'.

9
  • After your answer there was a progress (see the update of question), but no highlight on the map.. nothing happens after a click
    – khamyl
    Commented Mar 25, 2015 at 12:37
  • Your GetFeatureInfo response does not contain geometries. I edited my answer to show you how to configure your LAYER in the mapfile to include geometries.
    – ahocevar
    Commented Mar 25, 2015 at 13:25
  • Getting closer.. now I have this in my getFeatureResponse: <!-- Warning: Cannot write geometry- no line/multiline geometry defined. -->
    – khamyl
    Commented Mar 25, 2015 at 15:09
  • Ok problem solved.. this was because the Layer TYpe was set to TYPE LINE and the geometry type was POINT.. many thanks for help!!!
    – khamyl
    Commented Mar 25, 2015 at 15:20
  • Glad I could help. I changed the layer and geometry type in my answer to LINE so future readers don't run into the same problem.
    – ahocevar
    Commented Mar 25, 2015 at 17:04

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.