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?
1 Answers
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
- 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.
- uploading XML or uploading CSV instead of KML if these methods support what you need to get done
- 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

- 9,465
- 6
- 57
- 93
-
sorry, but I get a 400 Bad Request Bad request and I don't know how to debug this. – Jörg Beyer Oct 06 '10 at 19:24
-
@Jörg Beyer Strange. You normally only get a 400 if the request is not a valid HTTP request for some reason. I've updated my answer, please give the script a go, should be runnable as is. – Day Oct 06 '10 at 20:31
-
I still get a "400 Bad Request". I am confident, that the auth-token is OK, because I get a 401 when I modify my freshly generated token. – Jörg Beyer Oct 08 '10 at 18:37
-
@Jörg Beyer: Perhaps Google doesn't like your KML and thinks it's invalid? Are you using your own or my example? Note I haven't actually had time to try my script with my a real auth-token, may do tomorrow. Keep trying ;) – Day Oct 09 '10 at 01:06
-
You've probably checked already, but what does the body of the 400 response say? Does it give further details on the error? – Day Oct 09 '10 at 01:09
-
no, it just repeats the "Bad Request" - any ideas about further debugging? – Jörg Beyer Oct 12 '10 at 19:14
-
1@Jörg Beyer: Ok, so it looks like this is a known problem with the API/docs! See http://code.google.com/p/gmaps-api-issues/issues/detail?id=2590 and please vote for the issue (star it) and/or add your comments there to help push it along. You may have more luck uploading XML or CSV if your problem doesn't require KML to solve? I'll update my answer again with my latest code which I've now fully tested with a real google loging which also gives me the 400 error :( – Day Oct 12 '10 at 23:08
-
Looks like Google are responding to the issue now, so please work through your specific problem with them there and please please report back here when you have a fix or workaround ;) – Day Oct 13 '10 at 07:32
-
Google have now confirmed the issue and suggested a workaround: http://code.google.com/p/gmaps-api-issues/issues/detail?id=2590#c9 – Day Oct 27 '10 at 17:06
-
Google have today set the status of this bug to WontFix https://code.google.com/p/gmaps-api-issues/issues/detail?id=2590#c13. Best let them know if you feel otherwise. – Day Dec 09 '10 at 14:04