Skip to content

Commit

Permalink
Add utility to download tile RGB
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Apr 18, 2020
1 parent 2632874 commit 655b518
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion geoviews/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
import param
import numpy as np
import shapely.geometry as sgeom
import pandas as pd

from cartopy import crs as ccrs
from cartopy.io.img_tiles import GoogleTiles, QuadtreeTiles
from holoviews.element import Tiles
from holoviews.core.util import basestring
from shapely.geometry.base import BaseMultipartGeometry
from shapely.geometry import (
MultiLineString, LineString, MultiPolygon, Polygon, LinearRing,
Point, MultiPoint
Point, MultiPoint, box
)

geom_types = (MultiLineString, LineString, MultiPolygon, Polygon,
Expand Down Expand Up @@ -660,3 +663,43 @@ def from_xarray(da, crs=None, apply_transform=False, nan_nodata=False, **kwargs)
if hasattr(el.data, 'attrs'):
el.data.attrs = da.attrs
return el


def get_tile_rgb(tile_source, bbox, zoom_level, bbox_crs=ccrs.PlateCarree()):
"""
Returns an RGB element given a tile_source, bounding box and zoom level.
Parameters
----------
tile_source: WMTS element or string URL
The tile source to download the tiles from.
bbox: tuple
A four tuple specifying the (left, bottom, right, top) corners of the
domain to download the tiles for.
zoom_level: int
The zoom level at which to download the tiles
bbox_crs: ccrs.CRs
cartopy CRS defining the coordinate system of the supplied bbox
Returns
-------
RGB element containing the tile data in the specified bbox
"""

from .element import RGB, WMTS
if isinstance(tile_source, (WMTS, Tiles)):
tile_source = tile_source.data

bbox = project_extents(bbox, bbox_crs, ccrs.GOOGLE_MERCATOR)

if '{Q}' in tile_source:
tile_source = QuadtreeTiles(url=tile_source.replace('{Q}', '{tile}'))
else:
tile_source = GoogleTiles(url=tile_source)

bounds = box(*bbox)
rgb, extent, orient = tile_source.image_for_domain(bounds, zoom_level)
if orient == 'lower':
rgb = rgb[::-1]
x0, x1, y0, y1 = extent
return RGB(rgb, bounds=(x0, y0, x1, y1), crs=ccrs.GOOGLE_MERCATOR, vdims=['R', 'G', 'B'])

0 comments on commit 655b518

Please sign in to comment.