1

I wrote some SLD code to style my map layer, which is stored on GeoServer. The GeoServer input window for SLD does not show any error message, but when I open my map in OpenLayers it does not show the map as i wish. In this case, my intend is to color the country polygons similar to the safety level, which is a decimal in my postgres database, so that finally dangerous regions should be like red and safe regions like green.

My code:

<!-- Template taken from: http://docs.geoserver.org/stable/en/user/styling/sld/cookbook/polygons.html#attribute-based-polygon -->
<?xml version="1.0" encoding="UTF-8"?>
<sld:UserStyle xmlns="http://www.opengis.net/sld" xmlns:sld="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml">
  <sld:Title/>
  <FeatureTypeStyle>
     <Rule>
       <Name>HighRisk</Name>
       <Title>WGI &lt; -0.5</Title>
       <ogc:Filter>
         <ogc:PropertyIsLessThan>
           <ogc:PropertyName>wgi</ogc:PropertyName>
           <ogc:Literal>-0.5</ogc:Literal>
         </ogc:PropertyIsLessThan>
       </ogc:Filter>
       <PolygonSymbolizer>
         <Fill>
           <CssParameter name="fill">#fc8d59</CssParameter>
         </Fill>
       </PolygonSymbolizer>
     </Rule>
     <Rule>
       <Name>MediumRisk</Name>
       <Title>WGI &gt; -0.5 und &lt; 0.5</Title>
       <ogc:Filter>
         <ogc:And>
           <ogc:PropertyIsGreaterThanOrEqualTo>
             <ogc:PropertyName>wgi</ogc:PropertyName>
             <ogc:Literal>-0.5</ogc:Literal>
           </ogc:PropertyIsGreaterThanOrEqualTo>
           <ogc:PropertyIsLessThan>
             <ogc:PropertyName>wgi</ogc:PropertyName>
             <ogc:Literal>0.5</ogc:Literal>
           </ogc:PropertyIsLessThan>
         </ogc:And>
       </ogc:Filter>
       <PolygonSymbolizer>
         <Fill>
           <CssParameter name="fill">#ffffbf</CssParameter>
         </Fill>
       </PolygonSymbolizer>
     </Rule>
     <Rule>
       <Name>LowRisk</Name>
       <Title>WGI &gt; 0.5</Title>
       <ogc:Filter>
         <ogc:PropertyIsGreaterThan>
           <ogc:PropertyName>WGI</ogc:PropertyName>
           <ogc:Literal>0.5</ogc:Literal>
         </ogc:PropertyIsGreaterThan>
       </ogc:Filter>
       <PolygonSymbolizer>
         <Fill>
           <CssParameter name="fill">#91cf60</CssParameter>
         </Fill>
       </PolygonSymbolizer>
     </Rule>
      <Rule>
        <Title>Boundary</Title>
        <LineSymbolizer>
          <Stroke>
            <CssParameter name="stroke-width">0.2</CssParameter>
            <CssParameter name="stroke">#e2e2e2</CssParameter>
          </Stroke>
        </LineSymbolizer>
        <TextSymbolizer>
          <Label>
            <ogc:PropertyName>STATE_ABBR</ogc:PropertyName>
          </Label>
          <Font>
            <CssParameter name="font-family">Times New Roman</CssParameter>
            <CssParameter name="font-style">Normal</CssParameter>
            <CssParameter name="font-size">14</CssParameter>
          </Font>
          <LabelPlacement>
            <PointPlacement>
              <AnchorPoint>
                <AnchorPointX>-0.5</AnchorPointX>
                <AnchorPointY>0.5</AnchorPointY>
              </AnchorPoint>
            </PointPlacement>
          </LabelPlacement>
        </TextSymbolizer>
        </Rule>
   </FeatureTypeStyle>
</sld:UserStyle>

Unfortunately my map looks like this so far:

enter image description here

Any help and tips are appreciated!

WillG
  • 41
  • 7

1 Answers1

1

First, in my case your SLD did not validate correctly on my GeoServer.

You are missing the StyledLayerDescriptor-tag around them all and the NamedLayer-tag around the UserStyle. Also, there was a closing tag for sld:Title which is not necessary and and may let the interpreter behind ending up in some errors.

