Skip to content

Commit

Permalink
Use Point or numerical type data to create a tuple for Nominatim view…
Browse files Browse the repository at this point in the history
…_box (#299)
  • Loading branch information
paulefoe authored and KostyaEsmukov committed Jun 7, 2018
1 parent 4a104e9 commit ce32d03
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
12 changes: 11 additions & 1 deletion geopy/geocoders/osm.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"""
OpenStreetMap geocoder, contributed by Alessandro Pasotti of ItOpen.
"""
from itertools import chain

from geopy.compat import urlencode
from geopy.exc import GeocoderQueryError
from geopy.geocoders.base import DEFAULT_SENTINEL, Geocoder
from geopy.location import Location
from geopy.util import logger
from geopy.point import Point

__all__ = ("Nominatim", )

Expand Down Expand Up @@ -53,6 +55,10 @@ def __init__(
See :attr:`geopy.geocoders.options.default_format_string`.
:param tuple view_box: Coordinates to restrict search within.
Accepts instances of the :class:`geopy.point.Point`
``[Point(22, 180), Point(-22, -180)]``,
or iterables of numeric and string types ``[180, 22, -180, -22]``,
``["180", "22", "-180", "-22"]``
:param str country_bias: Bias results to this country.
Expand Down Expand Up @@ -202,7 +208,11 @@ def geocode(

# `viewbox` apparently replaces `view_box`
if self.view_box:
params['viewbox'] = ','.join(self.view_box)
if all(isinstance(point, Point) for point in self.view_box):
p1, p2 = self.view_box
params['viewbox'] = ','.join(str(p) for p in chain(p1[1::-1], p2[1::-1]))
else:
params['viewbox'] = ','.join(str(coord) for coord in self.view_box)

if self.country_bias:
params['countrycodes'] = self.country_bias
Expand Down
20 changes: 20 additions & 0 deletions test/geocoders/nominatim.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ class NominatimTestCase(GeocoderTestBase):
def setUpClass(cls):
cls.geocoder = Nominatim()

def tearDown(self):
try:
del self.geocoder
except AttributeError:
pass

def test_geocode(self):
"""
Nominatim.geocode
Expand Down Expand Up @@ -207,3 +213,17 @@ def test_missing_reverse_details(self):
'address',
res
)

def test_view_box(self):
self.geocode_run(
{"query": "Maple Street"},
{"latitude": 36.809551, "longitude": -97.050604},
)
for view_box in [(-0.11, 52, -0.15, 50),
[Point(52, -0.11), Point(50, -0.15)],
("-0.11", "52", "-0.15", "50")]:
self.geocoder = Nominatim(view_box=view_box)
self.geocode_run(
{"query": "Maple Street"},
{"latitude": 51.5223513, "longitude": -0.1382104}
)

0 comments on commit ce32d03

Please sign in to comment.