azavea / python-sld

A simple python library that enables dynamic SLD creation and manipulation.
http://azavea.github.com/python-sld/
Apache License 2.0
27 stars 17 forks source link

Multiple NamedLayer #5

Closed abusquets closed 12 years ago

abusquets commented 12 years ago

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)
dzwarg commented 12 years ago

Hello abusquets,

At the current time, python-sld only supports one NamedLayer (as you have discovered).

What is the result of manually inserting the nodes? The code doc.create_namedlayer may be returning the same node to you over & over again.

-David

abusquets commented 12 years ago

I found a solution, I make a copy of element

doc = sld.StyledLayerDescriptor()

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

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, n)
    i=i+1

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