Skip to content

Commit

Permalink
for #1304
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed Oct 10, 2023
1 parent e414fbe commit 98ec9b9
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 24 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -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, area, 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, 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, elongate, emptyGeoms, erase, extend, ext, "ext<-", extract, expanse, fillHoles, fillTime, flip, focal, focal3D, focalCor, focalPairs, focalReg, focalCpp, focalValues, forceCCW, freq, gaps, geom, geomtype, global, gridDist, gridDistance, has.colors, has.RGB, hasMinMax, hasValues, hist, head, 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, lapp, layerCor, levels, linearUnits, lines, Logic, varnames, "varnames<-", logic, longnames, "longnames<-", makeValid, mask, match, math, Math, Math2, mean, median, meta, merge, mergeLines, mergeTime, 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, 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, svc, symdif, t, "set.tags", tags, tail, tapp, terrain, tighten, makeNodes, makeTiles, time, timeInfo, "time<-", text, 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)
exportMethods("[", "[[", "!", "%in%", activeCat, "activeCat<-", "add<-", addCats, adjacent, all.equal, aggregate, allNA, align, animate, anyNA, app, area, 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, 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, elongate, emptyGeoms, erase, extend, ext, "ext<-", extract, expanse, fillHoles, fillTime, flip, focal, focal3D, focalCor, focalPairs, focalReg, focalCpp, focalValues, forceCCW, freq, gaps, geom, geomtype, global, gridDist, gridDistance, has.colors, has.RGB, hasMinMax, hasValues, hist, head, 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, lapp, layerCor, levels, linearUnits, lines, Logic, varnames, "varnames<-", logic, longnames, "longnames<-", makeValid, mask, match, math, Math, Math2, mean, median, meta, merge, mergeLines, mergeTime, 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, 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, svc, symdif, t, tags, "tags<-", tail, tapp, terrain, tighten, makeNodes, makeTiles, time, timeInfo, "time<-", text, 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)

S3method(cbind, SpatVector)
S3method(rbind, SpatVector)
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# version 1.7-50


## bug fixes

- `mosaic` ignored the filename argument if the SpatRasterCollection only had a single SpatRaster [#1267](https://github.com/rspatial/terra/issues/1267) by Michael Mahoney
Expand All @@ -22,6 +23,10 @@ Hernangómez
- `extract(fun="table")` now returns an easier to use data.frame [#1294] (https://github.com/rspatial/terra/issues/1294) by Fernando Aramburu.


## new
- `tags<-` and `tags` to set arbitrary metadata [#1304](https://github.com/rspatial/terra/issues/1304) by Francesco Chianucci


# version 1.7-46

Released 2023-09-06
Expand Down
2 changes: 1 addition & 1 deletion R/Agenerics.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#if (!isGeneric("#")) { setGeneric("#", function(x, ...) standardGeneric("#")) }

if (!isGeneric("tags")) {setGeneric("tags", function(x, ...) standardGeneric("tags"))}
if (!isGeneric("set.tags")) {setGeneric("set.tags", function(x, ...) standardGeneric("set.tags"))}
if (!isGeneric("tags<-")) {setGeneric("tags<-", function(x, ..., value) standardGeneric("tags<-"))}

if (!isGeneric("forceCCW")) {setGeneric("forceCCW", function(x, ...) standardGeneric("forceCCW"))}
if (!isGeneric("addCats")) {setGeneric("addCats", function(x, ...) standardGeneric("addCats"))}
Expand Down
32 changes: 20 additions & 12 deletions R/generics.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,28 @@ setMethod("tags", signature(x="SpatRaster"),
}
)

setMethod("set.tags", signature(x="SpatRaster"),
function(x, name, value) {
if (is.null(name)) {
v <- tags(x)
v[,2] <- ""
} else {
if (is.null(value)) value <- ""
v <- cbind(name, value)
v[is.na(v)] <- ""
setMethod("tags<-", signature(x="SpatRaster"),
function(x, value) {
if (is.null(value)) {
value <- tags(x)
value[,2] <- ""
} else if (NCOL(value) == 1) {
value <- strsplit(value, "=")
i <- sapply(value, length) == 1
if (length(i) > 0) {
j <- which(i)
for (i in j) value[[i]] <- c(value[[i]], "")
}
i <- sapply(value, length) == 2
value <- do.call(rbind, value[i])
} else if (NCOL(value) != 2) {
error("tags<-", "expecting a vector of 'name=value' or a two column matrix")
}
value[is.na(value)] <- ""
x <- deepcopy(x)
if (nrow(v) > 0) {
out <- sapply(1:nrow(v), function(i) {
x@cpp$addTag(v[i,1], v[i,2])
if (NROW(value) > 0) {
sapply(1:nrow(value), function(i) {
x@cpp$addTag(value[i,1], value[i,2])
})
}
x
Expand Down
22 changes: 12 additions & 10 deletions man/tags.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

\alias{tags}
\alias{tags,SpatRaster-method}
\alias{set.tags}
\alias{set.tags,SpatRaster-method}
\alias{tags<-}
\alias{tags<-,SpatRaster-method}


\title{Set or get tags}
Expand All @@ -13,7 +13,7 @@ You can set arbitrary metadata to a SpatRaster using "name=value" tags.
}

\usage{
\S4method{set.tags}{SpatRaster}(x, name, value)
\S4method{tags}{SpatRaster}(x)<-value

\S4method{tags}{SpatRaster}(x, name=NULL)
}
Expand All @@ -23,29 +23,31 @@ You can set arbitrary metadata to a SpatRaster using "name=value" tags.

\item{name}{character}

\item{value}{character}
\item{value}{character of "name=value" or two-column matrix}
}

\value{
SpatRaster (\code{set.tags}), matrix (\code{tags}) or character (\code{tags} and \code{name} is not NULL)
SpatRaster (\code{tags<-}), matrix (\code{tags}) or character (\code{tags} and \code{name} is not NULL)
}

\examples{
r <- rast()
r <- set.tags(r, c("one", "two", "three"), c("ABC", "123", "hello"))
m <- cbind(c("one", "two", "three"), c("ABC", "123", "hello"))
tags(r) <- m
tags(r)

r <- set.tags(r, "another_tag", "another_value")
tags(r) <- c("another_tag=another_value", "one more=this value")
tags(r)

tags(r, "two")

# remove one tag
r <- set.tags(r, c("one"), c(""))
# remove a tag
tags(r) <- cbind("one", "")
tags(r) <- "two="
tags(r)

# remove all tags
r <- set.tags(r, NULL)
tags(r) <- NULL
tags(r)
}

Expand Down

0 comments on commit 98ec9b9

Please sign in to comment.