[GIS] Making Layers change color based on attribute expression in GeoServer

dynamicgeoserverlayers

At the moment I'm working with a dataset that has a count value as the only value in the database.

However, if I add &VIEWPARAMS:Sex=Male to the given url from GeoServer, the results change based on this query.

I want to change the layer of GeoServer based on what is given in this query.

For instance, if Sex=Male I want the color of the layer to be blue and if Sex=Female I want the color to be pink (in order to give a beter representation and eventually split up the rasters).

Does anyone have a solution for this, because I have been looking for days and I have not found any possible solutions?

-EDIT-

Added both pictures the picture in pink is how the dataset for the female set should look like and the blue one is the male set. What I now want to realise is that if I change the url by adding: 'VIEWPARAMS=Sex:male' I want the color of the map to turn blue and if I do 'VIEWPARAMS=Sex:female' I want the color of the map to turn pink. Without adding another kind of command like '&layers=Male_Layer' or '&layers=Female_Layer'. Is this possible in geoserver?

Picture1
Picture2

Incase the pictures are not working here are the url's http://imgur.com/VCrkoss and http://imgur.com/bmFNFCB

Best Answer

Because the only attribute in the selected data is count you can't make different colors with one SLD by using filter. I believe that the only alternative is then to have two basic styles which use different default styles. You can configure named styles on server side and select them with WMS url as &Sex=Male&styles=male. Or you can add the whole SDL into GetMap request.

Here are two fast ugly examples which should work for you if you have local GeoServer running on localhost:8080 with topp:states demo layer installed. Green output:

enter image description here

http://localhost:8080/geoserver/wms?REQUEST=GetMap&SERVICE=WMS&VERSION=1.1.1&TILED=TRUE&TRANSPARENT=TRUE&WIDTH=256&HEIGHT=256&FORMAT=image/jpeg&LAYERS=topp:states&SRS=EPSG:4326&bbox=-79.828125,36.50857729743935,-73.125,42.032974332441405&SLD_BODY=%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%3Csld%3AStyledLayerDescriptor%20xmlns%3Asld%3D%22http%3A%2F%2Fwww.opengis.net%2Fsld%22%20xmlns%3Ajava%3D%22java%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%20xmlns%3Awfs%3D%22http%3A%2F%2Fwww.opengis.net%2Fwfs%22%20xmlns%3Axsi%3D%22http%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema-instance%22%20xmlns%3Axslutil%3D%22de.latlon.deejump.plugin.style.XSLUtility%22%20xmlns%3Adeegreewfs%3D%22http%3A%2F%2Fwww.deegree.org%2Fwfs%22%20xmlns%3Afo%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2FXSL%2FFormat%22%20xmlns%3Agml%3D%22http%3A%2F%2Fwww.opengis.net%2Fgml%22%20xmlns%3Aogc%3D%22http%3A%2F%2Fwww.opengis.net%2Fogc%22%20xmlns%3D%22http%3A%2F%2Fwww.opengis.net%2Fsld%22%20xsi%3AschemaLocation%3D%22http%3A%2F%2Fwww.opengis.net%2Fsld%20http%3A%2F%2Fschemas.opengis.net%2Fsld%2F1.0.0%2FStyledLayerDescriptor.xsd%22%20version%3D%221.0.0%22%3E%0A%3Csld%3ANamedLayer%3E%0A%3Csld%3AName%3Etopp%3Astates%3C%2Fsld%3AName%3E%0A%3Csld%3AUserStyle%3E%0A%3Csld%3AName%3Estates%3C%2Fsld%3AName%3E%0A%3Csld%3ATitle%3Estates%3C%2Fsld%3ATitle%3E%0A%3Csld%3AIsDefault%3E1%3C%2Fsld%3AIsDefault%3E%0A%3Csld%3AFeatureTypeStyle%3E%0A%3Csld%3AName%3Estates%3C%2Fsld%3AName%3E%0A%3Csld%3ARule%3E%3Csld%3AName%3EbasicPolyStyle%3C%2Fsld%3AName%3E%0A%3Csld%3AMinScaleDenominator%3E0.0%3C%2Fsld%3AMinScaleDenominator%3E%0A%3Csld%3AMaxScaleDenominator%3E4.724409448771654E14%3C%2Fsld%3AMaxScaleDenominator%3E%0A%3Csld%3APolygonSymbolizer%3E%0A%3Csld%3AGeometry%3E%0A%3Cogc%3APropertyName%3Ethe_geom%3C%2Fogc%3APropertyName%3E%0A%3C%2Fsld%3AGeometry%3E%0A%3Csld%3AFill%3E%0A%3Csld%3ACssParameter%20name%3D%22fill%22%3E%2300b400%3C%2Fsld%3ACssParameter%3E%0A%3Csld%3ACssParameter%20name%3D%22fill-opacity%22%3E0.588%3C%2Fsld%3ACssParameter%3E%0A%3C%2Fsld%3AFill%3E%0A%3Csld%3AStroke%3E%0A%3Csld%3ACssParameter%20name%3D%22stroke%22%3E%23007d00%3C%2Fsld%3ACssParameter%3E%0A%3Csld%3ACssParameter%20name%3D%22stroke-opacity%22%3E0.588%3C%2Fsld%3ACssParameter%3E%0A%3Csld%3ACssParameter%20name%3D%22stroke-width%22%3E1%3C%2Fsld%3ACssParameter%3E%0A%3C%2Fsld%3AStroke%3E%3C%2Fsld%3APolygonSymbolizer%3E%3C%2Fsld%3ARule%3E%0A%3C%2Fsld%3AFeatureTypeStyle%3E%0A%3C%2Fsld%3AUserStyle%3E%0A%3C%2Fsld%3ANamedLayer%3E%0A%3C%2Fsld%3AStyledLayerDescriptor%3E

Pink output:

enter image description here

http://localhost:8080/geoserver/wms?REQUEST=GetMap&SERVICE=WMS&VERSION=1.1.1&TILED=TRUE&TRANSPARENT=TRUE&WIDTH=256&HEIGHT=256&FORMAT=image/jpeg&LAYERS=topp:states&SRS=EPSG:4326&bbox=-79.828125,36.50857729743935,-73.125,42.032974332441405&SLD_BODY=%3C%3Fxml%20version%3D%221.0%22%20encoding%3D%22UTF-8%22%3F%3E%3Csld%3AStyledLayerDescriptor%20xmlns%3Asld%3D%22http%3A%2F%2Fwww.opengis.net%2Fsld%22%20xmlns%3Ajava%3D%22java%22%20xmlns%3Axlink%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2Fxlink%22%20xmlns%3Awfs%3D%22http%3A%2F%2Fwww.opengis.net%2Fwfs%22%20xmlns%3Axsi%3D%22http%3A%2F%2Fwww.w3.org%2F2001%2FXMLSchema-instance%22%20xmlns%3Axslutil%3D%22de.latlon.deejump.plugin.style.XSLUtility%22%20xmlns%3Adeegreewfs%3D%22http%3A%2F%2Fwww.deegree.org%2Fwfs%22%20xmlns%3Afo%3D%22http%3A%2F%2Fwww.w3.org%2F1999%2FXSL%2FFormat%22%20xmlns%3Agml%3D%22http%3A%2F%2Fwww.opengis.net%2Fgml%22%20xmlns%3Aogc%3D%22http%3A%2F%2Fwww.opengis.net%2Fogc%22%20xmlns%3D%22http%3A%2F%2Fwww.opengis.net%2Fsld%22%20xsi%3AschemaLocation%3D%22http%3A%2F%2Fwww.opengis.net%2Fsld%20http%3A%2F%2Fschemas.opengis.net%2Fsld%2F1.0.0%2FStyledLayerDescriptor.xsd%22%20version%3D%221.0.0%22%3E%0A%3Csld%3ANamedLayer%3E%0A%3Csld%3AName%3Etopp%3Astates%3C%2Fsld%3AName%3E%0A%3Csld%3AUserStyle%3E%0A%3Csld%3AName%3Estates%3C%2Fsld%3AName%3E%0A%3Csld%3ATitle%3Estates%3C%2Fsld%3ATitle%3E%0A%3Csld%3AIsDefault%3E1%3C%2Fsld%3AIsDefault%3E%0A%3Csld%3AFeatureTypeStyle%3E%0A%3Csld%3AName%3Estates%3C%2Fsld%3AName%3E%0A%3Csld%3ARule%3E%3Csld%3AName%3EbasicPolyStyle%3C%2Fsld%3AName%3E%0A%3Csld%3AMinScaleDenominator%3E0.0%3C%2Fsld%3AMinScaleDenominator%3E%0A%3Csld%3AMaxScaleDenominator%3E4.724409448771654E14%3C%2Fsld%3AMaxScaleDenominator%3E%0A%3Csld%3APolygonSymbolizer%3E%0A%3Csld%3AGeometry%3E%0A%3Cogc%3APropertyName%3Ethe_geom%3C%2Fogc%3APropertyName%3E%0A%3C%2Fsld%3AGeometry%3E%0A%3Csld%3AFill%3E%0A%3Csld%3ACssParameter%20name%3D%22fill%22%3E%23FF0066%3C%2Fsld%3ACssParameter%3E%0A%3Csld%3ACssParameter%20name%3D%22fill-opacity%22%3E0.588%3C%2Fsld%3ACssParameter%3E%0A%3C%2Fsld%3AFill%3E%0A%3Csld%3AStroke%3E%0A%3Csld%3ACssParameter%20name%3D%22stroke%22%3E%23007d00%3C%2Fsld%3ACssParameter%3E%0A%3Csld%3ACssParameter%20name%3D%22stroke-opacity%22%3E0.588%3C%2Fsld%3ACssParameter%3E%0A%3Csld%3ACssParameter%20name%3D%22stroke-width%22%3E1%3C%2Fsld%3ACssParameter%3E%0A%3C%2Fsld%3AStroke%3E%3C%2Fsld%3APolygonSymbolizer%3E%3C%2Fsld%3ARule%3E%0A%3C%2Fsld%3AFeatureTypeStyle%3E%0A%3C%2Fsld%3AUserStyle%3E%0A%3C%2Fsld%3ANamedLayer%3E%0A%3C%2Fsld%3AStyledLayerDescriptor%3E

