Skip to content

Commit

Permalink
Merge pull request #329 from avanderm/master
Browse files Browse the repository at this point in the history
Updates for mapbox and bbox functionality for here
  • Loading branch information
DenisCarriere committed Feb 9, 2018
2 parents b5e317e + 914f4d1 commit 492312d
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 27 deletions.
20 changes: 18 additions & 2 deletions docs/providers/HERE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@ Geocoding
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`'.

A bounding box can be supplied as an array of the form [minX, minY, maxX, maxY] to restrict results.

.. code-block:: python
>>> import geocoder
>>> bbox = [-118.604794, 34.172684, -118.500938, 34.236144]
>>> g = geocoder.here("Winnetka", bbox=bbox)
>>> g.address
"Winnetka, CA, United States"
>>> g = geocoder.here("Winnetka")
>>> g.address
"Winnetka, IL, United States"
...
Please refer to :ref:`this section <bbox>` for more details.
Reverse Geocoding
~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -55,15 +70,16 @@ To make sure your API key is store safely on your computer, you can define that

.. code-block:: bash
$ export APP_ID=<Secret APP ID>
$ export APP_CODE=<Secret APP Code>
$ export HERE_APP_ID=<Secret APP ID>
$ export HERE_APP_CODE=<Secret APP Code>
Parameters
----------

- `location`: Your search location you want geocoded.
- `app_code`: (optional) use your own Application Code from HERE.
- `app_id`: (optional) use your own Application ID from HERE.
- `bbox`: Search within a bounding box [minX, minY, maxX, maxY]. Pass as an array.
- `maxRows`: (default=1) Max number of results to fetch
- `method`: (default=geocode) Use the following:

Expand Down
43 changes: 24 additions & 19 deletions docs/providers/Mapbox.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,40 @@ Geocoding
This provider may return multiple results. You can access those results as described in the page ':doc:`/results`'.

Reverse Geocoding
~~~~~~~~~~~~~~~~~
Request feature data that best matches input and is biased to the given {latitude} and {longitude} coordinates. In the above example, a query of "200 Queen Street" returns a subset of all relevant addresses in the world. By adding the proximity option, this subset can be biased towards a given area, returning a more relevant set of results. In addition, a bounding box can be supplied to restrict results.

.. code-block:: python
>>> import geocoder
>>> latlng = [45.3, -105.1]
>>> g = geocoder.mapbox(latlng, method='reverse')
>>> g.json
>>> latlng = [45.3, -66.1]
>>> g = geocoder.mapbox("200 Queen Street", proximity=latlng)
>>> g.address
"200 Queen St W, Saint John, New Brunswick E2M 2C8, Canada"
>>> g = geocoder.mapbox("200 Queen Street")
>>> g.address
"200 Queen Street, Colac, Victoria 3250, Australia"
>>> bbox = [-118.604794, 34.172684, -118.500938, 34.236144]
>>> g = geocoder.mapbox("Winnetka", bbox=bbox)
>>> g.address
"Winnetka, Winnetka, California 91306, United States"
>>> g = geocoder.mapbox("Winnetka")
>>> g.address
"Winnetka Heights, Dallas, Texas 75211, United States"
...
Geocoding with Proximity
~~~~~~~~~~~~~~~~~~~~~~~~
Please refer to :ref:`this section <bbox>` for more details.

Request feature data that best matches input and is biased to the given {latitude} and {longitude} coordinates. In the above example, a query of "200 Queen Street" returns a subset of all relevant addresses in the world. By adding the proximity option, this subset can be biased towards a given area, returning a more relevant set of results.
Reverse Geocoding
~~~~~~~~~~~~~~~~~

.. code-block:: python
>>> import geocoder
>>> latlng = [45.3, -66.1]
>>> g = geocoder.mapbox("200 Queen Street", proximity=latlng)
>>> g.address
"200 Queen St, Saint John, E2L 2X1, New Brunswick, Canada"
>>> g = geocoder.mapbox("200 Queen Street")
"200 Queen St W, Toronto, M5T 1T9, Ontario, Canada"
>>> latlng = [45.3, -105.1]
>>> g = geocoder.mapbox(latlng, method='reverse')
>>> g.json
...
For consistency purpose, geocoder also accepts other formats for the 'proximity' parameter, which could be a bbox, bounds or a dictionnary with all directions. Please refer to :ref:`this section <bbox>` for more details.


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

Expand All @@ -68,6 +72,7 @@ Parameters

- `location`: Your search location you want geocoded.
- `proximity`: Search nearby [lat, lng].
- `bbox`: Search within a bounding box [minX, minY, maxX, maxY]. Pass as an array.
- `key`: Use your own access token from Mapbox.
- `country`: Filtering by country code {cc} ISO 3166 alpha 2.
- `proximity`: Search within given area (bbox, bounds, or around latlng)
Expand All @@ -79,5 +84,5 @@ Parameters
References
----------

- `Mabpox Geocoding API <https://www.mapbox.com/developers/api/geocoding/>`_
- `Get Mabpox Access Token <https://www.mapbox.com/account>`_
- `Mapbox Geocoding API <https://www.mapbox.com/developers/api/geocoding/>`_
- `Get Mapbox Access Token <https://www.mapbox.com/account>`_
16 changes: 15 additions & 1 deletion geocoder/here.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from geocoder.base import OneResult, MultipleResultsQuery
from geocoder.keys import here_app_id, here_app_code

