隨著圖資的開放和普及,GIS資料也越來越受到重視.要結合一般的資料和圖資資料之間一個關鍵就是地址和經緯度(這邊不討論不同座標標示系統的用法...)通常我們比較容易知道的是地址資料,經緯度就要透過握有圖資的單位才能查到(例如:
http://tgos.nat.gov.tw/tgos/web/tgos_home.aspx).但是政府查詢還要申請,使用上也有所限制,好險偉大的google大神也有開放API可以查詢(Orz),為了使用方便就寫了一個簡單的API跟Python接口:
廢話不多說,直接看github:https://github.com/bryanyang0528/geocodeQuery
說明頁面應該寫得滿清楚了(
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib, urllib2 | |
import json | |
class GeocodeQuery: | |
def __init__(self, language=None, region=None): | |
self.url = 'https://maps.googleapis.com/maps/api/geocode/json?language={0}®ion={1}&sensor=false'.format(language, region) | |
self.jsonResponse = {} | |
def get_geocode(self, addr): | |
addr = urllib.quote(addr) | |
url = self.url + '&address={}'.format(addr) | |
response = urllib2.urlopen(url) | |
self.jsonResponse = json.loads(response.read()) | |
return self.jsonResponse | |
def get_lat(self): | |
if len(self.jsonResponse["results"]) is not 0: | |
return self.jsonResponse["results"][0]["geometry"]["location"]["lat"] | |
def get_lng(self): | |
if len(self.jsonResponse["results"]) is not 0: | |
return self.jsonResponse["results"][0]["geometry"]["location"]["lng"] | |
def get_cuntry(self): | |
if len(self.jsonResponse["results"]) is not 0: | |
return self.jsonResponse["results"][0]["address_components"][4]["long_name"] | |
def get_area(self): | |
if len(self.jsonResponse["results"]) is not 0: | |
return self.jsonResponse["results"][0]["address_components"][3]["long_name"] |
如果使用上有任何問題或建議歡迎發ISSUE或REQUEST跳下來改XD,還有很多待改進的地方我會慢慢修理XD.
想請問一下,關於這支程式碼所轉換的經緯度,我該如何取出來存成CSV檔?
回覆刪除