-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #275 from xiangmeng4geo/master
add baidu reverse
- Loading branch information
Showing
14 changed files
with
472 additions
and
3 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
Gaode | ||
===== | ||
|
||
Gaode(AMap) Maps Geocoding API is a free open the API, the default quota | ||
one 2000 times / day. | ||
|
||
This API only support china. | ||
|
||
Geocoding | ||
~~~~~~~~~ | ||
|
||
.. code-block:: python | ||
>>> import geocoder # pip install geocoder | ||
>>> g = geocoder.gaode('方恒国际中心A座', key='<API KEY>') | ||
>>> g.json | ||
... | ||
Command Line Interface | ||
---------------------- | ||
|
||
.. code-block:: bash | ||
$ geocode '方恒国际中心A座' --provider gaode | ||
Environment Variables | ||
--------------------- | ||
|
||
To make sure your API key is store safely on your computer, you can define that API key using your system's environment variables. | ||
|
||
.. code-block:: bash | ||
$ export GAODE_API_KEY=<Secret API Key> | ||
Parameters | ||
---------- | ||
|
||
- `location`: Your search location you want geocoded. | ||
- `key`: Gaode API key. | ||
- `method`: (default=geocode) Use the following: | ||
|
||
- geocode | ||
- reverse | ||
|
||
References | ||
---------- | ||
|
||
- `API Reference <http://lbs.amap.com/api/webservice/guide/api/georegeo>`_ | ||
- `Get Gaode key <http://lbs.amap.com/dev/>`_ |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#!/usr/bin/python | ||
# coding: utf8 | ||
|
||
from __future__ import absolute_import | ||
|
||
from geocoder.baidu import Baidu | ||
from geocoder.keys import baidu_key | ||
from geocoder.location import Location | ||
|
||
|
||
class BaiduReverse(Baidu): | ||
""" | ||
Baidu Geocoding API | ||
=================== | ||
Baidu Maps Geocoding API is a free open the API, the default quota | ||
one million times / day. | ||
Params | ||
------ | ||
:param location: Your search location you want geocoded. | ||
:param key: Baidu API key. | ||
:param referer: Baidu API referer website. | ||
References | ||
---------- | ||
API Documentation: http://developer.baidu.com/map | ||
Get Baidu Key: http://lbsyun.baidu.com/apiconsole/key | ||
""" | ||
provider = 'baidu' | ||
method = 'reverse' | ||
|
||
def __init__(self, location, **kwargs): | ||
self.url = 'http://api.map.baidu.com/geocoder/v2/' | ||
self.location = location | ||
location = Location(location) | ||
coordtype = 'wgs84ll' | ||
if 'coordtype' in kwargs: | ||
coordtype = kwargs['coordtype'] | ||
self.params = { | ||
'location': str(location), | ||
'ret_coordtype': coordtype, | ||
'output': 'json', | ||
'ak': self._get_api_key(baidu_key, **kwargs), | ||
} | ||
if ('lang_code' in kwargs): | ||
self.params['accept-language'] = kwargs.get('lang_code') | ||
self._initialize(**kwargs) | ||
|
||
@property | ||
def address(self): | ||
return self.parse['result']['formatted_address'] | ||
|
||
@property | ||
def country(self): | ||
return self.parse['addressComponent']['country'] | ||
|
||
@property | ||
def province(self): | ||
return self.parse['addressComponent']['province'] | ||
|
||
@property | ||
def state(self): | ||
return self.parse['addressComponent']['province'] | ||
|
||
@property | ||
def city(self): | ||
return self.parse['addressComponent']['city'] | ||
|
||
@property | ||
def district(self): | ||
return self.parse['addressComponent']['district'] | ||
|
||
@property | ||
def street(self): | ||
return self.parse['addressComponent']['street'] | ||
|
||
@property | ||
def housenumber(self): | ||
return self.parse['addressComponent']['street_number'] | ||
|
||
|
||
if __name__ == '__main__': | ||
g = BaiduReverse("39.983424,116.32298", key='35d0b72b3e950e5d0b74b037262f8b41') | ||
g.debug() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
#!/usr/bin/python | ||
# coding: utf8 | ||
|
||
from __future__ import absolute_import | ||
|
||
import requests | ||
|
||
from geocoder.base import Base | ||
from geocoder.keys import gaode_key | ||
|
||
|
||
class Gaode(Base): | ||
""" | ||
Gaode AMap Geocoding API | ||
=================== | ||
Gaode Maps Geocoding API is a free open the API, the default quota | ||
2000 times / day. | ||
Params | ||
------ | ||
:param location: Your search location you want geocoded. | ||
:param key: Gaode API key. | ||
References | ||
---------- | ||
API Documentation: http://lbs.amap.com/api/webservice/guide/api/georegeo | ||
Get AMap Key: http://lbs.amap.com/dev/ | ||
""" | ||
provider = 'gaode' | ||
method = 'geocode' | ||
|
||
def __init__(self, location, **kwargs): | ||
self.url = 'http://restapi.amap.com/v3/geocode/geo' | ||
self.location = location | ||
self.params = { | ||
'address': location, | ||
'output': 'JSON', | ||
'key': self._get_api_key(gaode_key, **kwargs), | ||
} | ||
self.headers = {'Referer': kwargs.get('referer', '')} | ||
self._initialize(**kwargs) | ||
|
||
def _initialize(self, **kwargs): | ||
# Remove extra URL from kwargs | ||
if 'url' in kwargs: | ||
kwargs.pop('url') | ||
self.json = {} | ||
self.parse = self.tree() | ||
self.content = None | ||
self.encoding = kwargs.get('encoding', 'utf-8') | ||
self.session = kwargs.get('session', requests.Session()) | ||
self._connect(url=self.url, **kwargs) | ||
### | ||
try: | ||
for result in self.content['geocodes']: # Convert to iterator in each of the search tools | ||
self._build_tree(result) | ||
self._exceptions() | ||
self._catch_errors() | ||
self._json() | ||
except: | ||
self._build_tree(self.content) | ||
self._exceptions() | ||
self._catch_errors() | ||
self._json() | ||
|
||
@property | ||
def lat(self): | ||
return float(self.parse['location'].replace("'", '').split(',')[1]) | ||
|
||
@property | ||
def lng(self): | ||
return float(self.parse['location'].replace("'", '').split(',')[0]) | ||
|
||
@property | ||
def quality(self): | ||
return self.parse['level'] | ||
|
||
@property | ||
def address(self): | ||
return self.parse['formatted_address'] | ||
|
||
@property | ||
def country(self): | ||
return '中国' | ||
|
||
@property | ||
def province(self): | ||
return self.parse['province'] | ||
|
||
@property | ||
def state(self): | ||
return self.parse['province'] | ||
|
||
@property | ||
def city(self): | ||
return self.parse['city'] | ||
|
||
@property | ||
def district(self): | ||
return self.parse['district'] | ||
|
||
@property | ||
def street(self): | ||
return self.parse['street'] | ||
|
||
@property | ||
def adcode(self): | ||
return self.parse['adcode'] | ||
|
||
@property | ||
def housenumber(self): | ||
return self.parse['number'] | ||
|
||
|
||
if __name__ == '__main__': | ||
g = Gaode('纽约') | ||
g.debug() |
Oops, something went wrong.