python - how can I upload a kml file with a script to google maps? -
i have python script, generates kml files. want upload kml file within script (not per hand) "my maps" section of google maps. have python or other script/code so?
summary: can't until issue 2590 fixed, may while because google have closed issue wontfix. there workarounds can try achieve same end result, stands cannot upload kml file using google maps data api.
long version:
i don't didn't have python code this, google maps data api allows series of http requests. see uploading kml in http protocol section of developers guide documentation on how this. 1 possible python solution use httplib in standard library appropriate http requests you.
after various edits , feedback in comments, here script takes google username , password via command line (be careful how use it!) obtain authorization_token
variable making clientlogin authentication request. valid username , password, auth token can used in authorization
header posting kml data 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 login: %s %s" % ( response.status, response.reason)) body = response.read() 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 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 login") parser.add_option("-p", "--passwd", help="password login") options, args = parser.parse_args() if not (options.email , options.passwd): parser.error("email , 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, when using valid login credentials obtain valid authorization token , using valid kml file containing example given in documentation, api responds kml post 400 bad request
. apparently known issue (2590 reported july 22nd 2010) please vote , comment on if you'd google fix.
in meantime, without bug fixed, try
- create map without uploading kml, , upload kml features appropriate, suggested in comment #9 on issue google, when confirmed bug exists.
- uploading xml or uploading csv instead of kml if these methods support need done
- fiddling format of kml data. this post in google group api suggests might help, looks complicated.
good luck
Comments
Post a Comment