StyleMaps

72 views
Skip to first unread message

Basil Veerman

unread,
Jul 6, 2012, 1:54:07 PM7/6/12
to simp...@googlegroups.com
I'm having some issues with StyleMaps and I'm not sure if I'm proceeding correctly of if it is a bug...

from simplekml import *
kml = Kml()

# set up styles
normal = Style()
normal.balloonstyle.text='$[description]'
normal.labelstyle.scale = 0

highlighted = Style()
highlighted.balloonstyle.text='$[description]'
highlighted.labelstyle.scale = 1

# create stylemap
stylemap = StyleMap(normalstyle=normal, highlightstyle=highlighted)
 
# assign stylemap to new point
pnt = kml.newpoint(name="Test", coords=[(0,0)])
pnt.style = stylemap

print kml.kml() results in:

<?xml version="1.0" encoding="UTF-8"?>
    <Document id="feat_1">
        <StyleMap id="stylesel_2">
            <Pair>
                <key>normal</key>
                <styleUrl>#stylesel_0</styleUrl>
            </Pair>
            <Pair>
                <key>highlight</key>
                <styleUrl>#stylesel_1</styleUrl>
            </Pair>
        </StyleMap>
        <Placemark id="feat_2">
            <name>Test</name>
            <styleUrl>#stylesel_2</styleUrl>
            <Point id="geom_0">
                <coordinates>0,0,0.0</coordinates>
            </Point>
        </Placemark>
    </Document>
</kml>

I would expect the styles referenced in the stylemap to be included in the document, but this is not happening in my tests.  Are these supposed to be attached manually to the document?  I'm guessing it sees a global style that is not directly used for any feature and does not include it.

Thanks for any help,
Basil

Basil Veerman

unread,
Jul 6, 2012, 2:28:28 PM7/6/12
to simp...@googlegroups.com
Did a bit more investigation and it looks like it should be working... I'm using 0.9.3

The respective styles are added on stylemap init:

class StyleMap(StyleSelector):
    """Styles affect how Geometry is presented.

    Arguments are the same as the properties.
    """
    def __init__(self,
                 normalstyle=None,
                 highlightstyle=None):
        super(StyleMap, self).__init__()
        self._pairnormal = None
        self._pairhighlight = None
        self.normalstyle = normalstyle
        self.highlightstyle = highlightstyle

And this is processed correctly (as far as I can tell) when the feature's __str__ is called:

class Feature(Kmlable):
.....
    def __str__(self):
        buf = []
        for stylemap in self._stylemaps:
            self._addstyle(stylemap.normalstyle)
            self._addstyle(stylemap.highlightstyle)
        str = '<{0} id="{1}">'.format(self.__class__.__name__, self._id)
        buf.append(str)
        for style in self._styles:
            buf.append(style.__str__())
        for stylemap in self._stylemaps:
            buf.append(stylemap.__str__())
...

Makes me think it may be a user error....

Cheers,
Basil

Kyle Lancaster

unread,
Jul 6, 2012, 4:02:30 PM7/6/12
to simp...@googlegroups.com
Hi Basil

Sorry to say, but it is user error :)

Replace the line:
pnt.style = stylemap
With:
pnt.stylemap = stylemap

That's it! A feature has a style or stylemap, they are not the same thing.

Cheers,
Kyle 

Basil Veerman

unread,
Jul 6, 2012, 7:11:52 PM7/6/12
to simp...@googlegroups.com
Works like a charm... as always :).

Thanks!
Reply all
Reply to author
Forward
0 new messages