0

My goal is to create legends, for WMS layers, which can be shown in a JavaScript/OpenLayers 3 client.

Furthermore I found out, that GeoServer can generate legends via the SLD styles. This legends can be requested with a GetLegendGraphic-call.

I chose YSLD over SLD to write my styles because it is more readable and easier to write. Therefore my YSLD files look more or less like this:

name: simple-poly
title: 'A simple ysld style'
feature-styles: 
- rules:
  - name: Rule uses Categorize()
    scale: [min,max]
    symbolizers:  
    - polygon:
        fill-color: ${Categorize(foo,
          '#006600','73',
          '#00CC00','79',
          '#66FF66','85',
          '#996600','88',
          '#FFCC66','94',
          '#CCCCCC')}

Especially the Categorize() function saves a lot of lines. With these few lines my WMS layer gets rendered as expected.

The drawback with this approach seems to be that GeoServer can not handle the Categorize() function when creating a legend. Because the result of the GetLegendGraphic-call is an image with a gray square: The created legend

What is the best way to create a legend using YSLD, especially when Categorize() is used? Or is it simply not possible to use Categorize() and creating legends automatically?

  • I'm not sure what you are asking. On the one hand you seem to be saying that you know that Categorize() doesn't work in YSLD in GeoServer, and on the other you want to know how to use Categorize() with YSLD in Geoserver. – nmtoken May 16 '17 at 14:43
  • The YSLD style works for rendering the layer, but not for creating the legend for that layer. I added a sentence to clarify that the style works when rendering. – gillesB May 16 '17 at 15:11
  • GetLegendGraphic is an (WMS +) SLD operation – nmtoken May 16 '17 at 18:04
  • So to get a proper legend I have to forget about YSLD and use SLD instead? – gillesB May 16 '17 at 21:14
  • I don't know how the ysld plugin works with GeoServer and WMS operations; I guess that if what you have in YSLD is not possible in SLD, then your GetLegendGraphic won't work as expected. The answer to this question seems to confirm this thought. – nmtoken May 17 '17 at 6:19
2

I took a look into into the generated SLD and saw that it also uses the Categorize() function. Therefore I tried to rewrite the YSLD without using Categorize(). With that YSLD the legend was created as expected. This makes sense as that YSLD can be translated by GeoServer to a SLD file, which does not use functions. Therefore I also assume that other YSLD functions break the legend functionality.

Results

name: simple-poly
title: 'A simple ysld style'
feature-styles:
- name: 'A Rule'  
  rules:
  - name: below 73
    filter: ${foo < 73}
    scale: [min,max]
    symbolizers:  
    - polygon:
        fill-color: '#006600'
  - name: between 73 and 79
    filter: ${foo >= 73 AND foo < 79}
    scale: [min,max]
    symbolizers:  
    - polygon:
        fill-color: '#00CC00'       
  - name: between 79 and 85
    filter: ${foo >= 79 AND foo < 85}
    scale: [min,max]
    symbolizers:  
    - polygon:
        fill-color: '#66FF66'       
  - name: between 85 and 88
    filter: ${foo >= 85 AND foo < 88}
    scale: [min,max]
    symbolizers:  
    - polygon:
        fill-color: '#996600'
  - name: between 88 and 94
    filter: ${foo >= 88 AND foo < 94}
    scale: [min,max]
    symbolizers:  
    - polygon:
        fill-color: '#FFCC66'
  - name: below 94
    filter: ${foo > 94}
    scale: [min,max]
    symbolizers:  
    - polygon:
        fill-color: '#CCCCCC'  

The created legend

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.