Skip to content

Commit

Permalink
Add random user ID for Nominatim and allow arbitrary geocoder (#164)
Browse files Browse the repository at this point in the history
Co-authored-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
  • Loading branch information
darribas and jorisvandenbossche authored Oct 16, 2020
1 parent 83b0f68 commit 27d60da
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
22 changes: 18 additions & 4 deletions contextily/place.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@
import geopy as gp
import numpy as np
import matplotlib.pyplot as plt
from warnings import warn
import warnings

from .tile import howmany, bounds2raster, bounds2img, _sm2ll, _calculate_zoom
from .plotting import INTERPOLATION, ZOOM, add_attribution
from . import providers
from ._providers import TileProvider

# Set user ID for Nominatim
_val = np.random.randint(1000000)
_default_user_agent = f"contextily_user_{_val}"


class Place(object):
"""Geocode a place by name and get its map.
Expand Down Expand Up @@ -45,6 +50,8 @@ class Place(object):
Source url for web tiles, or path to local file. If
local, the file is read with `rasterio` and all
bands are loaded into the basemap.
geocoder : geopy.geocoders
[Optional. Default: geopy.geocoders.Nominatim()] Geocoder method to process `search`
Attributes
----------
Expand All @@ -69,7 +76,14 @@ class Place(object):
"""

def __init__(
self, search, zoom=None, path=None, zoom_adjust=None, source=None, url=None
self,
search,
zoom=None,
path=None,
zoom_adjust=None,
source=None,
url=None,
geocoder=gp.geocoders.Nominatim(user_agent=_default_user_agent),
):
self.path = path
if url is not None and source is None:
Expand All @@ -93,7 +107,7 @@ def __init__(
self.zoom_adjust = zoom_adjust

# Get geocoded values
resp = gp.geocoders.Nominatim().geocode(search)
resp = geocoder.geocode(search)
bbox = np.array([float(ii) for ii in resp.raw["boundingbox"]])

if "display_name" in resp.raw.keys():
Expand Down Expand Up @@ -240,7 +254,7 @@ def plot_map(
ax : instance of matplotlib Axes object or None
The axis on the map is plotted.
"""
warn(
warnings.warn(
(
"The method `plot_map` is deprecated and will be removed from the"
" library in future versions. Please use either `add_basemap` or"
Expand Down
2 changes: 2 additions & 0 deletions contextily/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ def add_basemap(
image, bounds, _ = _warper(
image, img_transform, raster.crs, crs, resampling
)
else:
bounds = raster.bounds
image = image.transpose(1, 2, 0)
extent = bounds.left, bounds.right, bounds.bottom, bounds.top
# Plotting
Expand Down

0 comments on commit 27d60da

Please sign in to comment.