Beispiel #1
0
 def _genkml(self, format=True):
     """Returns the kml as a string or "prettyprinted" if format = True."""
     kml_str = self._feature.__str__()
     xml_str = u("<kml {0}>{1}</kml>").format(Kmlable._getnamespaces(), kml_str)
     if format:
        KmlElement.patch()
        kml_str = xml.dom.minidom.parseString(xml_str.encode("utf-8"))
        KmlElement.unpatch()
        return kml_str.toprettyxml(indent="    ", newl="\n", encoding="UTF-8").decode("utf-8")
     else:
         return xml_str
Beispiel #2
0
 def __str__(self):
     """This is where the magic happens."""
     buf = []
     for var, val in self._kml.items():
         if val is not None:  # Exclude all variables that are None
             if var.endswith("_"):
                 buf.append("{0}".format(val))  # Use the variable's __str__ as is
             else:
                 if var in ['name', 'description', 'text'] and Kmlable._parse: # Parse value for HTML and convert
                     val = Kmlable._chrconvert(val)
                 elif (var == 'href' and os.path.exists(val) and Kmlable._kmz == True)\
                         or (var == 'targetHref' and os.path.exists(val) and Kmlable._kmz == True): # Check for local images
                     Kmlable._addimage(val)
                     val = os.path.join('files', os.path.split(val)[1])
                 buf.append(u("<{0}>{1}</{0}>").format(var, val))  # Enclose the variable's __str__ with the variables name
                 # Add namespaces
                 if var.startswith("atom:") and 'xmlns:atom="http://www.w3.org/2005/Atom"' not in Kmlable._namespaces:
                     Kmlable._namespaces.append('xmlns:atom="http://www.w3.org/2005/Atom"')
                 elif var.startswith("xal:") and 'xmlns:xal="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"' not in Kmlable._namespaces:
                     Kmlable._namespaces.append('xmlns:xal="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"')
     return "".join(buf)