Skip to content

Commit

Permalink
cleanin up
Browse files Browse the repository at this point in the history
  • Loading branch information
mdsumner committed Apr 19, 2023
1 parent 0220581 commit f77b554
Show file tree
Hide file tree
Showing 16 changed files with 190 additions and 347 deletions.
3 changes: 0 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export(get_tiles_dim)
export(get_tiles_zoom)
export(mercator_tile_extent)
export(plot_tiles)
export(slippy_cache)
export(tiles_to_polygon)
importFrom(curl,curl_download)
importFrom(dplyr,filter)
Expand All @@ -33,13 +32,11 @@ importFrom(magrittr,"%>%")
importFrom(purrr,pmap)
importFrom(rappdirs,user_cache_dir)
importFrom(raster,"projection<-")
importFrom(raster,cellsFromExtent)
importFrom(raster,crop)
importFrom(raster,extent)
importFrom(rlang,.data)
importFrom(sp,plot)
importFrom(stats,approx)
importFrom(stats,setNames)
importFrom(terra,rast)
importFrom(tibble,tibble)
importFrom(utils,askYesNo)
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

# ceramic 0.7.0

* Removed raster handling support.

* Removed virtual tiles (see hypertidy/grout).

* Begin move to use GDAL for the read, separate tile downloading from raster input.

