Giter VIP home page Giter VIP logo

python-sld's Introduction

python-sld

A python library for reading, writing, and manipulating SLD files.

Requirements

The lxml library is required for XML parsing and XML Schema validation. This requirement is listed in requirements.txt, and may be installed using pip with this command:

> sudo pip install -r requirements.txt

Installation

> easy_install python-sld

OR

> pip install python-sld

Usage

Using python-sld to create SLD documents is as easy as instantiating a StyledLayerDescriptor class object.

from sld import StyledLayerDescriptor
mysld = StyledLayerDescriptor()

You may also read an existing SLD document in by passing it as a parameter:

from sld import StyledLayerDescriptor
mysld = StyledLayerDescriptor('mysld.sld')

Addition of most elements are performed on the parent element, since they are related to parent nodes in order to preserve compliance:

nl = mysld.create_namedlayer()
ustyle = nl.create_style()

A couple class objects represent collections of nodes, such as Rules and CssParameters. They are properties of their parent classes (FeatureTypeStyle and Fill/Stroke/Font respectively). They behave as python lists, and you can access any of their items using a python list pattern:

fts = ustyle.create_featuretypestyle()
rule1 = fts.Rules[0]
print len(fts.Rules)
fts.Rules[0] = rule1

Filter objects are pythonic, and when combined with the '+' operator, they become ogc:And filters. When combined with the '|' operator, they become ogc:Or filters.

from sld import Filter

filter_1 = Filter(rule)
# set filter 1 properties

filter_2 = Filter(rule)
# set filter 2 properties

rule.Filter = filter_1 + filter_2

You may also construct a filter from an expression when using the create_filter method on the Rule object:

filter = rule.create_filter('population', '>', '100')

Implementation

At the current time, python-sld does ''not'' support the full SLD specification. The current implementation supports the following SLD elements:

  • StyledLayerDescriptor
  • NamedLayer
  • Name (of NamedLayer)
  • UserStyle
  • Title (of UserStyle and Rule)
  • Abstract
  • FeatureTypeStyle
  • Rule
  • ogc:Filter
  • ogc:And
  • ogc:Or
  • ogc:PropertyIsNotEqualTo
  • ogc:PropertyIsLessThan
  • ogc:PropertyIsLessThanOrEqualTo
  • ogc:PropertyIsEqualTo
  • ogc:PropertyIsGreaterThanOrEqualTo
  • ogc:PropertyIsGreaterThan
  • ogc:PropertyIsLike
  • ogc:PropertyName
  • ogc:Literal
  • MinScaleDenominator
  • MaxScaleDenominator
  • PointSymbolizer
  • LineSymbolizer
  • PolygonSymbolizer
  • TextSymbolizer
  • Mark
  • Graphic
  • Fill
  • Stroke
  • Font
  • CssParameter

Support

If you have any problems or questions, please visit the python-sld project on github: https://github.com/azavea/python-sld/

Contributors

@ewsterrenburg

python-sld's People

Contributors

dzwarg avatar ewsterrenburg avatar kshepard avatar tomkralidis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

python-sld's Issues

How to add multiple filters dynamically?

Hi, appreciate the work very much.
My Python skills are a bit rusty but I can't seem to find a way to add multiple “AND” and “OR” filters dynamically!

My objective is to translate a parsed query string (SQL) to the corresponding SLD syntax. For example: "a > 1 and b < 2".
As far as I can tell, I must first create two separate filters for the rule:
import sld

...

r = fs.create_rule('r1')
f1 = sld.Filter(r)
f1.a = sld.PropertyCriterion(f1, 'PropertyIsGreaterThan')
f1.a.PropertyName = 'a'
f1.a.Literal = '1'
f2 = sld.Filter(r)
f2.b = sld.PropertyCriterion(f2, 'PropertyIsLessThan')
f2.b.PropertyName = 'b'
f2.b.Literal = '2'
and finally combine them like so:
r.Filter = f1 + f2

Now, how can I accomplish this when the number of criterion and operands are unknown? Essentially, I need to execute the Filter assignment dynamically but the filter references (f1 and f2) must already be defined!
Consider the following example: "(a > 1 and b < 2 and c = 3 ) or (d = 4 and e >= 5)"

Did I miss something? Thanks

Roland Hansson

NameError: name 'StyledLayerDescriptor' is not defined

David, finally got around to trying python-sld and I can't get past this. I'm sure I'm doing something stupid.
from python shell:

import sld
mysld = StyledLayerDescriptor()

this returns error above.

did sudo easy_install --upgrade python-sld beforehand

thanks,
Michael

as_sld function

When I go to print my sld out my filter ends up below my symbolizer. Is there any way to avoid since this violates sld styling policies? Here is my code.

