I'm writing a simple script using simplekml 1.3.5 (it's a Python liblary for generating KML) for climate data visualization.
import simplekml
kml_filename ='sample.kml'
kml = simplekml.Kml(open=1)
model = kml.newmodel(name="test_model")
model.altitudemode = simplekml.AltitudeMode.absolute
model.location.altitude = 500
model.location.latitude = 36.939055000000025
model.location.longitude = 140.037
model.orientation.heading = 90
model.orientation.tilt = 0
model.orientation.roll = 0
model.scale.x = 1000
model.scale.y = 1000
model.scale.z = 1000
model.link.href = "images/canvas.dae"
model.resourcemap.newalias(targethref="instance.png", sourcehref="mapping.png")
kml.save(kml_filename)
this code generates following KML.
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2">
<Document id="1672">
<open>1</open>
<Placemark id="1674">
<name>test_model</name>
<Model id="1673">
<altitudeMode>absolute</altitudeMode>
<Location>
<longitude>140.037</longitude>
<latitude>36.939055000000025</latitude>
<altitude>500</altitude>
</Location>
<Orientation>
<heading>90</heading>
<tilt>0</tilt>
<roll>0</roll>
</Orientation>
<Scale>
<x>1000</x>
<y>1000</y>
<z>1000</z>
</Scale>
<Link id="1678">
<href>images/canvas.dae</href>
</Link>
<ResourceMap>
<targetHref>instance.png</targetHref>
<sourceHref>mapping.png</sourceHref>
</ResourceMap>
</Model>
</Placemark>
</Document>
</kml>
It looks almost good. But Alias tag was not output in ResourceMap tag. I expected following output.
<ResourceMap>
<Alias>
<targetHref>instance.png</targetHref>
<sourceHref>mapping.png</sourceHref>
</Alias>
</ResourceMap>
The google earth didn't display the texture without this tag. (And add the tag manually, can display it.)
How I can output the tag in the KML using simplekml?
I think this is a ResourceMap class issue on the design poliy of the library.
import simplekml
rc = ResourceMap()
rc.newalias(targethref="foo", sourcehref="bar")
print(rc)
This code output following.
<targetHref>foo</targetHref><sourceHref>bar</sourceHref>
I hacked once by subclassing, but outer Model class checks the type and reject subclass. So I resolved with an adhoc hack to Alias class instead of ResourceMap.
class AliasModded(simplekml.Alias):
def __init__(self, targethref=None, sourcehref=None):
super(AliasModded, self).__init__(targethref, sourcehref)
def __str__(self):
return "<Alias>{}</Alias>".format(super(AliasModded, self).__str__())
and use this.
tp_model.resourcemap.aliases.append(AliasModded(targethref="instance.png", sourcehref="mapping.png"))
This code output as expected. Thanks.