* Fixed `cc_kingston` location.
Expand Down
17 changes: 9 additions & 8 deletions R/cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ tile_zoom <- function(x) {
#' ceramic_cache()
#' }
ceramic_cache <- function(force = FALSE) {
cache <- file.path(rappdirs::user_cache_dir(), ".ceramic")
## normalize else gdal creates ./~/<cache>
cache <- file.path( normalizePath(rappdirs::user_cache_dir()), ".ceramic")
if (!fs::dir_exists(cache)) {
if (!force) {
val <- TRUE
Expand All @@ -190,16 +191,16 @@ ceramic_cache <- function(force = FALSE) {
}
fs::dir_create(cache)
}
gdalwmspath <- file.path(cache, "ceramic.gdalwmscache")
curr <- vapour::vapour_get_config("GDAL_DEFAULT_WMS_CACHE_PATH")
if (!nzchar(curr)) {
fs::dir_create(gdalwmspath)
vapour::vapour_set_config("GDAL_DEFAULT_WMS_CACHE_PATH", gdalwmspath)
}
cache
}

#' @name ceramic_cache
#' @keywords internal
#' @export
slippy_cache <- function(...) {
.Deprecated("ceramic_cache")
ceramic_cache(...)
}

url_to_cache <- function(x) {
base_filepath <- file.path(ceramic_cache(), gsub("^//", "", gsub("^https\\:", "", gsub("^https\\:", "", x))))
## chuck off any ? junk
Expand Down
9 changes: 6 additions & 3 deletions R/ceramic-package.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#' Obtain imagery tiles
#'
#' The ceramic package provides tools to download and load imagery and raster tiles from online servers.
#' The ceramic package provides tools to download raster tiles from online servers.
#'
#' Any process that can trigger downloads will first check the [ceramic_cache()] in case the tile already exists.
#'
#' It can also load raster data from online servers to obtain imagery, but we let GDAL manage that.
#'
#' If you want to deal with the tiles downloaded directly, see [ceramic_tiles()].
#'
#' The main functions are for downloading tiles and loading them as raster objects, and each accepts a spatial
#' The main functions are for downloading tiles and each accepts a spatial
#' object for the first argument, alternatively a raster extent, or location:
#'
#'
Expand All @@ -16,7 +19,7 @@
#' \code{\link{get_tiles_zoom}}\tab Download tiles base on extent and zoom level \cr
#' }
#'
#' Two helper functions will trigger the download of tiles and also collate the result into a raster object:
#' Two helper functions will load imagery into a raster object:
#'
#'
#' \tabular{ll}{
Expand Down
14 changes: 0 additions & 14 deletions R/format-utils.R

This file was deleted.

170 changes: 93 additions & 77 deletions R/raster.R
Original file line number Diff line number Diff line change
@@ -1,79 +1,95 @@
#' @importFrom terra rast
make_raster <- function(loc_data) {
files <- loc_data$files
tile_grid <- loc_data$tiles
user_extent <- loc_data$extent
files <- normalizePath(files)
if (find_format(files[1]) == "tif") {
out <- terra::merge(terra::sprc(lapply(files, terra::rast)))
#terra::crs(out) <- "EPSG:3857"
out <- raster::raster(out)
raster::projection(out) <- "+proj=merc +a=6378137 +b=6378137"
## short circuit, the old way is not working
return(out)
}
br <- lapply(files, raster_brick)
# is_jpeg <- function(x) {
# if (!file.exists(x[1])) return(FALSE)
# if (file.info(x[1])$size <= 11L) return(FALSE)
# rawb <- readBin(x[1], "raw", n = 11L)
# all(rawb[1:2] == as.raw(c(0xff, 0xd8))) && rawToChar(rawb[7:11]) == "JFIF"
# }
#
# is_png <- function(x) {
# #"89 50 4e 47 0d 0a 1a 0a"
# if (!file.exists(x[1])) return(FALSE)
# if (file.info(x[1])$size <= 8L) return(FALSE)
# rawb <- readBin(x[1], "raw", n = 8L)
# all(rawb == as.raw(c(0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a)))
# }

for (i in seq_along(br)) {
br[[i]] <- raster::setExtent(br[[i]],
mercator_tile_extent(tile_grid$tiles$x[i], tile_grid$tiles$y[i], zoom = tile_grid$zoom))
}

out <- fast_merge(br)
raster::crs(out) <- sp::CRS(.merc(), doCheckCRSArgs = FALSE)
if (!is.null(user_extent)) out <- raster::crop(out, user_extent , snap = "out")
out
}

raster_brick <- function(x) {
out <- NULL
if (find_format(x) == "tif") {
#out <- raster::brick(terra::rast(x))
#return(raster::setExtent(out, raster::extent(0, nrow(out), 0, ncol(out))))
return(terra::rast(out))
}
if (find_format(x)== "jpg") {
out <- jpeg::readJPEG(x)
}
if (find_format(x) == "png") {
out <- png::readPNG(x)
}

if (is.null(out)) stop(sprintf("cannot read %s", x))
out <- out*255
mode(out) <- "integer"
## in case it's greyscale ...
if (length(dim(out)) == 2L) out <- array(out, c(dim(out), 1L))
# raster::setExtent(raster::brick(out), raster::extent(0, nrow(out), 0, ncol(out)))
raster::brick(out)
}

raster_readAll <- function(x) {
if (!raster::hasValues(x)) x <- raster::readAll(x)
x
}

find_format <- function(x) {
fmt <- NULL
if (grepl("tif$", x)) return("tif")
## jpg or png
if (is_jpeg(x)) fmt <- "jpg"
if (is_png(x)) fmt <- "png"
if (is.null(fmt)) stop(sprintf("unknown format", x))
fmt
}


#' @importFrom raster cellsFromExtent
fast_merge <- function(x) {

## about 3 times faster than reduce(, merge
crs <- raster::projection(x[[1]])

out <- raster::raster(purrr::reduce(lapply(x, raster::extent), raster::union), crs = crs)
raster::res(out) <- raster::res(x[[1]])
# cells <- unlist(purrr::map(x, ~raster::cellsFromExtent(out, .x)))
cells <- unlist(lapply(x, function(.x) cellsFromExtent(out, .x)), use.names = FALSE)
vals <- do.call(rbind, lapply(x, function(.x) raster::values(raster_readAll(.x))))
raster::setValues(raster::brick(out, out, out), vals[order(cells), ])
}
#' #' @importFrom terra rast
#' make_raster <- function(loc_data) {
#' files <- loc_data$files
#' tile_grid <- loc_data$tiles
#' user_extent <- loc_data$extent
#' files <- normalizePath(files)
#' if (find_format(files[1]) == "tif") {
#' out <- terra::merge(terra::sprc(lapply(files, terra::rast)))
#' #terra::crs(out) <- "EPSG:3857"
#' out <- raster::raster(out)
#' raster::projection(out) <- "+proj=merc +a=6378137 +b=6378137"
#' ## short circuit, the old way is not working
#' return(out)
#' }
#' br <- lapply(files, raster_brick)
#'
#' for (i in seq_along(br)) {
#' br[[i]] <- raster::setExtent(br[[i]],
#' mercator_tile_extent(tile_grid$tiles$x[i], tile_grid$tiles$y[i], zoom = tile_grid$zoom))
#' }
#'
#' out <- fast_merge(br)
#' raster::crs(out) <- sp::CRS(.merc(), doCheckCRSArgs = FALSE)
#' if (!is.null(user_extent)) out <- raster::crop(out, user_extent , snap = "out")
#' out
#' }
#'
#' raster_brick <- function(x) {
#' out <- NULL
#' if (find_format(x) == "tif") {
#' #out <- raster::brick(terra::rast(x))
#' #return(raster::setExtent(out, raster::extent(0, nrow(out), 0, ncol(out))))
#' return(terra::rast(out))
#' }
#' if (find_format(x)== "jpg") {
#' out <- jpeg::readJPEG(x)
#' }
#' if (find_format(x) == "png") {
#' out <- png::readPNG(x)
#' }
#'
#' if (is.null(out)) stop(sprintf("cannot read %s", x))
#' out <- out*255
#' mode(out) <- "integer"
#' ## in case it's greyscale ...
#' if (length(dim(out)) == 2L) out <- array(out, c(dim(out), 1L))
#' # raster::setExtent(raster::brick(out), raster::extent(0, nrow(out), 0, ncol(out)))
#' raster::brick(out)
#' }
#'
#' raster_readAll <- function(x) {
#' if (!raster::hasValues(x)) x <- raster::readAll(x)
#' x
#' }
#'
#' find_format <- function(x) {
#' fmt <- NULL
#' if (grepl("tif$", x)) return("tif")
#' ## jpg or png
#' if (is_jpeg(x)) fmt <- "jpg"
#' if (is_png(x)) fmt <- "png"
#' if (is.null(fmt)) stop(sprintf("unknown format", x))
#' fmt
#' }
#'
#'
#' #' @importFrom raster cellsFromExtent
#' fast_merge <- function(x) {
#'
#' ## about 3 times faster than reduce(, merge
#' crs <- raster::projection(x[[1]])
#'
#' out <- raster::raster(purrr::reduce(lapply(x, raster::extent), raster::union), crs = crs)
#' raster::res(out) <- raster::res(x[[1]])
#' # cells <- unlist(purrr::map(x, ~raster::cellsFromExtent(out, .x)))
#' cells <- unlist(lapply(x, function(.x) cellsFromExtent(out, .x)), use.names = FALSE)
#' vals <- do.call(rbind, lapply(x, function(.x) raster::values(raster_readAll(.x))))
#' raster::setValues(raster::brick(out, out, out), vals[order(cells), ])
#' }
19 changes: 15 additions & 4 deletions R/spatial.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ spatial_bbox <- function(loc, buffer = NULL) {

## convert loc to mercator meters
loc <- slippymath::lonlat_to_merc(loc)

xp <- buffer[1] ## buffer is meant to be from a central point, so a radius
yp <- buffer[2]

Expand Down Expand Up @@ -94,6 +94,11 @@ spex_to_pt <- function(x) {
srcproj <- "EPSG:4267"
is_ll <- TRUE
}
if (srcproj == "WGS 84") {
srcproj <- "+proj=longlat"
is_ll <- TRUE
}

if (is.na(srcproj)) {
if (raster::couldBeLonLat(x, warnings = FALSE)) {
warning("loc CRS is not set, assuming longlat")
Expand All @@ -110,17 +115,23 @@ spex_to_pt <- function(x) {
}
#' @importFrom stats approx
project_spex <- function(x, crs) {

ex <- c(raster::xmin(x), raster::xmax(x), raster::ymin(x), raster::ymax(x))
idx <- c(1, 1, 2, 2, 1,
3, 4, 4, 3, 3)
xy <- matrix(ex[idx], ncol = 2L)
afun <- function(aa) stats::approx(seq_along(aa), aa, n = 180L)$y
srcproj <- raster::projection(x)
is_ll <- raster::couldBeLonLat(x, warnings = FALSE)
srcproj <- raster::projection(x)
is_ll <- raster::couldBeLonLat(x, warnings = FALSE)
if (srcproj == "NAD27") {
srcproj <- "EPSG:4267"
is_ll <- TRUE
}
}
if (srcproj == "WGS 84") {
srcproj <- "+proj=longlat"
is_ll <- TRUE
}

if (is.na(srcproj)) {
if (is_ll) {
warning("loc CRS is not set, assuming longlat")
Expand Down
28 changes: 14 additions & 14 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
.merc <- function() {
# if (PROJ::ok_proj6()) {
# return("EPSG:3857")
# }

# [1] "+proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs"
## testepsg
## "PROJ.4 rendering of [+proj=merc +a=6378137] =
##+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +R=6378137 +units=m +no_defs


"+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +R=6378137 +units=m +no_defs"
}
.ll <- function() "+proj=longlat +datum=WGS84 +no_defs"
.merc <- function() {
# if (PROJ::ok_proj6()) {
# return("EPSG:3857")
# }

# [1] "+proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs"
## testepsg
## "PROJ.4 rendering of [+proj=merc +a=6378137] =
##+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +R=6378137 +units=m +no_defs


"+proj=merc +R=6378137"
}
.ll <- function() "+proj=longlat +datum=WGS84"
23 changes: 0 additions & 23 deletions R/virtual_tiles.R

This file was deleted.

4 changes: 4 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.onLoad <- function(libname, pkgname) {
cache <- ceramic_cache()
invisible(NULL)
}

0 comments on commit f77b554

Please sign in to comment.