Seems like the GeoServer falls back to the default style, which is a grey fill and black stroke.

I think your SLD should be something like this, the GeoServer validates it as correctly and can even create a legend for that.

<?xml version="1.0" encoding="ISO-8859-1"?>
<StyledLayerDescriptor version="1.0.0"
    xsi:schemaLocation="http://www.opengis.net/sld StyledLayerDescriptor.xsd"
    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">
  <NamedLayer>
    <Name>A named Layer</Name>
    <UserStyle>
      <Title>Dangerous and safe regions</Title>
      <FeatureTypeStyle>
       <Rule>
         <Name>HighRisk</Name>
         <Title>WGI &lt; -0.5</Title>
         <ogc:Filter>
           <ogc:PropertyIsLessThan>
             <ogc:PropertyName>wgi</ogc:PropertyName>
             <ogc:Literal>-0.5</ogc:Literal>
           </ogc:PropertyIsLessThan>
         </ogc:Filter>
         <PolygonSymbolizer>
           <Fill>
             <CssParameter name="fill">#fc8d59</CssParameter>
           </Fill>
         </PolygonSymbolizer>
       </Rule>
       <Rule>
         <Name>MediumRisk</Name>
         <Title>WGI &gt; -0.5 und &lt; 0.5</Title>
         <ogc:Filter>
           <ogc:And>
             <ogc:PropertyIsGreaterThanOrEqualTo>
               <ogc:PropertyName>wgi</ogc:PropertyName>
               <ogc:Literal>-0.5</ogc:Literal>
             </ogc:PropertyIsGreaterThanOrEqualTo>
             <ogc:PropertyIsLessThan>
               <ogc:PropertyName>wgi</ogc:PropertyName>
               <ogc:Literal>0.5</ogc:Literal>
             </ogc:PropertyIsLessThan>
           </ogc:And>
         </ogc:Filter>
         <PolygonSymbolizer>
           <Fill>
             <CssParameter name="fill">#ffffbf</CssParameter>
           </Fill>
         </PolygonSymbolizer>
       </Rule>
       <Rule>
         <Name>LowRisk</Name>
         <Title>WGI &gt; 0.5</Title>
         <ogc:Filter>
           <ogc:PropertyIsGreaterThan>
             <ogc:PropertyName>WGI</ogc:PropertyName>
             <ogc:Literal>0.5</ogc:Literal>
           </ogc:PropertyIsGreaterThan>
         </ogc:Filter>
         <PolygonSymbolizer>
           <Fill>
             <CssParameter name="fill">#91cf60</CssParameter>
           </Fill>
         </PolygonSymbolizer>
       </Rule>
        <Rule>
          <Title>Boundary</Title>
          <LineSymbolizer>
            <Stroke>
              <CssParameter name="stroke-width">0.2</CssParameter>
              <CssParameter name="stroke">#e2e2e2</CssParameter>
            </Stroke>
          </LineSymbolizer>
          <TextSymbolizer>
            <Label>
              <ogc:PropertyName>STATE_ABBR</ogc:PropertyName>
            </Label>
            <Font>
              <CssParameter name="font-family">Times New Roman</CssParameter>
              <CssParameter name="font-style">Normal</CssParameter>
              <CssParameter name="font-size">14</CssParameter>
            </Font>
            <LabelPlacement>
              <PointPlacement>
                <AnchorPoint>
                  <AnchorPointX>-0.5</AnchorPointX>
                  <AnchorPointY>0.5</AnchorPointY>
                </AnchorPoint>
              </PointPlacement>
            </LabelPlacement>
          </TextSymbolizer>
          </Rule>
     </FeatureTypeStyle>
    </UserStyle>
  </NamedLayer>
</StyledLayerDescriptor>

Also ensure, setting the style as default for the layer in the GeoServer itself or sending a styles-Parameter in the request OpenLayers is creating.

Hope I helped here, let me know if I am wrong or it is not appear to work either.

  • Thanks Alex, I had some trouble with referencing the PropertyName (didn't know SLD is case sensitive) but then your script worked out. – WillG Oct 31 '19 at 17:09