From 825554d5a635f2d870d467b53934a49fea7e83af Mon Sep 17 00:00:00 2001 From: rhijmans Date: Sun, 15 Dec 2024 16:46:10 -0800 Subject: [PATCH] linscale --- NAMESPACE | 2 +- NEWS.md | 3 ++- R/Agenerics.R | 1 + R/generics.R | 7 ++++++ man/scale.Rd | 4 ++-- man/scale_linear.Rd | 37 ++++++++++++++++++++++++++++++ src/RcppModule.cpp | 1 + src/raster_methods.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++ src/spatRaster.h | 2 ++ 9 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 man/scale_linear.Rd diff --git a/NAMESPACE b/NAMESPACE index 0bd81df4f..e1950b04e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,7 +1,7 @@ useDynLib(terra, .registration=TRUE) import(methods, Rcpp) exportClasses(SpatExtent, SpatRaster, SpatRasterDataset, SpatRasterCollection, SpatVector, SpatVectorProxy, SpatVectorCollection) -exportMethods("[", "[[", "!", "%in%", activeCat, "activeCat<-", "add<-", addCats, adjacent, all.equal, aggregate, allNA, align, animate, anyNA, app, Arith, approximate, as.bool, as.int, as.contour, as.lines, as.points, as.polygons, as.raster, as.array, as.data.frame, as.factor, as.list, as.logical, as.matrix, as.numeric, atan2, atan_2, autocor, barplot, blocks, boundaries, boxplot, buffer, cartogram, categories, cats, catalyze, clamp, clamp_ts, classify, clearance, cellSize, cells, cellFromXY, cellFromRowCol, cellFromRowColCombine, centroids, click, bestMatch, colFromX, colFromCell, colorize, coltab, "coltab<-", combineGeoms, compare, concats, Compare, compareGeom, contour, convHull, countNA, costDist, crds, cover, crop, crosstab, crs, "crs<-", datatype, deepcopy, delaunay, densify, density, depth, "depth<-", describe, diff, disagg, direction, distance, dots, draw, droplevels, edges, elongate, emptyGeoms, erase, extend, ext, "ext<-", extract, extractRange, expanse, fillHoles, fillTime, flip, focal, focal3D, focalPairs, focalReg, focalCpp, focalValues, forceCCW, freq, gaps, geom, geomtype, getTileExtents, global, gridDist, has.colors, has.RGB, has.time, hasMinMax, hasValues, hist, head, identical, ifel, impose, init, image, inext, interpIDW, interpNear, inMemory, inset, interpolate, intersect, is.bool, is.int, is.lonlat, is.rotated, isTRUE, isFALSE, is.empty, is.factor, is.lines, is.points, is.polygons, is.related, is.valid, k_means, lapp, layerCor, levels, "levels<-", linearUnits, lines, Logic, varnames, "varnames<-", logic, longnames, "longnames<-", makeValid, mask, match, math, Math, Math2, mean, median, meta, merge, mergeLines, mergeTime, minCircle, minmax, minRect, modal, mosaic, na.omit, not.na, NAflag, "NAflag<-", nearby, nearest, ncell, ncol, "ncol<-", nlyr, "nlyr<-", noNA, normalize.longitude, nrow, "nrow<-", nsrc, origin, "origin<-", pairs, panel, patches, perim, persp, plot, plotRGB, plet, prcomp, princomp, RGB, "RGB<-", polys, points, predict, project, quantile, query, rangeFill, rapp, rast, rasterize, rasterizeGeom, rasterizeWin, readStart, readStop, readValues, rectify, regress, relate, removeDupNodes, res, "res<-", resample, rescale, rev, rcl, roll, rotate, rowFromY, rowColCombine, rowColFromCell, rowFromCell, sapp, scale, scoff, "scoff<-", sds, sort, sprc, sel, selectRange, setMinMax, setValues, segregate, selectHighest, set.cats, set.crs, set.ext, set.names, set.RGB, set.values, size, sharedPaths, shift, sieve, simplifyGeom, snap, sources, spatSample, split, spin, stdev, stretch, subset, subst, summary, Summary, surfArea, svc, symdif, t, metags, "metags<-", tail, tapp, terrain, tighten, makeNodes, makeTiles, time, timeInfo, "time<-", text, toMemory, trans, trim, units, union, "units<-", unique, unwrap, update, vect, values, "values<-", viewshed, voronoi, vrt, weighted.mean, where.min, where.max, which.lyr, which.min, which.max, which.lyr, width, window, "window<-", writeCDF, writeRaster, wrap, wrapCache, writeStart, writeStop, writeVector, writeValues, xmin, xmax, "xmin<-", "xmax<-", xres, xFromCol, xyFromCell, xFromCell, ymin, ymax, "ymin<-", "ymax<-", yres, yFromCell, yFromRow, zonal, zoom, cbind2, readRDS, saveRDS, unserialize, serialize, xapp, area, gridDistance, colSums, rowSums, colMeans, rowMeans) +exportMethods("[", "[[", "!", "%in%", activeCat, "activeCat<-", "add<-", addCats, adjacent, all.equal, aggregate, allNA, align, animate, anyNA, app, Arith, approximate, as.bool, as.int, as.contour, as.lines, as.points, as.polygons, as.raster, as.array, as.data.frame, as.factor, as.list, as.logical, as.matrix, as.numeric, atan2, atan_2, autocor, barplot, blocks, boundaries, boxplot, buffer, cartogram, categories, cats, catalyze, clamp, clamp_ts, classify, clearance, cellSize, cells, cellFromXY, cellFromRowCol, cellFromRowColCombine, centroids, click, bestMatch, colFromX, colFromCell, colorize, coltab, "coltab<-", combineGeoms, compare, concats, Compare, compareGeom, contour, convHull, countNA, costDist, crds, cover, crop, crosstab, crs, "crs<-", datatype, deepcopy, delaunay, densify, density, depth, "depth<-", describe, diff, disagg, direction, distance, dots, draw, droplevels, edges, elongate, emptyGeoms, erase, extend, ext, "ext<-", extract, extractRange, expanse, fillHoles, fillTime, flip, focal, focal3D, focalPairs, focalReg, focalCpp, focalValues, forceCCW, freq, gaps, geom, geomtype, getTileExtents, global, gridDist, has.colors, has.RGB, has.time, hasMinMax, hasValues, hist, head, identical, ifel, impose, init, image, inext, interpIDW, interpNear, inMemory, inset, interpolate, intersect, is.bool, is.int, is.lonlat, is.rotated, isTRUE, isFALSE, is.empty, is.factor, is.lines, is.points, is.polygons, is.related, is.valid, k_means, lapp, layerCor, levels, "levels<-", linearUnits, lines, Logic, varnames, "varnames<-", logic, longnames, "longnames<-", makeValid, mask, match, math, Math, Math2, mean, median, meta, merge, mergeLines, mergeTime, minCircle, minmax, minRect, modal, mosaic, na.omit, not.na, NAflag, "NAflag<-", nearby, nearest, ncell, ncol, "ncol<-", nlyr, "nlyr<-", noNA, normalize.longitude, nrow, "nrow<-", nsrc, origin, "origin<-", pairs, panel, patches, perim, persp, plot, plotRGB, plet, prcomp, princomp, RGB, "RGB<-", polys, points, predict, project, quantile, query, rangeFill, rapp, rast, rasterize, rasterizeGeom, rasterizeWin, readStart, readStop, readValues, rectify, regress, relate, removeDupNodes, res, "res<-", resample, rescale, rev, rcl, roll, rotate, rowFromY, rowColCombine, rowColFromCell, rowFromCell, sapp, scale, scale_linear, scoff, "scoff<-", sds, sort, sprc, sel, selectRange, setMinMax, setValues, segregate, selectHighest, set.cats, set.crs, set.ext, set.names, set.RGB, set.values, size, sharedPaths, shift, sieve, simplifyGeom, snap, sources, spatSample, split, spin, stdev, stretch, subset, subst, summary, Summary, surfArea, svc, symdif, t, metags, "metags<-", tail, tapp, terrain, tighten, makeNodes, makeTiles, time, timeInfo, "time<-", text, toMemory, trans, trim, units, union, "units<-", unique, unwrap, update, vect, values, "values<-", viewshed, voronoi, vrt, weighted.mean, where.min, where.max, which.lyr, which.min, which.max, which.lyr, width, window, "window<-", writeCDF, writeRaster, wrap, wrapCache, writeStart, writeStop, writeVector, writeValues, xmin, xmax, "xmin<-", "xmax<-", xres, xFromCol, xyFromCell, xFromCell, ymin, ymax, "ymin<-", "ymax<-", yres, yFromCell, yFromRow, zonal, zoom, cbind2, readRDS, saveRDS, unserialize, serialize, xapp, area, gridDistance, colSums, rowSums, colMeans, rowMeans) exportMethods(watershed, pitfinder, NIDP, flowAccumulation) diff --git a/NEWS.md b/NEWS.md index 6196f450f..50ec5829b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -13,7 +13,8 @@ ## new -- `$` can now be used to set the active category for categorical rasters +- `$` can now be used to get a categorical SpatRaster with a different active category +- `scale_linear` method for linear scaling between e.g., 0 and 1 # version 1.8-5 diff --git a/R/Agenerics.R b/R/Agenerics.R index 6ff4bb322..bf7c61678 100644 --- a/R/Agenerics.R +++ b/R/Agenerics.R @@ -334,6 +334,7 @@ if (!isGeneric("rescale")) {setGeneric("rescale", function(x, ...) standardGener #if (!isGeneric("select")) {setGeneric("select", function(x, ...) standardGeneric("select"))} if (!isGeneric("setMinMax")) {setGeneric("setMinMax", function(x, ...) standardGeneric("setMinMax"))} if (!isGeneric("scale")) {setGeneric("scale", function(x, center=TRUE, scale=TRUE) standardGeneric("scale"))} +if (!isGeneric("scale_linear")) { setGeneric("scale_linear", function(x, ...) standardGeneric("scale_linear"))} if (!isGeneric("shift")) {setGeneric("shift", function(x, ...) standardGeneric("shift"))} if (!isGeneric("stdev")) { setGeneric("stdev", function(x, ...) standardGeneric("stdev")) } if (!isGeneric("subset")) {setGeneric("subset", function(x, ...) standardGeneric("subset")) } diff --git a/R/generics.R b/R/generics.R index 4dbb5ba68..ea174d996 100644 --- a/R/generics.R +++ b/R/generics.R @@ -968,6 +968,13 @@ setMethod("rescale", signature(x="SpatVector"), } ) +setMethod("scale_linear", signature(x="SpatRaster"), + function(x, min=0, max=1, filename="", ...) { + opt <- spatOptions(filename=filename, ...) + x@pntr <- x@pntr$scale_linear(min, max, opt) + messages(x, "scale_linear") + } +) setMethod("scale", signature(x="SpatRaster"), function(x, center=TRUE, scale=TRUE) { diff --git a/man/scale.Rd b/man/scale.Rd index 0e611bd79..c13e740b5 100644 --- a/man/scale.Rd +++ b/man/scale.Rd @@ -7,7 +7,7 @@ \title{Scale values} \description{ -Center and/or scale raster data. For details see \code{\link{scale}} +Center and/or scale raster data. For details see \code{\link[base]{scale}} } \usage{ @@ -26,7 +26,7 @@ SpatRaster } -\seealso{ \code{\link[base]{scale}} } +\seealso{ \code{\link{scale_linear}} } \examples{ r <- rast(system.file("ex/logo.tif", package="terra")) diff --git a/man/scale_linear.Rd b/man/scale_linear.Rd new file mode 100644 index 000000000..7a2a451d3 --- /dev/null +++ b/man/scale_linear.Rd @@ -0,0 +1,37 @@ +\name{scale_linear} + +\alias{scale_linear} +\alias{scale_linear,SpatRaster-method} + + +\title{Scale values linearly} + +\description{ +Linear scaling of raster cell values. +} + +\usage{ +\S4method{scale_linear}{SpatRaster}(x, min=0, max=1) +} + + +\arguments{ + \item{x}{SpatRaster} + \item{min}{minimum value to scale to} + \item{max}{maximum value to scale to} +} + +\value{ +SpatRaster +} + + +\seealso{ \code{\link{scale}} } + +\examples{ +r <- rast(system.file("ex/logo.tif", package="terra")) +s1 <- scale_linear(r) +s2 <- scale_linear(r, 1, 10) +} + +\keyword{ spatial } diff --git a/src/RcppModule.cpp b/src/RcppModule.cpp index 03cad4629..cd70b9086 100644 --- a/src/RcppModule.cpp +++ b/src/RcppModule.cpp @@ -955,6 +955,7 @@ RCPP_MODULE(spat){ .method("sampleRandomRaster", &SpatRaster::sampleRandomRaster) .method("sampleRandomValues", &SpatRaster::sampleRandomValues) .method("scale", &SpatRaster::scale) + .method("scale_linear", &SpatRaster::scale_linear) .method("shift", &SpatRaster::shift) .method("similarity", &SpatRaster::similarity) .method("terrain", &SpatRaster::terrain) diff --git a/src/raster_methods.cpp b/src/raster_methods.cpp index c43f70ec5..502893481 100644 --- a/src/raster_methods.cpp +++ b/src/raster_methods.cpp @@ -4732,6 +4732,57 @@ SpatRaster SpatRaster::scale(std::vector center, bool docenter, std::vec } +SpatRaster SpatRaster::scale_linear(double smin, double smax, SpatOptions &opt) { + SpatRaster out = geometry(); + if (!hasValues()) return out; + if (smin >= smax) { + out.setError("min scaling value must be smaller than the max scaling value"); + return out; + } + + SpatOptions opts(opt); + setRange(opts, false); + std::vector rmin = SpatRaster::range_min(); + std::vector rmax = SpatRaster::range_max(); + std::vector rdif; + rdif.reserve(rmin.size()); + double dmnmx = smax - smin; + + for (size_t i=0; i v; + readBlock(v, out.bs, i); + size_t lyroff = 0; + size_t cellperlyr = out.bs.nrows[i] * ncol(); + for (size_t lyr = 0; lyr < nl; lyr++) { + for (size_t j=lyroff; j<(lyroff+cellperlyr); j++) { + v[j] = ((v[j] - rmin[lyr]) / rdif[i]) + smin; + } + lyroff = lyroff + cellperlyr; + } + if (!out.writeBlock(v, i)) return out; + } + readStop(); + out.writeStop(); + return out; +} + + + /* bool can_use_replace(const std::vector &from, const std::vector &to) { // test if any "to" later occurs in "from" diff --git a/src/spatRaster.h b/src/spatRaster.h index 88b94cbe3..bb5a35216 100644 --- a/src/spatRaster.h +++ b/src/spatRaster.h @@ -801,6 +801,8 @@ class SpatRaster { SpatRaster sort(bool decreasing, bool order, SpatOptions &opt); SpatRaster scale(std::vector center, bool docenter, std::vector scale, bool doscale, SpatOptions &opt); + SpatRaster scale_linear(double smin, double smax, SpatOptions &opt); + SpatRaster similarity(std::vector x, SpatOptions &opt); SpatRaster terrain(std::vector v, unsigned neighbors, bool degrees, unsigned seed, SpatOptions &opt);