python使用百度或高德地圖獲取地理位置并轉(zhuǎn)換
一、地理編碼與逆編碼
地理編碼與逆編碼表示的是地名地址與地理坐標(biāo)(經(jīng)緯度)互相轉(zhuǎn)換的過程。其中,將地址信息映射為地理坐標(biāo)的過程稱之為地理編碼;將地理坐標(biāo)轉(zhuǎn)換為地址信息的過程稱之為逆地理編碼。(ps:猜猜我在哪)
地理編碼能對一些只有名稱或地址的數(shù)據(jù)進(jìn)行空間化,從而可以開展空間分析和制圖,對于數(shù)據(jù)分析人員十分重要。
大量地圖廠商都提供了相關(guān)的API,可以直接利用這些API進(jìn)行轉(zhuǎn)化。國外的如谷歌、esri、osm等,國內(nèi)的有百度、高德、騰訊等公司,python提供了專業(yè)的geopy包集成了這些API調(diào)用。
地理處理包將單獨(dú)研究,本文直接使用高德和百度的開放API進(jìn)行地理編碼。使用開放API前需要注冊應(yīng)用獲取KEY
提示:以下是本篇文章正文內(nèi)容,下面案例可供參考
二、高德地圖地理編碼
詳細(xì)信息可參考官方開發(fā)文檔,可以看到,請求參數(shù)必填的有key和address,其他的根據(jù)需要設(shè)置
具體代碼如下:
def getGDCor(key,addr): baseUrl = 'http://restapi.amap.com/v3/geocode/geo?' params = {'key': key, ##應(yīng)用key 'address': addr, 'city': u'武漢' ##指定城市,限制范圍 } url = baseUrl + urllib.parse.urlencode(params) req = urllib.request.Request(url) content = urllib.request.urlopen(req).read() jsonData = json.loads(content) lon, lat = '', '' if jsonData['status'] == '1': try: corr = jsonData['geocodes'][0]['location'] lon,lat = corr.split(',')[0],corr.split(',')[1] except: lon,lat = '0','0' else: print('error') return (lon,lat)
三、百度地圖地理編碼
百度地圖與高德類似,同樣需要注冊應(yīng)用獲取key,詳細(xì)內(nèi)容參考官網(wǎng)文檔
def getBDCor(ak,addr): #sleep(random.random()) baseUrl = 'http://api.map.baidu.com/geocoding/v3/?' params = {'address': addr, 'city':u'武漢市', 'output':'json', 'ak': ak, ##應(yīng)用key 'callback': 'showLocation' } url = baseUrl + urllib.parse.urlencode(params) req = urllib.request.urlopen(url).read().decode() #'utf-8' 針對中文需要設(shè)施編碼 cont1 = req.replace("showLocation&&showLocation(",'') cont = cont1.replace(")",'') jsonData = json.loads(cont) lon, lat = 0, 0 if jsonData['status'] == 0: try: lon = jsonData['result']['location']['lng'] lat = jsonData['result']['location']['lat'] except: print(addr) return (lon,lat)
四、坐標(biāo)轉(zhuǎn)換和空間化
高德或百度獲取的地理位置都是經(jīng)過加密的,高德地圖位置使用的是火星坐標(biāo),百度地圖是bd-09坐標(biāo),與WGS84坐標(biāo)系間有偏差,通常需要進(jìn)行轉(zhuǎn)換。
- 火星坐標(biāo)(GCJ-02):國測局坐標(biāo),由WGS-84加密而成,國內(nèi)必須至少使用GCJ-02坐標(biāo)系,或者使用在GCJ-02加密后再進(jìn)行加密的坐標(biāo)系,如百度坐標(biāo)系。高德和Google在國內(nèi)都是使用GCJ-02坐標(biāo)系,可以說,GCJ-02是國內(nèi)最廣泛使用的坐標(biāo)系;
- 百度坐標(biāo)系是在GCJ-02坐標(biāo)系的基礎(chǔ)上再次加密偏移后形成的坐標(biāo)系。
各坐標(biāo)間可以通過坐標(biāo)轉(zhuǎn)換互轉(zhuǎn),通常有三參數(shù)或七參數(shù)法,網(wǎng)上可找到相關(guān)的轉(zhuǎn)換參數(shù),貼一下轉(zhuǎn)換函數(shù)(不知道哪位大神寫的)
import math x_pi = 3.14159265358979324 * 3000.0 / 180.0 pi = 3.1415926535897932384626 # π a = 6378245.0 # 長半軸 ee = 0.00669342162296594323 # 扁率 def gcj02_to_bd09(lng, lat): """ 火星坐標(biāo)系(GCJ-02)轉(zhuǎn)百度坐標(biāo)系(BD-09) 谷歌、高德——>百度 :param lng:火星坐標(biāo)經(jīng)度 :param lat:火星坐標(biāo)緯度 :return: """ z = math.sqrt(lng * lng + lat * lat) + 0.00002 * math.sin(lat * x_pi) theta = math.atan2(lat, lng) + 0.000003 * math.cos(lng * x_pi) bd_lng = z * math.cos(theta) + 0.0065 bd_lat = z * math.sin(theta) + 0.006 return [bd_lng, bd_lat] def bd09_to_gcj02(bd_lon, bd_lat): """ 百度坐標(biāo)系(BD-09)轉(zhuǎn)火星坐標(biāo)系(GCJ-02) 百度——>谷歌、高德 :param bd_lat:百度坐標(biāo)緯度 :param bd_lon:百度坐標(biāo)經(jīng)度 :return:轉(zhuǎn)換后的坐標(biāo)列表形式 """ x = bd_lon - 0.0065 y = bd_lat - 0.006 z = math.sqrt(x * x + y * y) - 0.00002 * math.sin(y * x_pi) theta = math.atan2(y, x) - 0.000003 * math.cos(x * x_pi) gg_lng = z * math.cos(theta) gg_lat = z * math.sin(theta) return [gg_lng, gg_lat] def wgs84_to_gcj02(lng, lat): """ WGS84轉(zhuǎn)GCJ02(火星坐標(biāo)系) :param lng:WGS84坐標(biāo)系的經(jīng)度 :param lat:WGS84坐標(biāo)系的緯度 :return: """ if out_of_china(lng, lat): # 判斷是否在國內(nèi) return lng, lat dlat = _transformlat(lng - 105.0, lat - 35.0) dlng = _transformlng(lng - 105.0, lat - 35.0) radlat = lat / 180.0 * pi magic = math.sin(radlat) magic = 1 - ee * magic * magic sqrtmagic = math.sqrt(magic) dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi) dlng = (dlng * 180.0) / (a / sqrtmagic * math.cos(radlat) * pi) mglat = lat + dlat mglng = lng + dlng return [mglng, mglat] def gcj02_to_wgs84(lng, lat): """ GCJ02(火星坐標(biāo)系)轉(zhuǎn)GPS84 :param lng:火星坐標(biāo)系的經(jīng)度 :param lat:火星坐標(biāo)系緯度 :return: """ if out_of_china(lng, lat): return lng, lat dlat = _transformlat(lng - 105.0, lat - 35.0) dlng = _transformlng(lng - 105.0, lat - 35.0) radlat = lat / 180.0 * pi magic = math.sin(radlat) magic = 1 - ee * magic * magic sqrtmagic = math.sqrt(magic) dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi) dlng = (dlng * 180.0) / (a / sqrtmagic * math.cos(radlat) * pi) mglat = lat + dlat mglng = lng + dlng return [lng * 2 - mglng, lat * 2 - mglat] def bd09_to_wgs84(bd_lon, bd_lat): lon, lat = bd09_to_gcj02(bd_lon, bd_lat) return gcj02_to_wgs84(lon, lat) def wgs84_to_bd09(lon, lat): lon, lat = wgs84_to_gcj02(lon, lat) return gcj02_to_bd09(lon, lat) def _transformlat(lng, lat): ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + \ 0.1 * lng * lat + 0.2 * math.sqrt(math.fabs(lng)) ret += (20.0 * math.sin(6.0 * lng * pi) + 20.0 * math.sin(2.0 * lng * pi)) * 2.0 / 3.0 ret += (20.0 * math.sin(lat * pi) + 40.0 * math.sin(lat / 3.0 * pi)) * 2.0 / 3.0 ret += (160.0 * math.sin(lat / 12.0 * pi) + 320 * math.sin(lat * pi / 30.0)) * 2.0 / 3.0 return ret def _transformlng(lng, lat): ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + \ 0.1 * lng * lat + 0.1 * math.sqrt(math.fabs(lng)) ret += (20.0 * math.sin(6.0 * lng * pi) + 20.0 * math.sin(2.0 * lng * pi)) * 2.0 / 3.0 ret += (20.0 * math.sin(lng * pi) + 40.0 * math.sin(lng / 3.0 * pi)) * 2.0 / 3.0 ret += (150.0 * math.sin(lng / 12.0 * pi) + 300.0 * math.sin(lng / 30.0 * pi)) * 2.0 / 3.0 return ret def out_of_china(lng, lat): """ 判斷是否在國內(nèi),不在國內(nèi)不做偏移 :param lng: :param lat: :return: """ return not (lng > 73.66 and lng < 135.05 and lat > 3.86 and lat < 53.55)
總結(jié)
到此這篇關(guān)于python使用百度或高德地圖獲取地理位置并轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)python獲取地理位置并轉(zhuǎn)換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pytorch實(shí)現(xiàn)好萊塢明星識別的示例代碼
本文主要介紹了pytorch實(shí)現(xiàn)好萊塢明星識別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01在keras中實(shí)現(xiàn)查看其訓(xùn)練loss值
這篇文章主要介紹了在keras中實(shí)現(xiàn)查看其訓(xùn)練loss值,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06python 實(shí)現(xiàn)數(shù)據(jù)庫中數(shù)據(jù)添加、查詢與更新的示例代碼
這篇文章主要介紹了python 實(shí)現(xiàn)數(shù)據(jù)庫中數(shù)據(jù)添加、查詢與更新的示例代碼,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12