Title: | Raster Georeferencing, Grid Affine Transforms, Cell Abstraction |
---|---|
Description: | Tools for raster georeferencing, grid affine transforms, and general raster logic. These functions provide converters between raster specifications, world vector, geotransform, 'RasterIO' window, and 'RasterIO window' in 'sf' package list format. There are functions to offset a matrix by padding any of four corners (useful for vectorizing neighbourhood operations), and helper functions to harvesting user clicks on a graphics device to use for simple georeferencing of images. Methods used are available from <https://en.wikipedia.org/wiki/World_file> and <https://gdal.org/user/raster_data_model.html>. |
Authors: | Michael D. Sumner [aut, cre] |
Maintainer: | Michael D. Sumner <[email protected]> |
License: | GPL-3 |
Version: | 0.2.5 |
Built: | 2024-11-11 04:45:05 UTC |
Source: | https://github.com/hypertidy/affinity |
Functions 'bottom left', 'top left', 'bottom right', and 'top right' named by their initials, provide very low level relative positional structures for use in raster logic. These are used to traverse the divide left by area-based rasters which are inherently a discrete value across a finite element. If we want that element as part of a continuous surface we need to find local relative values for its corners. Used in quadmesh and anglr packages, and useful for calculating neighbourhood values.
bl(x) tl(x) br(x) tr(x) image0(x, ...) image1(x, ...) text0(x, ...)
bl(x) tl(x) br(x) tr(x) image0(x, ...) image1(x, ...) text0(x, ...)
x |
matrix |
... |
arguments passed to image() |
Some tiny functions 'image0', 'image1', 'text0' exist purely to illustrate the ideas in a vignette.
matrix, padded by one row and one column relative to input
(m <- matrix(1:12, 3)) tl(m) tr(m) bl(m) br(m) tl(br(m)) image0(tl(br(m))) text0(tl(br(m)))
(m <- matrix(1:12, 3)) tl(m) tr(m) bl(m) br(m) tl(br(m)) image0(tl(br(m))) text0(tl(br(m)))
User clicks are collected in a controlled way for use by domath()
.
affinething(x, rgb = FALSE)
affinething(x, rgb = FALSE)
x |
a raster |
rgb |
use RGB plot for a raster with 3 layers |
Obtain control points for the simple affine transform (offset and scale) on an ungeoreferenced image.
matrix of x,y coordinates in the space of the current raster extent
## Not run: library(raster) r <- raster("my_unreferenced_raster.png") xy <- affinething(r) ## click on two points that you know a location of my_x <- c(1000, 2000) my_y <- c(-1000, -500) prj <- "+proj=laea +lon=147 +lat_0=-42" ## use your own map projection, that correspond to my_x/my_y pt <- cbind(my_x, my_y) ## now convert those control points to an extent for your raster ex <- domath(pt, xy, r, prj) ## now we can fix up the data r <- raster::setExtent(r, ex) raster::projection(r) <- prj ## hooray! ## End(Not run)
## Not run: library(raster) r <- raster("my_unreferenced_raster.png") xy <- affinething(r) ## click on two points that you know a location of my_x <- c(1000, 2000) my_y <- c(-1000, -500) prj <- "+proj=laea +lon=147 +lat_0=-42" ## use your own map projection, that correspond to my_x/my_y pt <- cbind(my_x, my_y) ## now convert those control points to an extent for your raster ex <- domath(pt, xy, r, prj) ## now we can fix up the data r <- raster::setExtent(r, ex) raster::projection(r) <- prj ## hooray! ## End(Not run)
Set the projection of a spatial object.
assignproj(x, proj = "+proj=longlat +datum=WGS84")
assignproj(x, proj = "+proj=longlat +datum=WGS84")
x |
spatial object for use with |
proj |
PROJ.4 string |
a spatial object with the projection set
Given relative location and absolute locations, convert to an actual real world extent for a matrix of data.
domath(pts, xy, r = NULL, proj = NULL)
domath(pts, xy, r = NULL, proj = NULL)
pts |
known points of 'xy' |
xy |
'xy' obtain from |
r |
raster in use |
proj |
optional projection, if the pts are longlat and the raster is not |
Convert known geographic points with raw graphic control points and a reference raster to an extent for the raster in geography.
raster extent
## not a real example, but the extent we could provide volcano if the second set of points ## described the real world positions of the first set of points within the matrix domath(cbind(c(147, 148), c(-42, -43)), cbind(c(0.2, 0.3), c(0.1, 0.5)), raster::raster(volcano))
## not a real example, but the extent we could provide volcano if the second set of points ## described the real world positions of the first set of points within the matrix domath(cbind(c(147, 148), c(-42, -43)), cbind(c(0.2, 0.3), c(0.1, 0.5)), raster::raster(volcano))
Create the geotransform (see geo_transform0()
) from extent and dimension.
extent_dim_to_gt(x, dim)
extent_dim_to_gt(x, dim)
x |
extent parameters, c(xmin,xmax,ymin,ymax) |
dim |
dimensions x,y of grid (ncol,nrow) |
The dimension is always ncol, nrow.
6-element geo_transform0()
extent_dim_to_gt(c(0, 5, 0, 10), c(5, 10))
extent_dim_to_gt(c(0, 5, 0, 10), c(5, 10))
Basic function to create a geotransform as used by GDAL.
geo_transform0(px, ul, sh = c(0, 0))
geo_transform0(px, ul, sh = c(0, 0))
px |
pixel resolution (XY, Y-negative) |
ul |
grid offset, top-left corner |
sh |
affine shear (XY) |
vector of parameters xmin, xres, yskew, ymax, xskew, yres
geo_world0()
which uses the same parameters in a different order
geo_transform0(px = c(1, -1), ul = c(0, 0))
geo_transform0(px = c(1, -1), ul = c(0, 0))
Basic function to create a 'world file' as used by various non-geo image formats
Reformat to world vector.
geo_world0(px, ul, sh = c(0, 0)) geotransform_to_world(x)
geo_world0(px, ul, sh = c(0, 0)) geotransform_to_world(x)
px |
pixel resolution (XY, Y-negative) |
ul |
grid offset, top-left corner |
sh |
affine shear (XY) |
x |
geotransform parameters, as per |
Note that xmin/xmax are centre_of_cell (of top-left cell) unlike the geotransform which is top-left corner_of_cell. The parameters are otherwise the same, but in a different order.
vector of parameters xres, yskew, xskew, yres, xmin, ymax
world vector, as per geo_world0()
geo_world0(px = c(1, -1), ul = c(0, 0)) (gt <- geo_transform0(px = c(1, -1), ul = c(0, 0))) wf <- geotransform_to_world(gt) world_to_geotransform(wf)
geo_world0(px = c(1, -1), ul = c(0, 0)) (gt <- geo_transform0(px = c(1, -1), ul = c(0, 0))) wf <- geotransform_to_world(gt) world_to_geotransform(wf)
Create the extent (xlim, ylim) from the geotransform and dimensions of the grid.
gt_dim_to_extent(x, dim)
gt_dim_to_extent(x, dim)
x |
geotransform parameters, as per |
dim |
dimensions x,y of grid (ncol,nrow) |
The extent is c(xmin, xmax, ymin, ymax)
.
4-element extent c(xmin,xmax,ymin,ymax)
gt_dim_to_extent(geo_transform0(c(1, -1), c(0, 10)), c(5, 10))
gt_dim_to_extent(geo_transform0(c(1, -1), c(0, 10)), c(5, 10))
Extent is in the examples, stolen from rayshader.
monterey
monterey
An object of class matrix
(inherits from array
) with 270 rows and 270 columns.
A matrix 270x270 of topography. Used in affinething()
examples.
ex <- c(-122.366765, -121.366765, 36.179392, 37.179392) raster::setExtent(raster::t(raster::raster(monterey)), ex)
ex <- c(-122.366765, -121.366765, 36.179392, 37.179392) raster::setExtent(raster::t(raster::raster(monterey)), ex)
Basic function to create the window paramers as used by GDAL RasterIO.
raster_io0( src_offset, src_dim, out_dim = src_dim, resample = "NearestNeighbour" )
raster_io0( src_offset, src_dim, out_dim = src_dim, resample = "NearestNeighbour" )
src_offset |
index offset (0-based, top left) |
src_dim |
source dimension (XY) |
out_dim |
output dimension (XY, optional src_dim will be used if not set) |
resample |
resampling algorith for GDAL see details |
Resampling algorithm is one of 'NearestNeighbour' (default), 'Average', 'Bilinear', 'Cubic', 'CubicSpline', 'Gauss', 'Lanczos', 'Mode', but more may be available given the version of GDAL in use.
numeric vector of values specifying offset, source dimension, output dimension
raster_io0(c(0L, 0L), src_dim = c(24L, 10L))
raster_io0(c(0L, 0L), src_dim = c(24L, 10L))
Return the geotransform defining the raster's offset and resolution.
raster_to_gt(x)
raster_to_gt(x)
x |
raster object (the raster package, extends BasicRaster) |
The geotransform vector is six coefficients xmin, xres, yskew, ymax, xskew, yres, values relative to the top left corner of the top left pixel. "yres" the y-spacing is traditionally negative.
a geotransform vector
raster_to_gt(raster::raster(volcano))
raster_to_gt(raster::raster(volcano))
Return the RasterIO window vector defining the raster's offset and resolution and dimensions.
raster_to_rasterio(x) raster_to_sfio(x)
raster_to_rasterio(x) raster_to_sfio(x)
x |
a raster object (BasicRaster, from raster package) |
The RasterIO window is a six element vector of offset (x,y), dimension of source (nx0, ny0) and dimension of output (nx, ny).
The sf RasterIO is the RasterIO window in a list format used by the sf package, it contains the same
information, and is created by raster_to_sfio()
.
RasterIO window vector 'c(x0, y0, nx0, ny0, nx, y)' see Details
raster_to_rasterio(raster::raster(volcano))
raster_to_rasterio(raster::raster(volcano))
Return the world transform defining the raster's offset and resolution.
raster_to_world(x)
raster_to_world(x)
x |
raster object (the raster package, extends BasicRaster) |
The world vector is the values xres, yres, xmin, ymax relative to the centre of the top left pixel. "yres" the y-spacing is traditionally negative.
a geotransform vector
raster_to_world(raster::raster(volcano))
raster_to_world(raster::raster(volcano))
We create the list as used by the stars/sf GDAL IO function 'gdal_read(, RasterIO_parameters)'.
rasterio_to_sfio(x)
rasterio_to_sfio(x)
x |
rasterio params as from |
Note that the input is a 4 or 6 element vector, with offset 0-based and output dimensions optional (will use the source window). The resample argument uses the syntax identical to that used in GDAL itself.
list in sf RasterIO format
rio <- raster_io0(c(0L, 0L), src_dim = c(24L, 10L)) rasterio_to_sfio(rio)
rio <- raster_io0(c(0L, 0L), src_dim = c(24L, 10L)) rasterio_to_sfio(rio)
Basic function to create the window parameters as used by GDAL RasterIO, in format used by sf, in 'gdal_read(,RasterIO_parameters)'.
sfio_to_rasterio(x)
sfio_to_rasterio(x)
x |
a RasterIO parameter list |
a sf-RasterIO parameter list
sfio_to_rasterio(rasterio_to_sfio(raster_io0(c(0L, 0L), src_dim = c(24L, 10L))))
sfio_to_rasterio(rasterio_to_sfio(raster_io0(c(0L, 0L), src_dim = c(24L, 10L))))
Convert world vector (centre offset) and x,y spacing to geotransform format.
world_to_geotransform(x)
world_to_geotransform(x)
x |
worldfile parameters, as per |
geotransform vector, see geo_transform0()
(wf <- geo_world0(px = c(1, -1), ul = c(0, 0))) gt <- world_to_geotransform(wf) geotransform_to_world(gt)
(wf <- geo_world0(px = c(1, -1), ul = c(0, 0))) gt <- world_to_geotransform(wf) geotransform_to_world(gt)