Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am working with OpenLayers (and GeoServer) and I am showing a WMS layer with countries in a map. Each object (country) in the layer has an attribute called NAME (the name of the country).

I created a filter like the following in order to see only Italy (it's an example)

myFilter = new OpenLayers.Filter.Comparison({
    type: OpenLayers.Filter.Comparison.EQUAL_TO,
    matchCase: false, 
    property: 'NAME',
    value: 'Italy'
});

This work perfectly and if I apply it to the layer and call redraw() method I can see only one country.

Now I would like to do the same thing with sytles, in order to highlight Italy but keep visible all the other countries in the map. I created a style like the one below, but now I don't know how I can apply this to the layer.

var style = new OpenLayers.Style();
var ruleLow = new OpenLayers.Rule({
    filter: myFilter,
    symbolizer: {fillColor: "green"}
});
style.addRules([ruleLow]);          

Can I apply the style to a WMS layer? Or only to vector layer?

Thank you

share|improve this question

2 Answers 2

up vote 2 down vote accepted

In general the approach you have outlined will only work with vector layers. WMS layers are pictures of the map sent to you by the WMS server so it is harder to change their style on the fly as you would like. You probably want to see if it is possible to make a WFS request for the highlighted polygon and overlay that over the WMS base layer.

There are ways to send an SLD file with the WMS request to ask the server to style the map in a way that suits you. However not all servers (or clients) support this as it is much less efficient (it prevents the server caching the result for other users for example). See this example if that is what you want to do.

share|improve this answer

It could be a bit of a work to associate a style defined on client side with a wms layer. You can easily highlight the features returned by the filter on a vector layer with the defined style. All you have to do is add a vector layer, specify the style in it and then add the features contained in the response returned by the filter. This piece of code might help:

var highlight_style = 
{
    fillColor:'#99CCFF', 
    strokeColor:'#000000', 
    fillOpacity:0.7
};
hilites = new OpenLayers.Layer.Vector("Highlighted",
{
    projection: 'EPSG:4326',
    visibility:false, 
    style:highlight_style
}
);
map.addLayer(hilites);

function highlight_them(response) {
    // use the GML parser to turn the XML into a list of Feature objects
    var features = new OpenLayers.Format.GML().read(response.responseText);
    // have the Vector layer purge its feature list, replace them with the new ones
    hilites.destroyFeatures();
    hilites.addFeatures(features);
    hilites.setVisibility(true);
}

You can get the response by making a request to goeserver with a wfs url.

 var wfs_url = 'http://localhost:8080/geoserver/wfs?service=wfs&version=1.0.0';

 function highlightFeatures(typename,attribute,value) {     
      var wfsurl = wfs_url + '&REQUEST=getfeature&typename=' + typename +
     '&Filter=<Filter><PropertyIsEqualTo><PropertyName>'+attribute+'</PropertyName><Literal>'+value+'</Literal></PropertyIsEqualTo></Filter>';

      OpenLayers.loadURL(wfsurl,'',null,highlight_them);
      }

highlight_them is the function mentioned earlier. Pass 'typename' as the layer in geoserver, 'attribute' as the name of the attribute and 'value' as the attribute value of the feature to be returned. If you know the feature id you can pass the wfs url as:

 var wfsurl = wfs_url + '&REQUEST=getfeature&typename=' + typename +
'&featureID='+value;

Here 'value' is the Id of the feature to be returned. You can have multiple ids assigned to value in a comma seperated format.

share|improve this answer
    
This is quite all clear; I have only one doubt: how do I get response? Thank you –  user1971270 May 23 '13 at 18:31
    
Try using a wfs url. I've edited my answer. check the code. –  Nik May 24 '13 at 5:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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