1

I need to color polygons based on HEX value from attribute. Is that possible?

To create categories by colors is not really good solution for me, since in future more colors will be added which I don't know at this point. And I want to avoid editing style each time.

GeoServer version 2.23.2. Data sourced in PostgreSQL databse.

Tried to create such SLD style in QGIS but don't see any ways to do that.

1 Answer 1

2

There are two ways to accomplish this. If you want to colour your polygon layer based on hard-coded values, you can achieve this by obtaining the HEX colour from the layer and assigning it to your SLD variable, where "Color" is the field containing HEX values.

<PolygonSymbolizer>
    <Fill>
        <CssParameter name="fill">
            <ogc:PropertyName>Color</ogc:PropertyName>
        </CssParameter>
    </Fill>
    <Stroke>
        <CssParameter name="stroke">#000000</CssParameter>
        <CssParameter name="stroke-width">1</CssParameter>
    </Stroke>
</PolygonSymbolizer>

In another case, you can use the interpolate approach so you don't need to specify any hard-coded colour value in your data. In the following code, "Value" is the field you want to symbolize your data based on that.

<PolygonSymbolizer>
   <Fill>
     <CssParameter name="fill">
       <ogc:Function name="Interpolate">
         <!-- Property to transform -->
         <ogc:PropertyName>Value</ogc:PropertyName>

         <!-- Mapping curve definition pairs (input, output) -->
         <ogc:Literal>0</ogc:Literal>
         <ogc:Literal>#fefeee</ogc:Literal>

         <ogc:Literal>9000000</ogc:Literal>
         <ogc:Literal>#00ff00</ogc:Literal>

         <ogc:Literal>23000000</ogc:Literal>
         <ogc:Literal>#ff0000</ogc:Literal>

         <!-- Interpolation method -->
         <ogc:Literal>color</ogc:Literal>

         <!-- Interpolation mode - defaults to linear -->
       </ogc:Function>
     </CssParameter>
   </Fill>
</PolygonSymbolizer>
1
  • Thanks a lot! That works, we have used first option. Commented Jun 25 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.