Introduction#
The requirement is to have a dataset of ocean features with 12 channels
, corresponding to data for 12 months. After the layer is published, styles can be used to select the corresponding channel to display data for a specific month. A simple and straightforward approach is to copy 12 styles
, but for easier future maintenance (most likely to be maintained by oneself), I wanted to find a way similar to "dynamic styles" that can fetch parameters from external sources, using the same style
to select different channels
through different parameters.
Here, I was slightly misled by my own assumptions: the version of GeoServer
used in the production environment is relatively low, 2.11.x
. When I looked at the documentation, I was looking at the latest version, and after testing it didn't work, I checked the 2.11
documentation. Although there are similar usages in the documentation, it is not applicable when selecting channels
.
Therefore, this method is only suitable for newer versions.
Layer Publishing#
Layer publishing can theoretically handle multi-channel images.
Setting Styles#
The general form of channel selection for band merging is as follows1.
<ChannelSelection>
<RedChannel>
<SourceChannelName>1</SourceChannelName>
</RedChannel>
<GreenChannel>
<SourceChannelName>2</SourceChannelName>
</GreenChannel>
<BlueChannel>
<SourceChannelName>3</SourceChannelName>
</BlueChannel>
</ChannelSelection>
In the style
, channels 1, 2, and 3 correspond to (R, G, B)
.
For selecting a single channel
for display, use Function
to obtain "environment variables" and replace default values.
<RasterSymbolizer>
<Opacity>1.0</Opacity>
<ChannelSelection>
<GrayChannel>
<SourceChannelName>
<Function name="env">
<ogc:Literal>m</ogc:Literal>
<ogc:Literal>1</ogc:Literal>
</ogc:Function>
</SourceChannelName>
</GrayChannel>
</ChannelSelection>
</RasterSymbolizer>
Here, the channel
name wraps a Function object, which provides 1
as the default value when the value of m
in env
is empty. If m
is not empty, the value of m
is used as the channel
name.
In the wms
request, adding &env=m:2 will select the channel numbered 2 for display.
http://localhost:8083/geoserver/wms?service=WMS&version=1.1.0&request=GetMap&layers=geosolutions:usa&styles=&bbox=-130.85168,20.7052,-62.0054,54.1141&width=768&height=372&srs=EPSG:4326&format=application/openlayers&env=m:2
Here is a complete style:
<?xml version="1.0" encoding="UTF-8"?>
<StyledLayerDescriptor xmlns="http://www.opengis.net/sld" 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.opengis.net/sld/1.0.0/StyledLayerDescriptor.xsd" version="1.0.0">
<NamedLayer>
<Name>saltsld</Name>
<UserStyle>
<Title>A raster style</Title>
<FeatureTypeStyle>
<Rule>
<RasterSymbolizer>
<Opacity>1.0</Opacity>
<ChannelSelection>
<GrayChannel>
<SourceChannelName><ogc:Function name="env">
<ogc:Literal>m</ogc:Literal>
<ogc:Literal>1</ogc:Literal>
</ogc:Function></SourceChannelName>
</GrayChannel>
</ChannelSelection>
<ColorMap>
<ColorMapEntry color="#0000ff" quantity="28.0"/>
<ColorMapEntry color="#009933" quantity="30.0"/>
<ColorMapEntry color="#ff9900" quantity="32.0" />
<ColorMapEntry color="#ff0000" quantity="34.0"/>
</ColorMap>
</RasterSymbolizer>
</Rule>
</FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>
Conclusion#
Ultimately, due to the difficulty in updating the production environment version, I ended up copying 12*2 styles myself.