kml

This one generates kml files.

#based on http://code.google.com/support/bin/answer.py?answer=82711

import urllib
import xml.dom.minidom

# Sign up for one here: http://code.google.com/apis/maps/signup.html
MAPS_KEY = 'ABQIAAAAEeSCthnR8JRVr1KT6BaG_RQMZpOArjdEOtW7EKQiKtof-X3NFhSVjqCZ7dXYYmHgopatyvyTIp_r7w'

def geocode(address):
    # This function queries the Google Maps API geocoder with an
    # address. It gets back a csv file, which it then parses and
    # returns a string with the longitude and latitude of the address.

    mapsKey = MAPS_KEY
    mapsUrl = 'http://maps.google.com/maps/geo?q='
     
    url = ''.join([mapsUrl,urllib.quote(address),'&output=csv&key=',mapsKey])

    print "Looking up %s..." % address,
    coordinates = urllib.urlopen(url).read().split(',')
    print coordinates
    coorText = '%s,%s' % (coordinates[3],coordinates[2])
    if coorText == '0,0':
        print "trying again in 2 seconds."
        import time
        time.sleep(2)
        print "Looking up %s..." % address,
        coordinates = urllib.urlopen(url).read().split(',')
        print coordinates
        coorText = '%s,%s' % (coordinates[3],coordinates[2])        
    return coorText

class KML():
    def __init__(self, name=None, description=None):
        self.kmlDoc = xml.dom.minidom.Document()

        kmlElement = self.kmlDoc.createElementNS('http://earth.google.com/kml/2.2','kml')

        kmlElement = self.kmlDoc.appendChild(kmlElement)

        documentElement = self.kmlDoc.createElement('Document')
        self.documentElement = kmlElement.appendChild(documentElement)

        if name:
            nameElement = self.kmlDoc.createElement('name')
            nameElement.appendChild(self.kmlDoc.createTextNode(name))   
            self.documentElement.appendChild(nameElement)

        if description:
            descriptionElement = self.kmlDoc.createElement('description')
            descriptionElement.appendChild(self.kmlDoc.createTextNode(description))   
            self.documentElement.appendChild(descriptionElement)

    def add_placemark(self, name, description, address):
        placemarkElement = self.kmlDoc.createElement('Placemark')

        nameElement = self.kmlDoc.createElement('name')
        nameElement.appendChild(self.kmlDoc.createTextNode(name))   
        placemarkElement.appendChild(nameElement)

        descriptionElement = self.kmlDoc.createElement('description')
        descriptionElement.appendChild(self.kmlDoc.createTextNode(description))   
        placemarkElement.appendChild(descriptionElement)

        pointElement = self.kmlDoc.createElement('Point')
        placemarkElement.appendChild(pointElement)

        coordinates = geocode(address)
        coorElement = self.kmlDoc.createElement('coordinates')
        coorElement.appendChild(self.kmlDoc.createTextNode(coordinates))
        pointElement.appendChild(coorElement)

        self.documentElement.appendChild(placemarkElement)

    def __str__(self):
        return self.kmlDoc.toprettyxml(' ')

def email(address):
    return '<a href="mailto:%s">%s</a>' % (address, address)

if __name__ == '__main__':
    k = KML()
    k.add_placemark('Google Headquarters', "1600 Amphitheatre Pkwy<br/>Mountain View, CA 94043<br/>", "1600 Amphitheatre Pkwy Mountain View, CA 94043")

    f = file('google.kml', 'w')
    f.write(str(k))
    f.close()
Advertisements