如何将带有脚本的kml文件上传到谷歌地图?

时间:2010-09-28 19:56:56

标签: python google-maps gdata gdata-python-client

我有一个生成kml文件的python脚本。现在我想将脚本中的这个kml文件(不是每手)上传到谷歌地图的“我的地图”部分。有人有python或其他脚本/代码吗?

1 个答案:

答案 0 :(得分:0)

摘要:直到issue 2590修复后才能修复,这可能有一段时间,因为Google有closed this issue as WontFix。有一些解决方法可以尝试达到相同的最终结果,但就目前而言,您无法使用Google Maps Data API上传KML文件。

长版:

没有任何Python代码来执行此操作,但Google Maps Data API允许您使用一系列HTTP请求执行此操作。有关如何执行此操作的文档,请参阅Uploading KML的HTTP协议部分中的Developers Guide。因此,一种可能的Python解决方案是在标准库中使用类似httplib的内容来为您执行适当的HTTP请求。

在评论中进行了各种编辑和反馈之后,这是一个脚本,它通过命令行获取Google用户名和密码(小心你如何使用它!)通过制作{authorization_token变量来获取Authorization变量{3}}。使用有效的用户名和密码,可以在#!/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()) 标头中使用身份验证令牌将KML数据发布到Maps Data API。

400 Bad Request

不幸,即使使用有效的登录凭据获取有效的授权令牌并使用完全包含文档中给出的示例的有效KML文件,API也会使用{来回复KML帖子{1}}。显然这是一个已知问题(ClientLogin authentication request报告于2010年7月22日)所以如果您希望Google修复,请投票并对此发表评论。

与此同时,如果没有修复该错误,您可以尝试

  1. 在不上传KML的情况下创建地图,然后在确认错误存在的情况下,按照2590中的建议,酌情上传KML功能。
  2. comment #9 on the issueuploading XML代替KML,如果这些方法支持您需要完成的工作
  3. 摆弄KML数据的格式。 API群组中的uploading CSV表示这可能有所帮助,但看起来很复杂。
  4. 祝你好运