Skip to content

Commit

Permalink
GoogleMap scale factor (#91)
Browse files Browse the repository at this point in the history
* Added the possibility to set the scale of a GoogleVisibleMap to 2, with safeties

* Forgotten a debug print

* Modified the addendum according to author desiderata
  • Loading branch information
tbridel authored and fmaussion committed Feb 14, 2018
1 parent 8b3bf36 commit 7fe2dca
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
24 changes: 14 additions & 10 deletions salem/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,8 @@ class GoogleCenterMap(GeoDataset):
"""

def __init__(self, center_ll=(11.38, 47.26), size_x=640, size_y=640,
zoom=12, maptype='satellite', use_cache=True, **kwargs):
scale=1, zoom=12, maptype='satellite', use_cache=True,
**kwargs):
"""Initialize
Parameters
Expand All @@ -486,6 +487,8 @@ def __init__(self, center_ll=(11.38, 47.26), size_x=640, size_y=640,
image size
size_y : int
image size
scale : int
image scaling factor
zoom int
google zoom level (https://developers.google.com/maps/documentation/
static-maps/intro#Zoomlevels)
Expand All @@ -497,18 +500,16 @@ def __init__(self, center_ll=(11.38, 47.26), size_x=640, size_y=640,
any keyword accepted by motionless.CenterMap (e.g. `key` for the API)
"""

if 'scale' in kwargs:
raise NotImplementedError('scale not supported')

# Google grid
grid = gis.googlestatic_mercator_grid(center_ll=center_ll,
nx=size_x, ny=size_y,
zoom=zoom)
zoom=zoom, scale=scale)

# Motionless
googleurl = motionless.CenterMap(lon=center_ll[0], lat=center_ll[1],
size_x=size_x, size_y=size_y,
maptype=maptype, zoom=zoom, **kwargs)
maptype=maptype, zoom=zoom, scale=scale,
**kwargs)

# done
self.googleurl = googleurl
Expand Down Expand Up @@ -536,7 +537,7 @@ class GoogleVisibleMap(GoogleCenterMap):
It's usually more practical to use than GoogleCenterMap.
"""

def __init__(self, x, y, crs=wgs84, size_x=640, size_y=640,
def __init__(self, x, y, crs=wgs84, size_x=640, size_y=640, scale=1,
maptype='satellite', use_cache=True, **kwargs):
"""Initialize
Expand All @@ -552,6 +553,8 @@ def __init__(self, x, y, crs=wgs84, size_x=640, size_y=640,
image size
size_y : int
image size
scale : int
image scaling factor
maptype : str, default: 'satellite'
'roadmap', 'satellite', 'hybrid', 'terrain'
use_cache : bool, default: True
Expand All @@ -577,13 +580,14 @@ def __init__(self, x, y, crs=wgs84, size_x=640, size_y=640,
zoom = 20
while zoom >= 0:
grid = gis.googlestatic_mercator_grid(center_ll=mc, nx=size_x,
ny=size_y, zoom=zoom)
ny=size_y, zoom=zoom,
scale=scale)
dx, dy = grid.transform(lon, lat, maskout=True)
if np.any(dx.mask):
zoom -= 1
else:
break

GoogleCenterMap.__init__(self, center_ll=mc, size_x=size_x,
size_y=size_y, zoom=zoom, maptype=maptype,
use_cache=use_cache, **kwargs)
size_y=size_y, zoom=zoom, scale=scale,
maptype=maptype, use_cache=use_cache, **kwargs)
11 changes: 8 additions & 3 deletions salem/gis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1437,14 +1437,14 @@ def mercator_grid(center_ll=None, extent=None, ny=600, nx=None,
pixel_ref='corner')


def googlestatic_mercator_grid(center_ll=None, nx=640, ny=640, zoom=12):
def googlestatic_mercator_grid(center_ll=None, nx=640, ny=640, zoom=12, scale=1):
"""Mercator map centered on a specified point (google API conventions).
Mostly useful for google maps.
"""

# Number of pixels in an image with a zoom level of 0.
google_pix = 256
google_pix = 256 * scale
# The equitorial radius of the Earth assuming WGS-84 ellipsoid.
google_earth_radius = 6378137.0

Expand All @@ -1453,6 +1453,10 @@ def googlestatic_mercator_grid(center_ll=None, nx=640, ny=640, zoom=12):
proj_params = dict(proj='merc', datum='WGS84')
projloc = pyproj.Proj(proj_params)

# The size of the image is multiplied by the scaling factor
nx *= scale
ny *= scale

# Meter per pixel
mpix = (2 * np.pi * google_earth_radius) / google_pix / (2**zoom)
xx = nx * mpix
Expand All @@ -1462,5 +1466,6 @@ def googlestatic_mercator_grid(center_ll=None, nx=640, ny=640, zoom=12):
corner = (-xx / 2. + e, yy / 2. + n)
dxdy = (xx / nx, - yy / ny)

return Grid(proj=projloc, x0y0=corner, nxny=(nx, ny), dxdy=dxdy,
return Grid(proj=projloc, x0y0=corner,
nxny=(nx, ny), dxdy=dxdy,
pixel_ref='corner')

0 comments on commit 7fe2dca

Please sign in to comment.