pythongoogle-mapsgdatagdata-python-client

how can I upload a kml file with a script to google maps?


I have a python script, that generates kml files. Now I want to upload this kml file within the script (not per hand) to the "my maps" section of google maps. Does anybody have a python or other script/code to do so?


Solution

  • Summary: You can't until issue 2590 is fixed, which may be a while because Google have closed this issue as WontFix. There are workarounds you can try to achieve the same end result, but as it stands you cannot simply upload a KML file using the Google Maps Data API.

    Long version:

    I don't didn't have any Python code to do this, but the Google Maps Data API allows you to do this with a series of HTTP requests. See Uploading KML in the HTTP Protocol section of the Developers Guide for the documentation on how to do this. So one possible Python solution would be to use something like httplib in the standard library to do the appropriate HTTP requests for you.

    After various edits and your feedback in the comments, here is a script that takes a Google username and password via the command line (be careful how you use it!) to obtain the authorization_token variable by making a ClientLogin authentication request. With a valid username and password, the auth token can be used in the Authorization header for POSTing the KML data to the Maps Data API.

    #!/usr/bin/env python
    import httplib
    import optparse
    import sys
    import urllib
    
    class GoogleMaps(object):
        source = "daybarr.com-kmluploader-0.1"
    
        def __init__(self, email, passwd):
            self.email = email
            self.passwd = passwd
            self._conn = None
            self._auth_token = None
    
        def _get_connection(self):
            if not self._auth_token:
                conn = httplib.HTTPSConnection("www.google.com")
                params = urllib.urlencode({
                    "accountType": "HOSTED_OR_GOOGLE",
                    "Email": self.email,
                    "Passwd": self.passwd,
                    "service": "local",
                    "source": self.source,
                })
                headers = {
                    "Content-type": "application/x-www-form-urlencoded",
                    "Accept": "text/plain",
                }
                conn.request("POST", "/accounts/ClientLogin", params, headers)
                response = conn.getresponse()
                if response.status != 200:
                    raise Exception("Failed to login: %s %s" % (
                        response.status,
                        response.reason))
                body = response.read()
                for line in body.splitlines():
                    if line.startswith("Auth="):
                        self._auth_token = line[5:]
                        break
                if not self._auth_token:
                    raise Exception("Cannot find auth token in response %s" % body)
            if not self._conn:
                self._conn = httplib.HTTPConnection("maps.google.com")
            return self._conn
    
        connection = property(_get_connection)
    
        def upload(self, kml_data):
            conn = self.connection
            headers = {
                "GData-Version": "2.0",
                "Authorization": 'GoogleLogin auth=%s' % (self._auth_token,),
                "Content-Type": "application/vnd.google-earth.kml+xml",
            }
            conn.request("POST", "/maps/feeds/maps/default/full", kml_data, headers)
            response = conn.getresponse()
            if response.status != 200:
                raise Exception("Failed to upload kml: %s %s" % (
                    response.status,
                    response.reason))
            return response.read()
    
    if __name__ == "__main__":
        parser = optparse.OptionParser()
        parser.add_option("-e", "--email", help="Email address for login")
        parser.add_option("-p", "--passwd", help="Password for login")
        options, args = parser.parse_args()
        if not (options.email and options.passwd):
            parser.error("email and passwd required")
        if args:
            kml_file = open(args[0], "r")
        else:
            kml_file = sys.stdin
        maps = GoogleMaps(options.email, options.passwd)
        print maps.upload(kml_file.read())
    

    Unfortunately, even when using valid login credentials to obtain a valid authorization token and using a valid KML file containing exactly the example as given in the documentation, the API responds to the KML post with a 400 Bad Request. Apparently this is a known issue (2590 reported July 22nd 2010) so please vote for and comment on that if you'd like Google to fix.

    In the meantime, without that bug fixed, you could try

    1. Create the map without uploading KML, and then upload KML features as appropriate, as suggested in comment #9 on the issue from Google, when they confirmed that the bug exists.
    2. uploading XML or uploading CSV instead of KML if these methods support what you need to get done
    3. fiddling with the format of your KML data. This post in the Google Group for the API suggests that this might help, but it looks complicated.

    Good luck