DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

KML Utility Class

11.16.2005
| 6354 views |
  • submit to reddit
        
__author__ = "Jon Goodall <jon.goodall@gmail.com> - http://www.duke.edu/~jgl34"
__version__ = "0.0.1"
__license__ = ""
__copyright__ =""

class KML_File:
    "For creating KML files used for Google Earth"
    def __init__(self, filepath):
        self.filepath = filepath
        "adds the kml header to a file (includes a default style)"
        file = open(filepath,"w")
        file.write(
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"\
        "<kml xmlns=\"http://earth.google.com/kml/2.0\">\n"\
        "<Document>\n"\
        "<Style id='normalPlaceMarker'>\n"\
        "  <IconStyle>\n"\
        "    <Icon>\n"\
        "      <href>root://icons/palette-3.png</href>\n"\
        "      <x>96</x>\n"\
        "      <y>160</y>\n"\
        "      <w>32</w>\n"\
        "      <h>32</h>\n"\
        "    </Icon>\n"\
        "  </IconStyle>\n"\
        "</Style>\n")
        file.close()

    def close(self):
        file = open(self.filepath,"a")
        file.write(
        "</Document>\n"\
        "</kml>")
        file.close()
        
    def open_folder(self, name):
        file = open(self.filepath,"a")
        file.write(
        "<Folder>\n"\
        "  <name>" + name + "</name>\n")
        file.close()

    def close_folder(self):
        file = open(self.filepath,"a")
        file.write(
        "</Folder>\n")
        file.close()
        
    def add_placemarker(self, latitude, longitude, altitude = 0.0, description = " ", name = " ", range = 6000, tilt = 45, heading = 0):
        "adds the point to a kml file"
        file = open(self.filepath,"a")
        file.write(
        "<Placemark>\n"\
        "  <description>" + description + "</description>\n"\
        "  <name>" + name + "</name>\n"\
        "  <styleUrl>#normalPlaceMarker</styleUrl>" + 
        "  <LookAt>\n"\
        "    <longitude>" + str(longitude) + "</longitude>\n"\
        "    <latitude>" + str(latitude) + "</latitude>\n"\
        "    <range>" + str(range) + "</range>\n"\
        "    <tilt>" + str(tilt) + "</tilt>\n"\
        "    <heading>" + str(heading) + "</heading>\n"\
        "  </LookAt>\n"\
        "  <visibility>0</visibility>\n"\
        "   <Point>\n"\
        "    <extrude>1</extrude>\n"\
        "    <altitudeMode>relativeToGround</altitudeMode>\n"\
        "    <coordinates>" + str(longitude) + "," + str(latitude) +", " +  str(altitude) + "</coordinates>\n"\
        "   </Point>\n"\
        " </Placemark>\n")
        file.close()