class Location(object): def __init__(self,baidu_api_key = '256d1d9f2a9509b557abde5b821d4e2d'): self.geolocator = Baidu(baidu_api_key) def geocode(self,query, exactly_one=True, timeout=None): '''获得经纬度''' return self.geolocator.geocode(query, exactly_one=exactly_one, timeout=timeout)
## 查询坐标 import geocoder as geo # bing g = geo.bing('中国人民大学',key='AtIY2sEa0AgKcn-9HXv7_kHyj29hepj0Ko4Pb4xZvoSUXN_ZXesx1z42EAIbDENL') place = geo.bing(g.latlng,method='reverse',key='AtIY2sEa0AgKcn-9HXv7_kHyj29hepj0Ko4Pb4xZvoSUXN_ZXesx1z42EAIbDENL') # baidu g = geo.baidu('中国人民大学',key='DPlowD7PIEfaVtpxLKGkXg8yDCCBanVO') #place = geo.baidu(g.latlng,method='reverse',key='DPlowD7PIEfaVtpxLKGkXg8yDCCBanVO') from geopy.geocoders import Baidu,Bing geoBaidu = Baidu(r'DPlowD7PIEfaVtpxLKGkXg8yDCCBanVO') location = geoBaidu.geocode("中国人民大学") place= geoBaidu.reverse((location.latitude,location.longitude)) geoBing = Bing('AtIY2sEa0AgKcn-9HXv7_kHyj29hepj0Ko4Pb4xZvoSUXN_ZXesx1z42EAIbDENL') location = geoBing.geocode("中国人民大学") place= geoBing.reverse((location.latitude,location.longitude),exactly_one=False) '''
async def test_invalid_ak(self): async with self.inject_geocoder(Baidu(api_key='DUMMYKEY1234')): with pytest.raises(GeocoderAuthenticationFailure) as exc_info: await self.geocode_run({"query": "baidu"}, None) assert str(exc_info.value) == 'Invalid AK'
def make_geocoder(cls, **kwargs): return Baidu( api_key=env['BAIDU_KEY'], timeout=3, **kwargs, )
def make_geocoder(cls, **kwargs): return Baidu( api_key='DUMMYKEY1234', user_agent='my_user_agent/1.0', **kwargs )
def setUpClass(cls): cls.geocoder = Baidu( scheme='http', api_key=env['BAIDU_KEY'] ) cls.delta_exact = 0.02
import geopandas as gpd import os import json from shapely.geometry import Point from geopy.geocoders import Baidu import time import matplotlib.pyplot as plt import requests import pandas as pd from datetime import datetime, timedelta GEOLOCATOR = Baidu( api_key='', #自己修改 timeout=60) def get_data_from_url(url): response = requests.get(url) data = json.loads(response.text) data = pd.DataFrame(data) return data def load_geo_code_dict(): with open("static/geo_map.json", "r", encoding="utf-8") as f: mm = f.read() geo_code_dict = json.loads(mm)
class GeoUtil(object): CHINA_SPECIAL = {u'香港特别行政区': 'HK', u'澳门特别行政区': 'MO', u'台湾省': 'TW'} Geolocator = Baidu('FDecb47114bb41c22f471828a4623800') FILE_NAME = 'country_code.dat' COUNTRY_CODE = {} @classmethod def init(cls): try: f = open(cls.FILE_NAME, 'r') for line in f: line = line.strip() if not len(line) or line.startswith('#'): continue code = line.split(',') if len(code) >= 4: cls.COUNTRY_CODE[int(code[1])] = { 'country': code[0], 'country_code': code[1], 'code': code[2], 'country_name': code[3] } f.close() except: traceback.print_exc() @classmethod def distance(cls, f, t): if not f or not t: return -1 try: f_location = [float(v) for v in f.split(',')] t_location = [float(v) for v in t.split(',')] return vincenty(f_location, t_location).km except: traceback.print_exc() return -1 @classmethod @retry(stop_max_attempt_number=3, retry_on_exception=retry_if_timeout) def decode(cls, geo): # try: r = cls.Geolocator.reverse(geo) return r.raw.get('addressComponent') # except GeocoderTimedOut: # logging.info('geo_decode error! geo:%s GeocoderTimedOut' % (geo,)) # except: # logging.error('geo_decode error! geo:%s %s' % (geo, traceback.format_exc())) # country, province, city @classmethod def get_locations(cls, geo): decode = cls.decode(geo) if not decode: return 'CN', '', '' c_code = decode.get('country_code') if c_code == 0: p = decode.get('province') if cls.CHINA_SPECIAL.get(p): return cls.CHINA_SPECIAL.get(p), decode.get( 'province'), decode.get('city') else: return 'CN', decode.get('province'), decode.get('city') elif cls.COUNTRY_CODE.get(c_code): return cls.COUNTRY_CODE.get(c_code)['code'], decode.get( 'province'), decode.get('city') else: # 显示其他 return 'QT', decode.get('province'), decode.get('city')
def test_invalid_ak(self): self.geocoder = Baidu(api_key='DUMMYKEY1234') with self.assertRaises(GeocoderAuthenticationFailure) as cm: self.geocode_run({"query": u("baidu")}, None) self.assertEqual(str(cm.exception), 'Invalid AK')
def setUpClass(cls): cls.geocoder = Baidu( api_key=env['BAIDU_KEY'], timeout=3, )
def setUpClass(cls): cls.geocoder = Baidu( api_key='DUMMYKEY1234', user_agent='my_user_agent/1.0' )
def __init__(self,baidu_api_key = '256d1d9f2a9509b557abde5b821d4e2d'): self.geolocator = Baidu(baidu_api_key)
def __init__(self): self.geoLocator = Baidu( configReader.getOptionValue('data_validation', 'baidu_map_ak'))
def setUpClass(cls): cls.geocoder = Baidu( api_key=env['BAIDU_KEY'], timeout=3, ) cls.delta_exact = 0.02
def setUpClass(cls): cls.geocoder = Baidu( api_key=env['BAIDU_KEY_REQUIRES_SK'], security_key=env['BAIDU_SEC_KEY'], timeout=3, )
def test_user_agent_custom(self): geocoder = Baidu(api_key='DUMMYKEY1234', user_agent='my_user_agent/1.0') self.assertEqual(geocoder.headers['User-Agent'], 'my_user_agent/1.0')
# bing g = geo.bing( '中国人民大学', key='AtIY2sEa0AgKcn-9HXv7_kHyj29hepj0Ko4Pb4xZvoSUXN_ZXesx1z42EAIbDENL') place = geo.bing( g.latlng, method='reverse', key='AtIY2sEa0AgKcn-9HXv7_kHyj29hepj0Ko4Pb4xZvoSUXN_ZXesx1z42EAIbDENL') # baidu g = geo.baidu('中国人民大学', key='DPlowD7PIEfaVtpxLKGkXg8yDCCBanVO') #place = geo.baidu(g.latlng,method='reverse',key='DPlowD7PIEfaVtpxLKGkXg8yDCCBanVO') from geopy.geocoders import Baidu, Bing geoBaidu = Baidu(r'DPlowD7PIEfaVtpxLKGkXg8yDCCBanVO') location = geoBaidu.geocode("中国人民大学") place = geoBaidu.reverse((location.latitude, location.longitude)) geoBing = Bing( 'AtIY2sEa0AgKcn-9HXv7_kHyj29hepj0Ko4Pb4xZvoSUXN_ZXesx1z42EAIbDENL') location = geoBing.geocode("中国人民大学") place = geoBing.reverse((location.latitude, location.longitude), exactly_one=False) ''' 百度 API 直接开发 地理位置坐标转换 1.构建查询字符串 http://api.map.baidu.com/geocoder/v2/ 2.查询gis 坐标 3.利用gis 坐标 reverse 查询附近地理位置 http://api.map.baidu.com/geosearch/v3/nearby/ baidu api key DPlowD7PIEfaVtpxLKGkXg8yDCCBanVO 终于好了,我去!