nsidc / qgreenland

Source code for generating the QGreenland package hosted at https://qgreenland.org/
https://qgreenland.readthedocs.io
Other
36 stars 9 forks source link

Raster colormap labels not showing units #685

Closed trey-stafford closed 1 year ago

trey-stafford commented 1 year ago

Raster colormap legends in QGIS 3.28 look better than they had previously (shows a color ramp with max/min values instead of an entry for each discrete color step). However, I noticed that units suffix isn't showing up (e.g., for "Terrain models/Bedmachine/Ice thickness (150m)" should indicate "m" for meters after the min/max value).

Can we fix / change how the color ramp is displayed in the legend so that the units are shown? Maybe this is a bug w/ QGIS?

MattF-NSIDC commented 1 year ago

@trey-stafford I think this will be interesting to you.

While we are correctly setting "unit suffix" in our styles, it only applies to non-continuous legends. In the symbology menu, below the "classes" input field, there is a "Legend Settings..." button. Here you can enable/disable the continuous legend (enabled by default), or set a prefix or suffix specific to the continuous legend.

Among other things, the orientation of the legend can also be changed to "horizontal" here, which I think is a much better use of space in the Layer Properties menu.

We could validate that the "Label unit suffix" matches the "continuous legend suffix" for every style. We could also validate that continuous legends are set to horizontal :)

For future reference, the continuous legend was added in 3.18: https://gis.stackexchange.com/a/156722 (the author of the answer to this post is a QGIS developer, I didn't find a corresponding changelog entry in my quick search).

MattF-NSIDC commented 1 year ago

Here's the XML that sets the continuous legend on and sets the suffix:

<rampLegendSettings orientation="1" direction="0" maximumLabel="" suffix="%" useContinuousLegend="1" prefix="" minimumLabel="">

The XML in our current styles sets the suffix by baking it into the label property of each item element within a colorrampshader element:

          <item value="15" color="#040613" alpha="255" label="15 %"/>
          <item value="15.3315" color="#050614" alpha="255" label="15.3315 %"/>
          <item value="15.3315" color="#050614" alpha="255" label="15.3315 %"/>
          <item value="15.663" color="#050715" alpha="255" label="15.663 %"/>

The problem is QGIS sometimes defaults to a continuous color ramp even if the style is built on a QGIS version before support for continuous color ramps were added. I think that happens when the "Mode" is set to "Continuous" in the symbology menu. But haven't figured out where that is in the XML.

MattF-NSIDC commented 1 year ago

"Mode is Continuous" appears to be represented by classificationMode="1" on the colorrampshader tag.

<colorrampshader clip="1" maximumValue="100" classificationMode="1" ...>
MattF-NSIDC commented 1 year ago

I think our validator logic should look like: