Skip to content

Commit

Permalink
Merge pull request #281 from DenisCarriere/feature_more_providers_upg…
Browse files Browse the repository at this point in the history
…raded

Feature more providers upgraded
  • Loading branch information
DenisCarriere committed Aug 29, 2017
2 parents d6b0aa5 + 04e1a22 commit 63cba7b
Show file tree
Hide file tree
Showing 22 changed files with 403 additions and 248 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ $ geocode "Ottawa, ON" \

| Provider | Optimal | Usage Policy | Mutiple results | Reverse | Proximity |
|:-------------------------------|:----------|:--------------------------------|:----------------|:--------|:----------|
| [ArcGIS][ArcGIS] | World | | | yes | |
| [ArcGIS][ArcGIS] | World | | yes | yes | |
| [Baidu][Baidu] | China | API key | | yes | |
| [Bing][Bing] | World | API key | yes | yes | |
| [CanadaPost][CanadaPost] | Canada | API key | | | |
Expand All @@ -156,10 +156,10 @@ $ geocode "Ottawa, ON" \
| [Mapzen][Mapzen] | World | API key | | yes | |
| [MaxMind][MaxMind] | World | | | | |
| [OpenCage][OpenCage] | World | API key | | yes | |
| [OpenStreetMap][OpenStreetMap] | World | [Policy][OpenStreetMap-Policy] | | yes | |
| [OpenStreetMap][OpenStreetMap] | World | [Policy][OpenStreetMap-Policy] | yes | yes | |
| [Tamu][Tamu] | US | API key | | | |
| [TomTom][TomTom] | World | API key | | | |
| [What3Words][What3Words] | World | API key | | yes | |
| [What3Words][What3Words] | World | API key | yes | yes | |
| [Yahoo][Yahoo] | World | | | | |
| [Yandex][Yandex] | Russia | | | yes | |
| [TGOS][TGOS] | Taiwan | | | | |
Expand Down
4 changes: 4 additions & 0 deletions docs/providers/ArcGIS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Geocoding
>>> g.json
...
This provider may return multiple results by setting the parameter `maxRows` to the desired number (1 by default). You can access those results as described in the page ':doc:`/results`'.

Command Line Interface
----------------------

Expand All @@ -28,6 +30,8 @@ Parameters
----------

- `location`: Your search location you want geocoded.
- `maxRows`: (default=1) Max number of results to fetch
- `limit`: *Deprecated*, same as maxRows
- `method`: (default=geocode) Use the following:

- geocode
Expand Down
2 changes: 2 additions & 0 deletions docs/providers/Bing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Geocoding
>>> g.json
...
This provider may return multiple results by setting the parameter `maxRows` to the desired number (1 by default). You can access those results as described in the page ':doc:`/results`'.

Reverse Geocoding
~~~~~~~~~~~~~~~~~

Expand Down
2 changes: 2 additions & 0 deletions docs/providers/MapQuest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Geocoding
>>> g.json
...
This provider may return multiple results by setting the parameter `maxRows` to the desired number (1 by default). You can access those results as described in the page ':doc:`/results`'.

Reverse Geocoding
~~~~~~~~~~~~~~~~~

Expand Down
2 changes: 2 additions & 0 deletions docs/providers/Mapbox.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Geocoding
>>> g.json
...
This provider may return multiple results. You can access those results as described in the page ':doc:`/results`'.

Reverse Geocoding
~~~~~~~~~~~~~~~~~

Expand Down
4 changes: 4 additions & 0 deletions docs/providers/OpenStreetMap.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Geocoding
>>> g.json
...
This provider may return multiple results by setting the parameter `maxRows` to the desired number (1 by default). You can access those results as described in the page ':doc:`/results`'.

Nominatim Server
~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -65,6 +67,8 @@ Parameters

- `location`: Your search location you want geocoded.
- `url`: Custom OSM Server (ex: localhost)
- `maxRows`: (default=1) Max number of results to fetch
- `limit`: *Deprecated*, same as maxRows
- `method`: (default=geocode) Use the following:

- geocode
Expand Down
2 changes: 1 addition & 1 deletion geocoder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
__title__ = 'geocoder'
__author__ = 'Denis Carriere'
__author_email__ = 'carriere.denis@gmail.com'
__version__ = '1.29.1'
__version__ = '1.30.0'
__license__ = 'MIT'
__copyright__ = 'Copyright (c) 2013-2016 Denis Carriere'

Expand Down
12 changes: 6 additions & 6 deletions geocoder/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@

