banner
陈不易

陈不易

没有技术想聊生活
twitter
medium
tg_channel

GeoServer Multi-band Image Using the Same Style

Introduction#

The requirement is to have data for ocean features with 12 channels, where each channel corresponds to data for a specific month. After publishing the layer, it should be possible to select the corresponding channel using a style and display the data for a specific month. The straightforward approach would be to create 12 copies of the style. However, for easier maintenance (which will most likely be done by myself), I wanted to find a way similar to "dynamic styles" where parameters can be obtained externally and the same style can be used to select different channels based on different parameters.

Here, I fell into a pitfall of my own assumption: the production environment is using a relatively old version of GeoServer, 2.11.x. When I was reading the documentation, I was referring to the latest version, and after testing it didn't work, I looked at the documentation for version 2.11. Although the documentation had a similar usage, it was not available for channel selection.

Therefore, this approach is only applicable to newer versions.

Layer Publishing#

The layer publishing can be done for multi-channel images.

Setting the Style#

The general channel selection for band fusion is done in the following format1:

<ChannelSelection>
  <RedChannel>
    <SourceChannelName>1</SourceChannelName>
  </RedChannel>
  <GreenChannel>
    <SourceChannelName>2</SourceChannelName>
  </GreenChannel>
  <BlueChannel>
    <SourceChannelName>3</SourceChannelName>
  </BlueChannel>
</ChannelSelection>

In the style, 1, 2, and 3 channels correspond to (R, G, B).

For selecting a single channel to display, use the Function to get the "environment variable" and replace the default value:

<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>

In this case, the channel name is wrapped in a Function object, which provides a default value of 1 when the value of m in env is empty. If m is not empty, its value is used as the channel name.

To select channel 2 for display, add &env=m:2 to the WMS request:

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>


September

March

Conclusion#

In the end, because it was not easy to update the production environment version, I ended up copying 12*2 styles myself.

Footnotes#

  1. GeoServer: RasterSymbolizer

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.