我有一個python腳本,生成kml文件。現在我想將這個kml文件上傳到腳本中(不是每手)到谷歌地圖的「我的地圖」部分。有沒有人有Python或其他腳本/代碼來做到這一點?我怎樣才能上傳一個腳本kml文件到谷歌地圖?
1
A
回答
0
摘要:您不能修改,因爲Google有closed this issue as WontFix,這可能會有一段時間。有些解決方法可以嘗試實現相同的最終結果,但按照現狀,您無法使用Google Maps Data API上傳KML文件。
龍版本:
我
不
沒有任何Python代碼要做到這一點,但Google Maps Data API允許您使用一系列HTTP請求的做到這一點。有關如何執行此操作的文檔,請參閱Developers Guide的HTTP協議部分中的Uploading KML。所以一個可能的Python解決方案是在標準庫中使用類似httplib的東西來爲你做適當的HTTP請求。
經過各種編輯和您在評論中的反饋,以下是一個腳本,它通過命令行使用Google用戶名和密碼(小心如何使用它!),通過製作ClientLogin authentication request來獲得authorization_token
變量。使用有效的用戶名和密碼,身份驗證令牌可用於Authorization
標題中,用於將KML數據發佈到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())
不幸的是,使用有效的登錄憑據,即使獲得令牌的有效授權和使用的文檔中給出恰好包含例如有效的KML文件,該API響應KML後用400 Bad Request
。顯然這是一個已知問題(2010年7月22日報道的2590),因此如果您希望Google進行修復,請投票發表評論。
在此期間,沒有這個bug固定的,你可以嘗試
- 而不上傳KML創建地圖,然後上傳KML適當的功能,如comment #9 on the issue來自谷歌,建議當他們證實,該bug存在。
- uploading XML或uploading CSV而不是KML如果這些方法支持您需要完成的操作
- 擺弄您的KML數據的格式。 Google Group for the API中的This post表明這可能有幫助,但看起來很複雜。
好運
相關問題
- 1. 谷歌地圖:我怎樣才能找到附近的標記?
- 2. 我怎樣才能讓這個谷歌地圖黑暗?
- 3. 我怎樣才能顯示我的位置谷歌地圖android
- 4. 我怎樣才能使谷歌地圖像接口
- 5. 我怎樣才能讓谷歌地圖泡泡更長?
- 6. 我怎樣才能看到我上傳到Heroku的文件?
- 7. 我怎樣才能在谷歌地圖中獲得並標記一個位置
- 8. 通KML到谷歌地圖
- 9. 我怎樣才能獲得地圖標記在谷歌地圖api v3?
- 10. 我怎樣才能實現谷歌地圖的視網膜地圖標籤
- 11. 谷歌地圖kml
- 12. 谷歌地圖重寫我的鏈接傳遞給它在一個KML文件
- 13. 我怎樣才能找到上傳的文件名在Python cgi
- 14. 谷歌地圖不鏈接kml文件
- 15. 我怎樣才能導入一個谷歌地圖android應用程序到另一個android項目?
- 16. kml文件一半在谷歌地圖上加載
- 17. 我怎樣才能看到sdf文件
- 18. 我怎樣才能找到一個DataTable
- 19. 我怎樣才能得到一個TD
- 20. 我怎樣才能得到谷歌適合的數據的
- 21. 我怎樣才能一次導入所有的谷歌字體?
- 22. 我怎樣才能上傳zip文檔中的文件
- 23. 它可能與kml和谷歌地圖
- 24. C#:我怎樣才能得到某個文件的圖標?
- 25. 我怎樣才能要求數據文件夾中的腳本
- 26. 我怎樣才能以谷歌地圖的方式詢問用戶的位置?
- 27. 我怎樣才能爲谷歌地圖相同的搜索結果
- 28. 我怎樣才能讓cron執行一個php腳本?
- 29. 我怎樣才能讓這兩個jQuery腳本一起工作?
- 30. 谷歌地圖KML層
對不起,但我得到一個 400錯誤的請求 壞請求 ,我不知道如何調試這一點。 – 2010-10-06 19:24:49
@Jörg拜爾奇怪。由於某種原因,如果請求不是有效的HTTP請求,通常只會得到400。我已經更新了我的答案,請給腳本一下,應該可以運行。 – Day 2010-10-06 20:31:59
我仍然收到「400錯誤請求」。我相信,auth-token是可以的,因爲當我修改新生成的令牌時,我得到了401。 – 2010-10-08 18:37:30