from geocoder.gaode import Gaode
from geocoder.gaode_reverse import GaodeReverse
from geocoder.osm import Osm
from geocoder.w3w import W3W
from geocoder.osm import OsmQuery
from geocoder.w3w import W3WQuery
from geocoder.bing import BingQuery
from geocoder.here import Here
from geocoder.tamu import Tamu
from geocoder.tgos import Tgos
from geocoder.yahoo import Yahoo
from geocoder.baidu import Baidu
from geocoder.tomtom import Tomtom
from geocoder.arcgis import Arcgis
from geocoder.arcgis import ArcgisQuery
from geocoder.ottawa import Ottawa
from geocoder.yandex import Yandex
from geocoder.mapbox import MapboxQuery
Expand Down Expand Up @@ -59,7 +59,7 @@

options = {
'osm': {
'geocode': Osm,
'geocode': OsmQuery,
'reverse': OsmReverse,
},
'tgos': {
Expand All @@ -80,7 +80,7 @@
'yahoo': {'geocode': Yahoo},
'tomtom': {'geocode': Tomtom},
'arcgis': {
'geocode': Arcgis,
'geocode': ArcgisQuery,
'reverse': ArcgisReverse
},
'ottawa': {'geocode': Ottawa},
Expand All @@ -99,7 +99,7 @@
},
'freegeoip': {'geocode': FreeGeoIP},
'w3w': {
'geocode': W3W,
'geocode': W3WQuery,
'reverse': W3WReverse,
},
'yandex': {
Expand Down
108 changes: 62 additions & 46 deletions geocoder/arcgis.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,46 @@
#!/usr/bin/python
# coding: utf8

from __future__ import absolute_import
from geocoder.base import Base

import logging
import json

from geocoder.base import OneResult, MultipleResultsQuery


class ArcgisResult(OneResult):

@property
def address(self):
return self.raw.get('name', '')

@property
def lat(self):
return self.raw['feature']['geometry'].get('y')

@property
def lng(self):
return self.raw['feature']['geometry'].get('x')

@property
def score(self):
return self.raw['feature']['attributes'].get('Score', '')

@property
def quality(self):
return self.raw['feature']['attributes'].get('Addr_Type', '')

@property
def bbox(self):
if self.raw['extent']:
south = self.raw['extent'].get('ymin')
west = self.raw['extent'].get('xmin')
north = self.raw['extent'].get('ymax')
east = self.raw['extent'].get('xmax')
return self._get_bbox(south, west, north, east)


class Arcgis(Base):
class ArcgisQuery(MultipleResultsQuery):
"""
ArcGIS REST API
=======================
Expand All @@ -22,54 +57,35 @@ class Arcgis(Base):
provider = 'arcgis'
method = 'geocode'

def __init__(self, location, **kwargs):
self.url = 'https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/find'
self.location = location
self.params = {
_URL = 'https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/find'
_RESULT_CLASS = ArcgisResult

@classmethod
def _get_api_key(cls, key=None):
# No API KEY for OSM
return None

def _build_params(self, location, provider_key, **kwargs):
# backward compatitibility for 'limit' (now maxRows)
if 'limit' in kwargs:
logging.warning(
"argument 'limit' in OSM is deprecated and should be replaced with maxRows")
kwargs['maxRows'] = kwargs['limit']
# build params
return {
'f': 'json',
'text': location,
'maxLocations': kwargs.get('limit', 1),
'maxLocations': kwargs.get('maxRows', 1),
}
self._initialize(**kwargs)

def _exceptions(self):
if self.parse['locations']:
self._build_tree(self.parse['locations'][0])

def __iter__(self):
for item in self.content['locations']:
yield item

@property
def lat(self):
return self.parse['geometry'].get('y')

@property
def lng(self):
return self.parse['geometry'].get('x')

@property
def address(self):
return self.parse.get('name', '')

@property
def score(self):
return self.parse['attributes'].get('Score', '')

@property
def quality(self):
return self.parse['attributes'].get('Addr_Type', '')

@property
def bbox(self):
if self.parse['extent']:
south = self.parse['extent'].get('ymin')
west = self.parse['extent'].get('xmin')
north = self.parse['extent'].get('ymax')
east = self.parse['extent'].get('xmax')
return self._get_bbox(south, west, north, east)
def _adapt_results(self, json_response):
return json_response['locations']


if __name__ == '__main__':
g = Arcgis('Toronto')
logging.basicConfig(level=logging.INFO)
g = ArcgisQuery('Toronto')
g.debug()
g = ArcgisQuery('Ottawa, Ontario', maxRows=5)
print(json.dumps(g.geojson, indent=4))
print([result.address for result in g][:3])

0 comments on commit 63cba7b

Please sign in to comment.