0

I am working on a GIS API, and I can't share the code, so I will try to explain what I want.

I want to apply a style map on a single layer, where I have some elements with opacity 1 and others with opacity 0.5.

Please, look at the image below (where I show the desired feature using a "vector" layer):

vec_partial_opacity

I have tried using the opacity property from OpenLayers.Layer, but it sets the whole layer to 0.5 opacity.

I know WMS is an image layer, so it is maybe not possible to set multiple values for opacity. Even though, it thought it would be worth asking.

Is it possible to use a style map with multiple opacities in a WMS layer?

With my current SLD Body, I get the object with only opacity 1.

<sld:StyledLayerDescriptor xmlns:sld="http://www.opengis.net/sld" version="1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd" xmlns:ogc="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml"><sld:NamedLayer><sld:Name>topp:tasmania_water_bodies</sld:Name><sld:UserStyle><sld:FeatureTypeStyle><sld:Rule><ogc:Filter><ogc:Or><ogc:PropertyIsGreaterThan><ogc:PropertyName>AREA</ogc:PropertyName><ogc:Literal>1067500000</ogc:Literal></ogc:PropertyIsGreaterThan></ogc:Or></ogc:Filter><sld:PolygonSymbolizer><sld:Fill><sld:CssParameter name="fill">#0000ff</sld:CssParameter><sld:CssParameter name="fill-opacity">1</sld:CssParameter></sld:Fill><sld:Stroke><sld:CssParameter name="stroke">#000000</sld:CssParameter></sld:Stroke></sld:PolygonSymbolizer></sld:Rule><sld:Rule><ogc:Filter><ogc:Or><ogc:PropertyIsLessThanOrEqualTo><ogc:PropertyName>AREA</ogc:PropertyName><ogc:Literal>1067500000</ogc:Literal></ogc:PropertyIsLessThanOrEqualTo></ogc:Or></ogc:Filter><sld:PolygonSymbolizer><sld:Fill><sld:CssParameter name="fill">#0000ff</sld:CssParameter><sld:CssParameter name="fill-opacity">0.5</sld:CssParameter></sld:Fill><sld:Stroke><sld:CssParameter name="stroke">#000000</sld:CssParameter></sld:Stroke></sld:PolygonSymbolizer></sld:Rule></sld:FeatureTypeStyle></sld:UserStyle><sld:UserStyle><sld:FeatureTypeStyle><sld:Rule><ogc:Filter><ogc:Or><ogc:PropertyIsGreaterThan><ogc:PropertyName>AREA</ogc:PropertyName><ogc:Literal>1067500000</ogc:Literal></ogc:PropertyIsGreaterThan></ogc:Or></ogc:Filter><sld:PolygonSymbolizer><sld:Fill><sld:CssParameter name="fill">#0000ff</sld:CssParameter><sld:CssParameter name="fill-opacity">1</sld:CssParameter></sld:Fill><sld:Stroke><sld:CssParameter name="stroke">#000000</sld:CssParameter></sld:Stroke></sld:PolygonSymbolizer></sld:Rule><sld:Rule><ogc:Filter><ogc:Or><ogc:PropertyIsLessThanOrEqualTo><ogc:PropertyName>AREA</ogc:PropertyName><ogc:Literal>1067500000</ogc:Literal></ogc:PropertyIsLessThanOrEqualTo></ogc:Or></ogc:Filter><sld:PolygonSymbolizer><sld:Fill><sld:CssParameter name="fill">#0000ff</sld:CssParameter><sld:CssParameter name="fill-opacity">0.5</sld:CssParameter></sld:Fill><sld:Stroke><sld:CssParameter name="stroke">#000000</sld:CssParameter></sld:Stroke></sld:PolygonSymbolizer></sld:Rule></sld:FeatureTypeStyle></sld:UserStyle></sld:NamedLayer></sld:StyledLayerDescriptor>
  • 1
    Is your selection of features that should be rendered with lower opacity static or does it need to be dynamic? – Thomas B Feb 16 '15 at 16:58
  • Hi @Thomas. I wanted it to be dynamic, i.e., be able to set a new style map on run time. But even it only would work in a static way, I would appreciate some help. Thanks – joaorodr84 Feb 16 '15 at 17:42
1

You could define two or more rules. Either static in your sld style or dynamic with mergeNewParams:

theSLD_BODY= '<?xml version="1.0" encoding="utf-8"?>' + '<StyledLayerDescriptor version="1.0.0" xmlns="http://www.opengis.net/sld" xmlns:gml="http://www.opengis.net/gml" xmlns:ogc="http://www.opengis.net/ogc" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengeospatial.net/sld/1.0.0/StyledLayerDescriptor.xsd"><NamedLayer><Name>topp:states</Name>'+
//'<NamedStyle><Name>pophatch</Name></NamedStyle>'+
'<UserStyle><FeatureTypeStyle>'+

'<Rule><ogc:Filter><ogc:PropertyIsEqualTo><ogc:PropertyName>STATE_NAME</ogc:PropertyName><ogc:Literal>Nebraska</ogc:Literal></ogc:PropertyIsEqualTo></ogc:Filter><PolygonSymbolizer>'+
'<Fill><CssParameter name="fill">#0077A2</CssParameter>'+
'<CssParameter name="fill-opacity">1.0</CssParameter></Fill>'+
'<Stroke><CssParameter name="stroke">%23FF0000</CssParameter></Stroke></PolygonSymbolizer></Rule>'+

'<Rule><ogc:Filter><ogc:PropertyIsGreaterThan><ogc:PropertyName>MALE</ogc:PropertyName><ogc:Literal>2433346</ogc:Literal></ogc:PropertyIsGreaterThan></ogc:Filter><PolygonSymbolizer>'+
'<Fill><CssParameter name="fill">#0077A2</CssParameter>'+
'<CssParameter name="fill-opacity">0.1</CssParameter></Fill>'+
'<Stroke><CssParameter name="stroke">%23FF0000</CssParameter></Stroke></PolygonSymbolizer></Rule>'+


'</FeatureTypeStyle></UserStyle>'+
'</NamedLayer></StyledLayerDescriptor>';



        map.layers[0].mergeNewParams({"SLD_BODY" : theSLD_BODY});

If you copy this code into your clipboard and run it in your console on the following webpage you should see polygons with two different opacities:

http://demo.opengeo.org/geoserver/topp/wms?service=WMS&version=1.1.0&request=GetMap&layers=topp:states&styles=&bbox=-124.73142200000001,24.955967,-66.969849,49.371735&width=780&height=330&srs=EPSG:4326&format=application/openlayers

Update: See my jsfiddle: http://jsfiddle.net/expedio/x1xxb08g/

Update2: This is also a nice example for changing wms-Style with SLD_BODY: http://gis.ibbeck.de/OLClient/examples/wms_sld_world.asp

  • Hi @Thomas. Thank you for you intervention. I tried your example but I still see only elements with opacity 1. Your example is quite like mine. Please see my EDIT 1. Is it really possible to have different opcitis in the same image/wms? – joaorodr84 Feb 17 '15 at 14:37
  • 1
    I just made a jsfiddle to show how i set the WMS to display features with different opacity: jsfiddle.net/expedio/x1xxb08g/2 – Thomas B Feb 17 '15 at 15:32
  • Thanks a lot for your fiddle, @Thomas. I was able to pinpoint my mistake thanks to that. I was using "palette = 'safe'" in the WMS params, and it seems it does not work like this. I removed the safe palette and it works like a charm. Thanks! :) – joaorodr84 Feb 17 '15 at 16:25

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.