from geocoder.location import BBox

class HereResult(OneResult):

Expand Down Expand Up @@ -124,8 +125,21 @@ def _build_params(self, location, provider_key, **kwargs):
'app_code': app_code,
'gen': 9,
'maxresults': kwargs.get('maxRows', 1),
'language': kwargs.get('language', 'en')
'language': kwargs.get('language', 'en'),
}

# bounding box if present
bbox = kwargs.get('bbox')
if bbox:
bbox = BBox(bbox=bbox)
# do not forget to convert bbox to mapbox expectations...
params['bbox'] = u'{north},{west};{south},{east}'.format(
west=bbox.west,
east=bbox.east,
south=bbox.south,
north=bbox.north
)

for value in self.qualified_address:
if kwargs.get(value) is not None:
params[value] = kwargs.get(value)
Expand Down
24 changes: 19 additions & 5 deletions geocoder/mapbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from __future__ import absolute_import
from geocoder.base import OneResult, MultipleResultsQuery
from geocoder.keys import mapbox_access_token
from geocoder.location import BBox
from geocoder.location import BBox, Location


class MapboxResult(OneResult):
Expand Down Expand Up @@ -113,11 +113,25 @@ def _build_params(self, location, provider_key, **kwargs):
'types': kwargs.get('types'),
}
# handle proximity
bbox = kwargs.get('proximity', None)
if bbox is not None:
bbox = BBox(bbox)
proximity = kwargs.get('proximity', None)
if proximity is not None:
proximity = Location(proximity)
# do not forget to convert bbox to mapbox expectations...
base_params['proximity'] = u'{0},{1}'.format(bbox.xy)
base_params['proximity'] = u'{longitude},{latitude}'.format(
longitude=proximity.longitude,
latitude=proximity.latitude
)

bbox = kwargs.get('bbox')
if bbox:
bbox = BBox(bbox=bbox)
# do not forget to convert bbox to mapbox expectations...
base_params['bbox'] = u'{west},{south},{east},{north}'.format(
west=bbox.west,
east=bbox.east,
south=bbox.south,
north=bbox.north
)

return base_params

Expand Down
15 changes: 15 additions & 0 deletions tests/test_here.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
location = 'Ottawa, Ontario'
ottawa = (45.4215296, -75.6971930)

winnetka = 'Winnetka'
winnetka_bbox = [-118.604794,34.172684,-118.500938,34.236144]


def test_here():
g = geocoder.here(location)
Expand All @@ -15,6 +18,18 @@ def test_here():
assert fields_count >= 13


def test_here_with_bbox():
g = geocoder.here(winnetka, bbox=winnetka_bbox)
assert g.ok
osm_count, fields_count = g.debug()[0]
assert osm_count >= 2
assert fields_count >= 11

for result in g:
assert (result.lng >= winnetka_bbox[0]) and (result.lng <= winnetka_bbox[2])
assert (result.lat >= winnetka_bbox[1]) and (result.lat <= winnetka_bbox[3])


def test_here_reverse():
g = geocoder.here(ottawa, method='reverse')
assert g.ok
23 changes: 23 additions & 0 deletions tests/test_mapbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
city = 'Ottawa'
ottawa = (45.4215296, -75.6971930)

winnetka = 'Winnetka'
winnetka_bbox = [-118.604794,34.172684,-118.500938,34.236144]


def test_mapbox():
g = geocoder.mapbox(location)
Expand All @@ -16,6 +19,26 @@ def test_mapbox():
assert fields_count >= 11


def test_mapbox_with_proximity():
g = geocoder.mapbox(location, proximity=ottawa)
assert g.ok
osm_count, fields_count = g.debug()[0]
assert osm_count >= 2
assert fields_count >= 11


def test_mapbox_with_bbox():
g = geocoder.mapbox(winnetka, bbox=winnetka_bbox)
assert g.ok
osm_count, fields_count = g.debug()[0]
assert osm_count >= 2
assert fields_count >= 11

for result in g:
assert (result.lng >= winnetka_bbox[0]) and (result.lng <= winnetka_bbox[2])
assert (result.lat >= winnetka_bbox[1]) and (result.lat <= winnetka_bbox[3])


def test_mapbox_reverse():
g = geocoder.mapbox(ottawa, method='reverse')
assert g.ok
Expand Down

0 comments on commit 492312d

Please sign in to comment.