def getGeoInfo(strCommunity): try: g = geocoder.baidu(strCommunity, key=AK_BAIDU) if g.quality != '城市': r = geocoder.baidu(g.latlng, key=AK_BAIDU, method="reverse") print("检索【"+strCommunity+"】,坐标:"+str(g.latlng)+",城市:"+r.city+",区域:"+r.district+",地址:"+r.address) return [g.lat, g.lng, r.city, r.district, r.address] else: print("检索【"+strCommunity+"】,找不到!") return None except Exception as e: print("检索【"+strCommunity+"】,出错了!") return None
def checkout(self, name): """ Check out position of name. inputs: name: name of location outputs: lat: latitude of location lng: longitude of location yield: Update memory if not remembered """ if name in self.memory.index: lat = self.memory.loc[name].latitude lng = self.memory.loc[name].longitude else: message = f'Search online: {name}.' print(message) logging.info(message) try: g = geocoder.baidu(name, key=profiles.baidu_ak) lat, lng = g.latlng se = pd.Series(data={ 'latitude': lat, 'longitude': lng }, name=name) self.memory = self.memory.append(se) except KeyError as err: lat, lng = None, None message = repr(err) print(f'Error occured on checkout {name}: {message}') logging.error(message) return lat, lng
def test_baidu(): """ Expected result : http://api.map.baidu.com/geocoder/v2/?callback=renderOption&output=json&address=百度大厦&city=北京市&ak=<Your API Key> """ g = geocoder.baidu(location, key='35d0b72b3e950e5d0b74b037262f8b41') assert g.ok osm_count, fields_count = g.debug()[0] assert osm_count == 0 assert fields_count == 7
def test_baidu_reverse(): """ Expected result : http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location=39.9789660352,116.497157786&output=json&pois=1&ak=<Your API Key> """ g = geocoder.baidu(place, method='reverse', key='35d0b72b3e950e5d0b74b037262f8b41') assert g.ok assert g.country == u'中国' assert g.state == u'北京市' assert g.city == u'北京市' assert g.street == u'酒仙桥路'
def geocode_address(self): # Note: google API might not be accessible in China # Change the geocoder to Baidu or other Chinese search engine for Chinese tool try: # Try different geocoders: Google -> ArcGIS -> Bing -> Baidu self.geo_coder = geocoder.google(self.bldg_address) if (self.geo_coder.latlng is None): self.geo_coder = geocoder.arcgis(self.bldg_address) if (self.geo_coder.latlng is None): self.geo_coder = geocoder.bing(self.bldg_address) if (self.geo_coder.latlng is None): self.geo_coder = geocoder.baidu(self.bldg_address) except: raise ("Try another geocoder provider") self.coord = self.geo_coder.latlng self.latitude, self.longitude = self.coord self.geo_address = self.geo_coder.address
def test_baidu(): g = geocoder.baidu(china) assert g.ok
def crawl_geo_data(self, provider: str = 'arcgis') -> dict: """ Crawl continuous geo data based on categorical geo data :param provider: str Name of the provider to use: -> arcgis: ArcGis -> google: Google Maps :return: Dictionary containing the results of the geo-coding """ _geo: dict = {} _status: str = '' for i, loc in enumerate(self.location): #while _status.find('REQUEST_DENIED') >= 0 or _status == '': if provider == 'arcgis': _g: geocoder = geocoder.arcgis(location=loc, maxRows=1, method='geocode') elif provider == 'google': _g: geocoder = geocoder.google(location=loc, maxRows=1, method='geocode') elif provider == 'bing': _g: geocoder = geocoder.bing(location=loc, maxRows=1, method='geocode') elif provider == 'baidu': _g: geocoder = geocoder.baidu(location=loc, maxRows=1, method='geocode') elif provider == 'freegeoip': _g: geocoder = geocoder.freegeoip(location=loc, maxRows=1, method='geocode') elif provider == 'osm': _g: geocoder = geocoder.osm(location=loc, maxRows=1, method='geocode') elif provider == 'tomtom': _g: geocoder = geocoder.tomtom(location=loc, maxRows=1, method='geocode') elif provider == 'yahoo': _g: geocoder = geocoder.yahoo(location=loc, maxRows=1, method='geocode') else: raise HappyLearningUtilsException( 'Provider "{}" for geocoding not supported'.format( provider)) _status = _g.status if _status.find('OK') >= 0: _geo.update({loc: _g.json}) elif _status.find('ERROR') >= 0: _geo.update({loc: 'NaN'}) else: if _status.find('REQUEST_DENIED') < 0: raise HappyLearningUtilsException( 'Unknown request error "{}"'.format(_g.status)) if self.full_path is not None: DataExporter(obj=_geo, file_path=self.full_path).file() return _geo
## great-circle distance from geopy.distance import great_circle newport_ri = (41.49008, -71.312796) cleveland_oh = (41.499498, -81.695391) print(great_circle(newport_ri, cleveland_oh).miles) ## 查询坐标 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("中国人民大学")
def get_location(address): g = geocoder.baidu(address, key="TKBO91Psu8cjIXzkO1wnK4h8jgiQPlRR") return g.latlng
def getAddr(addr): address=geocoder.baidu(addr,key='V6CLFD6xWiHKXrCb0c8p4qGheETOImaQ') return address.json
print(great_circle(newport_ri, cleveland_oh).miles) ## 查询坐标 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) ''' 百度 API 直接开发 地理位置坐标转换
import geocoder if __name__ == '__main__': g = geocoder.baidu('北京天安门', key='5Ee1glQ0PwgQcDmZgMF9mi6XYgTVV5Ei') print((g.json)) latlng = [39.915446357113886, 116.40384918664363] h = geocoder.baidu(latlng, method='reverse', key='5Ee1glQ0PwgQcDmZgMF9mi6XYgTVV5Ei') print((h.json))