Created
June 28, 2019 16:32
-
-
Save Sillson/cb9b8f6a9146c779bb9467bcb40e4d34 to your computer and use it in GitHub Desktop.
Slippy Map Tile Composite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(slippymath) | |
library(sf) | |
library(purrr) | |
library(curl) | |
library(glue) | |
library(raster) | |
library(rgdal) | |
# pass map.bounds() & zoom from leaflet | |
# xmin <- map.getBounds()["_southWest"]['lat'] | |
# xmax <- map.getBounds()["_northEast"]['lat'] | |
# ymin <- map.getBounds()["_southWest"]['lng'] | |
# ymax <- map.getBounds()["_northEast"]['lng'] | |
# get a bbox | |
outlook_bbox <- | |
st_bbox(c(xmin = -122.35688209533693, | |
xmax = -122.32357978820802, | |
ymin = 47.64424145665395, | |
ymax = 47.663898895823536), | |
crs = sp::CRS("+init=epsg:4326")) | |
# slippy math to tile grid | |
max_tiles_param <- 15 # set programatically? | |
bbox_to_tile_grid(outlook_bbox, max_tiles = max_tiles_param) | |
tile_grid <- bbox_to_tile_grid(outlook_bbox, max_tiles = max_tiles_param) | |
# esri free tile server (used in leaflet) | |
# can be configurable to any of these: http://leaflet-extras.github.io/leaflet-providers/preview/ | |
esri_query_string <- | |
paste0("https://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer/tile/{zoom}/{y}/{x}") | |
# create tmp_dir | |
dir.create('tmp_tiles/') | |
# curl images | |
images <- | |
pmap(tile_grid$tiles, | |
function(x, y, zoom){ | |
outfile <- glue("tmp_tiles/{x}_{y}.jpg") | |
curl_download(url = glue(esri_query_string), | |
destfile = outfile) | |
outfile | |
}, | |
zoom = tile_grid$zoom) | |
# returns a raster brick | |
raster_out <- compose_tile_grid(tile_grid, images) | |
# remove the curled tmp_tiles | |
unlink('tmp_tiles', recursive=TRUE) | |
# make a png to preview | |
raster_to_png(raster_out, "curr_leaflet_view.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment