4

i want to update my sld. In my sld, there is not a filter but i want to apply a filter using the python,dynamically not by manually putting the values in sld. This is my sld.

<StyledLayerDescriptor 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" version="1.0.0" xsi:schemaLocation="http://www.opengis.net/sld StyledLayerDescriptor.xsd">`
<NamedLayer>
<Name>Simple polygon with stroke</Name>
<UserStyle>
<Title>SLD Cook Book: Simple polygon with stroke</Title>
<FeatureTypeStyle>
<Rule>
<PolygonSymbolizer>
<Fill>
<CssParameter name="fill">#000080</CssParameter>
</Fill>
<Stroke>
<CssParameter name="stroke">#FFFFFF</CssParameter>
<CssParameter name="stroke-width">2</CssParameter>
</Stroke>
</PolygonSymbolizer>
</Rule>
</FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>

Now i want to add a filter on my table attribute name_1 is there any way to do this in python. New filter will be in a Rule tag and will be under the FeatureTypeStyle.

tech_geek
  • 1,624
  • 3
  • 21
  • 44

1 Answers1

1

Python SLD

Well, I used the python-sld package to create the SLD structure dinamically.

You can find here http://azavea.github.io/python-sld/ the project. I must say the documentation is not exhaustive, so many times you have to try-error to do what you want. For me was neccesary overwrite the existing SLD with the new one created, I couldn't modify the existing one.

Said this, once you have the package installed:

Create the SLD structure

from sld import StyledLayerDescriptor, PolygonSymbolizer, Filter

mysld = StyledLayerDescriptor()
nl = mysld.create_namedlayer('Simple polygon with stroke')
ustyle = nl.create_userstyle()
fts = ustyle.create_featuretypestyle()

First rule

fts.create_rule('First Rule', PolygonSymbolizer)

mysld.NamedLayer.UserStyle.FeatureTypeStyle.Rules[0].PolygonSymbolizer.Fill.CssParameters[0].Value = '#000080'
mysld.NamedLayer.UserStyle.FeatureTypeStyle.Rules[0].PolygonSymbolizer.Stroke.CssParameters[0].Value = '#FFFFFF'
mysld.NamedLayer.UserStyle.FeatureTypeStyle.Rules[0].PolygonSymbolizer.Stroke.CssParameters[1].Value = '2'

Second rule and filter

fts.create_rule('Second Rule', PolygonSymbolizer)

fts.Rules[1].create_filter('name_1', comparator, value)

A rule example:

fts.Rules[1].create_filter('name_1', '>=', '0')

You can do a lot of things in the creation of the rules and filters adding variables, for example I code this:

fts.create_rule(str(int(round(e))) + '-' + str(int(round(v[i + 1]))), PolygonSymbolizer)

fts.Rules[i].create_filter(field, '>=', str(e))

Hope this help, blessings.