Question

I am using SLD to style output from my geoserver. I have couple types of objects to be displayed. Each type have different icons, but text description is generated from the same information (text field in the database). I cannot use object_type as image name because you cannot use variable in image name (or I do not know how to do it...). So I Have SLD like (without unimportant parts):

<StyledLayerDescriptor>
  <NamedLayer>
    <UserStyle>
      <FeatureTypeStyle>
        <Rule>
          <Filter>
            <PropertyIsEqualTo>
              <PropertyName>object_type</PropertyName>
              <Literal>1</Literal>
            </PropertyIsEqualTo>
          </Filter>
          <PointSymbolizer>
            <Graphic>
              <ExternalGraphic>
                <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="/images/1.png" />
                <Format>image/png</Format>
              </ExternalGraphic>
              <Size>16</Size>
              <Opacity>1</Opacity>
            </Graphic>
          </PointSymbolizer>
          <TextSymbolizer>
            <Label>
              <ogc:PropertyName>name</ogc:PropertyName>
            </Label>
            <Font>
              <CssParameter name="font-family">Times New Roman</CssParameter>
              <CssParameter name="font-style">Normal</CssParameter>
              <CssParameter name="font-size">12</CssParameter>
            </Font>
            <Fill>
              <CssParameter name="fill">#110011</CssParameter>
              <CssParameter name="fill-opacity">1</CssParameter>
            </Fill>
            <Halo />
          </TextSymbolizer>
        </Rule>

        <Rule>
          <Filter>
            <PropertyIsEqualTo>
              <PropertyName>object_type</PropertyName>
              <Literal>2</Literal>
            </PropertyIsEqualTo>
          </Filter>
          <PointSymbolizer>
            <Graphic>
              <ExternalGraphic>
                <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="/images/2.png" />
                <Format>image/png</Format>
              </ExternalGraphic>
              <Size>16</Size>
              <Opacity>1</Opacity>
            </Graphic>
          </PointSymbolizer>
          <TextSymbolizer>
            <Label>
              <ogc:PropertyName>name</ogc:PropertyName>
            </Label>
            <Font>
              <CssParameter name="font-family">Times New Roman</CssParameter>
              <CssParameter name="font-style">Normal</CssParameter>
              <CssParameter name="font-size">12</CssParameter>
            </Font>
            <Fill>
              <CssParameter name="fill">#110011</CssParameter>
              <CssParameter name="fill-opacity">1</CssParameter>
            </Fill>
            <Halo />
          </TextSymbolizer>
        </Rule>
      </FeatureTypeStyle>
    </UserStyle>
  </NamedLayer>
</StyledLayerDescriptor>

And the thing I'd love to do would be writing TextSymbolizer once and using it in two or more places... I have not heard of anything like that in XML, but maybe you have? :-)

One solution that comes to me, is to write some very simple generator (xml.erb? :P) and let it generate XML...

Was it helpful?

Solution

GeoServer's "Dynamic Symbolizer" extension to SLD does allow you to use database properties as all or part of icon URLs. You can even perform calculations and call filter functions if you want (they use embedded CQL in the URL.

Here's a blog post on the feature: http://blog.geoserver.org/2008/12/08/dynamic-symbolizers-part-1/

OTHER TIPS

Did you tried defining an XML Schema or optionally an XML DTD for your XML, and then using element identifiers http://www.w3.org/TR/xmlschema-0/#schemaConstraintsVsXML1, something like:



<StyledLayerDescriptor>
  <NamedLayer>
    <CommonStyles>
      <TextSymbolizer id="name_times_normal_12">
        <Label>
          <ogc:PropertyName>name</ogc:PropertyName>
        </Label>
        <Font>
          <CssParameter name="font-family">Times New Roman</CssParameter>
          <CssParameter name="font-style">Normal</CssParameter>
          <CssParameter name="font-size">12</CssParameter>
        </Font>
        <Fill>
          <CssParameter name="fill">#110011</CssParameter>
          <CssParameter name="fill-opacity">1</CssParameter>
        </Fill>
        <Halo />
      </TextSymbolizer>
    </CommonStyles>
    <UserStyle>
      <FeatureTypeStyle>
        <Rule>
          <Filter>
            <PropertyIsEqualTo>
              <PropertyName>object_type</PropertyName>
              <Literal>1</Literal>
            </PropertyIsEqualTo>
          </Filter>
          <PointSymbolizer>
            <Graphic>
              <ExternalGraphic>
                <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="/images/1.png" />
                <Format>image/png</Format>
              </ExternalGraphic>
              <Size>16</Size>
              <Opacity>1</Opacity>
            </Graphic>
          </PointSymbolizer>
          <TextSymbolizer ref="#name_times_normal_12" />
        </Rule>
        <Rule>
          <Filter>
            <PropertyIsEqualTo>
              <PropertyName>object_type</PropertyName>
              <Literal>2</Literal>
            </PropertyIsEqualTo>
          </Filter>
          <PointSymbolizer>
            <Graphic>
              <ExternalGraphic>
                <OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="/images/2.png" />
                <Format>image/png</Format>
              </ExternalGraphic>
              <Size>16</Size>
              <Opacity>1</Opacity>
            </Graphic>
          </PointSymbolizer>
          <TextSymbolizer ref="#name_times_normal_12" />
        </Rule>
      </FeatureTypeStyle>
    </UserStyle>
  </NamedLayer>
</StyledLayerDescriptor>

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top