Title: | A Dynamic Tile Server for R |
---|---|
Description: | Makes it possible to serve map tiles for web maps (e.g. leaflet) based on a function or a stars object without having to render them in advance. This enables parallelization of the rendering, separating the data source and visualization location and to provide web services. |
Authors: | Bart Kranstauber [aut, cre] |
Maintainer: | Bart Kranstauber <[email protected]> |
License: | GPL-3 |
Version: | 0.1.1 |
Built: | 2024-11-07 05:14:03 UTC |
Source: | https://gitlab.com/bartk/starstileserver |
Converts a matrix of values to to a png image that it the tile to be returned
matrixToPng(targetMatrix, colFun, ...)
matrixToPng(targetMatrix, colFun, ...)
targetMatrix |
A matrix with values to convert to an image. |
colFun |
A color function, cat converts numeric values to hex colors. This can for example be generated with |
... |
Additional arguments to the colFun argument. |
A raw vector containing the png image.
matrixToPng(matrix(1:9, ncol = 3), leaflet::colorNumeric("Reds", domain = c(1, 10)))
matrixToPng(matrix(1:9, ncol = 3), leaflet::colorNumeric("Reds", domain = c(1, 10)))
Creates a tile server based on the R6 class plumber::Plumber
.
In can be created both with a stars
grid as well as a function or list of functions.
The main methods are run
and new
.
plumber::Hookable
-> plumber::Plumber
-> starsTileServer
plumber::Hookable$registerHooks()
plumber::Plumber$addAssets()
plumber::Plumber$addEndpoint()
plumber::Plumber$addFilter()
plumber::Plumber$addGlobalProcessor()
plumber::Plumber$call()
plumber::Plumber$filter()
plumber::Plumber$getApiSpec()
plumber::Plumber$getDebug()
plumber::Plumber$handle()
plumber::Plumber$mount()
plumber::Plumber$onHeaders()
plumber::Plumber$onWSOpen()
plumber::Plumber$openAPIFile()
plumber::Plumber$print()
plumber::Plumber$registerHook()
plumber::Plumber$removeHandle()
plumber::Plumber$route()
plumber::Plumber$run()
plumber::Plumber$serve()
plumber::Plumber$set404Handler()
plumber::Plumber$setApiSpec()
plumber::Plumber$setDebug()
plumber::Plumber$setDocs()
plumber::Plumber$setDocsCallback()
plumber::Plumber$setErrorHandler()
plumber::Plumber$setParsers()
plumber::Plumber$setSerializer()
plumber::Plumber$swaggerFile()
plumber::Plumber$unmount()
new()
This method is used to initialize a new tile server
starsTileServer$new(grid, colorFun = NULL, tileSize = 256, ...)
grid
Either a stars grid, a path pointing towards a gridded file, a function or a list of named functions
colorFun
a color function to use for coloring the map tiles, the function needs to be the same format as returned from leaflet::colorNumeric()
or the other leaflet color functions. It is important to specify a color function as it is important to keep the range of the color scale similar between tiles, therefore the minimum and maximum needs to be fixed. It can also be a list of color functions.
tileSize
The size of the tile (generally 256 pixels, and not tested with other sizes)
...
Arguments passed on to the plumber::Plumber, most important is the port
number.
If grid
is a function it should take a stars grid as the first argument and return the grid with the same topology with values attached.
Any other arguments to the function will be part of the API and will be passed to the function as characters.
An starsTileServer
object.
add_tile_endpoint()
Add three endpoints to the tile server, to return both the tiles and the color scale used.
starsTileServer$add_tile_endpoint(prefix, handlerFun, colorFun, params)
prefix
the name to be used by the server for this tile server
handlerFun
The function that handles the api request and returns the grid
colorFun
The color function to use for example leaflet::colorNumeric()
params
parameters passed on to the new
method of plumber::PlumberEndpoint
get_grid()
return the grid used to initialize the function
starsTileServer$get_grid()
get_attributes()
return the attributes of the stars grid
starsTileServer$get_attributes()
get_dimensions()
return the names of the dimensions of the grid
starsTileServer$get_dimensions()
get_dimension_values_chr()
return the values of a dimension as a character
starsTileServer$get_dimension_values_chr(x)
x
the name of the dimension
get_non_spatial_dimensions()
return all non spatial dimensions
starsTileServer$get_non_spatial_dimensions()
clone()
The objects of this class are cloneable with this method.
starsTileServer$clone(deep = FALSE)
deep
Whether to make a deep clone.
m <- matrix(1:20, nrow = 5, ncol = 4) dim(m) <- c(x = 5, y = 4) # named dim (s <- stars::st_as_stars(m)) sf::st_crs(s) <- 4326 starsTileServer$new(s) # Working directly from a file grid <- system.file("tif/L7_ETMs.tif", package = "stars") starsTileServer$new(grid) ## Not run: starsTileServer$new(s)$run() ## End(Not run)
m <- matrix(1:20, nrow = 5, ncol = 4) dim(m) <- c(x = 5, y = 4) # named dim (s <- stars::st_as_stars(m)) sf::st_crs(s) <- 4326 starsTileServer$new(s) # Working directly from a file grid <- system.file("tif/L7_ETMs.tif", package = "stars") starsTileServer$new(grid) ## Not run: starsTileServer$new(s)$run() ## End(Not run)
Function to calculate a stars grid based on the x,y,z attributes that represents the coordinates of the map tile
targetGrid(x, y, z, tileSize = 256L)
targetGrid(x, y, z, tileSize = 256L)
x |
The location of the tile in the x dimension |
y |
The location of the tile in the y dimension |
z |
The zoom level |
tileSize |
The size of the tile generally 256 pixels |
This function is mostly useful for testing purpose.
A stars grid with all values being zero with the dimensions matching those required for the tile specified
targetGrid(4, 2, 4) targetGrid(4, 2, 4, 128)
targetGrid(4, 2, 4) targetGrid(4, 2, 4, 128)