mysld = sld.StyledLayerDescriptor()
nl = mysld.create_namedlayer('test')
ustyle = nl.create_userstyle()
fts = ustyle.create_featuretypestyle()
firstRule = fts.create_rule('test',sld.PolygonSymbolizer)
firstRule.create_filter('value', '>', 'T')
print mysld.as_sld(pretty_print=True)

More than one Rule?

Is it possible to create more than one rule in an Layer or FutureTypeStyle, by using a list or similar?
I'm running sld in a loop to create an sld file similar to the one generated by QGIS, but the final result only includes the last data from the loop. Thanks

Filter is set before PolygonSymbolizer

I am running this script:

from sld import *
sld_doc = StyledLayerDescriptor() 

# Create Layer
nl = sld_doc.create_namedlayer('Drivetime Polygon Style ')
us = nl.create_userstyle()
us.Title = 'Drivetime'


# Create Rule
fts = us.create_featuretypestyle()
r1 = fts.create_rule('Interval 1', PolygonSymbolizer)

# Create Filter
f1 = Filter(r1)
f1.PropertyIsEqualTo = PropertyCriterion(f1, 'PropertyIsEqualTo')
f1.PropertyIsEqualTo.PropertyName = 'drivetime' 
f1.PropertyIsEqualTo.Literal = 'v1'

r1.Filter = f1

content = sld_doc.as_sld(pretty_print=True)
print content

And it creates this sld file:

<sld:StyledLayerDescriptor xmlns:sld="http://www.opengis.net/sld" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ogc="http://www.opengis.net/ogc" version="1.0.0">
  <sld:NamedLayer>
    <sld:Name>Drivetime Polygon Style </sld:Name>
    <sld:UserStyle>
      <sld:Title>Drivetime</sld:Title>
      <sld:FeatureTypeStyle>
        <sld:Rule>
          <sld:Title>Interval 1</sld:Title>
          <sld:PolygonSymbolizer>
            <sld:Fill>
              <sld:CssParameter name="fill">#AAAAAA</sld:CssParameter>
            </sld:Fill>
            <sld:Stroke>
              <sld:CssParameter name="stroke">#000000</sld:CssParameter>
              <sld:CssParameter name="stroke-width">1</sld:CssParameter>
            </sld:Stroke>
          </sld:PolygonSymbolizer>
          <ogc:Filter>
            <ogc:PropertyIsEqualTo>
              <ogc:PropertyName>drivetime</ogc:PropertyName>
              <ogc:Literal>v1</ogc:Literal>
            </ogc:PropertyIsEqualTo>
          </ogc:Filter>
        </sld:Rule>
      </sld:FeatureTypeStyle>
    </sld:UserStyle>
  </sld:NamedLayer>
</sld:StyledLayerDescriptor>

The file is not valid, because the PolygonSymbolizer is before the filter. Is this a bug or am I doing something wrong?

StyledLayerDescriptor not safe for multiple processes

I have a project where I need to load multiple layers. To speed up this process, I'm launching multiple processes to retrieve the layer images. If there are multiple layers that have SLD's associated with them, they can break sometimes when there is contention over the schema document created within the init method of StyledLayerDescriptor. Is there a known workaround for this situation?

I'm looking at modifying the code locally to just keep the schema file around at all times to prevent this contention, but wanted to see if there were other ways of dealing with it.

Thanks much.

ExternalGraphic

Hi David,
I would like use your library, but I don't see how add ExternalGraphic node instead as mark.

The best way to do this is creating a custom PointSymbolizer class that uses a custom graphic class, not?

documentation mismatch

The Rule.create_filter is looking for == when doing a PropertyIsEqualTo comparitor. However, the documentation states that = is acceptable.

This had me looking around for a while. :(

Multiple NamedLayer

I would like create and sld style with multiple NamedLayer
This my code

doc = sld.StyledLayerDescriptor()

llista = MapStyle.objects.filter(map=id).order_by('order')
for mapstyle in llista:
nl = doc.create_namedlayer(mapstyle.style.name)

I created a namelayer for each element in llista but the result only includes the last NamedLayer.

I possible do this?

I try to do removing the namedlayer node and inserting after the namedlayer nodes but without success

doc = sld.StyledLayerDescriptor()

llista = MapStyle.objects.filter(map=2).order_by('order')
namedlayers=[]
for mapstyle in llista:
    namedlayers.append( doc.create_namedlayer(mapstyle.style.name)._node )

nl_node = doc._node.xpath('sld:NamedLayer', namespaces=sld.SLDNode._nsmap)[0]
doc._node.remove(nl_node)

i=0
for n in namedlayers:
    doc._node.insert(i, namedlayers[i])
    i=i+1

print etree.tostring(doc._node, with_tail=False, pretty_print=True)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.