Skip to content

Commit

Permalink
Added Baidu provider in API
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed Feb 28, 2015
1 parent 1dea375 commit 50840b8
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 71 deletions.
2 changes: 1 addition & 1 deletion geocoder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
# CORE
from .api import get, yahoo, bing, geonames, google, mapquest
from .api import nokia, osm, tomtom, geolytica, arcgis, opencage
from .api import maxmind, freegeoip, ottawa, here
from .api import maxmind, freegeoip, ottawa, here, baidu

# EXTRAS
from .api import timezone, elevation, ip, canadapost, reverse
Expand Down
14 changes: 13 additions & 1 deletion geocoder/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .bing import Bing
from .here import Here
from .yahoo import Yahoo
from .baidu import Baidu
from .tomtom import Tomtom
from .google import Google
from .arcgis import Arcgis
Expand Down Expand Up @@ -42,6 +43,7 @@ def get(location, **kwargs):
'reverse': HereReverse,
},
'nokia': {'geocode': Here},
'baidu': {'geocode': Baidu}
'yahoo': {'geocode': Yahoo},
'tomtom': {'geocode': Tomtom},
'arcgis': {'geocode': Arcgis},
Expand Down Expand Up @@ -100,10 +102,20 @@ def google(location, **kwargs):
return get(location, provider='google', **kwargs)


def baidu(location, **kwargs):
"""Baidu Provider
:param location: Your search location you want geocoded.
:param key: Baidu API key.
:param referer: Baidu API referer website.
"""
return get(location, provider='baidu', **kwargs)


def ottawa(location, **kwargs):
"""Ottawa Provider
:param location: Your search location you want to retrieve elevation data.
:param location: Your search location you want geocoded.
"""
return get(location, provider='ottawa', **kwargs)

Expand Down
122 changes: 57 additions & 65 deletions geocoder/baidu.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,56 @@
#!/usr/bin/python
# coding: utf8

from .base import Base
from .keys import baidu_key
from base import Base
from keys import baidu_key


class Baidu(Base):
"""
Baidu Geocoding API
===================
Baidu Maps Geocoding API is a free open the API, the default quota
one million times / day.
API Reference
-------------
http://developer.baidu.com/map/index.php?
title=webapi/guide/webservice-geocoding
Get Baidu key
------------
http://lbsyun.baidu.com/apiconsole/key
OSM Quality (0/6)
-----------------
- [ ] addr:housenumber
- [ ] addr:street
- [ ] addr:city
- [ ] addr:state
- [ ] addr:country
- [ ] addr:postal
Attributes (8/18)
-----------------
- [ ] accuracy
- [ ] address
- [ ] bbox
- [ ] city
- [ ] confidence
- [ ] country
- [x] encoding
- [ ] housenumber
- [x] lat
- [x] lng
- [x] location
- [x] ok
- [ ] postal
- [x] provider
- [x] quality
- [ ] state
- [x] status
- [ ] street
"""
provider = 'baidu'
method = 'geocode'
Expand All @@ -19,81 +63,29 @@ def __init__(self, location, **kwargs):
'output': 'json',
'ak': kwargs.get('key', baidu_key),
}
self.headers = {
'Referer': kwargs.get('referer', 'http://developer.baidu.com'),
}
self._initialize(**kwargs)

def _exceptions(self):
# Build intial Tree with results
sets = self.parse['resourceSets']
if sets:
resources = sets[0]['resources']
if resources:
self._build_tree(resources[0])

for item in self.parse['geocodePoints']:
self._build_tree(item)

@property
def lat(self):
coord = self.parse['point']['coordinates']
if coord:
return coord[0]
return self.parse['location'].get('lat')

@property
def lng(self):
coord = self.parse['point']['coordinates']
if coord:
return coord[1]

@property
def address(self):
return self.parse['address'].get('formattedAddress')

@property
def housenumber(self):
if self.street:
expression = r'\d+'
pattern = re.compile(expression)
match = pattern.search(str(self.street))
if match:
return match.group(0)

@property
def street(self):
return self.parse['address'].get('addressLine')

@property
def city(self):
return self.parse['address'].get('locality')

@property
def state(self):
return self.parse['address'].get('adminDistrict')

@property
def country(self):
return self.parse['address'].get('countryRegion')
return self.parse['location'].get('lng')

@property
def quality(self):
return self.parse.get('entityType')
return self.parse['result'].get('level')

@property
def accuracy(self):
return self.parse.get('calculationMethod')

@property
def postal(self):
return self.parse['address'].get('postalCode')

@property
def bbox(self):
if self.parse['bbox']:
south = self.parse['bbox'][0]
north = self.parse['bbox'][2]
west = self.parse['bbox'][1]
east = self.parse['bbox'][3]
return self._get_bbox(south, west, north, east)
return self.parse['result'].get('confidence')

if __name__ == '__main__':
g = Baidu('China')
g.debug()
g = Baidu('中国')
import json
print(json.dumps(g.json, indent=4))
g.debug()
4 changes: 2 additions & 2 deletions geocoder/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
import requests
import sys
import json
import sys
from collections import defaultdict
from .haversine import haversine
from haversine import haversine


class Base(object):
Expand All @@ -32,6 +31,7 @@ class Base(object):
# Essential attributes for Street Address
address = ''
housenumber = ''
street = ''
road = ''
city = ''
state = ''
Expand Down
1 change: 0 additions & 1 deletion geocoder/bing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from .base import Base
from .keys import bing_key
import json
import re


Expand Down
2 changes: 1 addition & 1 deletion geocoder/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
canadapost_key = ''
opencage_key = '519418b47cfac1f8d435a7f3fda6a7e0'
mapquest_key = 'Kmjtd|luua2qu7n9,7a=o5-lzbgq'
baidu_key = 'XhSKPeEhiTqCnGmgqTyBvdFD'
baidu_key = 'E4805d16520de693a3fe707cdc962045'
7 changes: 7 additions & 0 deletions test_geocoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
location = 'Ottawa, Ontario'
city = 'Ottawa'
ip = '74.125.226.99'
china = '中国'
repeat = 3
ottawa = (45.4215296, -75.6971930)
toronto = (43.653226, -79.3831843)
Expand All @@ -18,6 +19,7 @@ def test_entry_points():
geocoder.osm
geocoder.bing
geocoder.here
geocoder.baidu
geocoder.yahoo
geocoder.google
geocoder.tomtom
Expand Down Expand Up @@ -48,6 +50,11 @@ def test_maxmind():
assert g.ok


def test_baidu():
g = geocoder.maxmind(china)
assert g.ok


def test_google():
g = geocoder.google(location)
assert g.ok
Expand Down

0 comments on commit 50840b8

Please sign in to comment.