-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from rOpenSpain/hlpprov
Add esp_make_provider
- Loading branch information
Showing
11 changed files
with
279 additions
and
138 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
#' Create a custom tile provider | ||
#' | ||
#' @description | ||
#' Helper function for [esp_getTiles()] that helps to create a custom provider. | ||
#' | ||
#' @family imagery utilities | ||
#' @seealso [esp_getTiles()]. | ||
#' | ||
#' For a list of potential providers from Spain check | ||
#' [IDEE Directory](https://www.idee.es/segun-tipo-de-servicio). | ||
#' | ||
#' | ||
#' @return | ||
#' A named list with two elements `id` and `q`. | ||
#' | ||
#' @export | ||
#' | ||
#' @param id An identifier for the user. Would be used also for identifying | ||
#' cached tiles. | ||
#' | ||
#' @param q The base url of the service | ||
#' | ||
#' @param service The type of tile service, either `"WMS"` or `"WMTS"`. | ||
#' | ||
#' @param layers The name of the layer to retrieve | ||
#' | ||
#' @param ... Additional parameters to the query, like `version`, `format`, | ||
#' `crs/srs`, `style`, ... depending on the capabilities of the service. | ||
#' | ||
#' @details | ||
#' This function is meant to work with services provided as of the | ||
#' [OGC Standard](http://opengeospatial.github.io/e-learning/wms/text/operations.html#getmap). | ||
#' | ||
#' Note that: | ||
#' - \pkg{mapSpain} would not provide advice on the parameter `q` to be | ||
#' provided. | ||
#' - Currently, on **WMTS** requests only services with | ||
#' `tilematrixset=GoogleMapsCompatible` are supported. | ||
#' | ||
#' @examples | ||
#' \dontrun{ | ||
#' # This script downloads tiles to your local machine | ||
#' # Run only if you are online | ||
#' | ||
#' custom_wms <- esp_make_provider( | ||
#' id = "an_id_for_caching", | ||
#' q = "https://idecyl.jcyl.es/geoserver/ge/wms?", | ||
#' service = "WMS", | ||
#' layers = "geolog_cyl_litologia" | ||
#' ) | ||
#' | ||
#' x <- esp_get_ccaa("Castilla y León", epsg = 3857) | ||
#' | ||
#' mytile <- esp_getTiles(x, type = custom_wms) | ||
#' | ||
#' tidyterra::autoplot(mytile) + | ||
#' ggplot2::geom_sf(data = x, fill = NA) | ||
#' } | ||
esp_make_provider <- function(id, q, service, layers, ...) { | ||
dots <- list(...) | ||
names(dots) <- tolower(names(dots)) | ||
|
||
# Ignore tilematrixset | ||
dots <- dots[names(dots) != "tilematrixset"] | ||
|
||
# Minimal for WMS | ||
|
||
if (toupper(service) == "WMS") { | ||
def_params <- list( | ||
q = q, | ||
request = "GetMap", | ||
service = "WMS", | ||
version = "1.0.0", | ||
format = "image/png", | ||
layers = layers, | ||
styles = "" | ||
) | ||
} else { | ||
def_params <- list( | ||
q = q, | ||
request = "GetTile", | ||
service = "WMTS", | ||
version = "1.0.0", | ||
format = "image/png", | ||
layer = layers, | ||
style = "", | ||
tilematrixset = "GoogleMapsCompatible" | ||
) | ||
} | ||
|
||
# Modify | ||
end <- modifyList(def_params, dots) | ||
|
||
# Here adjust crs values | ||
|
||
if (end$service == "WMS") { | ||
if (end$version < "1.3.0") { | ||
names(end) <- gsub("crs", "srs", names(end)) | ||
end$srs <- ifelse(is.null(end$srs), "EPSG:3857", end$srs) | ||
} else { | ||
names(end) <- gsub("srs", "crs", names(end)) | ||
end$crs <- ifelse(is.null(end$crs), "EPSG:3857", end$crs) | ||
} | ||
} | ||
|
||
|
||
# Create final list | ||
final <- list(id = id) | ||
|
||
# Create query | ||
q <- gsub("\\?\\?$", "?", paste0(end$q, "?")) | ||
rest <- end[names(end) != "q"] | ||
q_end <- paste0(q, paste0(names(rest), "=", rest, collapse = "&")) | ||
|
||
final$q <- q_end | ||
|
||
|
||
return(final) | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,7 @@ HOTFIX | |
Hernangomez | ||
Hexbin | ||
Hypsometry | ||
IDEE | ||
IDERioja | ||
IGN | ||
INE | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.