Package 'starsTileServer'

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

Help Index


Converts a matrix of values to to a png image that it the tile to be returned

Description

Converts a matrix of values to to a png image that it the tile to be returned

Usage

matrixToPng(targetMatrix, colFun, ...)

Arguments

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 leaflet::colorNumeric().

...

Additional arguments to the colFun argument.

Value

A raw vector containing the png image.

Examples

matrixToPng(matrix(1:9, ncol = 3), leaflet::colorNumeric("Reds", domain = c(1, 10)))

A R6 class that extends plumber to function as a tile server

Description

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.

Super classes

plumber::Hookable -> plumber::Plumber -> starsTileServer

Methods

Public methods

Inherited methods

Method new()

This method is used to initialize a new tile server

Usage
starsTileServer$new(grid, colorFun = NULL, tileSize = 256, ...)
Arguments
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.

Details

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.

Returns

An starsTileServer object.


Method add_tile_endpoint()

Add three endpoints to the tile server, to return both the tiles and the color scale used.

Usage
starsTileServer$add_tile_endpoint(prefix, handlerFun, colorFun, params)
Arguments
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


Method get_grid()

return the grid used to initialize the function

Usage
starsTileServer$get_grid()

Method get_attributes()

return the attributes of the stars grid

Usage
starsTileServer$get_attributes()

Method get_dimensions()

return the names of the dimensions of the grid

Usage
starsTileServer$get_dimensions()

Method get_dimension_values_chr()

return the values of a dimension as a character

Usage
starsTileServer$get_dimension_values_chr(x)
Arguments
x

the name of the dimension


Method get_non_spatial_dimensions()

return all non spatial dimensions

Usage
starsTileServer$get_non_spatial_dimensions()

Method clone()

The objects of this class are cloneable with this method.

Usage
starsTileServer$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

Examples

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

Description

Function to calculate a stars grid based on the x,y,z attributes that represents the coordinates of the map tile

Usage

targetGrid(x, y, z, tileSize = 256L)

Arguments

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

Details

This function is mostly useful for testing purpose.

Value

A stars grid with all values being zero with the dimensions matching those required for the tile specified

Examples

targetGrid(4, 2, 4)
targetGrid(4, 2, 4, 128)