1

I’ve a PostGIS table which is published as a Geoserver layer. Every feature in the table has some attributes that should style that feature (e.g color, size, opacity, external graphic url, ... etc).

I can create an SLD document and let it read its styling attributes from the table using <PropertyName>, but the problem is more complex than this, for example: some points features need to be styled as a simple circles, while some other need to be styled as external-graphic images. And from the SLD Reference, the SLD PointSymbolizer must have either <Mark> or <ExternalGraphic> element, for that reason I should create two rules one for the simple point symbolizer and the other for the external-graphic point symbolizer, and should add a filter to each rule so that it deals only with the appropriate features, something like:

<FeatureTypeStyle>
    <Rule>
        <ogc:Filter>
            <ogc:PropertyIsEqual>
                <ogc:PropertyName>is-external-graphic</ogc:PropertyName>
                <ogc:Literal>0</ogc:Literal>
            </ogc:PropertyIsEqual>
        </ogc:Filter>
        <PointSymbolizer>
            <Graphic>
                <Mark>
                    <WellKnownName>circle</WellKnownName>
                    <Fill>
                        <CssParameter name="fill">
                            <ogc:PropertyName>fill-color</ogc:PropertyName>
                        </CssParameter>
                    </Fill>
                </Mark>
                <Size>6</Size>
            </Graphic>
        </PointSymbolizer>
    </Rule>
    <Rule>
        <ogc:Filter>
            <ogc:PropertyIsEqual>
                <ogc:PropertyName>is-external-graphic</ogc:PropertyName>
                <ogc:Literal>1</ogc:Literal>
            </ogc:PropertyIsEqual>
        </ogc:Filter>
        <PointSymbolizer>
            <Graphic>
                <ExternalGraphic>
                    <OnlineResource
               xlink:type="simple"
               xlink:href="smileyface.png" />
                    <Format>image/png</Format>
                </ExternalGraphic>
                <Size>6</Size>
            </Graphic>
        </PointSymbolizer>
    </Rule>
</FeatureTypeStyle>

My question is: Is there any better solution that is more dynamic?

1

If you set up a single rule with an ExternalGraphic followed by a Mark then only the first one that can be drawn will be drawn.

So this rule draws faces or dots depending on the existance of the existance of the jepg file:

     <PointSymbolizer>
        <Graphic>
          <ExternalGraphic>
                <OnlineResource
           xlink:type="simple"
           xlink:href="smileyface.jpeg" />
                <Format>image/png</Format>
            </ExternalGraphic>

          <Mark>
            <WellKnownName>circle</WellKnownName>
            <Fill>
              <CssParameter name="fill">
                <ogc:Literal>#FFFFFF</ogc:Literal>
              </CssParameter>
            </Fill>
            <Stroke>
              <CssParameter name="stroke">
                <ogc:Literal>#000000</ogc:Literal>
              </CssParameter>
              <CssParameter name="stroke-width">
                <ogc:Literal>2</ogc:Literal>
              </CssParameter>
            </Stroke>
          </Mark>
          <Opacity>
            <ogc:Literal>1.0</ogc:Literal>
          </Opacity>
          <Size>
            <ogc:Literal>6</ogc:Literal>
          </Size>

        </Graphic>
      </PointSymbolizer>

So if you can fill the image name in with a file that doesn't exist (null.png should work) then the mark will be drawn. There is an if_then_else function that could be useful if you don't have an explicit file column.

  • Is there a way to read the styling of each feature from its database record? – Faz B Mar 11 '18 at 9:19
  • Yes, look around at other questions here – Ian Turton Mar 11 '18 at 13:24

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.