One of SLD files without URL-encoding - Yes, it is rather long and only one parameter is changing <sld:CssParameter name="fill">#00b400</sld:CssParameter>

<?xml version="1.0" encoding="UTF-8"?><sld:StyledLayerDescriptor xmlns:sld="http://www.opengis.net/sld" xmlns:java="java" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:wfs="http://www.opengis.net/wfs" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xslutil="de.latlon.deejump.plugin.style.XSLUtility" xmlns:deegreewfs="http://www.deegree.org/wfs" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:gml="http://www.opengis.net/gml" xmlns:ogc="http://www.opengis.net/ogc" xmlns="http://www.opengis.net/sld" xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd" version="1.0.0">
<sld:NamedLayer>
<sld:Name>topp:states</sld:Name>
<sld:UserStyle>
<sld:Name>states</sld:Name>
<sld:Title>states</sld:Title>
<sld:IsDefault>1</sld:IsDefault>
<sld:FeatureTypeStyle>
<sld:Name>states</sld:Name>
<sld:Rule><sld:Name>basicPolyStyle</sld:Name>
<sld:MinScaleDenominator>0.0</sld:MinScaleDenominator>
<sld:MaxScaleDenominator>4.724409448771654E14</sld:MaxScaleDenominator>
<sld:PolygonSymbolizer>
<sld:Geometry>
<ogc:PropertyName>the_geom</ogc:PropertyName>
</sld:Geometry>
<sld:Fill>
<sld:CssParameter name="fill">#00b400</sld:CssParameter>
<sld:CssParameter name="fill-opacity">0.588</sld:CssParameter>
</sld:Fill>
<sld:Stroke>
<sld:CssParameter name="stroke">#007d00</sld:CssParameter>
<sld:CssParameter name="stroke-opacity">0.588</sld:CssParameter>
<sld:CssParameter name="stroke-width">1</sld:CssParameter>
</sld:Stroke></sld:PolygonSymbolizer></sld:Rule>
</sld:FeatureTypeStyle>
</sld:UserStyle>
</sld:NamedLayer>
</sld:StyledLayerDescriptor>
Related Question