├── .Rbuildignore ├── .github ├── .gitignore └── workflows │ ├── pkgdown.yaml │ └── recheck.yaml ├── .gitignore ├── .travis.yml ├── DESCRIPTION ├── LICENSE ├── NAMESPACE ├── NEWS ├── R ├── AAAClasses.R ├── AAgeneric_functions.R ├── GDALtransient.R ├── Geary.R ├── RGB.R ├── RcppExports.R ├── addFiles.R ├── addLayer.R ├── adjacency.R ├── adjacent.R ├── aggregate_3d.R ├── aggregate_sp.R ├── alignExtent.R ├── animate.R ├── approxNA.R ├── area.R ├── arith.R ├── arith_sp.R ├── as.array.R ├── as.character.R ├── as.data.frame.R ├── as.logical.R ├── as.matrix.R ├── as.raster.R ├── as.spatial.R ├── atan2.R ├── bands.R ├── barplot.R ├── bbox.R ├── bilinearValue.R ├── bind.R ├── blend.R ├── blockSize.R ├── boundaries.R ├── boxplot.R ├── brick.R ├── buffer.R ├── calc.R ├── canProcessInMemory.R ├── cellFromLine.R ├── cellFromPolygon.R ├── cellRowCol.R ├── cellStats.R ├── cellValues.R ├── cellsFromExtent.R ├── clamp.R ├── clearValues.R ├── click.R ├── clump.R ├── clusterR.R ├── coerce.R ├── col2RGB.R ├── colortable.R ├── commonDataType.R ├── compare.R ├── compareCRS.R ├── compare_Logical.R ├── connection.R ├── contour.R ├── cor.R ├── corLocal.R ├── cover.R ├── coverBrick.R ├── coverPolygons.R ├── crop.R ├── cropSpatial.R ├── crosstab.R ├── cut.R ├── cv.R ├── dataProperties.R ├── dataType.R ├── density.R ├── destair.R ├── dim.R ├── direction.R ├── disaggregate.R ├── distance.R ├── distanceFromPoints.R ├── distanceRows.R ├── distanceToEdge.R ├── dotdens.R ├── drawExtent.R ├── drawPoly.R ├── drivers.R ├── dropLayer.R ├── erase.R ├── extend.R ├── extension.R ├── extent.R ├── extentUnion.R ├── extract.R ├── extractExtent.R ├── extractLines.R ├── extractPoints.R ├── extractPoints_sp.R ├── extractPolygons.R ├── factor.R ├── fasterize.R ├── filler.R ├── fixDBFnames.R ├── flip.R ├── flowpath.R ├── focal.R ├── focalFun.R ├── focalWeight.R ├── fourCellsFromXY.R ├── frbind.R ├── freq.R ├── fullFileName.R ├── gainoffset.R ├── gdal.R ├── gdalFormats.R ├── geom.R ├── getData.R ├── getValues.R ├── getValuesBlock.R ├── getValuesFocal.R ├── getValuesRows.R ├── gridDistance.R ├── gridDistance2.R ├── hdr.R ├── hdrBIL.R ├── hdrBov.R ├── hdrEnvi.R ├── hdrErdasRaw.R ├── hdrIDRISI.R ├── hdrPRJ.R ├── hdrRaster.R ├── hdrSAGA.R ├── hdrVRT.R ├── hdrWorldFile.R ├── head.R ├── hillShade.R ├── hist.R ├── idwValue.R ├── ifelse.R ├── image.R ├── imageplot.R ├── imageplot2.R ├── index.R ├── indexReplace.R ├── indexReplaceBrick.R ├── inifile.R ├── init.R ├── intDataType.R ├── interpolate.R ├── intersect.R ├── intersect_sp.R ├── is.na.R ├── isLonLat.R ├── kernelDens.R ├── kernelDensity.R ├── kml.R ├── kml_multiple.R ├── labels.R ├── layerStats.R ├── layerize.R ├── localFun.R ├── makeProjString.R ├── makeRasterList.R ├── makeTiles.R ├── mask.R ├── match.R ├── math.R ├── maxDataType.R ├── mean.R ├── median.R ├── merge.R ├── metadata.R ├── minValue.R ├── modal.R ├── modalRaster.R ├── moran.R ├── mosaic.R ├── movingFun.R ├── multiCore.R ├── naValue.R ├── names.R ├── ncell.R ├── netCDFread.R ├── netCDFreadCells.R ├── netCDFtoRasterCD.R ├── netCDFtoRasterGMT.R ├── netCDFtoStack.R ├── netCDFutil.R ├── netCDFwriteCD.R ├── newPLot.R ├── nlayers.R ├── notused.R ├── nsidcICE.R ├── origin.R ├── overlay.R ├── pairs.R ├── persp.R ├── plot.R ├── plot2rasters.R ├── plotCT.R ├── plotExent.R ├── plotRGB.R ├── plotRaster.R ├── plotRaster2.R ├── pointdistance.R ├── predict.R ├── print.R ├── progressBar.R ├── project.R ├── projectRaster.R ├── projection.R ├── properties.R ├── quad.R ├── quantile.R ├── randomize.R ├── range.R ├── raster.R ├── rasterFromASCII.R ├── rasterFromBIL.R ├── rasterFromCells.R ├── rasterFromFile.R ├── rasterFromGDAL.R ├── rasterFromIDRISI.R ├── rasterFromRasterFile.R ├── rasterFromSAGA.R ├── rasterFromSurferFile.R ├── rasterFromXYZ.R ├── rasterOptions.R ├── rasterToPoints.R ├── rasterToPolygons.R ├── rasterize.R ├── rasterizeLines.R ├── rasterizePoints.R ├── rasterizePointsNGB.R ├── rasterizePolygons.R ├── ratify.R ├── read.R ├── readAscii.R ├── readCells.R ├── readCellsGDAL.R ├── readRasterBrick.R ├── readRasterLayer.R ├── reclassify.R ├── rectify.R ├── replaceProperties.R ├── resample.R ├── rotate.R ├── roundExtent.R ├── rowMinMax.R ├── rowSums.R ├── sampleAlong.R ├── sampleInt.R ├── sampleRandom.R ├── sampleRegular.R ├── sampleStratified.R ├── scale.R ├── scalebar.R ├── select.R ├── setCV.R ├── setExtent.R ├── setFileExt.R ├── setMinMax.R ├── setValues.R ├── setZ.R ├── sf.R ├── shift.R ├── show.R ├── shp.R ├── simplifyPols.R ├── slopeAspect.R ├── sparse.R ├── speasy.R ├── spplot.R ├── srs.R ├── stack.R ├── stackApply.R ├── stackFile.R ├── stackQuick.R ├── stackSelect.R ├── stretch.R ├── subs.R ├── subset.R ├── summary-methods.R ├── summary.R ├── symdif.R ├── terrain.R ├── text.R ├── tmpFile.R ├── transpose.R ├── trim.R ├── union.R ├── union_sp.R ├── unique.R ├── unstack.R ├── update.R ├── validCell.R ├── values.R ├── weighted.mean.R ├── which.R ├── which.max.R ├── whiches.max.R ├── writeAllAscii.R ├── writeAllGDAL.R ├── writeAllRaster.R ├── writeRaster.R ├── writeStartStop.R ├── writeStartStopAscii.R ├── writeStartStopGDAL.R ├── writeStartStopRaster.R ├── writeValues.R ├── xyCell.R ├── xyMinMax.R ├── xyResolution.R ├── xyValuesBuffer.R ├── zApply.R ├── zonal.R ├── zoom.R └── zzz.R ├── README.md ├── TODO ├── inst ├── external │ ├── countries.rds │ ├── lux.dbf │ ├── lux.prj │ ├── lux.rds │ ├── lux.shp │ ├── lux.shx │ ├── rlogo.grd │ ├── rlogo.gri │ ├── test.grd │ └── test.gri └── tinytest │ ├── test_getvaluesblock.R │ ├── test_rasterize.R │ ├── test_sf-coercion.R │ ├── test_subset.R │ ├── test_wkt_grd.R │ └── tinytest.R ├── man ├── Arith-methods.Rd ├── Compare-methods.Rd ├── Extent-class.Rd ├── KML.Rd ├── Logic-methods.Rd ├── Math-methods.Rd ├── NAvalue.Rd ├── RGB.Rd ├── Raster-classes.Rd ├── Rcpp-classes.Rd ├── Summary-methods.Rd ├── addLayer.Rd ├── adjacent.Rd ├── aggregate.Rd ├── alignExtent.Rd ├── animate.Rd ├── approxNA.Rd ├── area.Rd ├── as.character.Rd ├── as.data.frame.Rd ├── as.list.Rd ├── as.logical-methods.Rd ├── as.matrix.Rd ├── as.raster.Rd ├── atan2.Rd ├── autocor.Rd ├── bands.Rd ├── barplot.Rd ├── bind.Rd ├── blockSize.Rd ├── boundaries.Rd ├── boxplot.Rd ├── brick.Rd ├── buffer.Rd ├── calc.Rd ├── cellFrom.Rd ├── cellStats.Rd ├── cellsFromExtent.Rd ├── clamp.Rd ├── clearValues.Rd ├── click.Rd ├── clump.Rd ├── cluster.Rd ├── colortable.Rd ├── compare.Rd ├── compareCRS.Rd ├── contour.Rd ├── coords.Rd ├── corLocal.Rd ├── cover.Rd ├── crop.Rd ├── crosstab.Rd ├── cut.Rd ├── cv.Rd ├── dataType.Rd ├── datasource.Rd ├── density.Rd ├── dimensions.Rd ├── direction.Rd ├── disaggregate.Rd ├── distance.Rd ├── distanceFromPoints.Rd ├── draw.Rd ├── drawExtent.Rd ├── erase.Rd ├── extend.Rd ├── extension.Rd ├── extent.Rd ├── extract.Rd ├── extractIndex.Rd ├── extremeValues.Rd ├── factor.Rd ├── filename.Rd ├── filledContour.Rd ├── flip.Rd ├── flowpath.Rd ├── focal.Rd ├── focalWeight.Rd ├── freq.Rd ├── gainoffset.Rd ├── geom.Rd ├── getData.Rd ├── getValues.Rd ├── getValuesBlock.Rd ├── getValuesFocal.Rd ├── gridDistance.Rd ├── hdrFiles.Rd ├── headtail.Rd ├── hillShade.Rd ├── hist.Rd ├── image.Rd ├── iniFile.Rd ├── init.Rd ├── interpolate.Rd ├── intersect.Rd ├── isLonLat.Rd ├── layerStats.Rd ├── layerize.Rd ├── localFun.Rd ├── mask.Rd ├── match.Rd ├── merge.Rd ├── metadata.Rd ├── modal.Rd ├── mosaic.Rd ├── movingFun.Rd ├── names.Rd ├── ncell.Rd ├── nlayers.Rd ├── origin.Rd ├── overlay.Rd ├── pairs.Rd ├── persp.Rd ├── plot.Rd ├── plotRGB.Rd ├── pointDistance.Rd ├── predict.Rd ├── programming.Rd ├── projectRaster.Rd ├── projection.Rd ├── properties.Rd ├── quantile.Rd ├── raster-package.Rd ├── raster.Rd ├── rasterFromCells.Rd ├── rasterFromXYZ.Rd ├── rasterOptions.Rd ├── rasterTmpFile.Rd ├── rasterToContour.Rd ├── rasterToPoints.Rd ├── rasterToPolygons.Rd ├── rasterize.Rd ├── readAll.Rd ├── reclassify.Rd ├── rectify.Rd ├── replacement.Rd ├── resample.Rd ├── resolution.Rd ├── rotate.Rd ├── rotated.Rd ├── round.Rd ├── roundExtent.Rd ├── rowFromCell.Rd ├── rowSums.Rd ├── sampleInt.Rd ├── sampleRandom.Rd ├── sampleRegular.Rd ├── sampleStratified.Rd ├── saveStack.Rd ├── scale.Rd ├── scalebar.Rd ├── select.Rd ├── setExtent.Rd ├── setMinMax.Rd ├── setValues.Rd ├── shapefile.Rd ├── shift.Rd ├── slopeAspect.Rd ├── spEasy.Rd ├── spplot.Rd ├── stack.Rd ├── stackApply.Rd ├── stackSelect.Rd ├── strech.Rd ├── subs.Rd ├── subset.Rd ├── summary.Rd ├── symdif.Rd ├── terrain.Rd ├── text.Rd ├── transpose.Rd ├── trim.Rd ├── union.Rd ├── unique.Rd ├── unstack.Rd ├── update.Rd ├── validCell.Rd ├── validNames.Rd ├── weighted.mean.Rd ├── which.Rd ├── which.minmax.Rd ├── writeFormats.Rd ├── writeRaster.Rd ├── writeValues.Rd ├── xyFromCell.Rd ├── zApply.Rd ├── zonal.Rd ├── zoom.Rd └── zvalues.Rd ├── src ├── Makevars.ucrt ├── RasterModule.cpp ├── RcppExports.cpp ├── aggregate.cpp ├── aggregate.h ├── bilinear.cpp ├── broom.cpp ├── cellRowCol.cpp ├── clamp.cpp ├── distance.cpp ├── distance.h ├── edge.cpp ├── focal_fun.cpp ├── focal_get.cpp ├── focal_sum.cpp ├── geodesic.c ├── geodesic.h ├── getPolygons.cpp ├── layerize.cpp ├── memory.cpp ├── memory.h ├── modal.cpp ├── ppmin.cpp ├── raster_aggregate.cpp ├── raster_distance.cpp ├── rasterize.cpp ├── reclass.cpp ├── spat.h ├── terrain.cpp ├── util.cpp ├── util.h └── xyCell.cpp └── tests └── tinytest.R /.Rbuildignore: -------------------------------------------------------------------------------- 1 | .gitignore 2 | LICENSE 3 | README.md 4 | TODO 5 | ^revdep/ 6 | ^.*\.Rproj$ 7 | ^\.Rproj\.user$ 8 | .travis.yml 9 | ^\.github$ 10 | -------------------------------------------------------------------------------- /.github/.gitignore: -------------------------------------------------------------------------------- 1 | *.html 2 | -------------------------------------------------------------------------------- /.github/workflows/recheck.yaml: -------------------------------------------------------------------------------- 1 | on: 2 | workflow_dispatch: 3 | inputs: 4 | which: 5 | type: choice 6 | description: Which dependents to check 7 | options: 8 | - strong 9 | - most 10 | 11 | name: Reverse dependency check 12 | 13 | jobs: 14 | revdep_check: 15 | name: Reverse check ${{ inputs.which }} dependents 16 | uses: r-devel/recheck/.github/workflows/recheck.yml@v1 17 | with: 18 | which: ${{ inputs.which }} -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.tar.gz 2 | *Rcheck/ 3 | *.o 4 | *.so 5 | *.exe 6 | *.Rhistory 7 | *.R~ 8 | *.dll 9 | *.zip 10 | .Rproj.user 11 | revdep -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # R for travis: see documentation at https://docs.travis-ci.com/user/languages/r 2 | 3 | language: r 4 | cache: packages 5 | 6 | sudo: required 7 | dist: bionic 8 | 9 | before_install: 10 | - sudo add-apt-repository ppa:ubuntugis/ubuntugis-unstable --yes 11 | - sudo apt-get --yes --force-yes update -qq 12 | - sudo apt-get install --yes libproj-dev libgeos-dev libgdal-dev r-cran-ncdf4 libudunits2-dev libpoppler-cpp-dev 13 | - tlmgr install hanging -------------------------------------------------------------------------------- /NEWS: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rspatial/raster/1d4965263e4fa2d23404648471530c0deace72c7/NEWS -------------------------------------------------------------------------------- /R/addFiles.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : June 2008 3 | # Version 0.9 4 | # Licence GPL v3 5 | 6 | 7 | 8 | .addFiles <- function(x, rasterfiles, bands=rep(1, length(rasterfiles))) { 9 | if (length(bands) == 1) { 10 | bands=rep(bands, length(rasterfiles)) 11 | } 12 | rasters <- list() 13 | for (i in 1:length(rasterfiles)) { 14 | if (bands[[i]] < 1) { 15 | r <- raster(rasterfiles[[i]], band=1) 16 | rasters <- c(rasters, r) 17 | if (nbands(r) > 1) { 18 | for (j in 2:nbands(r)) { 19 | r <- raster(rasterfiles[[i]], band=j) 20 | rasters <- c(rasters, r) 21 | } 22 | } 23 | } else { 24 | rasters <- c(rasters, raster(rasterfiles[[i]], FALSE, band=bands[[i]])) 25 | } 26 | } 27 | x <- addLayer(x, rasters) 28 | return(x) 29 | } 30 | 31 | 32 | -------------------------------------------------------------------------------- /R/addLayer.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : September 2009 3 | # Version 0.9 4 | # Licence GPL v3 5 | 6 | 7 | if (!isGeneric("addLayer")) { 8 | setGeneric("addLayer", function(x, ...) 9 | standardGeneric("addLayer")) 10 | } 11 | 12 | setMethod('addLayer', signature(x='Raster'), 13 | function(x, ...) { 14 | 15 | rasters <- .makeRasterList(...) 16 | 17 | if (! inherits(x, 'RasterStack')) { 18 | x <- stack(x) 19 | } 20 | 21 | if (length(rasters)==0) { 22 | return(x) 23 | } 24 | 25 | if (nlayers(x) > 0) { 26 | compareRaster(c(x, rasters)) 27 | } else if (length(rasters) > 1) { 28 | compareRaster(rasters) 29 | } 30 | 31 | vals <- sapply(rasters, hasValues) 32 | if (sum(vals) == 0 & nlayers(x) == 0) { 33 | vals[1] <- TRUE 34 | } 35 | if (sum(vals) != length(vals)) { 36 | warning('Cannot add a RasterLayer with no associated data in memory or on disk to a RasterStack') 37 | } 38 | rasters <- rasters[vals] 39 | 40 | if (nlayers(x) == 0) { 41 | r <- rasters[[1]] 42 | x@nrows <- r@nrows 43 | x@ncols <- r@ncols 44 | x@extent <- r@extent 45 | crs(x) <- .getCRS(r) 46 | if (rotated(r)) { 47 | x@rotated = r@rotated 48 | x@rotation = r@rotation 49 | } 50 | 51 | nl <- 1 52 | x@layers[[nl]] <- r 53 | rasters <- rasters[-1] 54 | if (length(rasters)==0) { return(x) } 55 | } 56 | 57 | x@layers <- c(x@layers, rasters) 58 | names(x) <- sapply(x@layers, names) 59 | 60 | return(x) 61 | } 62 | ) 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /R/animate.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | setMethod('animate', signature(x='RasterStackBrick'), 4 | function(x, pause=0.25, main, zlim, maxpixels=50000, n=10, ...) { 5 | nl <- nlayers(x) 6 | if (missing(main)) { 7 | main <- getZ(x) 8 | if (is.null(main)) { 9 | main <- names(x) 10 | } 11 | } 12 | 13 | x <- sampleRegular(x, size=maxpixels, asRaster=TRUE, useGDAL=TRUE) 14 | 15 | if (missing(zlim)) { 16 | zlim <- c(min(minValue(x)), max(maxValue(x))) 17 | } 18 | 19 | i <- 1 20 | reps <- 0 21 | while (reps < n) { 22 | plot(x[[i]], main = main[i], zlim=zlim, maxpixels=Inf, ...) 23 | grDevices::dev.flush() 24 | Sys.sleep(pause) 25 | i <- i + 1 26 | if (i > nl) { 27 | i <- 1 28 | reps <- reps+1 29 | } 30 | } 31 | } 32 | ) 33 | 34 | #anim(st, tvals) 35 | -------------------------------------------------------------------------------- /R/arith_sp.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : November 2011 3 | # Version 1.0 4 | # Licence GPL v3 5 | 6 | 7 | setMethod("+", signature(e1='SpatialPolygons', e2='SpatialPolygons'), 8 | function(e1, e2){ 9 | union(e1, e2) 10 | } 11 | ) 12 | 13 | setMethod("*", signature(e1='SpatialPolygons', e2='SpatialPolygons'), 14 | function(e1, e2){ 15 | intersect(e1, e2) } 16 | ) 17 | 18 | setMethod("-", signature(e1='SpatialPolygons', e2='SpatialPolygons'), 19 | function(e1, e2){ 20 | erase(e1, e2) 21 | } 22 | ) 23 | 24 | #setMethod("^", signature(e1='SpatialPolygons', e2='SpatialPolygons'), 25 | # function(e1, e2){ 26 | # crop(e1, e2) 27 | # } 28 | #) 29 | 30 | setMethod("+", signature(e1='SpatialPoints', e2='SpatialPoints'), 31 | function(e1, e2){ 32 | bind(e1, e2) 33 | } 34 | ) 35 | 36 | setMethod("+", signature(e1='SpatialLines', e2='SpatialLines'), 37 | function(e1, e2){ 38 | bind(e1, e2) 39 | } 40 | ) 41 | -------------------------------------------------------------------------------- /R/as.array.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : November 2010 3 | # Version 1.0 4 | # Licence GPL v3 5 | 6 | 7 | setMethod('as.array', signature(x='RasterLayer'), 8 | function(x, maxpixels, ...) { 9 | if (!hasValues(x)) { stop("'x' has no values") } 10 | if (! missing(maxpixels)) { 11 | x <- sampleRegular(x, maxpixels, asRaster=TRUE) 12 | } 13 | x <- array(as.matrix(x), c(dim(x))) 14 | x 15 | } ) 16 | 17 | setMethod('as.array', signature(x='RasterStackBrick'), 18 | function(x, maxpixels, transpose=FALSE) { 19 | if (!hasValues(x)) { stop("'x' has no values") } 20 | if (! missing(maxpixels)) { 21 | x <- sampleRegular(x, maxpixels, asRaster=TRUE) 22 | } 23 | dm <- dim(x) 24 | x <- getValues(x) 25 | if (transpose) { 26 | ar <- array(NA, c(dm[2], dm[1], dm[3])) 27 | for (i in 1:dm[3]) { 28 | ar[,,i] <- matrix(x[,i], nrow=dm[2], byrow=FALSE) 29 | } 30 | } else { 31 | ar <- array(NA, dm) 32 | for (i in 1:dm[3]) { 33 | ar[,,i] <- matrix(x[,i], nrow=dm[1], byrow=TRUE) 34 | } 35 | } 36 | ar 37 | } ) 38 | 39 | -------------------------------------------------------------------------------- /R/as.character.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | setMethod("as.character", signature(x="Extent"), 4 | function(x, ...) { 5 | e <- extent(x) 6 | paste0("extent(", paste(as.vector(e), collapse=", "), ")") 7 | } 8 | ) 9 | 10 | 11 | setMethod("as.character", signature(x="Raster"), 12 | function(x, ...) { 13 | e <- extent(x) 14 | crs <- proj4string(x) 15 | crs <- ifelse(is.na(crs), ", crs=''", paste0(", crs='", crs, "'")) 16 | if (nlayers(x) < 2) { 17 | paste0("raster(", 18 | "ncols=",ncol(x), 19 | ", nrows=",nrow(x), 20 | ", xmn=",e[1], 21 | ", xmx=",e[2], 22 | ", ymn=",e[3], 23 | ", ymx=",e[4], 24 | crs, ")" 25 | ) 26 | 27 | } else { 28 | paste0("brick(", 29 | "ncol=", ncol(x), 30 | ", nrow=", nrow(x), 31 | ", nl=", nlayers(x), 32 | ", xmn=",e[1], 33 | ", xmx=",e[2], 34 | ", ymn=",e[3], 35 | ", ymx=",e[4], 36 | crs, ")" 37 | ) 38 | } 39 | } 40 | ) 41 | #eval(parse(text=as.character(raster()))) 42 | #eval(parse(text=as.character(stack()))) 43 | -------------------------------------------------------------------------------- /R/as.matrix.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : October 2010 3 | # Version 1.0 4 | # Licence GPL v3 5 | 6 | 7 | setMethod('as.matrix', signature(x='RasterLayer'), 8 | function(x, maxpixels, ...) { 9 | if (!hasValues(x)) { stop("'x' has no values") } 10 | if (! missing(maxpixels)) { 11 | x <- sampleRegular(x, maxpixels, asRaster=TRUE) 12 | } 13 | return( getValues(x, format='matrix') ) 14 | }) 15 | 16 | 17 | setMethod('as.matrix', signature(x='RasterStackBrick'), 18 | function(x, maxpixels, ...){ 19 | if (!hasValues(x)) { stop("'x' has no values") } 20 | if (! missing(maxpixels)) { 21 | x <- sampleRegular(x, maxpixels, asRaster=TRUE) 22 | } 23 | return( getValues(x) ) 24 | }) 25 | 26 | 27 | setMethod('as.matrix', signature(x='Extent'), 28 | function(x, ...) { 29 | b <- bbox(x) 30 | rownames(b) <- c('x', 'y') 31 | b 32 | }) 33 | 34 | 35 | # mode argument is ignored as mode=mode gave an error on R-devel 36 | setMethod('as.vector', signature(x='Extent'), 37 | function(x, mode='any') { 38 | as.vector(c(x@xmin, x@xmax, x@ymin, x@ymax)) 39 | }) 40 | 41 | 42 | setMethod('as.vector', signature(x='Raster'), 43 | function(x, mode='any') { 44 | as.vector(getValues(x)) 45 | }) 46 | 47 | -------------------------------------------------------------------------------- /R/as.raster.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : July 2011 3 | # Version 0.9 4 | # Licence GPL v3 5 | 6 | 7 | # Note: these functions create a _r_aster object (small r) (grDevices) for use with the rasterImage function 8 | # _NOT_ a Raster* object as defined in this package 9 | 10 | 11 | setMethod('as.raster', signature(x='RasterLayer'), 12 | function(x, maxpixels=50000, col=rev(terrain.colors(255)), ...) { 13 | x <- as.matrix(sampleRegular(x, maxpixels, asRaster=TRUE)) 14 | r <- range(x, na.rm=TRUE) 15 | x <- (x - r[1])/ (r[2] - r[1]) 16 | x <- round(x * (length(col)-1) + 1) 17 | x[] <- col[x] 18 | as.raster(x) 19 | } ) 20 | 21 | 22 | #e <- as.vector(t(bbox(extent(r)))) 23 | #a <- as.raster(r) 24 | #plot(e[1:2], e[3:4], type = "n", xlab="", ylab="") 25 | #graphics::rasterImage(a, e[1], e[3], e[2], e[4]) 26 | 27 | -------------------------------------------------------------------------------- /R/atan2.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : March 2011 3 | # Version 1.0 4 | # Licence GPL v3 5 | 6 | 7 | 8 | setMethod("atan2", signature(y='Raster', x='Raster'), 9 | function(y, x) { 10 | 11 | compareRaster(x, y) 12 | 13 | ny <- nlayers(y) 14 | nx <- nlayers(x) 15 | nl <- max(ny, nx) 16 | if (nl > 1) { 17 | r <- brick(x, values=FALSE, nl=nl) 18 | } else { 19 | r <- raster(x) 20 | } 21 | 22 | if (canProcessInMemory(r, 3)) { 23 | r <- setValues(r, atan2(getValues(y), getValues(x))) 24 | } else { 25 | tr <- blockSize(x) 26 | pb <- pbCreate(tr$n) 27 | r <- writeStart(r, filename=rasterTmpFile()) 28 | for (i in 1:tr$n) { 29 | v <- atan2(getValues(y, row=tr$row[i], nrows=tr$nrows[i]), getValues(x, row=tr$row[i], nrows=tr$nrows[i]) ) 30 | r <- writeValues(r, v, tr$row[i]) 31 | pbStep(pb, i) 32 | } 33 | r <- writeStop(r) 34 | pbClose(pb) 35 | } 36 | return(r) 37 | } 38 | ) 39 | 40 | -------------------------------------------------------------------------------- /R/bands.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : October 2008 3 | # Version 0.9 4 | # Licence GPL v3 5 | 6 | 7 | 8 | if (!isGeneric("bandnr")) { 9 | setGeneric("bandnr", function(x, ...) 10 | standardGeneric("bandnr")) 11 | } 12 | 13 | 14 | setMethod('bandnr', signature(x='RasterLayer'), 15 | function(x) { 16 | return(x@data@band) 17 | } 18 | ) 19 | 20 | 21 | nbands <- function(x) { 22 | if (inherits(x, "RasterLayer") | inherits(x, "RasterBrick")) { 23 | return(x@file@nbands) 24 | } else { 25 | stop(paste("not implemented for", paste(class(x), collapse=", "), "objects")) 26 | } 27 | } 28 | 29 | 30 | .bandOrder <- function(x) { 31 | if (inherits(x, "RasterStack")) { 32 | stop(paste("not implemented for RasterStack objects")) 33 | } else { 34 | return(paste(x@file@bandorder)) 35 | } 36 | } 37 | 38 | -------------------------------------------------------------------------------- /R/barplot.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : September 2012 3 | # Version 1.0 4 | # Licence GPL v3 5 | 6 | 7 | 8 | setMethod('barplot', 'RasterLayer', 9 | function(height, maxpixels=1000000, digits=0, breaks=NULL, col=rainbow, ...) { 10 | 11 | x <- sampleRegular(height, maxpixels) 12 | adj <- length(x) / ncell(height) 13 | if (adj < 1) { 14 | warning('a sample of ', round(100*adj, 1), '% of the raster cells were used to estimate frequencies') 15 | } 16 | 17 | if (!is.null(digits)) { 18 | x <- round(x, digits) 19 | } 20 | if (!is.null(breaks)) { 21 | x <- cut(x, breaks) 22 | } 23 | 24 | x <- table(x) / adj 25 | if (is.function(col)) { 26 | col <- col(length(x)) 27 | } 28 | barplot(x, col=col, ...) 29 | } 30 | ) 31 | -------------------------------------------------------------------------------- /R/bbox.R: -------------------------------------------------------------------------------- 1 | # R function for the raster package 2 | # Author: Robert J. Hijmans 3 | # Date : January 2009 4 | # Version 0.9 5 | # Licence GPL v3 6 | 7 | 8 | setMethod("bbox", signature(obj="Extent"), 9 | function(obj) { 10 | bb <- matrix(ncol=2, nrow=2) 11 | colnames(bb) <- c("min","max") 12 | rownames(bb) <- c("s1","s2") 13 | bb[1,1] <- obj@xmin 14 | bb[1,2] <- obj@xmax 15 | bb[2,1] <- obj@ymin 16 | bb[2,2] <- obj@ymax 17 | return(bb) 18 | } 19 | ) 20 | 21 | setMethod("bbox", signature(obj="Raster"), 22 | function(obj) { 23 | e <- extent(obj) 24 | return( bbox(e) ) 25 | } 26 | ) 27 | 28 | -------------------------------------------------------------------------------- /R/blockSize.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : November 2009 3 | # Version 0.9 4 | # Licence GPL v3 5 | 6 | 7 | setMethod("blockSize", signature(x="Raster"), 8 | function(x, chunksize, n=nlayers(x), minblocks=4, minrows=1) { 9 | 10 | n <- max(n, 1) 11 | if (missing(chunksize)) { 12 | bs <- .chunk() 13 | } else { 14 | bs <- chunksize 15 | } 16 | 17 | blockrows <- try(methods::slot(x@file, 'blockrows'), silent=TRUE) 18 | if (inherits(blockrows, 'try-error')) { 19 | blockrows <- 1 20 | } 21 | blockrows <- max(blockrows, 1) 22 | 23 | 24 | nr <- nrow(x) 25 | size <- min(nr, max(1, floor(bs / (ncol(x) * n * 8)))) 26 | # min number of chunks 27 | if (size > 1) { 28 | minblocks <- min(nr, max(1, minblocks)) 29 | size <- min(ceiling(nr/minblocks), size) 30 | } 31 | size <- min(max(size, minrows), nr) 32 | size <- max(minrows, blockrows * round(size / blockrows)) 33 | 34 | nb <- ceiling(nr / size) 35 | row <- (0:(nb-1))*size + 1 36 | nrows <- rep(size, length(row)) 37 | 38 | dif = nb * size - nr 39 | nrows[length(nrows)] = nrows[length(nrows)] - dif 40 | 41 | return(list(row=row, nrows=nrows, n=nb)) 42 | } 43 | ) 44 | 45 | -------------------------------------------------------------------------------- /R/boxplot.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : November 2010 3 | # Version 1.0 4 | # Licence GPL v3 5 | 6 | 7 | 8 | setMethod('boxplot', signature(x='RasterStackBrick'), 9 | function(x, maxpixels=100000, ...) { 10 | nl <- nlayers(x) 11 | cn <- names(x) 12 | if ( canProcessInMemory(x)) { 13 | x <- getValues(x) 14 | } else { 15 | warning('taking a sample of ', maxpixels, ' cells') 16 | x <- sampleRegular(x, maxpixels, useGDAL=TRUE) 17 | } 18 | colnames(x) <- cn 19 | boxplot(x, ...) 20 | } 21 | ) 22 | 23 | 24 | setMethod('boxplot', signature(x='RasterLayer'), 25 | function(x, y=NULL, maxpixels=100000, ...) { 26 | if (is.null(y)) { 27 | cn <- names(x) 28 | if ( canProcessInMemory(x)) { 29 | x <- getValues(x) 30 | } else { 31 | warning('taking a sample of ', maxpixels, ' cells') 32 | x = sampleRegular(x, maxpixels, useGDAL=TRUE) 33 | } 34 | x <- matrix(x) 35 | colnames(x) <- cn 36 | boxplot(x, ...) 37 | } else { 38 | s <- stack(x, y) 39 | if ( canProcessInMemory(s)) { 40 | s <- getValues(s) 41 | } else { 42 | warning('taking a sample of ', maxpixels, ' cells') 43 | s <- sampleRegular(s, maxpixels, useGDAL=TRUE) 44 | } 45 | cn <- colnames(s) 46 | if (is.null(cn)) { #apparently this can happen. 47 | cn <- c('layer1', 'layer2') 48 | colnames(s) <- cn 49 | } 50 | f <- stats::as.formula(paste(cn[1], '~', cn[2])) 51 | boxplot(f, data=s, ...) 52 | } 53 | } 54 | ) 55 | 56 | -------------------------------------------------------------------------------- /R/cellFromLine.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : December 2009 3 | # Version 0.9 4 | # Licence GPL v3 5 | 6 | cellFromLine <- function(object, lns) { 7 | spbb <- sp::bbox(lns) 8 | rsbb <- bbox(object) 9 | addres <- 2 * max(res(object)) 10 | nlns <- length( lns@lines ) 11 | res <- list() 12 | res[[nlns+1]] = NA 13 | 14 | if (spbb[1,1] > rsbb[1,2] | spbb[1,2] < rsbb[1,1] | spbb[2,1] > rsbb[2,2] | spbb[2,2] < rsbb[2,1]) { 15 | return(res[1:nlns]) 16 | } 17 | 18 | rr <- raster(object) 19 | for (i in 1:nlns) { 20 | pp <- lns[i,] 21 | spbb <- sp::bbox(pp) 22 | 23 | if (! (spbb[1,1] > rsbb[1,2] | spbb[1,2] < rsbb[1,1] | spbb[2,1] > rsbb[2,2] | spbb[2,2] < rsbb[2,1]) ) { 24 | rc <- crop(rr, extent(pp)+addres) 25 | rc <- .linesToRaster(pp, rc, silent=TRUE) 26 | xy <- rasterToPoints(rc)[,-3,drop=FALSE] 27 | if (length(xy) > 0) { # always TRUE? 28 | res[[i]] <- cellFromXY(object, xy) 29 | } 30 | } 31 | } 32 | 33 | return( res[1:nlns] ) 34 | } 35 | 36 | -------------------------------------------------------------------------------- /R/cellFromPolygon.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : January 2011 3 | # Version 1.0 4 | # Licence GPL v3 5 | 6 | 7 | cellFromPolygon <- function(object, p, weights=FALSE) { 8 | 9 | spbb <- sp::bbox(p) 10 | rsbb <- bbox(object) 11 | addres <- max(res(object)) 12 | npol <- length(p@polygons) 13 | res <- list() 14 | res[[npol+1]] = NA 15 | 16 | if (spbb[1,1] >= rsbb[1,2] | spbb[1,2] <= rsbb[1,1] | spbb[2,1] >= rsbb[2,2] | spbb[2,2] <= rsbb[2,1]) { 17 | return(res[1:npol]) 18 | } 19 | rr <- raster(object) 20 | for (i in 1:npol) { 21 | pp <- p[i,] 22 | spbb <- sp::bbox(pp) 23 | 24 | if (spbb[1,1] >= rsbb[1,2] | spbb[1,2] <= rsbb[1,1] | spbb[2,1] >= rsbb[2,2] | spbb[2,2] <= rsbb[2,1]) { 25 | # do nothing; res[[i]] <- NULL 26 | } else { 27 | rc <- crop(rr, extent(pp)+addres) 28 | if (weights) { 29 | rc <- .polygonsToRaster(pp, rc, getCover=TRUE, silent=TRUE, datatype="FLT4S") 30 | rc[rc==0] <- NA 31 | xy <- rasterToPoints(rc) 32 | weight <- xy[,3] / 100 33 | xy <- xy[,-3] 34 | } else { 35 | rc <- .polygonsToRaster(pp, rc, silent=TRUE) 36 | xy <- rasterToPoints(rc)[,-3,drop=FALSE] 37 | } 38 | 39 | if (length(xy) > 0) { # catch holes or very small polygons 40 | cell <- cellFromXY(object, xy) 41 | if (weights) { 42 | res[[i]] <- cbind(cell, weight) 43 | } else { 44 | res[[i]] <- cell 45 | } 46 | } 47 | } 48 | } 49 | 50 | return( res[1:npol] ) 51 | } 52 | 53 | 54 | 55 | -------------------------------------------------------------------------------- /R/cellsFromExtent.R: -------------------------------------------------------------------------------- 1 | # R function for the raster package 2 | # Author: Robert J. Hijmans 3 | # Date : January 2009 4 | # Version 0.9 5 | # Licence GPL v3 6 | 7 | 8 | cellsFromExtent <- function(object, extent, expand=FALSE) { 9 | object <- raster(object) 10 | extent <- alignExtent(extent(extent), object) 11 | innerBox <- intersect(extent(object), extent) 12 | if (is.null(innerBox)) { 13 | return(NULL) 14 | } 15 | 16 | srow <- rowFromY(object, innerBox@ymax - 0.5 * yres(object)) 17 | erow <- rowFromY(object, innerBox@ymin + 0.5 * yres(object)) 18 | scol <- colFromX(object, innerBox@xmin + 0.5 * xres(object)) 19 | ecol <- colFromX(object, innerBox@xmax - 0.5 * xres(object)) 20 | 21 | if (expand) { 22 | srow <- srow - round((extent@ymax - innerBox@ymax) / yres(object)) 23 | erow <- erow + round((innerBox@ymin - extent@ymin) / yres(object)) 24 | scol <- scol - round((innerBox@xmin - extent@xmin) / xres(object)) 25 | ecol <- ecol + round((extent@xmax - innerBox@xmax) / xres(object)) 26 | } 27 | 28 | return(cellFromRowColCombine(object, srow:erow, scol:ecol)) 29 | } 30 | 31 | 32 | # By Mike Sumner 33 | extentFromCells <- function (object, cells) { 34 | cells <- stats::na.omit(unique(round(cells))) 35 | cells <- cells[cells > 0 & cells <= ncell(object)] 36 | if (length(cells) < 1) { 37 | stop('no valid cells') 38 | } 39 | r <- res(object) 40 | dx <- r[1] * c(-0.5, 0.5) 41 | dy <- r[2] * c(-0.5, 0.5) 42 | extent(range(xFromCell(object, cells)) + dx, range(yFromCell(object, cells)) + dy) 43 | } 44 | 45 | -------------------------------------------------------------------------------- /R/clearValues.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : June 2008 3 | # Version 0.9 4 | # Licence GPL v3 5 | 6 | 7 | .clearRaster <- function(object) { 8 | object@data@inmemory <- FALSE 9 | 10 | 11 | # object@data@indices = vector(mode='numeric') 12 | object@data@values <- vector() 13 | if ( ! fromDisk(object) ) { 14 | object@data@min <- Inf 15 | object@data@max <- -Inf 16 | object@data@haveminmax <- FALSE 17 | } 18 | return(object) 19 | } 20 | 21 | 22 | clearValues <- function(x) { 23 | if (inherits(x, "BasicRaster")) { 24 | return(x) 25 | } else if (inherits(x, "RasterLayer" )) { 26 | x <- .clearRaster(x) 27 | } else if (inherits(x, "RasterStack") ) { 28 | for (i in seq(along.with=nlayers(x))) { 29 | if (fromDisk(x@layers[[i]])) { 30 | x@layers[[i]] <- .clearRaster(x@layers[[i]]) 31 | } 32 | } 33 | } else if (inherits(x, 'RasterBrick')) { 34 | x@data@values <- matrix(NA,0,0) 35 | x@data@inmemory <- FALSE 36 | 37 | # x@data@indices = c(0,0) 38 | if ( ! fromDisk(x) ) { 39 | x@data@min <- rep(Inf, nlayers(x)) 40 | x@data@max <- rep(-Inf, nlayers(x)) 41 | x@data@haveminmax <- FALSE 42 | } 43 | } 44 | return(x) 45 | } 46 | 47 | 48 | .clearFile <- function(x) { 49 | x@file@name <- '' 50 | x@data@fromdisk <- FALSE 51 | x@file@driver <- "" 52 | return(x) 53 | } 54 | -------------------------------------------------------------------------------- /R/col2RGB.R: -------------------------------------------------------------------------------- 1 | 2 | .col2RGB <- function(x) { 3 | d <- t( grDevices::col2rgb(x@legend@colortable) ) 4 | d <- data.frame(id=0:255, d) 5 | subs(x, d, which=2:4) 6 | } 7 | 8 | 9 | .alphaCT <- function(x, alpha) { 10 | ct <- colortable(x) 11 | z <- t(grDevices::col2rgb(ct)) 12 | ct <- apply(z, 1, function(i) grDevices::rgb(i[1], i[2], i[3], alpha*255, maxColorValue=255)) 13 | colortable(x) <- ct 14 | return(x) 15 | } 16 | -------------------------------------------------------------------------------- /R/colortable.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | colortable <- function(x) { 4 | if (.hasSlot(x, 'legend')) { 5 | x@legend@colortable 6 | } else { 7 | logical(0) 8 | } 9 | } 10 | 11 | 12 | 'colortable<-' <- function(x, value) { 13 | # for now assuming values are between 0 and 255!! 14 | x@legend@colortable <- value 15 | return(x) 16 | } 17 | -------------------------------------------------------------------------------- /R/commonDataType.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : October 2011 3 | # Version 1.0 4 | # Licence GPL v3 5 | 6 | 7 | .commonDataType <- function(dtype) { 8 | dtype <- as.vector(unlist(dtype, use.names = FALSE)) 9 | dtype <- unique(dtype) 10 | if (length(dtype)==1) { 11 | datatype <- dtype 12 | } else { 13 | dsize <- dataSize(dtype) 14 | dtype <- .shortDataType(dtype) 15 | if (any(dtype == 'FLT')) { 16 | dsize <- max(dsize[dtype=='FLT']) 17 | datatype <- paste('FLT', dsize, 'S', sep='') 18 | } else { 19 | signed <- dataSigned(dtype) 20 | dsize <- max(dsize) 21 | if (all(signed)) { 22 | datatype <- paste('INT', dsize, 'S', sep='') 23 | } else if (all(!signed)) { 24 | datatype <- paste('INT', dsize, 'U', sep='') 25 | } else { 26 | dsize <- ifelse(dsize == 1, 2, ifelse(dsize == 2, 4, 8)) 27 | datatype <- paste('INT', dsize, 'S', sep='') 28 | } 29 | } 30 | } 31 | datatype 32 | } 33 | 34 | -------------------------------------------------------------------------------- /R/cor.R: -------------------------------------------------------------------------------- 1 | 2 | .cor <- function(x, n=Inf, ...) { 3 | 4 | nl <- nlayers(x) 5 | if (nl < 2) return(1) 6 | 7 | if (n < ncell(x)) { 8 | x <- sampleRegular(x, size=n, asRaster=TRUE) 9 | } 10 | 11 | if (canProcessInMemory(x, nlayers(x)*4)) { 12 | s <- stats::na.omit(getValues(x)) 13 | s <- stats::cor(s) 14 | } else { 15 | msk <- sum(x, na.rm=FALSE) 16 | x <- mask(x, msk) 17 | mx <- cellStats(x, 'mean') 18 | sx <- cellStats(x, 'sd') 19 | nc <- ncell(x) 20 | s <- matrix(NA, nrow=n, ncol=n) 21 | for (i in 1:(nl-1)) { 22 | for (j in (i+1):nl) { 23 | s[j,i] <- s[i,j] <- cellStats(((x[[i]] - mx[i]) * (x[[j]] - mx[j])) / (sx[i] * sx[j]), sum)/ (nc-1) 24 | } 25 | } 26 | diag(s) <- 1 27 | } 28 | if (nrow(s) == 2) { 29 | s[2,1] 30 | } else { 31 | colnames(s) <- rownames(s) <- names(x) 32 | s 33 | } 34 | } 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /R/destair.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | .destair <- function(x, keepExtent=TRUE) { 5 | 6 | pts <- data.frame(geom(as(x, 'SpatialPolygons'))) 7 | 8 | if (keepExtent) { 9 | bb <- sp::bbox(x) 10 | ptsx1 <- pts[,5] == bb[1,1] 11 | ptsx2 <- pts[,5] == bb[1,2] 12 | ptsy1 <- pts[,6] == bb[2,1] 13 | ptsy2 <- pts[,6] == bb[2,2] 14 | } 15 | 16 | u <- unique(pts$cump) 17 | for (j in u) { 18 | k <- pts$cump==j 19 | p <- pts[k, 5:6] 20 | p <- rbind(p[(nrow(p)-1), ,drop=FALSE], p, p[2,,drop=FALSE]) 21 | dx <- diff(p$x) 22 | dy <- diff(p$y) 23 | tf1 <- rowSums( cbind(dx[-length(dx)], dy[-1]) ) 24 | tf2 <- rowSums( cbind(dx[-1], dy[-length(dy)]) ) 25 | i <- which(tf1==0 | tf2==0) + 1 26 | p[i, ] <- (p[i-1, ] + p[i+1, ] + 2 * p[i, ]) / 4 27 | pts[k, 5:6] <- p[-c(1, nrow(p)),] 28 | } 29 | 30 | if (keepExtent) { 31 | pts[ptsx1,5] <- bb[1,1] 32 | pts[ptsx2,5] <- bb[1,2] 33 | pts[ptsy1,6] <- bb[2,1] 34 | pts[ptsy2,6] <- bb[2,2] 35 | } 36 | 37 | r <- as(pts, 'SpatialPolygons') 38 | row.names(r) <- row.names(x) 39 | crs(r) <- .getCRS(x) 40 | 41 | if (.hasSlot(x, 'data')) { 42 | r <- sp::SpatialPolygonsDataFrame(r, x@data) 43 | } 44 | 45 | r 46 | } 47 | 48 | 49 | -------------------------------------------------------------------------------- /R/distanceFromPoints.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : September 2009 3 | # Version 0.9 4 | # Licence GPL v3 5 | 6 | distanceFromPoints <- function(object, xy, filename='', ...) { 7 | 8 | pts <- .pointsToMatrix(xy) 9 | 10 | filename <- trim(filename) 11 | 12 | if (couldBeLonLat(object)) { 13 | longlat=TRUE 14 | } else { 15 | longlat=FALSE 16 | } 17 | 18 | out <- raster(object) 19 | a = 6378137.0 20 | f = 1/298.257223563 21 | if (canProcessInMemory(out, 4)) { 22 | xy <- xyFromCell(out, 1:ncell(out)) 23 | out <- setValues(out, .Call('_raster_distanceToNearestPoint', xy, pts, longlat, a, f , PACKAGE = 'raster')) 24 | 25 | if (filename != '') { 26 | out <- writeRaster(out, filename=filename, ...) 27 | } 28 | return(out) 29 | } 30 | 31 | out <- writeStart(out, filename=filename, ...) 32 | tr <- blockSize(out) 33 | pb <- pbCreate(tr$n, ...) 34 | xy <- cbind(rep(xFromCol(out, 1:ncol(out)), tr$nrows[1]), NA) 35 | for (i in 1:tr$n) { 36 | if (i == tr$n) { 37 | xy <- xy[1:(ncol(out)*tr$nrows[i]), ] 38 | } 39 | xy[,2] <- rep(yFromRow(out, tr$row[i]:(tr$row[i]+tr$nrows[i]-1)), each=ncol(out)) 40 | vals <- .Call('_raster_distanceToNearestPoint', xy, pts, longlat, a, f, PACKAGE='raster') 41 | out <- writeValues(out, vals, tr$row[i]) 42 | pbStep(pb) 43 | } 44 | pbClose(pb) 45 | out <- writeStop(out) 46 | return(out) 47 | } 48 | 49 | -------------------------------------------------------------------------------- /R/distanceToEdge.R: -------------------------------------------------------------------------------- 1 | 2 | .distToEdge <- function(x) { 3 | xy1 <- xyFromCell(x, 1) 4 | xy2 <- xyFromCell(x, ncell(x)) 5 | 6 | a <- cbind(xFromCol(x, 1), yFromRow(x, 1:nrow(x))) 7 | b <- cbind(xFromCol(x, 2), yFromRow(x, 1:nrow(x))) 8 | dX <- pointDistance(a,b,longlat=T) 9 | m = matrix(1:ncol(x), nrow=nrow(x), ncol=ncol(x), byrow=T) 10 | m <- m * dX 11 | 12 | z <- raster(x) 13 | z[] <- m 14 | z2 <- flip(z, 'x') 15 | z <- min(z, z2) 16 | 17 | 18 | dY1 <- pointDistance(xy1, cbind(xy1[1], yFromRow(x, 1:nrow(x))), longlat=T) 19 | dY2 <- pointDistance(xy2, cbind(xy2[1], yFromRow(x, 1:nrow(x))), longlat=T) 20 | dY <- pmin(dY1, dY2) 21 | 22 | b <- raster(x) 23 | b[] <- rep(dY, each=ncol(x)) 24 | 25 | d <- min(z,b) 26 | d 27 | } 28 | 29 | -------------------------------------------------------------------------------- /R/drawExtent.R: -------------------------------------------------------------------------------- 1 | # R function for the raster package 2 | # Author: Robert J. Hijmans 3 | # Date : January 2009, December 2011 4 | # Version 1.0 5 | # Licence GPL v3 6 | 7 | 8 | 9 | drawExtent <- function(show=TRUE, col="red") { 10 | if (show) { 11 | loc1 <- graphics::locator(n=1, type="p", pch='+', col=col) 12 | } else { 13 | loc1 <- graphics::locator(n=1) 14 | } 15 | loc2 <- graphics::locator(n=1) 16 | loc <- rbind(unlist(loc1), unlist(loc2)) 17 | e <- extent(min(loc[,'x']), max(loc[,'x']), min(loc[,'y']), max(loc[,'y'])) 18 | if (e@xmin == e@xmax) { 19 | e@xmin <- e@xmin - 0.0000001 20 | e@xmax <- e@xmax + 0.0000001 21 | } 22 | if (e@ymin == e@ymax) { 23 | e@ymin <- e@ymin - 0.0000001 24 | e@ymax <- e@ymax + 0.0000001 25 | } 26 | if (show) { 27 | p <- rbind(c(e@xmin, e@ymin), c(e@xmin, e@ymax), c(e@xmax, e@ymax), c(e@xmax, e@ymin), c(e@xmin, e@ymin) ) 28 | lines(p, col=col) 29 | } 30 | return(e) 31 | } 32 | -------------------------------------------------------------------------------- /R/drawPoly.R: -------------------------------------------------------------------------------- 1 | # R function for the raster package 2 | # Author: Robert J. Hijmans 3 | # Date : January 2009 4 | # Version 0.9 5 | # Licence GPL v3 6 | 7 | 8 | drawPoly <- function(sp=TRUE, col='red', lwd=2, ...) { 9 | xy <- graphics::locator(n=10000, type="l", col=col, lwd=lwd, ...) 10 | xy <- cbind(xy$x, xy$y) 11 | xy <- rbind(xy, xy[1,]) 12 | lines(xy[(length(xy[,1])-1):length(xy[,1]),], col=col, lwd=lwd, ...) 13 | if (sp) { 14 | return( sp::SpatialPolygons(list(sp::Polygons(list(sp::Polygon(xy)), 1))) ) 15 | } else { 16 | return(xy) 17 | } 18 | } 19 | 20 | 21 | drawLine <- function(sp=TRUE, col='red', lwd=2, ...) { 22 | xy <- graphics::locator(n=10000, type="l", col=col, lwd=lwd, ...) 23 | xy <- cbind(xy$x, xy$y) 24 | if (sp) { 25 | return( sp::SpatialLines(list(sp::Lines(list(sp::Line(xy)), "1"))) ) 26 | } else { 27 | return(xy) 28 | } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /R/drivers.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : November 2008 3 | # Version 0.9 4 | # Licence GPL v3 5 | 6 | 7 | .nativeDrivers <- function() { 8 | return( c("raster", "SAGA", "IDRISI", "IDRISIold", "BIL", "BSQ", "BIP") ) 9 | } 10 | 11 | .nativeDriversLong <- function() { 12 | return( c("R-raster", "SAGA GIS", "IDRISI", "IDRISI (img/doc)", "Band by Line", "Band Sequential", "Band by Pixel") ) 13 | } 14 | 15 | 16 | .isNativeDriver <- function(d) { 17 | return( d %in% .nativeDrivers() ) 18 | } 19 | 20 | 21 | writeFormats <- function() { 22 | ## if ( .requireRgdal(FALSE) ) { 23 | gd <- .gdalWriteFormats() 24 | short <- c(.nativeDrivers(), 'ascii', 'CDF', as.vector(gd[,1])) 25 | long <- c(.nativeDriversLong(), 'Arc ASCII', 'NetCDF', as.vector(gd[,2])) 26 | # short <- c(.nativeDrivers(), 'ascii', 'CDF', 'big', as.vector(gd[,1])) 27 | # long <- c(.nativeDriversLong(), 'Arc ASCII', 'NetCDF', 'big.matrix', as.vector(gd[,2])) 28 | #} else { 29 | # short <- c(.nativeDrivers(), 'ascii', 'CDF', "") 30 | # long <- c(.nativeDriversLong(), "Arc ASCII", "NetCDF", "", "rgdal not installed") 31 | # short <- c(.nativeDrivers(), 'ascii', 'CDF', 'big', "") 32 | # long <- c(.nativeDriversLong(), "Arc ASCII", "NetCDF", "big.matrix", "", "rgdal not installed") 33 | #} 34 | 35 | m <- cbind(short, long) 36 | colnames(m) <- c("name", "long_name") 37 | return(m) 38 | } 39 | 40 | -------------------------------------------------------------------------------- /R/dropLayer.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : June 2008 3 | # Version 0.9 4 | # Licence GPL v3 5 | 6 | 7 | if (!isGeneric("dropLayer")) { 8 | setGeneric("dropLayer", function(x, i, ...) 9 | standardGeneric("dropLayer")) 10 | } 11 | 12 | 13 | ...nameToIndex <- function(name, allnames) { 14 | # this is the same as match, I think 15 | k = NULL 16 | for (i in 1:length(name)) { 17 | k = c(k, which(allnames == name[i])[1]) 18 | } 19 | return(k) 20 | } 21 | 22 | 23 | setMethod('dropLayer', signature(x='RasterStack'), 24 | function(x, i, ...) { 25 | if (is.character(i)) { 26 | i = match(i, names(x)) 27 | } 28 | i <- sort(unique(round(i))) 29 | i <- i[i > 0 & i <= nlayers(x)] 30 | if (length(i) > 0) { 31 | x@layers <- x@layers[-i] 32 | } 33 | return(x) 34 | } 35 | ) 36 | 37 | 38 | setMethod('dropLayer', signature(x='RasterBrick'), 39 | function(x, i, ...) { 40 | if (is.character(i)) { 41 | i <- match(i, names(x)) 42 | } 43 | i <- sort(unique(round(i))) 44 | 45 | nl <- nlayers(x) 46 | i <- i[i > 0 & i <= nl] 47 | if (length(i) < 1) { 48 | return(x) 49 | } else { 50 | sel <- which(! 1:nl %in% i ) 51 | if (length(sel) == 0) { 52 | return(brick(x, values=FALSE)) 53 | } else { 54 | return(subset(x, sel, ...)) 55 | } 56 | } 57 | } 58 | ) 59 | 60 | -------------------------------------------------------------------------------- /R/extentUnion.R: -------------------------------------------------------------------------------- 1 | # Authors: Robert J. Hijmans 2 | # Date : October 2008 3 | # Version 0.9 4 | # Licence GPL v3 5 | -------------------------------------------------------------------------------- /R/extract.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : October 2010 3 | # Version 1.0 4 | # Licence GPL v3 5 | 6 | setMethod('extract', signature(x='Raster', y='vector'), 7 | function(x, y, ...){ 8 | y <- round(y) 9 | return( .cellValues(x, y, ...) ) 10 | }) 11 | 12 | 13 | setMethod('extract', signature(x='Raster', y='sf'), 14 | function(x, y, ...){ 15 | y <- .sf2sp(y) 16 | #if (is.list(x)) {} 17 | extract(x, y, ...) 18 | } 19 | ) 20 | 21 | -------------------------------------------------------------------------------- /R/filler.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | .filler <- function(x, y, maxv=12, circular=FALSE) { 4 | 5 | # should rewrite this using apply (or C) 6 | 7 | fill <- function(x, y) { 8 | r <- matrix(NA, nrow=length(x), ncol=maxv) 9 | if (circular) { 10 | for (i in 1:nrow(r)) { 11 | if (!is.na(y[i])) { 12 | if (x[i] < y[i]) { 13 | r[i, x[i]:y[i]] <- 1 14 | } else { 15 | r[i, c(x[i]:maxv, 1:y[i])] <- 1 16 | } 17 | } 18 | } 19 | r 20 | } else { 21 | for (i in 1:nrow(r)) { 22 | if (!is.na(y[i])) { 23 | r[i, x[i]:y[i]] <- 1 24 | } 25 | } 26 | r 27 | } 28 | } 29 | x <- overlay(x, y, fun=fill) 30 | names(x) = paste('v', 1:maxv, sep='') 31 | x 32 | } 33 | 34 | 35 | -------------------------------------------------------------------------------- /R/fixDBFnames.R: -------------------------------------------------------------------------------- 1 | 2 | .fixDBFNames <- function(x, verbose=TRUE) { 3 | n <- gsub('^[[:space:]]+', '', gsub('[[:space:]]+$', '', x) ) 4 | nn <- n 5 | n <- gsub('[^[:alnum:]]', '_', n) 6 | n[nchar(n) > 10] <- gsub('_', '', n[nchar(n) > 10]) 7 | n[n==''] <- 'field' 8 | n <- gsub('^[^[:alpha:]]', 'X', n) 9 | n <- substr(n, 1, 10) 10 | 11 | # duplicate names 12 | nn <- as.matrix(table(n)) 13 | i <- which(nn > 1) 14 | if (! is.null(i)) { 15 | names <- rownames(nn)[i] 16 | n[n %in% names] <- substr(n[n %in% names], 1, 9) 17 | n <- make.unique(n, sep = "") 18 | } 19 | if (verbose) { 20 | i <- x == n 21 | if (! all(i)) { 22 | x <- rbind(x, n) 23 | colnames(x) <- paste('col_', 1:ncol(x), sep="") 24 | x <- x[, !i, drop=FALSE] 25 | rownames(x) = c('original name', 'adjusted name') 26 | print(x) 27 | } 28 | } 29 | return(n) 30 | } 31 | 32 | -------------------------------------------------------------------------------- /R/fourCellsFromXY.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : March 2009, August 2012 3 | # Licence GPL v3 4 | # updated November 2011 5 | # version 1.0 6 | 7 | 8 | fourCellsFromXY <- function(object, xy, duplicates=TRUE) { 9 | # if duplicates is TRUE, the same cell number can be returned 10 | # twice (if point in the middle of division between two cells) or 11 | # four times (if point in center of cell) 12 | r <- raster(object) # use small object 13 | stopifnot(is.matrix(xy)) 14 | return( .doFourCellsFromXY(r@ncols, r@nrows, xmin(r), xmax(r), ymin(r), ymax(r), xy, duplicates, .isGlobalLonLat(r))) 15 | } 16 | -------------------------------------------------------------------------------- /R/fullFileName.R: -------------------------------------------------------------------------------- 1 | # raster package 2 | # Author: Robert J. Hijmans 3 | # Date : September 2009 4 | # Version 0.9 5 | # Licence GPL v3 6 | 7 | # this function adds the working directory to a filename, if the filename has no path name 8 | # and, thus, presumably exists in the working directory. 9 | # Storing the full file name is to avoid that a filename becomes invalid if the working directory 10 | # changes during an R session 11 | 12 | .fullFilename <- function(x, expand=FALSE) { 13 | x <- trim(x) 14 | if (identical(basename(x), x)) { 15 | # exclude PG:xxx and perhaps others 16 | if (length(grep(":", x)) == 0) { 17 | x <- file.path(getwd(), x) 18 | } 19 | } 20 | if (expand) { 21 | x <- path.expand(x) 22 | } 23 | return(x) 24 | } 25 | -------------------------------------------------------------------------------- /R/gainoffset.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : September 2010 3 | # Version 1.0 4 | # Licence GPL v3 5 | 6 | 7 | 'gain<-' <- function(x, value) { 8 | value <- as.numeric(value[1]) 9 | if (inherits(x, 'RasterStack')) { 10 | x@layers <- lapply( x@layers, 11 | function(z) { 12 | if (fromDisk(z)) { 13 | z@data@gain <- value 14 | } else if (hasValues(z)) { 15 | z <- z * value 16 | } 17 | return(z) 18 | } 19 | ) 20 | } else { 21 | if (fromDisk(x)) { 22 | x@data@gain <- value 23 | } else if (hasValues(x)) { 24 | x <- x * value 25 | } 26 | } 27 | return(x) 28 | } 29 | 30 | 31 | gain <- function(x) { 32 | if (inherits(x, 'RasterStack')) { 33 | r <- sapply( x@layers, function(z) { z@data@gain } ) 34 | } else { 35 | r <- x@data@gain 36 | } 37 | return(r) 38 | } 39 | 40 | 41 | 'offs<-' <- function(x, value) { 42 | value <- as.numeric(value[1]) 43 | if (inherits(x, 'RasterStack')) { 44 | 45 | x@layers <- lapply( x@layers, 46 | function(z) { 47 | if (fromDisk(z)) { 48 | z@data@offset <- value 49 | } else if (hasValues(z)) { 50 | z <- z + value 51 | } 52 | return(z) 53 | } 54 | ) 55 | 56 | } else { 57 | if (fromDisk(x)) { 58 | x@data@offset <- value 59 | } else if (hasValues(x)) { 60 | x <- x + value 61 | } 62 | } 63 | return(x) 64 | } 65 | 66 | 67 | offs <- function(x) { 68 | if (inherits(x, 'RasterStack')) { 69 | r <- sapply( x@layers, function(z) { z@data@offset } ) 70 | } else { 71 | r <- x@data@offset 72 | } 73 | return(r) 74 | } 75 | 76 | -------------------------------------------------------------------------------- /R/gdal.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : September 2012 3 | # Version 1.0 4 | # Licence GPL v3 5 | 6 | 7 | # .requireRgdal <- function(stopIfAbsent=TRUE) { 8 | 9 | # y <- getOption('rasterGDALLoaded') 10 | 11 | # suppressWarnings(x <- isTRUE( try( requireNamespace("rgdal", quietly=TRUE ) ) )) 12 | 13 | # if (! isTRUE(y) ) { 14 | 15 | # if (x) { 16 | # #pkg.info <- utils::packageDescription('rgdal') 17 | # #test <- utils::compareVersion(pkg.info[["Version"]], "0.7-21") > 0 18 | # #if (!test) { 19 | # # stop('you use rgdal version: ', pkg.info[["Version"]], '\nYou need version 0.7-22 or higher') 20 | # #} 21 | # options('rasterGDALLoaded'=TRUE) 22 | # return(TRUE) 23 | 24 | # } else if (stopIfAbsent) { 25 | # stop("package 'rgdal' is not available") 26 | # } else { 27 | # return(FALSE) 28 | # } 29 | # } 30 | 31 | # return(TRUE) 32 | # } 33 | 34 | .useproj6 <- function() { 35 | TRUE 36 | } 37 | 38 | # .useproj6 <- function() { 39 | # pkg.info <- utils::packageDescription('rgdal') 40 | # new_rgdal <- utils::compareVersion(pkg.info[["Version"]], "1.5-7") > 0 41 | # if (new_rgdal) { 42 | # if (rgdal::new_proj_and_gdal()) { 43 | # return (TRUE) 44 | # } else { 45 | # return (FALSE) 46 | # } 47 | # } else { 48 | # return (FALSE) 49 | # } 50 | # } 51 | 52 | 53 | -------------------------------------------------------------------------------- /R/hdrBov.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : December 2009 3 | # Version 0.9 4 | # Licence GPL v3 5 | 6 | 7 | .writeHdrBOV <- function(raster) { 8 | hdrfile <- filename(raster) 9 | extension(hdrfile) <- '.bov' 10 | thefile <- file(hdrfile, "w") # open an txt file connectionis 11 | cat("TIME: 1.23456", "\n", file = thefile) 12 | datf <- filename(raster) 13 | extension(datf) <- '.gri' 14 | cat("DATA_FILE:", datf, "\n", file = thefile) 15 | cat("DATA_SIZE:", nrow(raster), ncol(raster), nlayers(raster), "\n", file = thefile) 16 | 17 | dtype <- substr(raster@file@datanotation, 1, 3) 18 | if (dtype == 'INT' | dtype == 'LOG' ) { 19 | pixtype <- "INT" 20 | } else { 21 | pixtype <- "FLOAT" 22 | } 23 | cat("DATA_FORMAT:", pixtype, "\n", file = thefile) 24 | cat("VARIABLE: ", basename(filename(raster)), "\n", file = thefile) 25 | cat("BYTEORDER ", toupper(.Platform$endian), "\n", file = thefile) 26 | cat("CENTERING: zonal", "\n", file = thefile) 27 | cat("BRICK_ORIGIN:", xmin(raster), ymin(raster), "0.", "\n", file = thefile) 28 | cat("BRICK_SIZE:", xres(raster), yres(raster), "1.", "\n", file = thefile) 29 | 30 | close(thefile) 31 | return(invisible(TRUE)) 32 | } 33 | -------------------------------------------------------------------------------- /R/hdrPRJ.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : April 2011 3 | # Version 1.0 4 | # Licence GPL v3 5 | 6 | 7 | .writeHdrPRJ <- function(x, ESRI=TRUE) { 8 | # if (.requireRgdal()) { 9 | 10 | p4s <- wkt(x) 11 | if (! inherits(p4s, "try-error")) { 12 | prjfile <- filename(x) 13 | extension(prjfile) <- '.prj' 14 | cat(p4s, file=prjfile) 15 | } else { 16 | return(FALSE) 17 | } 18 | return(invisible(TRUE)) 19 | # } else { 20 | # return(FALSE) 21 | # } 22 | } 23 | 24 | 25 | -------------------------------------------------------------------------------- /R/hdrWorldFile.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : October 2008 3 | # Version 0.9 4 | # Licence GPL v3 5 | 6 | 7 | .worldFile <- function(raster, extension=".wld") { 8 | hdrfile <- filename(raster) 9 | extension(hdrfile) <- extension 10 | thefile <- file(hdrfile, "w") 11 | cat(as.character(xres(raster)), "\n", file = thefile) 12 | cat("0\n", file = thefile) 13 | cat("0\n", file = thefile) 14 | cat(-1 * yres(raster), "\n", file = thefile) 15 | cat(xmin(raster) + 0.5 * xres(raster), "\n", file = thefile) 16 | cat(ymax(raster) - 0.5 * yres(raster), "\n", file = thefile) 17 | close(thefile) 18 | } 19 | -------------------------------------------------------------------------------- /R/hillShade.R: -------------------------------------------------------------------------------- 1 | # Author: Andrew Bevan, Oscar Perpinan Lamigueiro, and Robert J. Hijmans 2 | # Date : March 2010 3 | # Version 1.0 4 | # Licence GPL v3 5 | 6 | hillShade <- function(slope, aspect, angle=45, direction=0, filename='', normalize=FALSE, ...) { 7 | compareRaster(slope, aspect) 8 | 9 | direction <- direction * pi/180 10 | zenith <- (90 - angle)*pi/180 11 | 12 | #x <- cos(slope) * cos(declination) + sin(slope) * sin(declination) * cos(direction-aspect) 13 | if (normalize) { 14 | fun <- function(slp, asp) { 15 | shade <- cos(slp) * cos(zenith) + sin(slp) * sin(zenith) * cos(direction-asp) 16 | shade[shade < 0] <- 0 17 | shade * 255 18 | } 19 | } else { 20 | fun <- function(slp, asp) { cos(slp) * cos(zenith) + sin(slp) * sin(zenith) * cos(direction-asp) } 21 | } 22 | x <- overlay(slope, aspect, fun=fun, filename=filename, ...) 23 | return(x) 24 | } 25 | -------------------------------------------------------------------------------- /R/ifelse.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : May 2019 3 | # Version 1.0 4 | # Licence GPL v3 5 | 6 | #setMethod("ifel", signature(test="Raster", yes="ANY", no="ANY"), 7 | 8 | .ifel <- function(test, yes, no, filename="", ...) { 9 | if (!inherits(no, "Raster")) { 10 | stopifnot(is.numeric(no)) 11 | if (length(no) > 1) warning('only the first element of "no" is used') 12 | no <- reclassify(test, rbind(c(0,no[1]), c(1,NA))) 13 | } else { 14 | no <- mask(no, test, maskvalue=TRUE) 15 | } 16 | if (!inherits(yes, "Raster")) { 17 | stopifnot(is.numeric(yes)) 18 | if (length(yes) > 1) warning('only the first element of "yes" is used') 19 | yes <- reclassify(test, rbind(c(1,yes[1]), c(0,NA))) 20 | } 21 | cover(no, yes, filename=filename) 22 | } 23 | #) 24 | -------------------------------------------------------------------------------- /R/image.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : April 2009 3 | # Version 0.9 4 | # Licence GPL v3 5 | 6 | 7 | setMethod("image", signature(x='RasterLayer'), 8 | function(x, maxpixels=500000, useRaster=TRUE, ...) { 9 | # coltab <- x@legend@colortable 10 | # if (is.null(coltab) | length(coltab) == 0 | is.null(list(...)$col)) { 11 | # colortab <- FALSE 12 | # } 13 | # if (missing(main)) { main <- names(x) } 14 | 15 | x <- sampleRegular(x, maxpixels, asRaster=TRUE, useGDAL=TRUE) 16 | y <- yFromRow(x, nrow(x):1) 17 | # drop=F fix by Daniel Schlaepfer for single row image 18 | value <- t(as.matrix(x)[nrow(x):1, ,drop=FALSE]) 19 | x <- xFromCol(x,1:ncol(x)) 20 | # if (colortab) { 21 | # image(x=x, y=y, z=value, col=coltab[value], useRaster=useRaster, ...) 22 | # } else { 23 | 24 | image(x=x, y=y, z=value, useRaster=useRaster, ...) 25 | # } 26 | } 27 | ) 28 | 29 | 30 | setMethod("image", signature(x='RasterStackBrick'), 31 | function(x, y=1, maxpixels=100000, useRaster=TRUE, main, ...) { 32 | y <- round(y) 33 | stopifnot(y > 0 & y <= nlayers(x)) 34 | x <- raster(x, y) 35 | if (missing(main)) { 36 | main <- names(x) 37 | } 38 | image(x, maxpixels=maxpixels, useRaster=useRaster, main=main, ...) 39 | } 40 | ) 41 | 42 | -------------------------------------------------------------------------------- /R/intersect.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : December 2011 3 | # Version 1.0 4 | # Licence GPL v3 5 | 6 | 7 | setMethod('intersect', signature(x='Raster', y='ANY'), 8 | function(x, y) { 9 | y <- extent(y) 10 | crop(x, y) 11 | } ) 12 | 13 | 14 | 15 | setMethod('intersect', signature(x='Extent', y='ANY'), 16 | function(x, y) { 17 | 18 | y <- extent(y) 19 | 20 | x@xmin <- max(x@xmin, y@xmin) 21 | x@xmax <- min(x@xmax, y@xmax) 22 | x@ymin <- max(x@ymin, y@ymin) 23 | x@ymax <- min(x@ymax, y@ymax) 24 | 25 | if ((x@xmax <= x@xmin) | (x@ymax <= x@ymin) ) { 26 | #warning('Objects do not overlap') 27 | return(NULL) 28 | } 29 | return(x) 30 | } ) 31 | 32 | 33 | 34 | 35 | .intersectExtent <- function(x, ..., validate=TRUE) { 36 | objects <- c(x, list(...)) 37 | if (length(objects) == 1) { 38 | return(extent(x)) 39 | } 40 | e <- extent(objects[[1]]) 41 | for (i in 2:length(objects)) { 42 | e2 <- extent(objects[[i]]) 43 | e@xmin <- max(e@xmin, e2@xmin) 44 | e@xmax <- min(e@xmax, e2@xmax) 45 | e@ymin <- max(e@ymin, e2@ymin) 46 | e@ymax <- min(e@ymax, e2@ymax) 47 | } 48 | if ((e@xmax <= e@xmin) | (e@ymax <= e@ymin) ) { 49 | if (validate) { 50 | stop('Objects do not intersect') 51 | } else { 52 | return(NULL) 53 | } 54 | } 55 | return(e) 56 | } 57 | 58 | 59 | -------------------------------------------------------------------------------- /R/kernelDens.R: -------------------------------------------------------------------------------- 1 | 2 | ### this is the kde2d function from the MASS packlage with minimal changes 3 | .kde2d <- function (x, y, h, n, lims) { 4 | nx <- length(x) 5 | gx <- seq.int(lims[1L], lims[2L], length.out = n[1L]) 6 | gy <- seq.int(lims[3L], lims[4L], length.out = n[2L]) 7 | h <- h/4 8 | ax <- outer(gx, x, "-")/h[1L] 9 | ay <- outer(gy, y, "-")/h[2L] 10 | tcrossprod(matrix(stats::dnorm(ax), , nx), matrix(stats::dnorm(ay), , nx))/(nx * h[1L] * h[2L]) 11 | } 12 | 13 | 14 | 15 | .kernelDens <- function(p, x, bandwidth, ...) { 16 | 17 | .bandwidth.nrd <- function(x) { 18 | ### this function is from the MASS package 19 | r <- stats::quantile(x, c(0.25, 0.75)) 20 | h <- (r[2L] - r[1L])/1.34 21 | 4 * 1.06 * min(sqrt(stats::var(x)), h) * length(x)^(-1/5) 22 | } 23 | 24 | if(missing(bandwidth)) { 25 | bw <- c(.bandwidth.nrd(p[,1]), .bandwidth.nrd(p[,2])) 26 | } else { 27 | bw <- rep(bandwidth, length.out = 2L) 28 | } 29 | v <- .kde2d(p[,1], p[,2], bw, dim(x)[1:2], as.vector(t(bbox(x)))) 30 | v <- t(v) 31 | v <- v[nrow(v):1, ] 32 | setValues(x, v) 33 | } 34 | 35 | #a = kernelDens(xy, r) 36 | 37 | -------------------------------------------------------------------------------- /R/kernelDensity.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : June 2016 3 | # Version 0.1 4 | # Licence GPL v3 5 | 6 | 7 | .kernelDensity <- function(xy, r, bandwidth) { 8 | requireNamespace("MASS") 9 | lims <- as.vector(extent(r)) + rep(res(r), each=2) * c(0.5,-0.5) 10 | n <- rev(dim(r)[1:2]) 11 | xy <- .pointsToMatrix(xy) 12 | k <- raster( MASS::kde2d(xy[,1], xy[,2], h=bandwidth, n=n, lims=lims) ) 13 | # to avoid possible small changes due to floating point math and to transfer crs 14 | setValues(r, getValues(k)) 15 | } 16 | 17 | -------------------------------------------------------------------------------- /R/labels.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | .polygonLabelPosition <- function(x, cex=1) { 4 | xy <- sp::coordinates(x) 5 | # make sure that labels are inside of polygons 6 | sx <- sp::geometry(x) 7 | k <- extract(sx, xy) 8 | k <- which(k[,1] != k[,2]) 9 | if (length(k) > 0) { 10 | for (i in k) { 11 | pol <- sx[i, ] 12 | e <- extent(pol) 13 | p1 <- xy[i, ,drop=FALSE] 14 | dx <- 0.25 * (e@xmax - e@xmin) 15 | dy <- 0.25 * (e@ymax - e@ymin) 16 | fixed <- FALSE 17 | for (j in 1:4) { 18 | if (j < 3) { 19 | p[1,1] <- p1[1,1] - dx 20 | } else { 21 | p[1,1] <- p1[1,1] + dx 22 | } 23 | if (j %in% c(2,3)) { 24 | p[1,2] <- p1[1,2] - dy 25 | } else { 26 | p[1,2] <- p1[1,2] + dy 27 | } 28 | z <- extract(pol, rbind(p,p)) 29 | if (!is.na(z[1,2])) { 30 | xy[i, ] <- p 31 | break 32 | fixed <- TRUE 33 | } 34 | } 35 | if (!fixed) print(paste(i, 'not fixed')) 36 | } 37 | } 38 | 39 | # make sure that labels do not overlap? 40 | xy 41 | } 42 | 43 | 44 | -------------------------------------------------------------------------------- /R/localFun.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : February 2014 3 | # Version 1.0 4 | # Licence GPL v3 5 | 6 | 7 | if ( !isGeneric("localFun") ) { 8 | setGeneric("localFun", function(x, y, ...) 9 | standardGeneric("localFun")) 10 | } 11 | 12 | 13 | setMethod('localFun', signature(x='RasterLayer', y='RasterLayer'), 14 | function(x, y, ngb=5, fun, filename='', ...) { 15 | 16 | compareRaster(x,y) 17 | out <- raster(x) 18 | nc1 <- 1:(ngb*ngb) 19 | nc2 <- ((ngb*ngb)+1):(2*(ngb*ngb)) 20 | 21 | if (canProcessInMemory(x, n=2*ngb)) { 22 | vx <- getValuesFocal(x, 1, nrow(x), ngb=ngb) 23 | vy <- getValuesFocal(y, 1, nrow(y), ngb=ngb) 24 | values(out) <- apply(cbind(vx, vy), 1, function(x, ...) fun(x[nc1], x[nc2], ...)) 25 | if (filename != '') { 26 | out <- writeRaster(out, filename, ...) 27 | } 28 | return(out) 29 | 30 | } else { 31 | tr <- blockSize(out) 32 | pb <- pbCreate(tr$n, label='localFun', ...) 33 | out <- writeStart(out, filename=filename, ...) 34 | for (i in 1:tr$n) { 35 | vx <- getValuesFocal(x, tr$row[i], tr$nrows[i], ngb=ngb) 36 | vy <- getValuesFocal(y, tr$row[i], tr$nrows[i], ngb=ngb) 37 | v <- apply(cbind(vx, vy), 1, function(x, ...) fun(x[nc1], x[nc2], ...)) 38 | out <- writeValues(out, v, tr$row[i]) 39 | } 40 | return(writeStop(out)) 41 | } 42 | } 43 | ) 44 | 45 | -------------------------------------------------------------------------------- /R/makeTiles.R: -------------------------------------------------------------------------------- 1 | 2 | .makeTiles <- function(x, y, filename="", ...) { 3 | res <- res(y) 4 | xy <- xyFromCell(y, 1:ncell(y)) 5 | xy1 <- xy - 0.5 * res 6 | xy2 <- xy + 0.5 * res 7 | tiles <- list() 8 | if (length(filename) > 1) { 9 | stopifnot(length(filename) == ncell(y)) 10 | } else if (filename != '') { 11 | ext <- extension(filename) 12 | extension(filename) <- '' 13 | filename <- paste0(filename, '_', 1:ncell(y), ext) 14 | } else if (!canProcessInMemory(x)) { 15 | filename <- rasterTmpFile() 16 | ext <- extension(filename) 17 | extension(filename) <- '' 18 | filename <- paste0(filename, '_', 1:ncell(y), ext) 19 | } else { 20 | filename <- rep("", ncell(y)) 21 | } 22 | 23 | for (i in 1:ncell(y)) { 24 | e <- extent(xy1[i,1], xy2[i,1], xy1[i,2], xy2[i,2]) 25 | tiles[[i]] <- crop(x, e, filename=filename[i], ...) 26 | } 27 | tiles 28 | } 29 | 30 | -------------------------------------------------------------------------------- /R/match.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : October 2011 3 | # October 2011 4 | # version 1 5 | # Licence GPL v3 6 | 7 | 8 | 9 | setMethod("%in%", signature(x='Raster', table='ANY'), 10 | function(x, table) { 11 | calc(x, function(x) x %in% table) 12 | } 13 | ) 14 | 15 | if (!isGeneric("match")) { 16 | setGeneric("match", function(x, table, nomatch=NA_integer_, incomparables=NULL) 17 | standardGeneric("match")) 18 | } 19 | 20 | 21 | setMethod("match", signature(x='Raster', table='ANY', nomatch='ANY', incomparables='ANY'), 22 | function(x, table, nomatch, incomparables) { 23 | calc(x, function(x) match(x, table, nomatch, incomparables)) 24 | } 25 | ) 26 | 27 | -------------------------------------------------------------------------------- /R/maxDataType.R: -------------------------------------------------------------------------------- 1 | 2 | .maxDatatype <- function(x) { 3 | x <- sort(x) 4 | x <- x[substr(x, 1, 3)== substr(x[1], 1, 3)] 5 | size <- max(as.integer(substr(x, 4, 4))) 6 | if (substr(x[1], 1, 3) == 'FLT') { 7 | return( paste('FLT', size, 'S', sep="") ) 8 | } else { 9 | # need to do better than this 10 | return( 'INT4S' ) 11 | } 12 | } -------------------------------------------------------------------------------- /R/metadata.R: -------------------------------------------------------------------------------- 1 | 2 | setMethod('metadata', signature(x='Raster'), 3 | function(x) { 4 | x@history 5 | } 6 | ) 7 | 8 | 'metadata<-' <- function(x, value) { 9 | stopifnot(is.list(value)) 10 | if (is.data.frame(values)) { 11 | values <- as.list(values) 12 | } 13 | if ( any(unlist(sapply(value, function(x)sapply(x, is.list)))) ) { 14 | stop('invalid metadata: list is nested too deeply') 15 | } 16 | nms <- c(names(value), unlist(sapply(value, names))) 17 | if (is.null(names) | any(nms == '')) { 18 | stop('invalid metadata: list elements without names') 19 | } 20 | if (any(unlist(sapply(value, is.data.frame)) )) { 21 | stop('invalid metadata: data.frames are not allowed') 22 | } 23 | type <- rapply(value, class) 24 | if (any(type == 'matrix')) { 25 | stop('invalid metadata: matrices are not allowed') 26 | } 27 | x@history <- value 28 | x 29 | } 30 | 31 | -------------------------------------------------------------------------------- /R/modalRaster.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : October 2008 3 | # revised: October 2011 4 | # Version 1.0 5 | # Licence GPL v3 6 | 7 | 8 | setMethod("modal", signature(x='Raster'), 9 | function(x, ..., ties='random', na.rm=FALSE, freq=FALSE){ 10 | 11 | dots <- list(...) 12 | if (length(dots) > 0) { 13 | x <- stack(.makeRasterList(x, ...)) 14 | add <- .addArgs(...) 15 | } else { 16 | add <- NULL 17 | } 18 | 19 | nl <- nlayers(x) 20 | if (nl < 2) { 21 | warning('there is not much point in computing a modal value for a single layer') 22 | return(x[[1]]) 23 | } else if (nl == 2) { 24 | warning('running modal with only two layers!') 25 | } 26 | 27 | out <- raster(x) 28 | 29 | if (canProcessInMemory(x)) { 30 | x <- cbind(getValues(x), add) 31 | x <- setValues(out, apply(x, 1, modal, ties=ties, na.rm=na.rm, freq=freq)) 32 | return(x) 33 | } 34 | 35 | tr <- blockSize(out) 36 | pb <- pbCreate(tr$n, label='modal') 37 | out <- writeStart(out, filename="") 38 | for (i in 1:tr$n) { 39 | v <- cbind( getValues( x, row=tr$row[i], nrows=tr$nrows[i] ), add) 40 | v <- apply(v, 1, modal, ties=ties, na.rm=na.rm, freq=freq) 41 | out <- writeValues(out, v, tr$row[i]) 42 | pbStep(pb, i) 43 | } 44 | pbClose(pb) 45 | writeStop(out) 46 | } 47 | ) 48 | 49 | -------------------------------------------------------------------------------- /R/movingFun.R: -------------------------------------------------------------------------------- 1 | # Author: Robert Hijmans 2 | # November 2009 3 | # License GPL3 4 | # First versions were based on the rollFun function implemented by Diethelm Wuertz in the 5 | # fTrading package # Version: 2100.76 # Published: 2009-09-29 6 | 7 | 8 | movingFun <- function(x, n, fun=mean, type='around', circular=FALSE, na.rm=FALSE) { 9 | n <- round(abs(n)) 10 | if (n == 0) { stop('n == 0') } 11 | x = as.vector(x) 12 | lng <- length(x) 13 | if (type == 'around') { 14 | hn <- floor(n/2) 15 | if (circular) { x <- c(x[(lng-hn+1):lng], x, x[1:hn]) 16 | } else { x <- c(rep(NA, hn), x, rep(NA, hn)) } 17 | } else if (type == 'to') { 18 | if (circular) { x <- c(x[(lng-n+2):lng], x) 19 | } else { x <- c(rep(NA, n-1), x) } 20 | } else if (type == 'from') { 21 | if (circular) { x <- c(x, x[1:n]) 22 | } else { x <- c(x, rep(NA, n)) } 23 | } else { 24 | stop('unknown type; should be "around", "to", or "from"') 25 | } 26 | m <- matrix(ncol=n, nrow=lng) 27 | for (i in 1:n) { m[,i] <- x[i:(lng+i-1)] } 28 | apply(m, MARGIN=1, FUN=fun, na.rm=na.rm) 29 | } 30 | 31 | 32 | .roll <- function(x, n) { 33 | # by Josh O'Brien 34 | x[(seq_along(x) - (n+1)) %% length(x) + 1] 35 | } 36 | 37 | -------------------------------------------------------------------------------- /R/naValue.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : June 2008 3 | # Version 1.0 4 | # Licence GPL v3 5 | 6 | .naChanged <- function(x) { 7 | if (.hasSlot(x@file, 'NAchanged')) { 8 | return(x@file@NAchanged) 9 | } else { 10 | return(TRUE) 11 | } 12 | } 13 | 14 | 'NAvalue<-' <- function(x, value) { 15 | if (inherits(x, 'RasterStack')) { 16 | nl <- nlayers(x) 17 | if (length(value) == 1) { 18 | value <- rep(value[[1]], nl) 19 | } else { 20 | v <- vector(length=nl) 21 | v[] <- as.vector(value) 22 | value <- v 23 | } 24 | for (i in 1:nl) { 25 | x@layers[[i]]@file@nodatavalue <- value[i] 26 | x@layers[[i]]@file@NAchanged <- TRUE 27 | } 28 | } else { 29 | x@file@nodatavalue <- value[[1]] 30 | x@file@NAchanged <- TRUE 31 | } 32 | return(x) 33 | } 34 | 35 | NAvalue <- function(x) { 36 | if (inherits(x, 'RasterStack')) { 37 | sapply(x@layers, function(x) { x@file@nodatavalue }) 38 | } else { 39 | return(x@file@nodatavalue) 40 | } 41 | } 42 | 43 | -------------------------------------------------------------------------------- /R/ncell.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : April 2009 3 | # Version 0.9 4 | # Licence GPL v3 5 | 6 | 7 | 8 | setMethod('ncell', signature(x='BasicRaster'), 9 | function(x) { 10 | return(as.numeric(x@ncols) * x@nrows) 11 | } 12 | ) 13 | 14 | 15 | setMethod('ncell', signature(x='ANY'), 16 | function(x) { 17 | NROW(x) * NCOL(x) 18 | } 19 | ) 20 | 21 | 22 | 23 | setMethod('length', signature(x='BasicRaster'), 24 | function(x) { 25 | ncell(x) * nlayers(x) 26 | } 27 | ) 28 | 29 | -------------------------------------------------------------------------------- /R/netCDFtoRasterGMT.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date: March 2013 3 | # Version 1.0 4 | # Licence GPL v3 5 | 6 | .rasterObjectFromCDF_GMT <- function(nc) { 7 | 8 | stopifnot(requireNamespace("ncdf4")) 9 | 10 | dims <- ncdf4::ncvar_get(nc, "dimension", 1) 11 | xr <- ncdf4::ncvar_get(nc, "x_range", 1) 12 | yr <- ncdf4::ncvar_get(nc, "y_range", 1) 13 | zr <- ncdf4::ncvar_get(nc, "z_range", 1) 14 | sp <- ncdf4::ncvar_get(nc, "spacing", 1) 15 | 16 | zvar = 'z' 17 | crs <- NA 18 | if (xr[1] > -181 & xr[2] < 181 & yr[1] > -91 & yr[2] < 91 ) { 19 | crs <- "+proj=longlat +datum=WGS84" 20 | } 21 | 22 | dif1 <- abs(((xr[2] - xr[1]) / dims[1]) - sp[2]) 23 | dif2 <- abs(((xr[2] - xr[1]) / (dims[1]-1)) - sp[2]) 24 | 25 | if (dif1 < dif2) { # 30 sec GEBCO data 26 | r <- raster(xmn=xr[1], xmx=xr[2], ymn=yr[1], ymx=yr[2], ncol=dims[1], nrow=dims[2], crs=crs) 27 | } else { # 1 min data 28 | resx <- (xr[2] - xr[1]) / (dims[1]-1) 29 | resy <- (yr[2] - yr[1]) / (dims[2]-1) 30 | r <- raster(xmn=xr[1]-(0.5*resx), xmx=xr[2]+(0.5*resx), ymn=yr[1]-(0.5*resy), ymx=yr[2]+(0.5*resy), ncol=dims[1], nrow=dims[2], crs=crs) 31 | } 32 | 33 | r@file@name <- nc$filename 34 | r@file@toptobottom <- FALSE 35 | attr(r@data, "zvar") <- zvar 36 | attr(r@data, "dim3") <- 1 37 | r@file@driver <- "netcdf" 38 | r@data@fromdisk <- TRUE 39 | return(r) 40 | } 41 | 42 | -------------------------------------------------------------------------------- /R/nlayers.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : October 2008 3 | # Version 1.0 4 | # Licence GPL v3 5 | 6 | 7 | if (!isGeneric("nlayers")) { 8 | setGeneric("nlayers", function(x) 9 | standardGeneric("nlayers")) 10 | } 11 | 12 | setMethod('nlayers', signature(x='BasicRaster'), 13 | function(x){ 14 | return(0) 15 | } 16 | ) 17 | 18 | setMethod('nlayers', signature(x='Raster'), 19 | function(x){ 20 | return(1) 21 | } 22 | ) 23 | 24 | setMethod('nlayers', signature(x='RasterStack'), 25 | function(x){ 26 | as.integer( sum(unlist( sapply(x@layers, nlayers) ) ) ) 27 | } 28 | ) 29 | 30 | setMethod('nlayers', signature(x='RasterBrick'), 31 | function(x){ 32 | return(x@data@nlayers) 33 | } 34 | ) 35 | 36 | setMethod('nlayers', signature(x='Spatial'), 37 | function(x){ 38 | if (! is.null( attr(x, 'data') ) ) { 39 | return( dim(x@data)[2] ) 40 | } else { 41 | return( 0 ) 42 | } 43 | } 44 | ) 45 | 46 | -------------------------------------------------------------------------------- /R/notused.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : June 2008 3 | # Version 0.9 4 | # Licence GPL v3 5 | 6 | 7 | # Not used 8 | .writeRasterAssign <- function(x, filename, ...) { 9 | name <- deparse(substitute(x)) 10 | x <- writeRaster(x, filename, ...) 11 | assign(name, x, envir=parent.frame()) 12 | return(invisible()) 13 | } 14 | 15 | 16 | .writeSparse <- function(raster, filename, overwrite=FALSE) { 17 | 18 | # raster@file@driver <- 'raster' 19 | if (!overwrite & file.exists(filename)) { 20 | stop(filename, "exists. Use 'overwrite=TRUE' if you want to overwrite it") 21 | } 22 | 23 | raster@data@values[is.nan(raster@data@values)] <- NA 24 | 25 | dtype <- .shortDataType(raster@data@datanotation) 26 | if (dtype == "integer") { 27 | raster@data@values <- as.integer(raster@data@values) 28 | } 29 | if (inherits(raster@data@values, 'integer')) { 30 | dataType(raster) <- 'INT4S' 31 | } 32 | raster <- setMinMax(raster) 33 | 34 | binraster <- .setFileExtensionValues(raster@file@name, 'raster') 35 | 36 | raster <- readStart(raster) 37 | writeBin( as.vector(raster@data@indices), raster@file@con, size = as.integer(4)) 38 | writeBin( as.vector(raster@data@values), raster@file@con, size = dataSize(raster@file@datanotation) ) 39 | raster <- readStop(raster) 40 | 41 | # add the 'sparse' key word to the hdr file!!! 42 | hdr(raster) 43 | return(raster) 44 | } 45 | 46 | -------------------------------------------------------------------------------- /R/origin.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : October 2008 3 | # Version 0.9 4 | # Licence GPL v3 5 | 6 | 7 | 8 | setMethod('origin', signature(x='BasicRaster'), 9 | function(x, ...) { 10 | e <- x@extent 11 | r <- res(x) 12 | x <- e@xmin - r[1]*(round(e@xmin / r[1])) 13 | y <- e@ymax - r[2]*(round(e@ymax / r[2])) 14 | 15 | if (isTRUE(all.equal((r[1] + x), abs(x)))) { 16 | x <- abs(x) 17 | } 18 | if (isTRUE(all.equal((r[2] + y), abs(y)))) { 19 | y <- abs(y) 20 | } 21 | return(c(x, y)) 22 | } 23 | ) 24 | 25 | 26 | 27 | setMethod("origin<-", signature('BasicRaster'), 28 | function(x, value) { 29 | value <- rep(value, length.out=2) 30 | dif <- value - origin(x) 31 | res <- res(x) 32 | dif[1] <- dif[1] %% res[1] 33 | dif[2] <- dif[2] %% res[2] 34 | for (i in 1:2) { 35 | if (dif[i] < 0) { 36 | if ((dif[i] + res[i]) < abs(dif[i])) { 37 | dif[i] <- dif[i] + res[i] 38 | } 39 | } else { 40 | if (abs(dif[i] - res[i]) < dif[i]) { 41 | dif[i] <- dif[i] - res[i] 42 | } 43 | } 44 | } 45 | e <- extent(x) 46 | e@xmin <- e@xmin + dif[1] 47 | e@xmax <- e@xmax + dif[1] 48 | e@ymin <- e@ymin + dif[2] 49 | e@ymax <- e@ymax + dif[2] 50 | x@extent <- e 51 | return(x) 52 | } 53 | ) 54 | 55 | -------------------------------------------------------------------------------- /R/pairs.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | setMethod('pairs', signature(x='RasterStackBrick'), 5 | function(x, hist=TRUE, cor=TRUE, use="pairwise.complete.obs", maxpixels=100000, ...) { 6 | 7 | panelhist <- function(x,...) { 8 | usr <- graphics::par("usr"); on.exit(graphics::par(usr)) 9 | graphics::par(usr = c(usr[1:2], 0, 1.5) ) 10 | h <- hist(x, plot = FALSE) 11 | breaks <- h$breaks 12 | nB <- length(breaks) 13 | y <- h$counts 14 | y <- y/max(y) 15 | graphics::rect(breaks[-nB], 0, breaks[-1], y, col="green") 16 | } 17 | 18 | panelcor <- function(x, y,...) { 19 | usr <- graphics::par("usr") 20 | on.exit(graphics::par(usr)) 21 | graphics::par(usr = c(0, 1, 0, 1)) 22 | r <- abs(stats::cor(x, y, use=use)) 23 | txt <- format(c(r, 0.123456789), digits=2)[1] 24 | text(0.5, 0.5, txt, cex = max(0.5, r * 2)) 25 | } 26 | 27 | if (hist) {dp <- panelhist} else {dp <- NULL} 28 | if (cor) {up <- panelcor} else {up <- NULL} 29 | 30 | 31 | d <- sampleRegular(x, maxpixels) 32 | 33 | dots <- list(...) 34 | cex <- dots$cex 35 | main <- dots$main 36 | if (is.null(cex)) cex <- 0.5 37 | if (is.null(main)) main <- '' 38 | 39 | pairs(d, main=main, cex=cex, upper.panel=up, diag.panel=dp) 40 | } 41 | ) 42 | 43 | -------------------------------------------------------------------------------- /R/persp.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : April 2009 3 | # Version 0.9 4 | # Licence GPL v3 5 | 6 | 7 | setMethod("persp", signature(x='RasterLayer'), 8 | function(x, maxpixels=100000, ext=NULL, ...) { 9 | x <- sampleRegular(x, size=maxpixels, ext=ext, asRaster=TRUE, useGDAL=TRUE) 10 | value <- t((getValues(x, format='matrix'))[nrow(x):1,]) 11 | y <- yFromRow(x, nrow(x):1) 12 | x <- xFromCol(x,1:ncol(x)) 13 | persp(x=x, y=y, z=value, ...) 14 | } 15 | ) 16 | 17 | setMethod("persp", signature(x='RasterStackBrick'), 18 | function(x, y=1, maxpixels=10000, ext=NULL, ...) { 19 | if (y < 1) { y <- 1 } 20 | if (y > nlayers(x)) { y <- nlayers(x) } 21 | x <- raster(x, y) 22 | persp(x=x, maxpixels=maxpixels, ext=ext, ...) 23 | } 24 | ) 25 | 26 | -------------------------------------------------------------------------------- /R/plotExent.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : January 2009 3 | # Version 1.0 4 | # Licence GPL v3 5 | 6 | 7 | .extentMatrix <- function(x) { 8 | xy <- matrix(NA, nrow=5, ncol=2) 9 | xy[c(1,4),1] <- x@xmin 10 | xy[2:3,1] <- x@xmax 11 | xy[1:2,2] <- x@ymax 12 | xy[3:4,2] <- x@ymin 13 | xy[5,] <- xy[1,] 14 | return(xy) 15 | } 16 | 17 | 18 | setMethod("plot", signature(x='Extent', y='missing'), 19 | function(x, y, type='l', add=FALSE, ...) { 20 | xy <- .extentMatrix(x) 21 | if (add) { 22 | lines(xy, ...) 23 | } else { 24 | plot(xy, type=type, ...) 25 | } 26 | } 27 | ) 28 | 29 | -------------------------------------------------------------------------------- /R/properties.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : October 2008 3 | # Version 0.9 4 | # Licence GPL v3 5 | 6 | 7 | .driver <- function(object, warn=TRUE) { 8 | if (inherits(object, 'RasterStack')) { 9 | d <- sapply(object@layers, function(x) x@file@driver) 10 | if (any(d == '' & warn)) { 11 | warning('There is no driver associated with one or more layers of this RasterStack') 12 | } 13 | } else { 14 | d <- object@file@driver 15 | if (d == '' & warn) { 16 | warning('no file/driver associated with this Raster object') 17 | } 18 | } 19 | return(d) 20 | } 21 | 22 | 23 | 24 | .nodatavalue <- function(object) { 25 | if (inherits(object, 'RasterStack')) { 26 | return( sapply(object@layers, function(x) x@file@nodatavalue) ) 27 | } 28 | return(object@file@nodatavalue) 29 | } 30 | 31 | 32 | filename <- function(x) { 33 | if (inherits(x, 'RasterStack')) { 34 | return(x@filename) 35 | } 36 | return(x@file@name) 37 | } 38 | 39 | 40 | 41 | 42 | # fileext <- toupper(extension(fn)) 43 | # if ( fileext == ".GRD" | fileext == ".GRI" ) { 44 | # return('raster') 45 | # } else { 46 | # return('gdal') 47 | # } 48 | 49 | # fcon <- class(try( object@file@con, silent = T ))[1] 50 | # if (fcon == 'file') { 51 | # return('raster') 52 | # } else if (fcon == "GDALReadOnlyDataset") { 53 | # return('gdal') 54 | # } else if (fcon == "try-error") { 55 | # return('NA') 56 | # } else { 57 | # stop('unknown driver') 58 | # } 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /R/quad.R: -------------------------------------------------------------------------------- 1 | 2 | # if (!isGeneric(".quad")) { 3 | # setGeneric(".quad", function(x, ...) 4 | # standardGeneric(".quad")) 5 | # } 6 | 7 | 8 | 9 | # setMethod('.quad', signature(x='missing'), 10 | # function(nrows=180, ncols=360, xmn=-180, xmx=180, ymn=-90, ymx=90, levels=1, steps=1, crs) { 11 | # e <- extent(xmn, xmx, ymn, ymx) 12 | # if (missing(crs)) { 13 | # if (e@xmin > -400 & e@xmax < 400 & e@ymin > -90.1 & e@ymax < 90.1) { 14 | # crs <- "+proj=longlat +datum=WGS84" 15 | # } else { 16 | # crs <- "" 17 | # } 18 | # } 19 | # b <- .quad(e, nrows=nrows, ncols=ncols, crs=crs, levels=levels, steps=steps) 20 | # return(b) 21 | # } 22 | # ) 23 | 24 | 25 | # setMethod('.quad', signature(x='Extent'), 26 | # function(x, nrows=10, ncols=10, levels=1, steps=1, crs='') { 27 | # bb <- extent(x) 28 | # nr = as.integer(round(nrows)) 29 | # nc = as.integer(round(ncols)) 30 | # if (nc < 1) { stop("ncols should be > 0") } 31 | # if (nr < 1) { stop("nrows should be > 0") } 32 | # b <- methods::new("RasterQuadBrick", extent=bb, ncols=nc, nrows=nr) 33 | # projection(b) <- sp::CRS 34 | # levels <- as.integer(max(round(levels), 0)) 35 | # steps <- as.integer(max(round(steps), 0)) 36 | # nl <- levels * steps 37 | # b@nlevels <- levels 38 | # b@nsteps <- steps 39 | # b@data@nlayers <- as.integer(nl) 40 | # return(b) 41 | # } 42 | # ) 43 | 44 | -------------------------------------------------------------------------------- /R/quantile.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # r.hijmans@gmail.com 3 | # Date : October 2008 4 | # Licence GPL v3 5 | 6 | 7 | setMethod('quantile', signature(x='Raster'), 8 | function(x, ..., na.rm=TRUE, ncells=NULL) { 9 | if (is.null(ncells)) { 10 | v <- try ( getValues(x) ) 11 | if (inherits(v, "try-error")) { 12 | stop('raster too large. You can sample it with argument "ncells"') 13 | } 14 | } else { 15 | if (ncells >= ncell(x)) { 16 | v <- try ( getValues(x) ) 17 | } else { 18 | v <- try ( sampleRandom(x, ncells) ) 19 | } 20 | if (inherits(v, "try-error")) { 21 | stop('ncells too large') 22 | } 23 | } 24 | #if (na.rm) { 25 | # v <- stats::na.omit(v) 26 | #} 27 | if (nlayers(x)==1) { 28 | return(quantile(v, ..., na.rm=na.rm)) 29 | } else { 30 | # t(apply(v, 2, quantile, na.rm=TRUE)) 31 | 32 | q <- stats::quantile(v[,1], ..., na.rm=na.rm) 33 | for (i in 2:nlayers(x)) { 34 | q <- rbind(q, stats::quantile(v[,i], ..., na.rm=na.rm)) 35 | } 36 | rownames(q) <- names(x) 37 | return(q) 38 | } 39 | } 40 | ) 41 | 42 | -------------------------------------------------------------------------------- /R/randomize.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | .randomize <- function(x, ...) { 4 | if (!hasValues(x)) { 5 | return(x) 6 | } 7 | nl <- nlayers(x) 8 | if (nl > 1) { 9 | y <- brick(x, values=FALSE) 10 | for (i in 1:nl) { 11 | y <- setValues(y, sample(getValues(x[[i]])), layer=i) 12 | } 13 | y 14 | } else { 15 | setValues(x, sample(getValues(x))) 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /R/rasterFromCells.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : April 2009 3 | # Version 0.9 4 | # Licence GPL v3 5 | 6 | 7 | rasterFromCells <- function(x, cells, values=TRUE) { 8 | x <- raster(x) 9 | u <- stats::na.omit(unique(cells)) # now removing NAs 2018-02-22 10 | u <- u[ u > 0 & u <= ncell(x) ] 11 | if (length(u) == 0) { 12 | stop('no valid cells') 13 | } 14 | cols <- colFromCell(x, u) 15 | rows <- rowFromCell(x, u) 16 | res <- res(x) 17 | x1 <- xFromCol(x, min(cols)) - 0.5 * res[1] 18 | x2 <- xFromCol(x, max(cols)) + 0.5 * res[1] 19 | y1 <- yFromRow(x, max(rows)) - 0.5 * res[2] 20 | y2 <- yFromRow(x, min(rows)) + 0.5 * res[2] 21 | e <- extent(x1, x2, y1, y2) 22 | r <- crop(x, e) 23 | if (values) { 24 | r <- setValues(r, cellsFromExtent(x, e)) 25 | } 26 | return(r) 27 | } 28 | 29 | -------------------------------------------------------------------------------- /R/rasterizePointsNGB.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | .p2r <- function(p, r=1, x, field, fun, ...) { 5 | points <- .pointsToMatrix(p) 6 | field <- .getPutVals(p, field, nrow(points), mask=FALSE) 7 | x <- raster(x) 8 | bf <- .xyvBuf(x, points, r, fun=NULL, na.rm=TRUE, cellnumbers=TRUE, small=TRUE, onlycells=TRUE) 9 | bf <- do.call(rbind, bf) 10 | bf <- bf[order(bf[,2]), ] 11 | field <- data.frame(field, value=1:NROW(field)) 12 | bf <- merge(bf, field, by='value') 13 | cellvs <- tapply(bf$field, bf[, 'cell', drop=F], fun) 14 | cellvs <- cbind(as.numeric(names(cellvs)), do.call(rbind, cellvs)) 15 | if (ncol(cellvs) > 2) { 16 | x <- brick(x, nl=ncol(cellvs)-1) 17 | } 18 | x[cellvs[,1]] <- cellvs[,-1] 19 | x 20 | } 21 | 22 | 23 | -------------------------------------------------------------------------------- /R/rectify.R: -------------------------------------------------------------------------------- 1 | # Robert J. Hijmans 2 | # May 2010 3 | # Version 1.0 4 | # Licence GPL v3 5 | 6 | 7 | rotated <- function(x) { 8 | isTRUE(try(x@rotated, silent=TRUE)) 9 | } 10 | 11 | setMethod("rectify", signature(x="Raster"), 12 | function(x, ext, res, method='ngb', filename='', ...) { 13 | stopifnot(rotated(x)) 14 | if ( missing(ext)) { 15 | ext <- extent(x) 16 | } else { 17 | ext <- extent(ext) 18 | } 19 | out <- raster(ext) 20 | if ( missing(res)) { 21 | res(out) <- abs(raster::res(x)) 22 | } else { 23 | res(out) <- res 24 | } 25 | resample(x, out, method=method, filename=filename, ...) 26 | } 27 | ) 28 | 29 | 30 | -------------------------------------------------------------------------------- /R/rotate.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : September 2009 3 | # Version 0.9 4 | # Licence GPL v3 5 | 6 | 7 | 8 | setMethod('rotate', signature(x='Raster'), 9 | function(x, filename='', ...) { 10 | 11 | e <- extent(x) 12 | 13 | if (e@xmin < -60) { 14 | warning('xmin is much smaller than zero. No rotation done') 15 | return(x) 16 | } 17 | 18 | xrange <- e@xmax - e@xmin 19 | if (xrange < 350 | xrange > 370 | e@xmin < -10 | e@xmax > 370) { 20 | if (xrange < 350 | xrange > 370 | e@xmin < -190 | e@xmax > 190) { 21 | warning('this does not look like an appropriate object for this function') 22 | } 23 | } 24 | 25 | xr <- xres(x) 26 | ext1 <- extent(-xr, 180, -100, 100) 27 | if (is.null(intersect(e, ext1 ))) { 28 | r1 <- NULL 29 | } else { 30 | r1 <- crop(x, ext1) 31 | } 32 | ext2 <- extent(180, 360+xr, -100, 100) 33 | if (is.null(intersect(e, ext2 ))) { 34 | r2 <- NULL 35 | } else { 36 | r2 <- crop(x, ext2) 37 | r2 <- shift(r2, -360) 38 | } 39 | ln <- names(x) 40 | if (is.null(r1)) { 41 | out <- r2 42 | } else if (is.null(r2)) { 43 | out <- r1 44 | } else { 45 | out <- merge(r1, r2, overlap=FALSE) 46 | } 47 | names(out) <- names(x) 48 | out@z <- x@z 49 | 50 | # suggested by Mike Sumner: 51 | p <- proj4string(out) 52 | if (length(grep("\\+over", p)) > 0) { 53 | projection(out) <- gsub("[[:space:]]\\+over", "", p) 54 | } 55 | 56 | if (filename != '') { 57 | out <- writeRaster(out, filename, ...) 58 | } 59 | return(out) 60 | } 61 | ) 62 | 63 | -------------------------------------------------------------------------------- /R/roundExtent.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : June 2008 3 | # Version 0.9 4 | # Licence GPL v3 5 | 6 | 7 | setMethod("Math2", signature(x='Extent'), 8 | function (x, digits=0) { 9 | #digits <- max(0, round(digits)) 10 | x@xmin <- methods::callGeneric( x@xmin, digits) 11 | x@xmax <- methods::callGeneric( x@xmax, digits) 12 | x@ymin <- methods::callGeneric( x@ymin, digits) 13 | x@ymax <- methods::callGeneric( x@ymax, digits) 14 | validObject(x) 15 | return(x) 16 | } 17 | ) 18 | 19 | setMethod("floor", signature(x='Extent'), 20 | function (x) { 21 | x@xmin <- floor( x@xmin) 22 | x@xmax <- ceiling( x@xmax) 23 | x@ymin <- floor( x@ymin) 24 | x@ymax <- ceiling( x@ymax) 25 | return(x) 26 | } 27 | ) 28 | 29 | setMethod("ceiling", signature(x='Extent'), 30 | function (x) { 31 | x@xmin <- ceiling( x@xmin) 32 | x@xmax <- floor( x@xmax) 33 | x@ymin <- ceiling( x@ymin) 34 | x@ymax <- floor( x@ymax) 35 | return(x) 36 | } 37 | ) 38 | 39 | -------------------------------------------------------------------------------- /R/rowMinMax.R: -------------------------------------------------------------------------------- 1 | 2 | .rowMin <- function(x, na.rm=TRUE) { 3 | # .Call('raster_doRowMin', PACKAGE = 'raster', x, narm=na.rm) 4 | .doRowMin(x, narm=na.rm) 5 | } 6 | 7 | .rowMax <- function(x, na.rm=TRUE) { 8 | .doRowMax(x, narm=na.rm) 9 | } 10 | 11 | .colMin <- function(x, na.rm=TRUE) { 12 | .doRowMin(t(x), narm=na.rm) 13 | } 14 | 15 | .colMax <- function(x, na.rm=TRUE) { 16 | .doRowMax(t(x), narm=na.rm) 17 | } 18 | 19 | 20 | -------------------------------------------------------------------------------- /R/sampleInt.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : Febrary 2009 3 | # Version 0.9 4 | # Licence GPL v3 5 | 6 | 7 | sampleInt <- function(n, size, replace=FALSE) { 8 | 9 | n <- round(n[1]) 10 | size <- round(size[1]) 11 | 12 | stopifnot(n > 0) 13 | stopifnot(size > 0) 14 | 15 | if (!replace) { 16 | switched <- FALSE 17 | done <- FALSE 18 | if (size > (0.66 * n)) { 19 | if (size > n ) { 20 | warning('size changed to n because it cannot be larger than n when replace is FALSE') 21 | size <- n 22 | } 23 | if (size == n) { 24 | done <- TRUE 25 | } 26 | switched <- TRUE 27 | size <- n - size 28 | } 29 | samp <- NULL 30 | 31 | while (! done) { 32 | f <- ceiling(stats::runif(size * 1.1) * n) 33 | samp <- unique(c(samp, f)) 34 | if (length(samp) >= size) { 35 | samp <- samp[1:size] 36 | done <- TRUE 37 | } 38 | } 39 | if (switched) { 40 | if (!is.null(samp)) { 41 | samp <- (1:n)[-samp] 42 | lsp <- length(samp) 43 | samp <- samp[sample.int(lsp)] 44 | } else { 45 | samp <- sample.int(n) 46 | } 47 | } 48 | 49 | } else { 50 | samp <- ceiling(stats::runif( size ) * n) 51 | } 52 | 53 | return( samp ) 54 | } 55 | -------------------------------------------------------------------------------- /R/scale.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : September 2012 3 | # Version 1.0 4 | # Licence GPL v3 5 | 6 | 7 | setMethod('scale', signature('Raster'), 8 | 9 | function(x, center=TRUE, scale=TRUE) { 10 | 11 | if (canProcessInMemory(x)) { 12 | v <- values(x) 13 | x <- setValues(x, scale(v, center=center, scale=scale)) 14 | return(x) 15 | } 16 | 17 | if (!is.logical(center)) { 18 | 19 | stopifnot(length(center) == nlayers(x)) 20 | x <- x - center 21 | 22 | } else if (center) { 23 | m <- cellStats(x, 'mean', na.rm=TRUE) 24 | x <- x - m 25 | } 26 | 27 | if (!is.logical(scale)) { 28 | stopifnot(length(scale) == nlayers(x)) 29 | x <- x / scale 30 | 31 | } else if (scale) { 32 | if (center[1] & is.logical(center[1])) { 33 | st <- cellStats(x, 'sd', na.rm=TRUE) 34 | } else { 35 | st <- cellStats(x, 'rms', na.rm=TRUE) 36 | } 37 | x <- x / st 38 | } 39 | x 40 | } 41 | ) 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /R/setCV.R: -------------------------------------------------------------------------------- 1 | 2 | .setCV <- function(x, v, col) { 3 | stopifnot(length(v) == (length(col)+1)) 4 | v <- as.numeric(v) 5 | x@legend@values <- v 6 | x@legend@color <- col 7 | x@legend@colortable <- vector() 8 | x 9 | } 10 | 11 | #val <- c(-1, -0.3, -0.2, 0, 0.1, 0.3, 0.4, 0.6, 0.8, 1, 10) 12 | #ct <- c(grDevices::col2rgb("white"),grDevices::col2rgb("blue"),grDevices::rgb(205,193,173, maxColorValue = 255), grDevices::rgb(150,150,150, maxColorValue = 255), grDevices::rgb(120,100,51, maxColorValue = 255), grDevices::rgb(120,200,100, maxColorValue = 255), grDevices::rgb(28,144,3, maxColorValue = 255), grDevices::rgb(6,55,0, maxColorValue = 255), grDevices::rgb(10,30,25, maxColorValue = 255), grDevices::rgb(6,27,7, maxColorValue = 255)) 13 | 14 | .setCT <- function(x, v, col, na='white') { 15 | v <- as.numeric(v) 16 | na <- which(is.na(v)) 17 | if (length(na)==0) { 18 | v <- c(NA, v) 19 | col <- c('white', col) 20 | } else { 21 | v <- c(v[na], v[-na]) 22 | col <- c(col[na], col[-na]) 23 | } 24 | notrgb <- which(substr(col, 1, 1) != '#') 25 | col[notrgb] <- grDevices::rgb(t(grDevices::col2rgb(col[notrgb])), maxColorValue=255) 26 | x@legend@values <- v 27 | x@legend@color <- col 28 | x 29 | } 30 | 31 | -------------------------------------------------------------------------------- /R/setFileExt.R: -------------------------------------------------------------------------------- 1 | # raster package 2 | # Author: Robert J. Hijmans 3 | # Date : June 2008 4 | # Version 0.9 5 | # Licence GPL v3 6 | 7 | 8 | .setFileExtensionValues <- function(fname, type='raster') { 9 | if (type == 'raster') { 10 | extension(fname) <- ".gri" 11 | } else if (type == 'SAGA') { 12 | extension(fname) <- ".sdat" 13 | } else if (type == 'IDRISI') { 14 | extension(fname) <- ".rst" 15 | } else if (type == 'IDRISIold') { 16 | extension(fname) <- ".img" 17 | } else if (type == 'BIL') { 18 | extension(fname) <- ".bil" 19 | } else if (type == 'BIP') { 20 | extension(fname) <- ".bip" 21 | } else if (type == 'BSQ') { 22 | extension(fname) <- ".bsq" 23 | # } else if (type == 'big.matrix') { 24 | # extension(fname) <- ".big" 25 | } else { 26 | stop('unknown file format') 27 | } 28 | return(fname) 29 | } 30 | 31 | .setFileExtensionHeader <- function(fname, type='raster') { 32 | if (type == 'raster') { 33 | extension(fname) <- ".grd" 34 | } else if (type == 'SAGA') { 35 | extension(fname) <- "sgrd" 36 | } else if (type == 'IDRISI') { 37 | extension(fname) <- ".rdc" 38 | } else if (type == 'IDRISIold') { 39 | extension(fname) <- ".doc" 40 | } else if (type %in% c('BIL', 'BSQ', 'BIP')) { 41 | extension(fname) <- ".hdr" 42 | } else if (type == 'big.matrix') { 43 | extension(fname) <- ".brd" 44 | } else { 45 | stop('unknown file format') 46 | } 47 | return(fname) 48 | } 49 | -------------------------------------------------------------------------------- /R/setZ.R: -------------------------------------------------------------------------------- 1 | # Robert J. Hijmans 2 | # June 2011 3 | # Version 1.0 4 | # Licence GPL v3 5 | 6 | 7 | 8 | setZ <- function(x, z, name='time') { 9 | if (is.null(z)) { 10 | x@z <- list() 11 | return(x) 12 | } 13 | if (is.list(z)) { 14 | z <- unlist(z) 15 | } 16 | stopifnot(length(z) == nlayers(x)) 17 | z <- list(z) 18 | names(z) <- name[1] 19 | x@z <- z 20 | x 21 | } 22 | 23 | 24 | getZ <- function(x) { 25 | if (length(x@z) == 0) { 26 | return(NULL) 27 | } else { 28 | return(x@z[[1]]) 29 | } 30 | } 31 | 32 | 33 | -------------------------------------------------------------------------------- /R/sf.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : September 2017 3 | # Version 1.0 4 | # Licence GPL v3 5 | 6 | .sf2sp <- function(from) { 7 | #if (!requireNamespace("sf")) { 8 | # stop("package sf is not available") 9 | #} 10 | # to do 11 | #if (from == "GEOMETRYCOLLECTION") { 12 | # x <- list() 13 | # for (i in 1:3 ) { } 14 | # return(x) 15 | #} 16 | p <- as(from, "Spatial") 17 | if (isTRUE(ncol(p) == 0)) { 18 | # for the degenerate Spatial*DataFrame that has zero variables 19 | if (inherits(p, "SpatialPolygons")) { 20 | p <- as(p, "SpatialPolygons") 21 | } else if (inherits(p, "SpatialLines")) { 22 | p <- as(p, "SpatialLines") 23 | } else if (inherits(p, "SpatialPoints")) { 24 | p <- as(p, "SpatialPoints") 25 | } 26 | } 27 | p 28 | } 29 | 30 | -------------------------------------------------------------------------------- /R/simplifyPols.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | .simplifyPolygons <- function(p) { 4 | g <- geom(p) 5 | out <- NULL 6 | for (i in 1:g[nrow(g), 'cump']) { 7 | gg <- g[g[,3]==i, ] 8 | keep <- rep(TRUE, nrow(gg)) 9 | for (j in 2:(nrow(gg)-1)) { 10 | if (gg[j,'x'] == gg[j-1,'x'] & gg[j,'x'] == gg[j+1,'x']) { 11 | keep[j] <- FALSE 12 | } else if (gg[j,'y'] == gg[j-1,'y'] & gg[j,'y'] == gg[j+1,'y']) { 13 | keep[j] <- FALSE 14 | } 15 | } 16 | gg <- gg[keep, ] 17 | out <- rbind(out, gg) 18 | } 19 | out <- as(data.frame(out), 'SpatialPolygons') 20 | out@proj4string <- p@proj4string 21 | if (.hasSlot(p, 'data')) { 22 | out <- sp::SpatialPolygonsDataFrame(out, p@data) 23 | } 24 | out 25 | } 26 | 27 | -------------------------------------------------------------------------------- /R/speasy.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : April 2015 3 | # Version 1.0 4 | # Licence GPL v3 5 | 6 | 7 | # easy functions for creating SpatialLines* and SpatialPolygons* 8 | 9 | spLines <- function(x, ..., attr=NULL, crs="") { 10 | x <- c(list(x), list(...)) 11 | x <- rapply(x, sp::Line, how='replace') 12 | x <- lapply(1:length(x), function(i) sp::Lines(x[[i]], as.character(i))) 13 | x <- sp::SpatialLines(x) 14 | 15 | if (!is.null(attr)) { 16 | if (nrow(attr) == length(x)) { 17 | x <- sp::SpatialLinesDataFrame(x, attr) 18 | } else { 19 | msg <- paste('number of rows in attr (', nrow(attr), ') does not match the number of lines (', length(x), ')', sep='') 20 | stop(msg) 21 | } 22 | } 23 | 24 | if (!is.na(crs)) { 25 | crs(x) <- crs 26 | } 27 | x 28 | } 29 | 30 | 31 | spPolygons <- function(x, ..., attr=NULL, crs="") { 32 | x <- c(list(x), list(...)) 33 | x <- rapply(x, sp::Polygon, how='replace') 34 | 35 | x <- lapply(1:length(x), function(i) { 36 | if (length(x[[i]]) == 1) { 37 | sp::Polygons(x[i], as.character(i)) 38 | } else { 39 | sp::Polygons(x[[i]], as.character(i)) 40 | } 41 | }) 42 | 43 | x <- sp::SpatialPolygons(x) 44 | if (!is.null(attr)) { 45 | if (nrow(attr) == length(x)) { 46 | x <- sp::SpatialPolygonsDataFrame(x, attr) 47 | } else { 48 | msg <- paste('number of rows in attr (', nrow(attr), ') does not match the number of polygons (', length(x), ')', sep='') 49 | stop(msg) 50 | } 51 | } 52 | 53 | if (!is.na(crs)) { 54 | crs(x) <- crs 55 | } 56 | 57 | x 58 | } 59 | -------------------------------------------------------------------------------- /R/stackFile.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : June 2008 3 | # Version 0.9 4 | # Licence GPL v3 5 | 6 | 7 | 8 | stackOpen <- function(stackfile) { 9 | f <- utils::read.table(stackfile, as.is=FALSE, strip.white=TRUE) 10 | if (dim(f)[2] > 1) { 11 | s <- stack(as.vector(f[,1]), bands=as.vector(f[,2])) 12 | } else { 13 | s <- stack(as.vector(f[,1])) 14 | } 15 | s@filename <- stackfile 16 | return(s) 17 | } 18 | 19 | ..stackOpen <- function(stackfile, quick=FALSE) { 20 | f <- utils::read.table(stackfile, as.is=FALSE, strip.white=TRUE) 21 | if (quick) { 22 | if (dim(f)[2] > 1) { 23 | s <- .quickStack(f[,1], f[,2], f[,3]) 24 | } else { 25 | s <- .quickStack(f[,1]) 26 | } 27 | } else { 28 | if (dim(f)[2] > 1) { 29 | s <- stack(as.vector(f[,1]), bands=as.vector(f[,2])) 30 | } else { 31 | s <- stack(as.vector(f[,1])) 32 | } 33 | } 34 | s@filename <- stackfile 35 | return(s) 36 | } 37 | 38 | stackSave <- function(x, filename) { 39 | filename <- trim(filename) 40 | if (filename == "") { 41 | stop('Provide a non empty filename.') 42 | } 43 | 44 | info <- t( sapply(x@layers, function(i) c(i@file@name, i@file@nbands, i@data@band)) ) 45 | if (any(info[,1] == '')) { 46 | stop("cannot save a RasterStack that has layers that only exist in memory. Use writeRaster first/instead.") 47 | } 48 | if (any(info[,2] != '1')) { 49 | utils::write.table(info, filename, row.names=FALSE, col.names=FALSE) 50 | } else { 51 | utils::write.table(info[,1], filename, row.names=FALSE, col.names=FALSE) 52 | } 53 | x@filename <- filename 54 | return(x) 55 | } 56 | 57 | -------------------------------------------------------------------------------- /R/symdif.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date: December 2011 3 | # Version 1.0 4 | # Licence GPL v3 5 | 6 | 7 | setMethod('symdif', signature(x='SpatialPolygons', y='SpatialPolygons'), 8 | function(x, y, ...) { 9 | 10 | 11 | # warning("this method will be removed. You can use 'terra::symdif' instead") 12 | 13 | z <- symdif(vect(x), vect(y)) 14 | return(as(z, "Spatial")) 15 | 16 | # valgeos <- .checkGEOS(); on.exit(rgeos::set_RGEOS_CheckValidity(valgeos)) 17 | 18 | # haswarned <- FALSE 19 | 20 | # yy <- list(y, ...) 21 | # for (y in yy) { 22 | # if (! identical( .proj4string(x), .proj4string(y)) ) { 23 | # if (!haswarned) { 24 | # warning('non identical crs') 25 | # haswarned <- TRUE 26 | # } 27 | # y@proj4string <- x@proj4string 28 | # } 29 | # if (rgeos::gIntersects(x, y)) { 30 | # part1 <- erase(x, y) 31 | # part2 <- erase(y, x) 32 | # x <- bind(part1, part2) 33 | # } 34 | # } 35 | # x 36 | } 37 | ) 38 | 39 | -------------------------------------------------------------------------------- /R/union.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : December 2011 3 | # Version 1.0 4 | # Licence GPL v3 5 | 6 | 7 | setMethod('union', signature(x='Extent', y='Extent'), 8 | function(x, y) { 9 | .unionExtent(x, y) 10 | } ) 11 | 12 | 13 | .unionExtent <- function(x, ...) { 14 | objects <- c(x, list(...)) 15 | if (length(objects) == 1) { 16 | return(extent(x)) 17 | } 18 | e <- extent(objects[[1]]) 19 | for (i in 2:length(objects)) { 20 | e2 <- extent(objects[[i]]) 21 | e@xmin <- min(e@xmin, e2@xmin) 22 | e@xmax <- max(e@xmax, e2@xmax) 23 | e@ymin <- min(e@ymin, e2@ymin) 24 | e@ymax <- max(e@ymax, e2@ymax) 25 | } 26 | return(e) 27 | } 28 | 29 | -------------------------------------------------------------------------------- /R/unstack.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : March 2009 3 | # Version 0.9 4 | # Licence GPL v3 5 | 6 | if (!isGeneric("unstack")) { 7 | setGeneric("unstack", function(x, ...) 8 | standardGeneric("unstack")) 9 | } 10 | 11 | 12 | setMethod("unstack", signature(x='RasterStack'), 13 | function(x) { 14 | return(x@layers) 15 | } ) 16 | 17 | 18 | setMethod("unstack", signature(x='RasterBrick'), 19 | function(x) { 20 | if (nlayers(x) == 0) { 21 | list() 22 | } else { 23 | lapply(1:nlayers(x), function(i) raster(x, i)) 24 | } 25 | } ) 26 | 27 | -------------------------------------------------------------------------------- /R/validCell.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : October 2008 3 | # Version 0.9 4 | # Licence GPL v3 5 | 6 | 7 | validCell <- function(object, cell) { 8 | cell <- round(cell) 9 | valid <- rep(FALSE, times=length(cell)) 10 | valid[cell > 0 & cell <= ncell(object)] <- TRUE 11 | return(valid) 12 | } 13 | 14 | validRow <- function(object, rownr) { 15 | rownr <- round(rownr) 16 | valid <- rep(FALSE, times=length(rownr)) 17 | valid[rownr > 0 & rownr <= object@nrows] <- TRUE 18 | return(valid) 19 | } 20 | 21 | validCol <- function(object, colnr) { 22 | colnr <- round(colnr) 23 | valid <- rep(FALSE, times=length(colnr)) 24 | valid[colnr > 0 & colnr <= object@ncols] <- TRUE 25 | return(valid) 26 | } 27 | -------------------------------------------------------------------------------- /R/values.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : October 2010 3 | # Version 1.0 4 | # Licence GPL v3 5 | 6 | 7 | setMethod('values', signature(x='Raster'), 8 | function(x, ...) { 9 | getValues(x, ...) 10 | }) 11 | 12 | 13 | 14 | setMethod('values<-', signature(x='RasterLayer'), 15 | function(x, value) { 16 | setValues(x, value) 17 | } ) 18 | 19 | setMethod('values<-', signature(x='RasterBrick'), 20 | function(x, value) { 21 | setValues(x, values=value, layer=-1) 22 | } ) 23 | 24 | setMethod('values<-', signature(x='RasterStack'), 25 | function(x, value) { 26 | setValues(x, values=value, layer=-1) 27 | } ) 28 | 29 | setMethod('values<-', signature(x='RasterLayerSparse'), 30 | function(x, value) { 31 | setValues(x, value, index=NULL) 32 | } ) 33 | -------------------------------------------------------------------------------- /R/which.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date: November 2009 3 | # Version 0.9 4 | # Licence GPL v3 5 | 6 | 7 | if (!isGeneric("Which")) { 8 | setGeneric("Which", function(x, ...) 9 | standardGeneric("Which")) 10 | } 11 | 12 | 13 | setMethod('Which', signature(x='RasterLayer'), 14 | function(x, cells=FALSE, na.rm=TRUE, ...) { 15 | 16 | 17 | if (canProcessInMemory(x, 2)){ 18 | if (cells) { 19 | return(which(as.logical(getValues(x)) == TRUE)) 20 | } else { 21 | x <- as.logical(x) 22 | if (na.rm) { 23 | x[is.na(x)] <- FALSE 24 | } 25 | return(x) 26 | } 27 | 28 | } else { 29 | out <- raster(x) 30 | if (cells) { 31 | vv <- vector() 32 | } else { 33 | filename <- rasterTmpFile() 34 | out <- writeStart(out, filename=filename, format=.filetype(), datatype='INT2S', overwrite=TRUE) 35 | } 36 | 37 | tr <- blockSize(out, n=2) 38 | pb <- pbCreate(tr$n, type=.progress() ) 39 | for (i in 1:tr$n) { 40 | v <- as.logical( getValuesBlock(x, row=tr$row[i], nrows=tr$nrows[i] ) ) 41 | 42 | if (cells) { 43 | offs <- (tr$row[i]-1) * out@ncols 44 | vv <- c(vv, which(v==TRUE) + offs) 45 | } else { 46 | v <- as.logical(v) 47 | if (na.rm) { 48 | v[is.na(v)] <- 0 49 | } 50 | out <- writeValues(out, v, tr$row[i]) 51 | } 52 | pbStep(pb, i) 53 | } 54 | pbClose(pb) 55 | 56 | 57 | if (cells) { 58 | return(vv) 59 | } else { 60 | out <- writeStop(out) 61 | return(out) 62 | } 63 | } 64 | } 65 | ) 66 | 67 | -------------------------------------------------------------------------------- /R/writeAllAscii.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : June 2008 3 | # Version 0.9 4 | # Licence GPL v3 5 | 6 | 7 | .writeAscii <- function(x, filename, datatype='FLT4S', prj=FALSE, ...) { 8 | 9 | v <- getValues(x) 10 | 11 | if (!is.finite( x@file@nodatavalue) ) { 12 | x@file@nodatavalue <- min(-9999, min(v, na.rm=TRUE)-1) 13 | } 14 | 15 | x <- .startAsciiWriting(x, filename, ...) 16 | 17 | datatype <- substr(datatype, 1, 3) 18 | if (datatype == 'INT') { 19 | on.exit(options(scipen=options('scipen'))) 20 | options(scipen=10) 21 | v <- round(v) 22 | } 23 | 24 | v[is.na(v)] <- x@file@nodatavalue 25 | if (datatype=='FLT') { 26 | # hack to make sure that ArcGIS does not 27 | # assume values are integers if the first 28 | # values have no decimal point 29 | v <- as.character(v) 30 | v[1] <- formatC(as.numeric(v[1]), 15, format='f') 31 | } 32 | v <- matrix(v, ncol=ncol(x), byrow=TRUE) 33 | 34 | utils::write.table(v, x@file@name, append = TRUE, quote = FALSE, sep = " ", eol = "\n", dec = ".", row.names = FALSE, col.names = FALSE) 35 | 36 | if (prj) { 37 | crs <- .getCRS(x) 38 | if (!is.na(crs)) { 39 | writeLines(wkt(x), extension(filename, 'prj') ) 40 | } 41 | } 42 | 43 | return( .stopAsciiWriting(x) ) 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /R/writeAllGDAL.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : January 2009 3 | # Version 0.9 4 | # Licence GPL v3 5 | 6 | 7 | .writeGDALall <- function(x, filename, options=NULL, setStatistics=TRUE, ...) { 8 | 9 | stat <- cbind(NA, NA) 10 | # if (nlayers(x) > 1) { 11 | # y <- brick(x, values=FALSE) 12 | # levels(y) <- levels(x) 13 | # x <- getValues(x) 14 | ## if (setStatistics) { 15 | ## stat <- t(apply(x, 2, function(z, ...) cbind(mean(z, na.rm=TRUE), stats::sd(z, na.rm=TRUE)))) 16 | ## } 17 | # } else { 18 | # y <- raster(x) 19 | # levels(y) <- levels(x) 20 | # y@legend@colortable <- colortable(x) 21 | # x <- getValues(x) 22 | ## if (setStatistics) { 23 | ## stat <- cbind(mean(x, na.rm=TRUE), stats::sd(x, na.rm=TRUE)) 24 | ## } 25 | # } 26 | ## filetype <- .filetype(format=format, filename=filename) 27 | ## y <- .startGDALwriting(y, filename, gdal=options, overwrite=overwrite, format=filetype, ...) 28 | 29 | # y <- .startGDALwriting(y, filename, gdal=options, ...) 30 | # x <- writeValues(y, x, start=1) 31 | y <- .startGDALwriting(x, filename, gdal=options, ...) 32 | x <- writeValues(y, getValues(x), start=1) 33 | .stopGDALwriting(x, stat) 34 | } 35 | 36 | -------------------------------------------------------------------------------- /R/xyMinMax.R: -------------------------------------------------------------------------------- 1 | # Author: Robert J. Hijmans 2 | # Date : October 2008 3 | # Version 0.9 4 | # Licence GPL v3 5 | 6 | 7 | 8 | setMethod('xmin', signature(x='BasicRaster'), 9 | function(x) { 10 | return(extent(x)@xmin) 11 | }) 12 | 13 | setMethod('xmax', signature(x='BasicRaster'), 14 | function(x) { 15 | return(extent(x)@xmax) 16 | }) 17 | 18 | setMethod('ymin', signature(x='BasicRaster'), 19 | function(x) { 20 | return(extent(x)@ymin) 21 | }) 22 | 23 | setMethod('ymax', signature(x='BasicRaster'), 24 | function(x) { 25 | return(extent(x)@ymax) 26 | }) 27 | 28 | setMethod('xmin', signature(x='Extent'), 29 | function(x) { 30 | return(x@xmin) 31 | }) 32 | 33 | setMethod('xmax', signature(x='Extent'), 34 | function(x) { 35 | return(x@xmax) 36 | }) 37 | 38 | setMethod('ymin', signature(x='Extent'), 39 | function(x) { 40 | return(x@ymin) 41 | }) 42 | 43 | setMethod('ymax', signature(x='Extent'), 44 | function(x) { 45 | return(x@ymax) 46 | }) 47 | 48 | 49 | setMethod('xmin', signature(x='Spatial'), 50 | function(x) { 51 | return(extent(x)@xmin) 52 | }) 53 | 54 | setMethod('xmax', signature(x='Spatial'), 55 | function(x) { 56 | return(extent(x)@xmax) 57 | }) 58 | 59 | setMethod('ymin', signature(x='Spatial'), 60 | function(x) { 61 | return(extent(x)@ymin) 62 | }) 63 | 64 | setMethod('ymax', signature(x='Spatial'), 65 | function(x) { 66 | return(extent(x)@ymax) 67 | }) 68 | 69 | 70 | -------------------------------------------------------------------------------- /R/zApply.R: -------------------------------------------------------------------------------- 1 | # Oscar Perpinan Lamigueiro 2 | 3 | zApply <- function(x, by, fun=mean, name='', ...){ 4 | z <- getZ(x) 5 | stopifnot(length(z) == nlayers(x)) 6 | 7 | ##from aggregate.zoo 8 | my.unique <- function(x) x[match(x, x) == seq_len(length(x))] 9 | my.sort <- function(x) x[order(x)] 10 | 11 | if (is.function(by)) { by <- by(z) } 12 | ##stopifnot(length(time(x)) == length(by)) 13 | b <- stackApply(x, as.numeric(factor(by)), match.fun(fun), ...) 14 | zval <- my.sort(my.unique(by)) 15 | b <- setZ(b, zval, name) 16 | names(b) <- as.character(zval) 17 | b 18 | } 19 | 20 | 21 | -------------------------------------------------------------------------------- /R/zzz.R: -------------------------------------------------------------------------------- 1 | 2 | loadModule("spmod", TRUE) 3 | 4 | #.onLoad <- function(lib, pkg) { 5 | # pkg.info <- utils::packageDescription('raster') 6 | # packageStartupMessage(paste("raster ", pkg.info[["Version"]], " (", pkg.info["Date"], ")", sep="")) 7 | # wd <- getwd() 8 | # options('startup.working.directory'=wd) 9 | # fn <- paste(wd, '/rasterOptions_', pkg.info[["Version"]], sep='') 10 | # .loadOptions(fn) 11 | 12 | # try( removeTmpFiles( .tmptime() ), silent=TRUE ) 13 | # return(invisible(0)) 14 | #} 15 | 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # raster 2 | 3 | 4 | [![CRAN 5 | status](https://www.r-pkg.org/badges/version/raster)](https://cran.r-project.org/package=raster) 6 | [![CRAN RStudio mirror downloads](http://cranlogs.r-pkg.org/badges/raster)](http://www.r-pkg.org/pkg/raster) 7 | 8 | `raster` is an R package for spatial data analysis. The package is not futher developed. It has been replaced by "terra": [CRAN](https://cran.r-project.org/web/packages/terra/index.html), [github](https://github.com/rspatial/terra). 9 | -------------------------------------------------------------------------------- /TODO: -------------------------------------------------------------------------------- 1 | --- to do 2 | focal for multi-layer objects 3 | step-size option for focal 4 | add equivalent to "subsWithNA" to reclassify 5 | add AMSR-E ice products and other raw binary products 6 | add orientation fixes for AVISO currents and similar NetCDFs lacking metadata on the internal CRS 7 | add VRT output to saveStack 8 | Fix lines falling between the cracks in extract (calling rasterzeLines): https://stat.ethz.ch/pipermail/r-sig-geo/2014-May/020973.html 9 | Better handling of NA values in 1 byte raster files 10 | -------------------------------------------------------------------------------- /inst/external/countries.rds: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rspatial/raster/1d4965263e4fa2d23404648471530c0deace72c7/inst/external/countries.rds -------------------------------------------------------------------------------- /inst/external/lux.dbf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rspatial/raster/1d4965263e4fa2d23404648471530c0deace72c7/inst/external/lux.dbf -------------------------------------------------------------------------------- /inst/external/lux.prj: -------------------------------------------------------------------------------- 1 | GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]] -------------------------------------------------------------------------------- /inst/external/lux.rds: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rspatial/raster/1d4965263e4fa2d23404648471530c0deace72c7/inst/external/lux.rds -------------------------------------------------------------------------------- /inst/external/lux.shp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rspatial/raster/1d4965263e4fa2d23404648471530c0deace72c7/inst/external/lux.shp -------------------------------------------------------------------------------- /inst/external/lux.shx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rspatial/raster/1d4965263e4fa2d23404648471530c0deace72c7/inst/external/lux.shx -------------------------------------------------------------------------------- /inst/external/rlogo.grd: -------------------------------------------------------------------------------- 1 | [general] 2 | creator=R package 'raster' 3 | created= 2010-04-16 14:57:47 4 | [georeference] 5 | nrows=77 6 | ncols=101 7 | xmin=0 8 | ymin=0 9 | xmax=101 10 | ymax=77 11 | projection= +proj=merc +datum=WGS84 12 | [data] 13 | datatype=FLT4S 14 | byteorder=little 15 | nbands=3 16 | bandorder=BIL 17 | categorical=FALSE 18 | levels=NA 19 | minvalue=0:0:0 20 | maxvalue=255:255:255 21 | nodatavalue=-3.4e+38 22 | [legend] 23 | legendtype= 24 | values= 25 | color= 26 | [description] 27 | layername=red:green:blue 28 | history= 29 | -------------------------------------------------------------------------------- /inst/external/rlogo.gri: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rspatial/raster/1d4965263e4fa2d23404648471530c0deace72c7/inst/external/rlogo.gri -------------------------------------------------------------------------------- /inst/external/test.grd: -------------------------------------------------------------------------------- 1 | [general] 2 | creator=R package 'raster' 3 | created=2020-05-02 16:05:26 4 | [georeference] 5 | nrows=115 6 | ncols=80 7 | xmin=178400 8 | ymin=329400 9 | xmax=181600 10 | ymax=334000 11 | projection=+proj=sterea +lat_0=52.1561605555556 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +datum=WGS84 +units=m +no_defs 12 | [data] 13 | datatype=FLT4S 14 | byteorder=little 15 | nbands=1 16 | bandorder=BIL 17 | categorical=FALSE 18 | minvalue=138.707073391626 19 | maxvalue=1736.05793034568 20 | nodatavalue=-3.4e+38 21 | [legend] 22 | legendtype= 23 | values= 24 | color= 25 | [description] 26 | layername=test 27 | -------------------------------------------------------------------------------- /inst/external/test.gri: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rspatial/raster/1d4965263e4fa2d23404648471530c0deace72c7/inst/external/test.gri -------------------------------------------------------------------------------- /inst/tinytest/test_getvaluesblock.R: -------------------------------------------------------------------------------- 1 | 2 | #test_that('we can extract from a single layer of a RasterStack using the lyrs argument', { 3 | 4 | rast <- raster(matrix(1:16, nrow=4), xmn=0, xmx=4, ymn=0, ymx=4) 5 | 6 | stk <- stack(list(a=rast, b=sqrt(rast))) 7 | 8 | expect_equivalent( 9 | getValuesBlock(stk[[1]], 1, 3, 3, 2, format='m'), 10 | getValuesBlock(stk, 1, 3, 3, 2, lyrs=1), 11 | ) 12 | 13 | -------------------------------------------------------------------------------- /inst/tinytest/test_rasterize.R: -------------------------------------------------------------------------------- 1 | 2 | p1 <- rbind(c(-180, -20), c(-140, 55), c(10, 0), c(-140, -60), c(-180, -20)) 3 | hole <- rbind(c(-150, -20), c(-100, -10), c(-110, 20), c(-150, -20)) 4 | p1 <- list(p1, hole) 5 | p2 <- rbind(c(-10, 0), c(140, 60), c(160, 0), c(140, -55), c(-10, 0)) 6 | p3 <- rbind(c(-125, 0), c(0, 60), c(40, 5), c(15, -45), c(-125, 0)) 7 | 8 | pols <- spPolygons(p1, p2, p3) 9 | r <- raster(ncol = 90, nrow = 45) 10 | x <- rasterize(pols, r, fun = sum) 11 | 12 | # rasterize works as before 13 | expect_equal(sum(values(x), na.rm=TRUE), 3481) 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /inst/tinytest/test_subset.R: -------------------------------------------------------------------------------- 1 | r <- raster::brick(nrows = 100, ncols = 100, nl = 2) 2 | r_z <- setZ(r, as.Date(c("2018-01-01", "2018-01-02"))) 3 | 4 | s <- stack(r) 5 | s_z <- setZ(s, as.Date(c("2018-01-01", "2018-01-02"))) 6 | 7 | #test_that("subset keeps the z attribute if present",{ 8 | # on brick 9 | expect_equal(r_z[[1]]@z$time, as.Date("2018-01-01")) 10 | expect_equal(s_z[[2]]@z$time, as.Date("2018-01-02")) 11 | # on stack 12 | expect_equal(s_z[[c(1,2)]]@z$time, as.Date(c("2018-01-01", "2018-01-02"))) 13 | expect_equal(s_z[[c(1,2)]]@z$time, as.Date(c("2018-01-01", "2018-01-02"))) 14 | # NULL if no z set 15 | expect_equal(r[[1]]@z$time, NULL) 16 | expect_equal(s[[1]]@z$time, NULL) 17 | 18 | 19 | -------------------------------------------------------------------------------- /inst/tinytest/test_wkt_grd.R: -------------------------------------------------------------------------------- 1 | fl <- system.file("external/test.grd", package="raster") 2 | tst <- raster::raster(fl) 3 | raster::crs(tst) <- raster::crs("EPSG:28992") 4 | tf <- tempfile(fileext=".grd") 5 | raster::writeRaster(tst, tf) 6 | tst1 <- raster::raster(tf) 7 | expect_identical(raster::wkt(tst), raster::wkt(tst1)) 8 | 9 | -------------------------------------------------------------------------------- /inst/tinytest/tinytest.R: -------------------------------------------------------------------------------- 1 | 2 | if ( requireNamespace("tinytest", quietly=TRUE) ) { 3 | tinytest::test_package("raster") 4 | } 5 | 6 | -------------------------------------------------------------------------------- /man/Extent-class.Rd: -------------------------------------------------------------------------------- 1 | \name{Extent-class} 2 | 3 | \docType{class} 4 | 5 | \alias{Extent} 6 | \alias{Extent-class} 7 | \alias{show,Extent-method} 8 | 9 | \title{Class "Extent" } 10 | 11 | \description{ 12 | Objects of class Extent are used to define the spatial extent (extremes) of objects of the BasicRaster and Raster* classes. 13 | } 14 | 15 | \section{Objects from the Class}{ 16 | You can use the \code{\link{extent}} function to create Extent objects, or to extract them from Raster* and Spatial* objects. 17 | } 18 | 19 | \section{Slots}{ 20 | \describe{ 21 | \item{\code{xmin}:}{minimum x coordinate} 22 | \item{\code{xmax}:}{maximum x coordinate} 23 | \item{\code{ymin}:}{minumum y coordinate} 24 | \item{\code{ymax}:}{maximum y coordinate} 25 | } 26 | } 27 | 28 | \section{Methods}{ 29 | \describe{ 30 | \item{show}{display values of a Extent object } 31 | } 32 | } 33 | 34 | 35 | 36 | 37 | \seealso{ \code{\link{extent}}, \code{\link[raster]{setExtent}} } 38 | 39 | \examples{ 40 | ext <- extent(-180,180,-90,90) 41 | ext 42 | } 43 | 44 | \keyword{classes} 45 | \keyword{spatial} 46 | -------------------------------------------------------------------------------- /man/Math-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{Math-methods} 2 | 3 | \docType{methods} 4 | 5 | \alias{Math-methods} 6 | \alias{Math2,Extent-method} 7 | \alias{Math2,Raster-method} 8 | \alias{Math,Raster-method} 9 | \alias{Math,RasterLayerSparse-method} 10 | 11 | 12 | \alias{log,Raster-method} 13 | 14 | \title{Mathematical functions} 15 | 16 | \description{ 17 | Generic mathematical functions that can be used with a Raster* object as argument: 18 | 19 | \code{"abs", "sign", "sqrt", "ceiling", "floor", "trunc", "cummax", "cummin", } 20 | 21 | \code{"cumprod", "cumsum", "log", "log10", "log2", "log1p", "acos", "acosh", "asin", } 22 | 23 | \code{"asinh", "atan", "atanh", "exp", "expm1", "cos", "cosh", "sin", "sinh", "tan", "tanh"}. 24 | } 25 | 26 | 27 | \section{Note}{ 28 | You can use the, somewhat more flexible, function \code{\link[raster]{calc}} instead of the Math-methods. 29 | } 30 | 31 | \seealso{ \code{\link[raster]{Arith-methods}}, \code{\link{calc}}, \code{\link{overlay}}, \code{\link{atan2}} } 32 | 33 | 34 | 35 | 36 | \examples{ 37 | r1 <- raster(nrow=10, ncol=10) 38 | r1 <- setValues(r1, runif(ncell(r1)) * 10) 39 | r2 <- sqrt(r1) 40 | s <- stack(r1, r2) - 5 41 | b <- abs(s) 42 | } 43 | 44 | \keyword{spatial} 45 | \keyword{methods} 46 | \keyword{math} 47 | -------------------------------------------------------------------------------- /man/NAvalue.Rd: -------------------------------------------------------------------------------- 1 | \name{NAvalue} 2 | 3 | \alias{NAvalue<-} 4 | \alias{NAvalue} 5 | 6 | \title{Set the NA value of a RasterLayer } 7 | 8 | \description{ 9 | NAvalue returns the value that is used to write NA values to disk (in 'raster' type files). 10 | If you set the NA value of a Raster* object, this value will be interpreted as NA when reading the values from a file. 11 | Values already in memory will not be affected. 12 | 13 | If the NA value is smaller than zero, all values smaller or equal to that number will be set to NA. 14 | } 15 | 16 | \usage{ 17 | NAvalue(x) <- value 18 | NAvalue(x) 19 | } 20 | 21 | \arguments{ 22 | \item{x}{A \code{Raster} object} 23 | \item{value}{the value to be interpreted as NA; set this before reading the values from the file. Integer values are matched exactly; for decimal values files any value <= the value will be interpreted as NA} 24 | } 25 | 26 | 27 | \value{ 28 | Returns or set the NA value used for storage on disk. 29 | } 30 | 31 | 32 | 33 | 34 | \examples{ 35 | r1 <- raster(system.file("external/rlogo.grd", package="raster")) 36 | r2 <- r1 37 | NAvalue(r2) 38 | NAvalue(r2) <- 255 39 | #plot(r1) 40 | #x11() 41 | #plot(r2) 42 | } 43 | 44 | \keyword{ spatial } 45 | 46 | -------------------------------------------------------------------------------- /man/Rcpp-classes.Rd: -------------------------------------------------------------------------------- 1 | \name{Rcpp-class} 2 | 3 | \docType{class} 4 | 5 | 6 | \alias{SpPoly} 7 | \alias{SpPolygons} 8 | \alias{SpPolyPart} 9 | \alias{SpExtent} 10 | 11 | \alias{SpPoly-class} 12 | \alias{SpPolyPart-class} 13 | \alias{SpPolygons-class} 14 | \alias{Rcpp_SpExtent-class} 15 | 16 | \alias{Rcpp_SpPolygons-class} 17 | \alias{Rcpp_SpPoly-class} 18 | \alias{Rcpp_SpPolyPart-class} 19 | 20 | \title{ Rcpp classes} 21 | 22 | \description{ 23 | These classes are for internal use only 24 | } 25 | 26 | 27 | \keyword{classes} 28 | \keyword{spatial} 29 | -------------------------------------------------------------------------------- /man/addLayer.Rd: -------------------------------------------------------------------------------- 1 | \name{addLayer} 2 | 3 | \alias{addLayer} 4 | \alias{addLayer,Raster-method} 5 | \alias{dropLayer} 6 | \alias{dropLayer,RasterStack-method} 7 | \alias{dropLayer,RasterBrick-method} 8 | 9 | \title{Add or drop a layer} 10 | 11 | \description{ 12 | Add a layer to a Raster* object or drop a layer from a RasterStack or RasterBrick. The object returned is always a RasterStack (unless nothing to add or drop was provided, in which case the original object is returned). 13 | } 14 | 15 | \usage{ 16 | addLayer(x, ...) 17 | dropLayer(x, i, ...) 18 | } 19 | 20 | \arguments{ 21 | \item{x}{Raster* object} 22 | \item{i}{integer. Indices of the layers to be dropped} 23 | \item{...}{Additional arguments. The layers to add for addLayer. None implemented for dropLayer)} 24 | } 25 | 26 | 27 | \value{ 28 | RasterStack 29 | } 30 | 31 | \seealso{ \code{\link[raster]{subset}}} 32 | 33 | \examples{ 34 | file <- system.file("external/test.grd", package="raster") 35 | s <- stack(file, file, file) 36 | r <- raster(file) 37 | s <- addLayer(s, r/2, r*2) 38 | s 39 | s <- dropLayer(s, c(3, 5)) 40 | nlayers(s) 41 | } 42 | 43 | \keyword{ spatial } 44 | -------------------------------------------------------------------------------- /man/alignExtent.Rd: -------------------------------------------------------------------------------- 1 | \name{alignExtent} 2 | 3 | \alias{alignExtent} 4 | 5 | \title{Align an extent (object of class Extent)} 6 | 7 | \description{ 8 | Align an Extent object with the (boundaries of the) cells of a Raster* object 9 | } 10 | 11 | \usage{ 12 | alignExtent(extent, object, snap='near') 13 | } 14 | 15 | \arguments{ 16 | \item{extent}{Extent object} 17 | \item{object}{Raster* object} 18 | \item{snap}{Character. One of 'near', 'in', or 'out', to determine in which direction the extent should be aligned. To the nearest border, inwards or outwards} 19 | } 20 | 21 | \value{ 22 | Extent object 23 | } 24 | 25 | \details{ 26 | Aligning an Extent object to another object assures that it gets the same origin and resolution. This should only be used to adjust objects because of imprecision in the data. alignExtent should not be used to force data to match that really does not match (use e.g. \code{\link{resample}} or (dis)aggregate for this). 27 | } 28 | 29 | 30 | 31 | 32 | \seealso{ \code{\link[raster]{extent}}, \code{\link[raster]{drawExtent}}, \code{\link[raster]{Extent-class}} } 33 | 34 | \examples{ 35 | r <- raster() 36 | e <- extent(-10.1, 9.9, -20.1, 19.9) 37 | ea <- alignExtent(e, r) 38 | e 39 | extent(r) 40 | ea 41 | 42 | } 43 | 44 | \keyword{spatial} 45 | -------------------------------------------------------------------------------- /man/animate.Rd: -------------------------------------------------------------------------------- 1 | \name{animate} 2 | 3 | \docType{methods} 4 | 5 | \alias{animate} 6 | \alias{animate,RasterStackBrick-method} 7 | 8 | \title{Animate layers of a Raster* object} 9 | 10 | \description{ 11 | Animate (sequentially plot) the layers of a RasterStack or RasterBrick* object to create a movie 12 | } 13 | 14 | \usage{ 15 | \S4method{animate}{RasterStackBrick}(x, pause=0.25, main, zlim, maxpixels=50000, n=10, ...) 16 | } 17 | 18 | \arguments{ 19 | \item{x}{Raster* object} 20 | \item{pause}{numeric. How long should be the pause be between layers?} 21 | \item{main}{title for each layer. If not supplied the z-value is used if available. Otherwise the names are used.} 22 | \item{zlim}{numeric vector of lenght 2. Range of values to plot} 23 | \item{maxpixels}{integer > 0. Maximum number of cells to use for the plot. If \code{maxpixels < ncell(x)}, \code{sampleRegular} is used before plotting} 24 | \item{n}{integer > 0. Number of loops} 25 | \item{...}{Additional arguments passed to \code{\link{plot}}} 26 | } 27 | 28 | 29 | \value{ 30 | None 31 | } 32 | 33 | \seealso{ \code{\link{plot}}, \code{\link{spplot}}, \code{\link{plotRGB}} } 34 | 35 | \examples{ 36 | b <- brick(system.file("external/rlogo.grd", package="raster")) 37 | animate(b, n=1) 38 | } 39 | 40 | \keyword{methods} 41 | \keyword{spatial} 42 | 43 | -------------------------------------------------------------------------------- /man/as.character.Rd: -------------------------------------------------------------------------------- 1 | \name{as.character} 2 | 3 | \alias{as.character} 4 | \alias{as.character,Raster-method} 5 | \alias{as.character,Extent-method} 6 | 7 | 8 | \title{Character representation of a Raster or Extent object} 9 | 10 | \description{ 11 | \code{as.character} returns a text (R code) representation of a Raster* or Extent object. The main purpose of this is to allow quick generation of objects to use in examples on, for example, stackoverflow.com. 12 | } 13 | 14 | \usage{ 15 | \S4method{as.character}{Raster}(x, ...) 16 | \S4method{as.character}{Extent}(x, ...) 17 | } 18 | 19 | \arguments{ 20 | \item{x}{ Raster* or Extent object } 21 | \item{...}{ additional arguments, none implemented } 22 | } 23 | 24 | 25 | \value{ 26 | character 27 | } 28 | 29 | \examples{ 30 | r <- raster(ncol=3, nrow=3) 31 | values(r) <- 1:ncell(r) 32 | as.character(r) 33 | s <- stack(r, r) 34 | as.character(s) 35 | as.character(extent(s)) 36 | 37 | x <- as.character(s) 38 | eval(parse(text=x)) 39 | 40 | y <- as.character(extent(s)) 41 | eval(parse(text=y)) 42 | } 43 | 44 | \keyword{spatial} 45 | \keyword{methods} 46 | -------------------------------------------------------------------------------- /man/as.list.Rd: -------------------------------------------------------------------------------- 1 | \name{as.list} 2 | 3 | \alias{as.list,Raster-method} 4 | 5 | \title{Create a list of RasterLayer objects} 6 | 7 | \description{ 8 | Create a list of RasterLayer objects from Raster* objects 9 | } 10 | 11 | \usage{ 12 | \S4method{as.list}{Raster}(x, ...) 13 | } 14 | 15 | \arguments{ 16 | \item{x}{ Raster* object } 17 | \item{...}{additional Raster* objects} 18 | } 19 | 20 | \value{ 21 | list 22 | } 23 | 24 | \examples{ 25 | r <- raster(ncol=3, nrow=3) 26 | values(r) <- 1:ncell(r) 27 | as.list(r) 28 | 29 | s <- stack(r,r*2,r*3) 30 | as.list(s, r) 31 | } 32 | 33 | \keyword{spatial} 34 | \keyword{methods} 35 | -------------------------------------------------------------------------------- /man/as.logical-methods.Rd: -------------------------------------------------------------------------------- 1 | \name{as.logical} 2 | 3 | \docType{methods} 4 | 5 | \alias{as.logical,Raster-method} 6 | \alias{as.integer,Raster-method} 7 | 8 | \title{ Change cell values to logical or integer values} 9 | 10 | \description{ 11 | Change values of a Raster* object to logical or integer values. With \code{as.logical}, zero becomes \code{FALSE}, all other values become \code{TRUE}. With \code{as.integer} values are truncated. 12 | } 13 | 14 | \usage{ 15 | \S4method{as.logical}{Raster}(x, filename='', ...) 16 | 17 | \S4method{as.integer}{Raster}(x, filename='', ...) 18 | } 19 | 20 | \arguments{ 21 | \item{x}{Raster* object} 22 | \item{filename}{character. Output filename (optional)} 23 | \item{...}{additional optional arguments as for \code{\link{writeRaster}}} 24 | } 25 | 26 | \seealso{ \code{\link[base]{logical}}, \code{\link[base]{integer}} } 27 | 28 | \examples{ 29 | r <- raster(nrow=10, ncol=10) 30 | set.seed(0) 31 | values(r) <- runif(ncell(r)) * 10 32 | r 33 | r <- as.integer(r) 34 | r 35 | as.logical(r) 36 | } 37 | 38 | 39 | \keyword{methods} 40 | \keyword{spatial} 41 | -------------------------------------------------------------------------------- /man/as.raster.Rd: -------------------------------------------------------------------------------- 1 | \name{as.raster} 2 | 3 | \alias{as.raster} 4 | \alias{as.raster,RasterLayer-method} 5 | 6 | \title{Coerce to a 'raster' object} 7 | 8 | \description{ 9 | Implementation of the generic \code{\link[grDevices]{as.raster}} function to create a 'raster' (small r) object. NOT TO BE CONFUSED with the Raster* (big R) objects defined by the raster package! Such objects can be used for plotting with the \code{\link[graphics]{rasterImage}} function. 10 | } 11 | 12 | \usage{ 13 | as.raster(x, ...) 14 | } 15 | 16 | \arguments{ 17 | \item{x}{ RasterLayer object } 18 | \item{...}{ Additional arguments. 19 | 20 | \code{maxpixels} Integer. To regularly subsample very large objects 21 | 22 | \code{col} Vector of colors. Default is col=rev(terrain.colors(255))) 23 | } 24 | } 25 | 26 | 27 | \value{ 28 | 'raster' object 29 | } 30 | 31 | 32 | 33 | \examples{ 34 | r <- raster(ncol=3, nrow=3) 35 | values(r) <- 1:ncell(r) 36 | as.raster(r) 37 | } 38 | 39 | \keyword{spatial} 40 | \keyword{methods} 41 | -------------------------------------------------------------------------------- /man/atan2.Rd: -------------------------------------------------------------------------------- 1 | \name{atan2} 2 | 3 | \alias{atan2,Raster,Raster-method} 4 | \alias{atan2} 5 | 6 | \title{Two argument arc-tangent} 7 | 8 | \description{ 9 | For Raster* objects x and y, atan2(y, x) returns the angle in radians for the tangent y/x, handling the case when x is zero. See \code{\link[base]{Trig}} 10 | 11 | See \code{\link[raster]{Math-methods}} for other trigonometric and mathematical functions that can be used with Raster* objects. 12 | } 13 | 14 | \usage{ 15 | atan2(y, x) 16 | } 17 | 18 | \arguments{ 19 | \item{y}{Raster* object} 20 | \item{x}{Raster* object} 21 | } 22 | 23 | \seealso{ \code{\link[raster]{Math-methods}} } 24 | 25 | 26 | \examples{ 27 | r1 <- r2 <- raster(nrow=10, ncol=10) 28 | values(r1) <- (runif(ncell(r1))-0.5) * 10 29 | values(r2) <- (runif(ncell(r1))-0.5) * 10 30 | atan2(r1, r2) 31 | } 32 | 33 | \keyword{methods} 34 | \keyword{math} 35 | -------------------------------------------------------------------------------- /man/bands.Rd: -------------------------------------------------------------------------------- 1 | \name{bands} 2 | 3 | \alias{bandnr} 4 | \alias{bandnr,RasterLayer-method} 5 | \alias{nbands} 6 | 7 | \title{Number of bands} 8 | 9 | \description{ 10 | A 'band' refers to a single layer for a possibly multi-layer file. Most RasterLayer objects will refer to files with a single layer. 11 | The term 'band' is frequently used in remote sensing to refer to a variable (layer) in a multi-variable dataset as these variables typically reperesent reflection in different bandwidths in the electromagnetic spectrum. But in that context, bands could be stored in a single or in separate files. In the context of the raster package, the term band is equivalent to a layer in a raster file. 12 | 13 | \code{nbands} returns the number of bands of the file that a RasterLayer points to (and 1 if it does not point at any file). This functions also works for a RasterStack for which it is equivalent to \code{\link{nlayers}}. 14 | 15 | \code{band} returns the specific band the RasterLayer refers to (1 if the RasterLayer points at single layer file or does not point at any file). 16 | } 17 | 18 | \usage{ 19 | nbands(x) 20 | bandnr(x, ...) 21 | } 22 | 23 | \arguments{ 24 | \item{x}{RasterLayer} 25 | \item{...}{Additional arguments (none at this time)} 26 | } 27 | 28 | \seealso{\code{\link[raster]{nlayers}}} 29 | 30 | \value{ 31 | numeric >= 1 32 | } 33 | 34 | 35 | \examples{ 36 | f <- system.file("external/rlogo.grd", package="raster") 37 | r <- raster(f, layer=2) 38 | nbands(r) 39 | bandnr(r) 40 | } 41 | 42 | \keyword{spatial} 43 | -------------------------------------------------------------------------------- /man/blockSize.Rd: -------------------------------------------------------------------------------- 1 | \name{blockSize} 2 | 3 | \alias{blockSize} 4 | \alias{blockSize,Raster-method} 5 | 6 | \title{Block size for writing files} 7 | 8 | \description{ 9 | This function can be used to suggest chunk sizes (always a number of entire rows), and corresponding row numbers, to be used when processing Raster* objects in chunks. Normally used together with \code{\link{writeValues}}. 10 | } 11 | 12 | \usage{ 13 | \S4method{blockSize}{Raster}(x, chunksize, n=nlayers(x), minblocks=4, minrows=1) 14 | } 15 | 16 | \arguments{ 17 | \item{x}{Raster* object} 18 | \item{chunksize}{Integer, normally missing. Can be used to set the block size; unit is number of cells. Block size is then computed in units of number of rows (always >= 1) } 19 | \item{n}{Integer. number of layers to consider. The function divides chunksize by n to determine blocksize } 20 | \item{minblocks}{Integer. Minimum number of blocks } 21 | \item{minrows}{Integer. Minimum number of rows in each block } 22 | } 23 | 24 | 25 | \value{ 26 | A list with three elements: 27 | 28 | \code{rows}, the suggested row numbers at which to start the blocks for reading and writing, 29 | 30 | \code{nrows}, the number of rows in each block, and, 31 | 32 | \code{n}, the total number of blocks 33 | } 34 | 35 | 36 | \seealso{ \code{\link[raster]{writeValues}} } 37 | 38 | \examples{ 39 | r <- raster(system.file("external/test.grd", package="raster")) 40 | blockSize(r) 41 | } 42 | 43 | \keyword{ spatial } 44 | \keyword{ methods } 45 | -------------------------------------------------------------------------------- /man/boxplot.Rd: -------------------------------------------------------------------------------- 1 | \name{boxplot} 2 | 3 | \docType{methods} 4 | 5 | \alias{boxplot} 6 | \alias{boxplot,RasterLayer-method} 7 | \alias{boxplot,RasterStackBrick-method} 8 | 9 | \title{ 10 | Box plot of Raster objects 11 | } 12 | 13 | \description{ 14 | Box plot of layers in a Raster object 15 | } 16 | 17 | \usage{ 18 | \S4method{boxplot}{RasterStackBrick}(x, maxpixels=100000, ...) 19 | 20 | \S4method{boxplot}{RasterLayer}(x, y=NULL, maxpixels=100000, ...) 21 | } 22 | 23 | \arguments{ 24 | \item{x}{Raster* object} 25 | \item{y}{If \code{x} is a RasterLayer object, y can be an additional RasterLayer to group the values of \code{x} by 'zone'} 26 | \item{maxpixels}{Integer. Number of pixels to sample from each layer of large Raster objects} 27 | \item{...}{Arguments passed to \code{graphics::\link[graphics]{boxplot}}} 28 | } 29 | 30 | \seealso{ 31 | \code{\link{pairs}, \link{hist}} 32 | } 33 | 34 | \examples{ 35 | r1 <- r2 <- r3 <- raster(ncol=10, nrow=10) 36 | values(r1) <- rnorm(ncell(r1), 100, 40) 37 | values(r2) <- rnorm(ncell(r1), 80, 10) 38 | values(r3) <- rnorm(ncell(r1), 120, 30) 39 | s <- stack(r1, r2, r3) 40 | names(s) <- c('A', 'B', 'C') 41 | 42 | boxplot(s, notch=TRUE, col=c('red', 'blue', 'orange'), main='Box plot', ylab='random' ) 43 | } 44 | 45 | 46 | \keyword{spatial} 47 | 48 | -------------------------------------------------------------------------------- /man/cellsFromExtent.Rd: -------------------------------------------------------------------------------- 1 | \name{cellsFromExtent} 2 | 3 | \alias{cellsFromExtent} 4 | \alias{extentFromCells} 5 | 6 | \title{Cells from extent, and vice versa} 7 | 8 | \description{ 9 | cellsFromExtent returns the cell numbers for a Raster* object that are within a specfied extent (rectangular area), supply an object of class Extent, or another Raster* object. 10 | 11 | extentFromCells returns an Extent object from a Raster* object and cell numbers. All cells are within the returned Extent. 12 | } 13 | 14 | \usage{ 15 | cellsFromExtent(object, extent, expand=FALSE) 16 | extentFromCells(object, cells) 17 | } 18 | 19 | \arguments{ 20 | \item{object}{A Raster* object} 21 | \item{extent}{An object of class Extent (which you can create with newExtent(), or another Raster* object )} 22 | \item{expand}{Logical. If \code{TRUE}, \code{NA} is returned for (virtual) cells implied by \code{bndbox}, 23 | that are outside the RasterLayer (\code{object}). If \code{FALSE}, only cell numbers for the area where \code{object} and \code{bndbox} 24 | overlap are returned (see \link[raster]{intersect}) } 25 | \item{cells}{numeric. A vector of cell numbers} 26 | } 27 | 28 | \value{ 29 | a vector of cell numbers 30 | } 31 | 32 | 33 | 34 | \seealso{ 35 | \code{\link[raster]{extent}}, \code{\link{cellFromXY}} 36 | } 37 | 38 | \examples{ 39 | r <- raster() 40 | bb <- extent(-5, 5, -5, 5) 41 | cells <- cellsFromExtent(r, bb) 42 | r <- crop(r, bb) 43 | values(r) <- cells 44 | 45 | e <- extentFromCells(r, 50:55) 46 | } 47 | 48 | \keyword{spatial} 49 | -------------------------------------------------------------------------------- /man/clamp.Rd: -------------------------------------------------------------------------------- 1 | \name{clamp} 2 | 3 | \alias{clamp} 4 | \alias{clamp,Raster-method} 5 | \alias{clamp,numeric-method} 6 | 7 | \title{Clamp values} 8 | 9 | \description{ 10 | Clamp values to a minimum and maximum value. That is, all values below the lower clamp value and above the upper clamp value become NA (or the lower/upper value if \code{useValue=TRUE}) 11 | } 12 | 13 | \usage{ 14 | \S4method{clamp}{Raster}(x, lower=-Inf, upper=Inf, useValues=TRUE, filename="", ...) 15 | \S4method{clamp}{numeric}(x, lower=-Inf, upper=Inf, ...) 16 | } 17 | 18 | \arguments{ 19 | \item{x}{RasterLayer, or numeric vector} 20 | \item{lower}{numeric. lowest value} 21 | \item{upper}{numeric. highest value} 22 | \item{useValues}{logical. If \code{FALSE} values outside the clamping range become \code{NA}, if \code{TRUE}, they get the extreme values} 23 | \item{filename}{character. Filename for the output RasterLayer (optional)} 24 | \item{...}{additional arguments as for \code{\link{writeRaster}}} 25 | } 26 | 27 | \value{ 28 | Raster object 29 | } 30 | 31 | \seealso{ 32 | \code{\link{reclassify}} 33 | } 34 | 35 | \examples{ 36 | r <- raster(ncols=12, nrows=12) 37 | values(r) <- 1:ncell(r) 38 | rc <- clamp(r, 25, 75) 39 | rc 40 | } 41 | 42 | \keyword{spatial} 43 | 44 | -------------------------------------------------------------------------------- /man/clearValues.Rd: -------------------------------------------------------------------------------- 1 | 2 | \name{clearValues} 3 | 4 | \alias{clearValues} 5 | 6 | \title{Clear values} 7 | 8 | \description{ 9 | Clear cell values of a Raster* object from memory 10 | } 11 | 12 | \usage{ 13 | clearValues(x) 14 | } 15 | 16 | \arguments{ 17 | \item{x}{Raster* object } 18 | } 19 | 20 | 21 | \seealso{ \code{\link{values}}, \code{\link[raster]{replacement} }} 22 | 23 | 24 | \value{ 25 | a Raster* object 26 | } 27 | 28 | 29 | 30 | \examples{ 31 | r <- raster(ncol=10, nrow=10) 32 | values(r) <- 1:ncell(r) 33 | r <- clearValues(r) 34 | } 35 | 36 | 37 | \keyword{ spatial } 38 | \keyword{ methods } 39 | 40 | -------------------------------------------------------------------------------- /man/colortable.Rd: -------------------------------------------------------------------------------- 1 | \name{colortable} 2 | 3 | \alias{colortable} 4 | \alias{colortable<-} 5 | 6 | \title{colortable} 7 | 8 | \description{ 9 | Get or set the colortable of a RasterLayer. A colortable is a vector of 256 colors in the RGB triple format as returned by the \code{\link{rgb}} function (e.g. "#C4CDDA"). 10 | 11 | When setting the colortable, it is assumed that the values are integers in the range [0,255] 12 | } 13 | 14 | \usage{ 15 | colortable(x) 16 | colortable(x) <- value 17 | } 18 | 19 | \arguments{ 20 | \item{x}{RasterLayer object} 21 | \item{value}{vector of 256 character values} 22 | } 23 | 24 | \seealso{ \code{\link[raster]{plotRGB}} } 25 | 26 | \examples{ 27 | r <- raster(ncol=10, nrow=10) 28 | values(r) <- sample(0:255, ncell(r), replace=TRUE) 29 | ctab <- sample(rainbow(256)) 30 | colortable(r) <- ctab 31 | plot(r) 32 | head(colortable(r)) 33 | } 34 | 35 | \keyword{spatial} 36 | -------------------------------------------------------------------------------- /man/compareCRS.Rd: -------------------------------------------------------------------------------- 1 | \name{compareCRS} 2 | 3 | \alias{compareCRS} 4 | 5 | 6 | \title{ Partially compare two CRS objects } 7 | 8 | \description{ 9 | Compare CRS objects 10 | } 11 | 12 | \usage{ 13 | compareCRS(x, y, unknown=FALSE, verbatim=FALSE, verbose=FALSE) 14 | } 15 | 16 | \arguments{ 17 | \item{x}{CRS object, or object from which it can be extracted with \code{\link{projection}}, or PROJ.4 format character string} 18 | \item{y}{same as \code{x}} 19 | \item{unknown}{logical. Return \code{TRUE} if \code{x} or \code{y} is \code{TRUE}} 20 | \item{verbatim}{logical. If \code{TRUE} compare \code{x} and \code{y}, verbatim (not partially)} 21 | \item{verbose}{logical. If \code{TRUE}, messages about the comparison may be printed} 22 | } 23 | 24 | 25 | \value{ 26 | logical 27 | } 28 | 29 | 30 | \seealso{\code{sp::identicalCRS}, \code{\link{crs} }} 31 | 32 | 33 | \examples{ 34 | compareCRS("+proj=lcc +lat_1=48 +lat_2=33 +lon_0=-100 +ellps=WGS84", 35 | "+proj=longlat +datum=WGS84") 36 | compareCRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0", 37 | "+proj=longlat +datum=WGS84") 38 | compareCRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0", 39 | "+proj=longlat +datum=WGS84", verbatim=TRUE) 40 | compareCRS("+proj=longlat +datum=WGS84", NA) 41 | compareCRS("+proj=longlat +datum=WGS84", NA, unknown=TRUE) 42 | } 43 | 44 | \keyword{ spatial } 45 | 46 | -------------------------------------------------------------------------------- /man/contour.Rd: -------------------------------------------------------------------------------- 1 | \name{contour} 2 | 3 | \docType{methods} 4 | 5 | \alias{contour} 6 | \alias{contour,RasterLayer-method} 7 | 8 | \title{Contour plot} 9 | 10 | \description{ 11 | Contour plot of a RasterLayer. 12 | } 13 | 14 | \usage{ 15 | \S4method{contour}{RasterLayer}(x, maxpixels=100000, ...) 16 | } 17 | 18 | \arguments{ 19 | \item{x}{Raster* object} 20 | \item{maxpixels}{maximum number of pixels used to create the contours} 21 | \item{...}{any argument that can be passed to \code{\link[graphics]{contour}} (graphics package)} 22 | } 23 | 24 | 25 | \seealso{ 26 | \code{\link{persp}}, \code{\link{filledContour}}, \code{\link{rasterToContour} } 27 | } 28 | 29 | \examples{ 30 | r <- raster(system.file("external/test.grd", package="raster")) 31 | plot(r) 32 | contour(r, add=TRUE) 33 | } 34 | 35 | \keyword{methods} 36 | \keyword{spatial} 37 | 38 | -------------------------------------------------------------------------------- /man/cut.Rd: -------------------------------------------------------------------------------- 1 | \name{cut} 2 | 3 | \docType{methods} 4 | 5 | \alias{cut} 6 | \alias{cut,Raster-method} 7 | 8 | \title{Convert values to classes} 9 | 10 | \description{ 11 | Cut uses the base function \code{\link[base]{cut}} to classify the values of a Raster* object according to which interval they fall in. The intervals are defined by the argument \code{breaks}. The leftmost interval corresponds to level one, the next leftmost to level two and so on. 12 | } 13 | 14 | \usage{ 15 | cut(x, ...) 16 | } 17 | 18 | \arguments{ 19 | \item{x}{A Raster* object} 20 | \item{...}{additional arguments. See \link[base]{cut}} 21 | } 22 | 23 | \value{ 24 | Raster* object 25 | } 26 | 27 | \seealso{ \code{ \link{subs}, \link{reclassify}, \link{calc}} } 28 | 29 | 30 | 31 | 32 | \examples{ 33 | 34 | r <- raster(ncols=36, nrows=18) 35 | values(r) <- rnorm(ncell(r)) 36 | breaks <- -2:2 * 3 37 | rc <- cut(r, breaks=breaks) 38 | } 39 | 40 | \keyword{spatial} 41 | -------------------------------------------------------------------------------- /man/cv.Rd: -------------------------------------------------------------------------------- 1 | \name{cv} 2 | 3 | \alias{cv} 4 | \alias{cv,ANY-method} 5 | \alias{cv,Raster-method} 6 | 7 | \title{Coefficient of variation} 8 | 9 | \description{ 10 | Compute the coefficient of variation (expressed as a percentage). If there is only a single value, \code{sd} is \code{NA} and \code{cv} returns \code{NA} if \code{aszero=FALSE} (the default). However, if (\code{aszero=TRUE}), \code{cv} returns \code{0}. 11 | } 12 | 13 | \usage{ 14 | \S4method{cv}{ANY}(x, ..., aszero=FALSE, na.rm = FALSE) 15 | 16 | \S4method{cv}{Raster}(x, ..., aszero=FALSE, na.rm = FALSE) 17 | } 18 | 19 | \arguments{ 20 | \item{x}{A vector of numbers (typically integers for modal), or a Raster* object} 21 | \item{...}{additional (vectors of) numbers, or Raster objects} 22 | \item{aszero}{logical. If \code{TRUE}, a zero is returned (rather than an NA) if the cv of single value is computed} 23 | \item{na.rm}{Remove (ignore) NA values} 24 | } 25 | 26 | \value{ 27 | vector or RasterLayer 28 | } 29 | 30 | 31 | 32 | \examples{ 33 | data <- c(0,1,2,3,3,3,3,4,4,4,5,5,6,7,7,8,9,NA) 34 | cv(data, na.rm=TRUE) 35 | } 36 | 37 | \keyword{univar} 38 | \keyword{math} 39 | -------------------------------------------------------------------------------- /man/datasource.Rd: -------------------------------------------------------------------------------- 1 | \name{datasource} 2 | 3 | \alias{fromDisk} 4 | \alias{inMemory} 5 | \alias{inMemory,BasicRaster-method} 6 | \alias{hasValues} 7 | \alias{hasValues,BasicRaster-method} 8 | 9 | 10 | \title{Are values in memory and/or on disk?} 11 | 12 | \description{ 13 | These are helper functions for programmers and for debugging that provide information about whether a Raster object has associated values, and if these are in memory or on disk. 14 | 15 | \code{fromDisk} is \code{TRUE} if the data source is a file on disk; and \code{FALSE} if the object only exists in memory. 16 | 17 | \code{inMemory}i is \code{TRUE} if all values are currently in memory (RAM); and \code{FALSE} if not (in which case they either are on disk, or there are no values). 18 | 19 | \code{hasValues} is \code{TRUE} if the object has cell values. 20 | } 21 | 22 | \usage{ 23 | fromDisk(x) 24 | \S4method{inMemory}{BasicRaster}(x) 25 | \S4method{hasValues}{BasicRaster}(x) 26 | } 27 | 28 | \arguments{ 29 | \item{x}{ Raster* object } 30 | } 31 | 32 | \value{ 33 | Logical 34 | } 35 | 36 | \examples{ 37 | rs <- raster(system.file("external/test.grd", package="raster")) 38 | inMemory(rs) 39 | fromDisk(rs) 40 | rs <- readAll(rs) 41 | inMemory(rs) 42 | fromDisk(rs) 43 | rs <- rs + 1 44 | inMemory(rs) 45 | fromDisk(rs) 46 | rs <- raster(rs) 47 | inMemory(rs) 48 | fromDisk(rs) 49 | rs <- setValues(rs, 1:ncell(rs)) 50 | inMemory(rs) 51 | fromDisk(rs) 52 | #rs <- writeRaster(rs, filename=rasterTmpFile(), overwrite=TRUE) 53 | #inMemory(rs) 54 | #fromDisk(rs) 55 | } 56 | 57 | \keyword{ spatial } 58 | -------------------------------------------------------------------------------- /man/density.Rd: -------------------------------------------------------------------------------- 1 | \name{density} 2 | 3 | \alias{density} 4 | \alias{density,Raster-method} 5 | 6 | \docType{methods} 7 | 8 | \title{Density plot} 9 | 10 | \description{ 11 | Create density plots of values in a Raster object 12 | } 13 | 14 | \usage{ 15 | \S4method{density}{Raster}(x, layer, maxpixels=100000, plot=TRUE, main, ...) 16 | } 17 | 18 | \arguments{ 19 | \item{x}{Raster object} 20 | \item{layer}{numeric. Can be used to subset the layers to plot in a multilayer object (RasterBrick or RasterStack)} 21 | \item{maxpixels}{the maximum number of (randomly sampled) cells to be used for creating the plot} 22 | \item{plot}{if \code{TRUE} produce a plot, else return a density object} 23 | \item{main}{main title for each plot (can be missing)} 24 | \item{...}{Additional arguments passed to base plot} 25 | } 26 | 27 | \value{ 28 | density plot (and a density object, returned invisibly if \code{plot=TRUE)} 29 | } 30 | 31 | \examples{ 32 | logo <- stack(system.file("external/rlogo.grd", package="raster")) 33 | density(logo) 34 | } 35 | 36 | \keyword{spatial} 37 | -------------------------------------------------------------------------------- /man/dimensions.Rd: -------------------------------------------------------------------------------- 1 | \name{dim} 2 | 3 | \alias{dim} 4 | \alias{dim,RasterStackBrick-method} 5 | \alias{dim,BasicRaster-method} 6 | \alias{dim<-,BasicRaster-method} 7 | \alias{dim<-,RasterLayer-method} 8 | \alias{dim<-,RasterBrick-method} 9 | 10 | \docType{methods} 11 | 12 | \title{Dimensions of a Raster* object} 13 | 14 | \description{ 15 | Get or set the number of rows, columns, and layers of a Raster* object. You cannot use this function to set the dimensions of a RasterStack object. 16 | 17 | When setting the dimensions, you can provide a row number, or a vector with the row and the column number (for a RasterLayer and a RasterBrick), or a row and column number and the number of layers (only for a RasterBrick) 18 | } 19 | 20 | \usage{ 21 | \S4method{dim}{BasicRaster}(x) 22 | } 23 | 24 | \arguments{ 25 | \item{x}{Raster(* object} 26 | } 27 | 28 | \value{ 29 | Integer or Raster* object 30 | } 31 | \seealso{ \code{ \link{ncell}, \link{extent}, \link{res} } } 32 | 33 | 34 | 35 | \examples{ 36 | r <- raster() 37 | dim(r) 38 | dim(r) <- c(18) 39 | dim(r) 40 | dim(r) <- c(18, 36) 41 | dim(r) 42 | b <- brick(r) 43 | dim(b) 44 | dim(b) <- c(10, 10, 5) 45 | dim(b) 46 | } 47 | 48 | \keyword{spatial} 49 | 50 | -------------------------------------------------------------------------------- /man/direction.Rd: -------------------------------------------------------------------------------- 1 | \name{direction} 2 | 3 | \alias{direction} 4 | \alias{direction,RasterLayer-method} 5 | 6 | \title{Direction} 7 | 8 | \description{ 9 | The direction (azimuth) to or from the nearest cell that is not \code{NA}. The direction unit is in radians, unless you use argument \code{degrees=TRUE}. 10 | } 11 | 12 | \usage{ 13 | \S4method{direction}{RasterLayer}(x, filename='', degrees=FALSE, from=FALSE, doEdge=FALSE, ...) 14 | } 15 | 16 | \arguments{ 17 | \item{x}{RasterLayer object} 18 | \item{filename}{Character. Output filename (optional)} 19 | \item{degrees}{Logical. If \code{FALSE} (the default) the unit of direction is radians.} 20 | \item{from}{Logical. Default is \code{FALSE}. If \code{TRUE}, the direction from (instead of to) the nearest cell that is not \code{NA} is returned} 21 | \item{doEdge}{Logical. If \code{TRUE}, the \code{\link{boundaries}} function is called first. This may be efficient in cases where you compute the distance to large blobs. Calling \code{boundaries} determines the edge cells that matter for direction computation} 22 | \item{...}{Additional arguments as for \code{\link{writeRaster}}} 23 | } 24 | 25 | 26 | 27 | \value{RasterLayer} 28 | 29 | 30 | \seealso{ 31 | \code{\link[raster]{distance}}, \code{\link[raster]{gridDistance}} 32 | 33 | For the direction between (longitude/latitude) points, see the \code{azimuth} function in the \code{geosphere} package 34 | } 35 | 36 | 37 | \examples{ 38 | r <- raster(ncol=36,nrow=18) 39 | values(r) <- NA 40 | r[306] <- 1 41 | b <- direction(r) 42 | #plot(b) 43 | } 44 | 45 | \keyword{spatial} 46 | -------------------------------------------------------------------------------- /man/draw.Rd: -------------------------------------------------------------------------------- 1 | \name{draw} 2 | 3 | \alias{drawPoly} 4 | \alias{drawLine} 5 | 6 | \title{ Draw a line or polygon } 7 | 8 | \description{ 9 | Draw a line or polygon on a plot (map) and save it for later use. After calling the function, start clicking on the map. To finish, right-click and select 'stop'. 10 | } 11 | 12 | \usage{ 13 | drawPoly(sp=TRUE, col='red', lwd=2, ...) 14 | drawLine(sp=TRUE, col='red', lwd=2, ...) 15 | } 16 | 17 | \arguments{ 18 | \item{sp}{logical. If \code{TRUE}, the output will be a sp object (SpatialPolygons or SpatialLines). Otherwise a matrix of coordinates is returned} 19 | \item{col}{the color of the lines to be drawn} 20 | \item{lwd}{the width of the lines to be drawn} 21 | \item{...}{additional arguments padded to locator} 22 | } 23 | 24 | \value{ 25 | If \code{sp==TRUE} a SpatialPolygons or SpatialLines object; otherwise a matrix of coordinates 26 | } 27 | 28 | \seealso{ \code{\link[graphics]{locator}} } 29 | 30 | 31 | 32 | 33 | 34 | \keyword{ spatial } 35 | -------------------------------------------------------------------------------- /man/drawExtent.Rd: -------------------------------------------------------------------------------- 1 | \name{drawExtent} 2 | \alias{drawExtent} 3 | 4 | \title{ Create an Extent object by drawing on a map} 5 | 6 | \description{ 7 | Click on two points of a plot (map) to obtain an object of class \code{\link{Extent}} ('bounding box') 8 | } 9 | 10 | \usage{ 11 | drawExtent(show=TRUE, col="red") 12 | } 13 | 14 | \arguments{ 15 | \item{show}{logical. If \code{TRUE}, the extent will be drawn on the map} 16 | \item{col}{sets the color of the lines of the extent } 17 | } 18 | 19 | \value{ 20 | Extent 21 | } 22 | 23 | 24 | \examples{ 25 | \dontrun{ 26 | r1 <- raster(nrow=10, ncol=10) 27 | values(r1) <- runif(ncell(r1)) 28 | plot(r1) 29 | # after running the following line, click on the map twice 30 | e <- drawExtent() 31 | # after running the following line, click on the map twice 32 | mean(values(crop(r1, drawExtent()))) 33 | } 34 | } 35 | 36 | \keyword{ spatial } 37 | -------------------------------------------------------------------------------- /man/extension.Rd: -------------------------------------------------------------------------------- 1 | \name{extension} 2 | 3 | \alias{extension} 4 | \alias{extension<-} 5 | 6 | \title{Filename extensions} 7 | 8 | \description{ 9 | Get or change a filename extension 10 | } 11 | 12 | \usage{ 13 | extension(filename, value=NULL, maxchar=10) 14 | extension(filename) <- value 15 | } 16 | 17 | \arguments{ 18 | \item{filename}{A filename, with or without the path} 19 | \item{value}{A file extension with or without a dot, e.g., ".txt" or "txt"} 20 | \item{maxchar}{Maximum number of characters after the last dot in the filename, for that string to be considered a filename extension } 21 | } 22 | 23 | \value{ 24 | A file extension, filename or path. 25 | 26 | If \code{ext(filename)} is used without a \code{value} argument, it returns the file extension; otherwise it returns the filename (with new extensions set to \code{value} 27 | } 28 | 29 | 30 | 31 | 32 | \examples{ 33 | fn <- "c:/temp folder/filename.exten sion" 34 | extension(fn) 35 | extension(fn) <- ".txt" 36 | extension(fn) 37 | fn <- extension(fn, '.document') 38 | extension(fn) 39 | extension(fn, maxchar=4) 40 | } 41 | 42 | \keyword{file} 43 | 44 | -------------------------------------------------------------------------------- /man/extremeValues.Rd: -------------------------------------------------------------------------------- 1 | \name{extremeValues} 2 | 3 | \alias{minValue} 4 | \alias{maxValue} 5 | \alias{minValue,RasterLayer-method} 6 | \alias{minValue,RasterStack-method} 7 | \alias{minValue,RasterBrick-method} 8 | \alias{maxValue,RasterLayer-method} 9 | \alias{maxValue,RasterStack-method} 10 | \alias{maxValue,RasterBrick-method} 11 | 12 | 13 | \title{Minimum and maximum values} 14 | \description{ 15 | Returns the minimum or maximum value of a RasterLayer or layer in a RasterStack 16 | } 17 | 18 | \usage{ 19 | minValue(x, ...) 20 | maxValue(x, ...) 21 | } 22 | 23 | \arguments{ 24 | \item{x}{RasterLayer or RasterStack object} 25 | \item{...}{Additional argument: layer number (for RasterStack or RasterBrick objects) } 26 | } 27 | 28 | \value{ 29 | a number 30 | } 31 | 32 | \details{ 33 | If a Raster* object is created from a file on disk, the min and max values are often not known (depending on the file format). You can use \code{\link[raster]{setMinMax}} to set them in the Raster* object. 34 | } 35 | 36 | 37 | \examples{ 38 | r <- raster() 39 | r <- setValues(r, 1:ncell(r)) 40 | minValue(r) 41 | maxValue(r) 42 | r <- setValues(r, round(100 * runif(ncell(r)) + 0.5)) 43 | minValue(r) 44 | maxValue(r) 45 | 46 | r <- raster(system.file("external/test.grd", package="raster")) 47 | minValue(r) 48 | maxValue(r) 49 | } 50 | 51 | \keyword{spatial} 52 | -------------------------------------------------------------------------------- /man/filename.Rd: -------------------------------------------------------------------------------- 1 | \name{filename} 2 | 3 | \alias{filename} 4 | 5 | \title{Filename} 6 | 7 | \description{ 8 | Get the filename of a Raster* object. You cannot set the filename of an object (except for RasterStack objects); but you can provide a 'filename= ' argument to a function that creates a new RasterLayer or RasterBrick* object. 9 | } 10 | 11 | \usage{ 12 | filename(x) 13 | } 14 | 15 | \arguments{ 16 | \item{x}{A Raster* object } 17 | } 18 | 19 | \value{ 20 | a Raster* object 21 | } 22 | 23 | 24 | 25 | \examples{ 26 | r <- raster( system.file("external/test.grd", package="raster") ) 27 | filename(r) 28 | } 29 | 30 | \keyword{ spatial } 31 | 32 | -------------------------------------------------------------------------------- /man/filledContour.Rd: -------------------------------------------------------------------------------- 1 | \name{filledContour} 2 | 3 | \alias{filledContour} 4 | 5 | \title{Filled contour plot} 6 | 7 | \description{ 8 | Filled contour plot of a RasterLayer. This is a wrapper around \code{\link[graphics]{filled.contour}} for RasterLayer objects. 9 | } 10 | 11 | \usage{ 12 | filledContour(x, y=1, maxpixels=100000, ...) 13 | } 14 | 15 | \arguments{ 16 | \item{x}{A Raster* object} 17 | \item{y}{Integer. The layer number of x (if x has multiple layers)} 18 | \item{maxpixels}{The maximum number of pixels} 19 | \item{...}{Any argument that can be passed to \code{\link[graphics]{filled.contour}} (graphics package)} 20 | } 21 | 22 | \seealso{ \code{\link[graphics]{filled.contour}}, \code{\link[raster]{persp}}, \code{\link[raster]{plot}} } 23 | 24 | 25 | 26 | \examples{ 27 | r <- raster(system.file("external/test.grd", package="raster")) 28 | filledContour(r) 29 | } 30 | 31 | \keyword{methods} 32 | \keyword{spatial} 33 | 34 | -------------------------------------------------------------------------------- /man/flip.Rd: -------------------------------------------------------------------------------- 1 | \name{flip} 2 | 3 | \docType{methods} 4 | 5 | \alias{flip} 6 | \alias{flip,RasterLayer-method} 7 | \alias{flip,RasterStackBrick-method} 8 | 9 | \title{Flip} 10 | 11 | \description{ 12 | Flip the values of a Raster* object by inverting the order of the rows (direction=y) or the columns direction='x'. 13 | } 14 | 15 | \usage{ 16 | \S4method{flip}{RasterLayer}(x, direction='y', filename='', ...) 17 | 18 | \S4method{flip}{RasterStackBrick}(x, direction='y', filename='', ...) 19 | } 20 | 21 | \arguments{ 22 | \item{x}{Raster* object} 23 | \item{direction}{Character. 'y' or 'x'; or 1 (=x) or 2 (=y)} 24 | \item{filename}{character. Output filename (optional)} 25 | \item{...}{if \code{x} is a Raster* object, additional arguments as for \code{\link{writeRaster}}} 26 | } 27 | 28 | \value{ 29 | RasterLayer or RasterBrick 30 | } 31 | 32 | 33 | \seealso{ transpose: \code{\link{t}}, \code{\link[raster]{rotate}} } 34 | 35 | 36 | 37 | \examples{ 38 | r <- raster(nrow=18, ncol=36) 39 | m <- matrix(1:ncell(r), nrow=18) 40 | values(r) <- as.vector(t(m)) 41 | rx <- flip(r, direction='x') 42 | values(r) <- as.vector(m) 43 | ry <- flip(r, direction='y') 44 | } 45 | 46 | \keyword{spatial} 47 | -------------------------------------------------------------------------------- /man/flowpath.Rd: -------------------------------------------------------------------------------- 1 | \name{flowPath} 2 | 3 | \alias{flowPath} 4 | 5 | \title{Flow path} 6 | 7 | \description{ 8 | Compute the flow path (drainage path) starting at a given point. See package \code{gdistance} for more path computations. 9 | } 10 | 11 | \usage{ 12 | flowPath(x, p, ...) 13 | } 14 | 15 | \arguments{ 16 | \item{x}{RasterLayer of flow direction (as can be created with \code{\link{terrain}}} 17 | \item{p}{starting point. Either two numbers: x (longitude) and y (latitude) coordinates; or a single cell number } 18 | \item{...}{additional arguments (none implemented)} 19 | } 20 | 21 | 22 | \value{ 23 | numeric (cell numbers) 24 | } 25 | 26 | \author{Ashton Shortridge} 27 | 28 | \examples{ 29 | data(volcano) 30 | v <- raster(volcano, xmn=2667400, xmx=2668010, ymn=6478700, ymx=6479570, crs="+init=epsg:27200") 31 | fd <- terrain(v, opt = "flowdir") 32 | path <- flowPath(fd, 2407) 33 | xy <- xyFromCell(fd, path) 34 | plot(v) 35 | lines(xy) 36 | } 37 | 38 | \keyword{spatial} 39 | -------------------------------------------------------------------------------- /man/focalWeight.Rd: -------------------------------------------------------------------------------- 1 | \name{focalWeight} 2 | 3 | \alias{focalWeight} 4 | 5 | \title{Focal weights matrix} 6 | 7 | \description{ 8 | Calculate focal ("moving window") weight matrix for use in the \code{\link{focal}} function. The sum of the values adds up to one. 9 | } 10 | 11 | \usage{ 12 | focalWeight(x, d, type=c('circle', 'Gauss', 'rectangle'), fillNA=FALSE) 13 | } 14 | 15 | \arguments{ 16 | \item{x}{Raster* object} 17 | \item{d}{numeric. If \code{type=circle}, the radius of the circle (in units of the CRS). If \code{type=rectangle} the dimension of the rectangle (one or two numbers). If \code{type=Gauss} the size of sigma, and optionally another number to determine the size of the matrix returned (default is 3 times sigma)} 18 | \item{type}{character indicating the type of filter to be returned} 19 | \item{fillNA}{logical. If \code{TRUE}, zeros are set to \code{NA} such that they are ignored in the computations. Only applies to \code{type="circle"}} 20 | } 21 | 22 | \value{ 23 | matrix that can be used in \code{\link{focal}} 24 | } 25 | 26 | 27 | \examples{ 28 | r <- raster(ncols=180, nrows=180, xmn=0, crs="+proj=utm +zone=1") 29 | # Gaussian filter for square cells 30 | gf <- focalWeight(r, .5, "Gauss") 31 | focalWeight(r, 2, "circle", fillNA=TRUE) 32 | } 33 | 34 | 35 | \keyword{spatial} 36 | 37 | -------------------------------------------------------------------------------- /man/gainoffset.Rd: -------------------------------------------------------------------------------- 1 | \name{Gain and offset} 2 | 3 | \alias{gain} 4 | \alias{offs} 5 | \alias{gain<-} 6 | \alias{offs<-} 7 | 8 | \title{Gain and offset of values on file} 9 | 10 | 11 | \description{ 12 | These functions can be used to get or set the gain and offset parameters used to transform values when reading them from a file. The gain and offset parameters are applied to the raw values using the formula below: 13 | 14 | \code{value <- value * gain + offset} 15 | 16 | The default value for gain is 1 and for offset is 0. 'gain' is sometimes referred to as 'scale'. 17 | 18 | Note that setting gain and/or offset are intended to be used with values that are stored in a file. For a Raster* object with values in memory, assigning gain or offset values will lead to the inmediate computation of new values; in such cases it would be clearer to use \code{\link[raster]{Arith-methods}}. 19 | } 20 | 21 | \usage{ 22 | gain(x) 23 | gain(x) <- value 24 | offs(x) 25 | offs(x) <- value 26 | } 27 | 28 | \arguments{ 29 | \item{x}{Raster* object} 30 | \item{value}{Single numeric value } 31 | } 32 | 33 | \value{ 34 | Raster* object or numeric value(s) 35 | } 36 | 37 | 38 | 39 | \examples{ 40 | r <- raster(system.file("external/test.grd", package="raster")) 41 | gain(r) 42 | offs(r) 43 | r[1505:1510] 44 | gain(r) <- 10 45 | offs(r) <- 5 46 | r[1505:1510] 47 | } 48 | 49 | \keyword{ spatial } 50 | \keyword{ methods } 51 | -------------------------------------------------------------------------------- /man/getData.Rd: -------------------------------------------------------------------------------- 1 | \name{getData} 2 | \alias{getData} 3 | \alias{ccodes} 4 | 5 | \title{Get geographic data } 6 | 7 | \description{ 8 | This function has been deprecated and does not work anymore. 9 | } 10 | 11 | \usage{ 12 | getData(...) 13 | ccodes() 14 | } 15 | 16 | \arguments{ 17 | \item{...}{arguments} 18 | } 19 | 20 | -------------------------------------------------------------------------------- /man/headtail.Rd: -------------------------------------------------------------------------------- 1 | \name{head} 2 | 3 | \docType{methods} 4 | 5 | \alias{head} 6 | \alias{head,RasterLayer-method} 7 | \alias{head,RasterStackBrick-method} 8 | \alias{head,Spatial-method} 9 | \alias{tail} 10 | \alias{tail,RasterLayer-method} 11 | \alias{tail,RasterStackBrick-method} 12 | \alias{tail,Spatial-method} 13 | 14 | \title{Show the head or tail of a Raster* object} 15 | 16 | \description{ 17 | Show the head (first rows/columns) or tail (last rows/columns) of the cell values of a Raster* object. 18 | } 19 | 20 | \usage{ 21 | head(x, ...) 22 | tail(x, ...) 23 | } 24 | 25 | \arguments{ 26 | \item{x}{Raster* object} 27 | \item{...}{Additional arguments: \code{rows=10} and \code{cols=20}, to set the maximum number of rows and columns that are shown. For RasterStack and RasterBrick objects there is an additional argument \code{lyrs} } 28 | } 29 | 30 | \value{ 31 | matrix 32 | } 33 | 34 | \seealso{ 35 | \code{\link{getValuesBlock}} 36 | } 37 | 38 | 39 | 40 | \examples{ 41 | r <- raster(nrow=25, ncol=25) 42 | values(r) = 1:ncell(r) 43 | head(r) 44 | tail(r, cols=10, rows=5) 45 | } 46 | 47 | \keyword{methods} 48 | \keyword{spatial} 49 | 50 | -------------------------------------------------------------------------------- /man/hillShade.Rd: -------------------------------------------------------------------------------- 1 | \name{hillShade} 2 | 3 | \alias{hillShade} 4 | 5 | \title{Hill shading} 6 | 7 | \description{ 8 | Compute hill shade from slope and aspect layers (both in radians). Slope and aspect can be computed with function \code{\link{terrain}}. 9 | 10 | A hill shade layer is often used as a backdrop on top of which another, semi-transparent, layer is drawn. 11 | } 12 | 13 | \usage{ 14 | hillShade(slope, aspect, angle=45, direction=0, filename='', normalize=FALSE, ...) 15 | } 16 | 17 | \arguments{ 18 | \item{slope}{RasterLayer object with slope values (in radians) } 19 | \item{aspect}{RasterLayer object with aspect values (in radians) } 20 | \item{angle}{ The the elevation angle of the light source (sun), in degrees} 21 | \item{direction}{ The direction (azimuth) angle of the light source (sun), in degrees} 22 | \item{filename}{Character. Optional filename} 23 | \item{normalize}{Logical. If \code{TRUE}, values below zero are set to zero and the results are multiplied with 255} 24 | \item{...}{Standard additional arguments for writing RasterLayer files} 25 | } 26 | 27 | 28 | \seealso{ \code{\link{terrain}} } 29 | 30 | \author{Andrew Bevan, Robert J. Hijmans} 31 | 32 | 33 | \references{ 34 | Horn, B.K.P., 1981. Hill shading and the reflectance map. Proceedings of the IEEE 69(1):14-47 35 | } 36 | 37 | 38 | \keyword{spatial} 39 | 40 | -------------------------------------------------------------------------------- /man/hist.Rd: -------------------------------------------------------------------------------- 1 | \name{hist} 2 | 3 | \docType{methods} 4 | 5 | \alias{hist} 6 | \alias{hist,Raster-method} 7 | 8 | \title{Histogram} 9 | 10 | \description{ 11 | Create a histogram of the values of a RasterLayer. For large datasets a sample is used. 12 | } 13 | 14 | \usage{ 15 | \S4method{hist}{Raster}(x, layer, maxpixels=100000, plot=TRUE, main, ...) 16 | } 17 | 18 | \arguments{ 19 | \item{x}{Raster* object} 20 | \item{layer}{integer (or character) to indicate layer number (or name). Can be used to subset the layers to plot in a multilayer Raster* object} 21 | \item{maxpixels}{integer. To regularly subsample very large objects} 22 | \item{plot}{logical. Plot the histogram or only return the histogram values} 23 | \item{main}{character. Main title(s) for the plot. Default is the value of \code{\link{names}}} 24 | \item{...}{Additional arguments. See under Methods and at \code{\link[graphics]{hist}}} 25 | } 26 | 27 | 28 | \value{ 29 | This function is principally used for the side-effect of plotting a histogram, but it also returns an S3 object of class 'histogram' (invisibly if \code{plot=TRUE}). 30 | } 31 | 32 | 33 | \seealso{ \code{\link{pairs}, \link{boxplot}} } 34 | 35 | \examples{ 36 | r1 <- raster(nrows=50, ncols=50) 37 | r1 <- setValues(r1, runif(ncell(r1))) 38 | r2 <- setValues(r1, runif(ncell(r1))) 39 | rs <- r1 + r2 40 | rp <- r1 * r2 41 | par(mfrow=c(2,2)) 42 | plot(rs, main='sum') 43 | plot(rp, main='product') 44 | hist(rs) 45 | a = hist(rp) 46 | a 47 | } 48 | 49 | \keyword{methods} 50 | \keyword{spatial} 51 | 52 | -------------------------------------------------------------------------------- /man/iniFile.Rd: -------------------------------------------------------------------------------- 1 | \name{inifile} 2 | 3 | \alias{readIniFile} 4 | 5 | \title{Read a .ini file} 6 | 7 | \description{ 8 | This function reads \code{'.ini'} files. These are text file databases that are organized in [sections] containing pairs of "name = value". 9 | } 10 | 11 | \usage{ 12 | readIniFile(filename, token='=', commenttoken=';', aslist=FALSE, case) 13 | } 14 | 15 | \arguments{ 16 | \item{filename}{Character. Filename of the .ini file} 17 | \item{token}{Character. The character that separates the "name" (variable name) from the "value"} 18 | \item{commenttoken}{Character. This token and everything that follows on the same line is considered a 'comment' that is not for machine consumption and is ignored in processing} 19 | \item{aslist}{Logical. Should the values be returned as a list} 20 | \item{case}{Optional. Function that operates on the text, such as \code{\link{toupper}} or \code{\link{tolower}} } 21 | } 22 | 23 | \details{ 24 | This function allows for using inistrings that have "=" as part of a value (but the token cannot be part of the 'name' of a variable!). Sections can be missing. 25 | } 26 | 27 | 28 | \value{ 29 | A n*3 matrix of characters with columns: section, name, value; or a list if \code{aslist=TRUE}. 30 | } 31 | 32 | 33 | 34 | \keyword{file} 35 | -------------------------------------------------------------------------------- /man/isLonLat.Rd: -------------------------------------------------------------------------------- 1 | \name{isLonLat} 2 | 3 | \alias{isLonLat} 4 | \alias{isLonLat,BasicRaster-method} 5 | \alias{isLonLat,Spatial-method} 6 | \alias{isLonLat,CRS-method} 7 | \alias{isLonLat,character-method} 8 | \alias{isLonLat,ANY-method} 9 | \alias{couldBeLonLat} 10 | \alias{couldBeLonLat,BasicRaster-method} 11 | \alias{couldBeLonLat,Spatial-method} 12 | \alias{couldBeLonLat,ANY-method} 13 | 14 | 15 | \title{Is this longitude/latitude data?} 16 | 17 | \description{ 18 | Test whether a Raster* or other object has a longitude/latitude coordinate reference system (CRS) by inspecting the PROJ.4 coordinate reference system description. \code{couldBeLonLat} also returns \code{TRUE} if the CRS is \code{NA} but the x coordinates are within -365 and 365 and the y coordinates are within -90.1 and 90.1. 19 | } 20 | 21 | \usage{ 22 | \S4method{isLonLat}{BasicRaster}(x, ...) 23 | \S4method{isLonLat}{Spatial}(x, ...) 24 | \S4method{couldBeLonLat}{BasicRaster}(x, warnings=TRUE, ...) 25 | \S4method{couldBeLonLat}{Spatial}(x, warnings=TRUE, ...) 26 | } 27 | 28 | \arguments{ 29 | \item{x}{Raster* or Spatial* object} 30 | \item{warnings}{logical. If \code{TRUE}, a warning is given if the CRS is \code{NA} or when the CRS is longitude/latitude but the coordinates do not match that} 31 | \item{...}{additional arguments. None implemented} 32 | } 33 | 34 | 35 | \value{ 36 | Logical 37 | } 38 | 39 | 40 | 41 | \examples{ 42 | r <- raster() 43 | isLonLat(r) 44 | crs(r) <- "+proj=lcc +lat_1=48 +lat_2=33 +lon_0=-100 +ellps=WGS84" 45 | isLonLat(r) 46 | } 47 | 48 | \keyword{spatial} 49 | -------------------------------------------------------------------------------- /man/match.Rd: -------------------------------------------------------------------------------- 1 | \name{match} 2 | 3 | \docType{methods} 4 | 5 | \alias{match} 6 | \alias{match,Raster-method} 7 | \alias{\%in\%} 8 | \alias{\%in\%,Raster-method} 9 | 10 | \title{Value matching for Raster* objects} 11 | 12 | \description{ 13 | \code{match} returns a Raster* object with the position of the matched values. The cell values are the index of the table argument. 14 | 15 | \code{\%in\%} returns a logical Raster* object indicating if the cells values were matched or not. 16 | 17 | } 18 | 19 | \usage{ 20 | match(x, table, nomatch = NA_integer_, incomparables = NULL) 21 | 22 | x \%in\% table 23 | } 24 | 25 | \arguments{ 26 | \item{x}{Raster* object} 27 | \item{table}{vector of the values to be matched against} 28 | \item{nomatch}{the value to be returned in the case when no match is found. Note that it is coerced to integer} 29 | \item{incomparables}{a vector of values that cannot be matched. Any value in x matching a value in this vector is assigned the nomatch value. For historical reasons, FALSE is equivalent to NULL} 30 | } 31 | 32 | \value{ 33 | Raster* object 34 | } 35 | 36 | \seealso{ \code{\link{calc}, \link[base]{match}} } 37 | 38 | 39 | 40 | 41 | \examples{ 42 | r <- raster(nrow=10, ncol=10) 43 | values(r) <- 1:100 44 | m <- match(r, c(5:10, 50:55)) 45 | n <- r \%in\% c(5:10, 50:55) 46 | } 47 | 48 | \keyword{spatial} 49 | \keyword{methods} 50 | -------------------------------------------------------------------------------- /man/modal.Rd: -------------------------------------------------------------------------------- 1 | \name{modal} 2 | 3 | \alias{modal} 4 | \alias{modal,ANY-method} 5 | \alias{modal,Raster-method} 6 | 7 | \title{modal value} 8 | 9 | \description{ 10 | Compute the mode for a vector of numbers, or across raster layers. The mode, or modal value, is the most frequent value in a set of values. 11 | } 12 | 13 | \usage{ 14 | \S4method{modal}{ANY}(x, ..., ties='random', na.rm=FALSE, freq=FALSE) 15 | 16 | \S4method{modal}{Raster}(x, ..., ties='random', na.rm=FALSE, freq=FALSE) 17 | } 18 | 19 | \arguments{ 20 | \item{x}{vector of numbers (typically integers), characters, logicals, or factors, or a Raster* object} 21 | \item{...}{additional argument of the same type as \code{x}} 22 | \item{ties}{character. Indicates how to treat ties. Either 'random', 'lowest', 'highest', 'first', or 'NA'} 23 | \item{na.rm}{logical. If \code{TRUE}, \code{NA} values are ignored. If \code{FALSE}, \code{NA} is returned if \code{x} has any \code{NA} values} 24 | \item{freq}{return the frequency of the modal value, instead of the modal value} 25 | } 26 | 27 | \value{ 28 | vector or RasterLayer. The vector has length 1 and is of the same type as \code{x}, except when \code{x} is a factor and additional arguments (values) are supplied, in which case the values are coerced to characters and a character value is returned. 29 | } 30 | 31 | \examples{ 32 | data <- c(0,1,2,3,3,3,3,4,4,4,5,5,6,7,7,8,9,NA) 33 | modal(data, na.rm=TRUE) 34 | } 35 | 36 | \keyword{univar} 37 | \keyword{math} 38 | -------------------------------------------------------------------------------- /man/names.Rd: -------------------------------------------------------------------------------- 1 | \name{names} 2 | 3 | \alias{labels,Raster-method} 4 | \alias{names} 5 | \alias{names<-} 6 | \alias{names,Raster-method} 7 | \alias{names,RasterStack-method} 8 | \alias{names<-,Raster-method} 9 | 10 | 11 | \title{Names of raster layers} 12 | 13 | \description{ 14 | Get or set the names of the layers of a Raster* object 15 | } 16 | 17 | \usage{ 18 | \S4method{names}{Raster}(x) 19 | 20 | \S4method{names}{Raster}(x)<-value 21 | 22 | \S4method{labels}{Raster}(object) 23 | } 24 | 25 | \arguments{ 26 | \item{x}{Raster* object} 27 | \item{object}{Raster* object} 28 | \item{value}{character (vector)} 29 | } 30 | 31 | \value{ 32 | Character 33 | } 34 | 35 | 36 | \seealso{ \code{\link{nlayers}, \link[raster]{bands}} } 37 | 38 | 39 | \examples{ 40 | r <- raster(ncols=5, nrows=5) 41 | values(r) <- 1:ncell(r) 42 | s <- stack(r, r, r) 43 | nlayers(s) 44 | names(s) 45 | names(s) <- c('a', 'b', 'c') 46 | names(s)[2] <- 'hello world' 47 | names(s) 48 | s 49 | labels(s) 50 | } 51 | 52 | \keyword{spatial} 53 | -------------------------------------------------------------------------------- /man/ncell.Rd: -------------------------------------------------------------------------------- 1 | \name{ncell} 2 | 3 | \alias{ncol} 4 | \alias{nrow} 5 | \alias{nrow,BasicRaster-method} 6 | \alias{ncol,BasicRaster-method} 7 | \alias{ncell} 8 | \alias{ncell,ANY-method} 9 | \alias{ncell,BasicRaster-method} 10 | \alias{length,BasicRaster-method} 11 | \alias{nrow<-} 12 | \alias{ncol<-} 13 | \alias{nrow<-,BasicRaster,numeric-method} 14 | \alias{ncol<-,BasicRaster,numeric-method} 15 | 16 | 17 | \title{Number or rows, columns, and cells of a Raster* object} 18 | 19 | \description{ 20 | Get the number of rows, columns, or cells of a Raster* object. 21 | } 22 | 23 | \usage{ 24 | ncol(x) 25 | nrow(x) 26 | ncell(x) 27 | ncol(x, ...) <- value 28 | nrow(x, ...) <- value 29 | } 30 | 31 | 32 | \arguments{ 33 | \item{x}{a Raster object} 34 | \item{value}{row or column number (integer > 0)} 35 | \item{...}{additional arguments. None implemented} 36 | } 37 | 38 | \value{ 39 | Integer 40 | } 41 | 42 | \seealso{ \code{\link{dim}, \link{extent}}, \link{res} } 43 | 44 | 45 | 46 | 47 | 48 | \examples{ 49 | r <- raster() 50 | ncell(r) 51 | ncol(r) 52 | nrow(r) 53 | dim(r) 54 | 55 | nrow(r) <- 18 56 | ncol(r) <- 36 57 | # equivalent to 58 | dim(r) <- c(18, 36) 59 | } 60 | 61 | \keyword{spatial} 62 | 63 | -------------------------------------------------------------------------------- /man/nlayers.Rd: -------------------------------------------------------------------------------- 1 | \name{nlayers} 2 | 3 | \alias{nlayers} 4 | \alias{nlayers,BasicRaster-method} 5 | \alias{nlayers,Raster-method} 6 | \alias{nlayers,RasterStack-method} 7 | \alias{nlayers,RasterBrick-method} 8 | \alias{nlayers,Spatial-method} 9 | 10 | \title{Number of layers} 11 | 12 | \description{ 13 | Get the number of layers in a Raster* object, typically used with a (multilayer) RasterStack or RasterBrick object 14 | } 15 | 16 | \usage{ 17 | nlayers(x) 18 | } 19 | 20 | \arguments{ 21 | \item{x}{Raster* object} 22 | } 23 | 24 | \value{ 25 | integer 26 | } 27 | 28 | \seealso{ \code{\link[raster]{names}} } 29 | 30 | 31 | 32 | \examples{ 33 | r <- raster(ncols=10, nrows=10) 34 | values(r) <- 1:ncell(r) 35 | s <- stack(r, r, r) 36 | nlayers(s) 37 | s <- stack(s,s) 38 | nlayers(s) 39 | s <- dropLayer(s, 2:3) 40 | nlayers(s) 41 | 42 | } 43 | 44 | \keyword{spatial} 45 | -------------------------------------------------------------------------------- /man/origin.Rd: -------------------------------------------------------------------------------- 1 | \name{origin} 2 | 3 | \alias{origin} 4 | \alias{origin,BasicRaster-method} 5 | \alias{origin<-} 6 | \alias{origin<-,BasicRaster-method} 7 | 8 | 9 | \title{Origin} 10 | 11 | \description{ 12 | Origin returns (or sets) the coordinates of the point of origin of a Raster* object. This is the point closest to (0, 0) that you could get if you moved towards that point in steps of the x and y resolution. 13 | } 14 | 15 | \usage{ 16 | origin(x, ...) 17 | origin(x) <- value 18 | } 19 | 20 | \arguments{ 21 | \item{x}{Raster* object} 22 | \item{value}{numeric vector of length 1 or 2} 23 | \item{...}{additional arguments. None implemented} 24 | } 25 | 26 | 27 | \value{ 28 | A vector of two numbers (x and y coordinates), or a changed origin for \code{x}. 29 | } 30 | 31 | \seealso{ \code{\link[raster]{extent}}} 32 | 33 | 34 | \examples{ 35 | r <- raster(xmn=-0.5, xmx = 9.5, ncols=10) 36 | origin(r) 37 | r 38 | origin(r) <- 0 39 | r 40 | } 41 | 42 | \keyword{spatial} 43 | -------------------------------------------------------------------------------- /man/pairs.Rd: -------------------------------------------------------------------------------- 1 | \name{pairs} 2 | 3 | \docType{methods} 4 | 5 | \alias{pairs} 6 | \alias{pairs,RasterStackBrick-method} 7 | 8 | \title{ 9 | Pairs plot (matrix of scatterplots) 10 | } 11 | 12 | \description{ 13 | Pair plots of layers in a RasterStack or RasterBrick. This is a wrapper around graphics function \code{\link[graphics]{pairs}}. 14 | } 15 | 16 | \usage{ 17 | \S4method{pairs}{RasterStackBrick}(x, hist=TRUE, cor=TRUE, use="pairwise.complete.obs", maxpixels=100000, ...) 18 | } 19 | 20 | \arguments{ 21 | \item{x}{RasterBrick or RasterStack} 22 | \item{hist}{Logical. If TRUE a histogram of the values is shown on the diagonal} 23 | \item{cor}{Logical. If TRUE the correlation coefficient is shown in the upper panels} 24 | \item{use}{Argument passed to the \code{\link[stats]{cor}} function} 25 | \item{maxpixels}{Integer. Number of pixels to sample from each layer of large Raster objects} 26 | \item{...}{Additional arguments (only \code{cex} and \code{main})} 27 | } 28 | 29 | \seealso{ 30 | \code{\link{boxplot}, \link{hist}, \link{density}} 31 | } 32 | 33 | \examples{ 34 | r <- raster(system.file("external/test.grd", package="raster") ) 35 | s <- stack(r, 1/r, sqrt(r)) 36 | pairs(s) 37 | 38 | \dontrun{ 39 | # to make indvidual histograms: 40 | hist(r) 41 | # or scatter plots: 42 | plot(r, 1/r) 43 | } 44 | } 45 | 46 | 47 | 48 | \keyword{spatial} 49 | 50 | -------------------------------------------------------------------------------- /man/persp.Rd: -------------------------------------------------------------------------------- 1 | \name{persp} 2 | 3 | \docType{methods} 4 | 5 | \alias{persp} 6 | \alias{persp,RasterLayer-method} 7 | \alias{persp,RasterStackBrick-method} 8 | 9 | \title{Perspective plot} 10 | 11 | \description{ 12 | Perspective plot of a RasterLayer. This is an implementation of a generic function in the graphics package. 13 | } 14 | 15 | 16 | \usage{ 17 | \S4method{persp}{RasterLayer}(x, maxpixels=1e+05, ext=NULL, ...) 18 | 19 | \S4method{persp}{RasterStackBrick}(x, y=1, maxpixels=10000, ext=NULL, ...) 20 | } 21 | 22 | \arguments{ 23 | \item{x}{Raster* object} 24 | \item{y}{integer \code{> 0 & <= nlayers(x)} to select the layer of \code{x} if \code{x} is a RasterLayer or RasterBrick} 25 | \item{maxpixels}{integer > 0. Maximum number of cells to use for the plot. If \code{maxpixels < ncell(x)}, \code{sampleRegular} is used before plotting} 26 | \item{ext}{Extent. Can be used to zoom in to a region (see also \code{\link{zoom}} and \code{\link{crop}(x, \link{drawExtent}())}} 27 | \item{...}{Any argument that can be passed to \code{\link[graphics]{persp}} (graphics package)} 28 | } 29 | 30 | 31 | \seealso{ \code{\link[rasterVis:plot3d]{plot3D}}, \code{\link[graphics]{persp}}, \code{\link[raster]{contour}}, \code{\link[raster]{plot}} } 32 | 33 | 34 | 35 | \examples{ 36 | r <- raster(system.file("external/test.grd", package="raster")) 37 | persp(r) 38 | } 39 | 40 | \keyword{methods} 41 | \keyword{spatial} 42 | 43 | -------------------------------------------------------------------------------- /man/properties.Rd: -------------------------------------------------------------------------------- 1 | \name{properties} 2 | 3 | \alias{dataSize} 4 | \alias{dataSigned} 5 | 6 | 7 | \title{Raster file properties} 8 | 9 | \description{ 10 | Properties of the values of the file that a RasterLayer object points to 11 | 12 | \code{dataSize} returns the number of bytes used for each value (pixel, grid cell) 13 | \code{dataSigned} is TRUE for data types that include negative numbers. 14 | 15 | } 16 | 17 | \usage{ 18 | dataSize(object) 19 | dataSigned(object) 20 | } 21 | 22 | \arguments{ 23 | \item{object}{Raster* object} 24 | } 25 | 26 | \seealso{ \code{\link[raster]{filename}} } 27 | 28 | 29 | \value{ 30 | varies 31 | } 32 | 33 | 34 | 35 | \examples{ 36 | r <- raster(system.file("external/test.grd", package="raster")) 37 | dataSize(r) 38 | dataSigned(r) 39 | dataType(r) 40 | } 41 | 42 | \keyword{spatial} 43 | -------------------------------------------------------------------------------- /man/quantile.Rd: -------------------------------------------------------------------------------- 1 | \name{quantile} 2 | 3 | \docType{methods} 4 | 5 | \alias{quantile} 6 | \alias{quantile,Raster-method} 7 | 8 | \title{Raster quantiles} 9 | 10 | \description{ 11 | Compute quantiles for the cell values of a RasterLayer. If you want to compute quantiles for each cell across a number of layers, you can use \code{\link{calc}(x, fun=quantile)}. 12 | } 13 | 14 | \usage{ 15 | quantile(x, ...) 16 | } 17 | 18 | \arguments{ 19 | \item{x}{Raster object} 20 | \item{...}{Additional arguments: \code{na.rm=TRUE}, \code{ncells=NULL}, and additional arguments to the stats::quantile function, see \code{\link[stats]{quantile}}} 21 | ncells can be used to set the number of cells to be sampled, for very large raster datasets. 22 | } 23 | 24 | \value{ 25 | A vector of quantiles 26 | } 27 | 28 | \seealso{ \code{\link[raster]{density}}, \code{\link[raster]{cellStats}} } 29 | 30 | 31 | 32 | \examples{ 33 | r <- raster(ncol=100, nrow=100) 34 | values(r) <- rnorm(ncell(r), 0, 50) 35 | quantile(r) 36 | quantile(r, probs = c(0.25, 0.75), type=7,names = FALSE) 37 | } 38 | 39 | \keyword{methods} 40 | \keyword{spatial} 41 | 42 | -------------------------------------------------------------------------------- /man/rasterFromCells.Rd: -------------------------------------------------------------------------------- 1 | \name{rasterFromCells} 2 | 3 | \alias{rasterFromCells} 4 | 5 | \title{Subset a raster by cell numbers} 6 | 7 | \description{ 8 | This function returns a new raster based on an existing raster and cell numbers for that raster. 9 | The new raster is cropped to the cell numbers provided, and, if \code{values=TRUE} has values that are the cell numbers of the original raster. 10 | } 11 | 12 | \usage{ 13 | rasterFromCells(x, cells, values=TRUE) 14 | } 15 | 16 | \arguments{ 17 | \item{x}{Raster* object (or a SpatialPixels* or SpatialGrid* object)} 18 | \item{cells}{vector of cell numbers} 19 | \item{values}{Logical. If \code{TRUE}, the new RasterLayer has cell values that correspond to the cell numbers of \code{x}} 20 | } 21 | 22 | \details{ 23 | Cell numbers start at 1 in the upper left corner, and increase from left to right, and then from top to bottom. 24 | The last cell number equals the number of cells of the Raster* object. 25 | } 26 | 27 | \value{ 28 | RasterLayer 29 | } 30 | 31 | \seealso{ \code{\link[raster]{rowFromCell}} } 32 | 33 | 34 | 35 | \examples{ 36 | r <- raster(ncols=100, nrows=100) 37 | cells <- c(3:5, 210) 38 | r <- rasterFromCells(r, cells) 39 | cbind(1:ncell(r), getValues(r)) 40 | } 41 | 42 | \keyword{spatial} 43 | -------------------------------------------------------------------------------- /man/rasterToContour.Rd: -------------------------------------------------------------------------------- 1 | \name{rasterToContour} 2 | 3 | \alias{rasterToContour} 4 | 5 | \title{ Raster to contour lines conversion} 6 | 7 | \description{ 8 | RasterLayer to contour lines. This is a wrapper around \code{\link[grDevices]{contourLines}} 9 | } 10 | 11 | \usage{ 12 | rasterToContour(x, maxpixels=100000, ...) 13 | } 14 | 15 | \arguments{ 16 | \item{x}{ a RasterLayer object } 17 | \item{maxpixels}{ Maximum number of raster cells to use; this function fails when too many cells are used} 18 | \item{...}{Any argument that can be passed to \code{\link[grDevices]{contourLines}} } 19 | } 20 | 21 | \details{ 22 | Most of the code was taken from maptools::ContourLines2SLDF, by Roger Bivand & Edzer Pebesma 23 | } 24 | 25 | \value{ 26 | SpatialLinesDataFrame 27 | } 28 | 29 | 30 | 31 | 32 | \examples{ 33 | f <- system.file("external/test.grd", package="raster") 34 | r <- raster(f) 35 | x <- rasterToContour(r) 36 | class(x) 37 | plot(r) 38 | plot(x, add=TRUE) 39 | } 40 | 41 | \keyword{ spatial } 42 | -------------------------------------------------------------------------------- /man/rasterToPoints.Rd: -------------------------------------------------------------------------------- 1 | \name{rasterToPoints} 2 | 3 | \alias{rasterToPoints} 4 | 5 | \title{ Raster to points conversion} 6 | 7 | \description{ 8 | Raster to point conversion. Cells with NA are not converted. A function can be used to select a subset of the raster cells (by their values). 9 | } 10 | 11 | \usage{ 12 | rasterToPoints(x, fun=NULL, spatial=FALSE, ...) 13 | } 14 | 15 | \arguments{ 16 | \item{x}{A Raster* object } 17 | \item{fun}{Function to select a subset of raster values} 18 | \item{spatial}{Logical. If \code{TRUE}, the function returns a SpatialPointsDataFrame object } 19 | \item{...}{Additional arguments. Currently only \code{progress} to specify a progress bar. "text", "window", or "" (the default, no progress bar)} 20 | } 21 | 22 | \details{ 23 | \code{fun} should be a simple function returning a logical value. 24 | 25 | E.g.: \code{fun=function(x){x==1}} or \code{fun=function(x){x>3}} 26 | } 27 | 28 | \value{ 29 | A matrix with three columns: x, y, and v (value), or a SpatialPointsDataFrame object 30 | } 31 | 32 | 33 | 34 | \examples{ 35 | r <- raster(nrow=18, ncol=36) 36 | values(r) <- runif(ncell(r)) * 10 37 | r[r>8] <- NA 38 | p <- rasterToPoints(r) 39 | p <- rasterToPoints(r, fun=function(x){x>6}) 40 | #plot(r) 41 | #points(p) 42 | } 43 | 44 | \keyword{ spatial } 45 | -------------------------------------------------------------------------------- /man/rasterToPolygons.Rd: -------------------------------------------------------------------------------- 1 | \name{rasterToPolygons} 2 | 3 | \alias{rasterToPolygons} 4 | 5 | \title{ Raster to polygons conversion} 6 | 7 | \description{ 8 | Raster to polygons conversion. Cells with NA are not converted. A function can be used to select a subset of the raster cells (by their values). 9 | } 10 | 11 | \usage{ 12 | rasterToPolygons(x, fun=NULL, n=4, na.rm=TRUE, digits=12, dissolve=FALSE) 13 | } 14 | 15 | \arguments{ 16 | \item{x}{ Raster* object } 17 | \item{fun}{ function to select a subset of raster values (only allowed if \code{x} has a single layer)} 18 | \item{n}{ integer. The number of nodes for each polygon. Only 4, 8, and 16 are allowed } 19 | \item{na.rm}{ If \code{TRUE}, cells with \code{NA} values in all layers are ignored } 20 | \item{digits}{ number of digits to round the coordinates to } 21 | \item{dissolve}{logical. If \code{TRUE}, polygons with the same attribute value will be dissolved into multi-polygon regions} 22 | } 23 | 24 | \details{ 25 | \code{fun} should be a simple function returning a logical value. 26 | 27 | E.g.: \code{fun=function(x){x==1}} or \code{fun=function(x){x>3 & x<6}} 28 | } 29 | 30 | \value{ 31 | SpatialPolygonsDataFrame 32 | } 33 | 34 | 35 | 36 | \examples{ 37 | r <- raster(nrow=18, ncol=36) 38 | values(r) <- runif(ncell(r)) * 10 39 | r[r>8] <- NA 40 | pol <- rasterToPolygons(r, fun=function(x){x>6}) 41 | 42 | #plot(r > 6) 43 | #plot(pol, add=TRUE, col='red') 44 | } 45 | 46 | \keyword{ spatial } 47 | -------------------------------------------------------------------------------- /man/readAll.Rd: -------------------------------------------------------------------------------- 1 | \name{readAll} 2 | 3 | \alias{readAll,RasterLayer-method} 4 | \alias{readAll,RasterStack-method} 5 | \alias{readAll,RasterBrick-method} 6 | 7 | \alias{readAll} 8 | 9 | \title{Read values from disk} 10 | 11 | \description{ 12 | Read all values from a raster file associated with a Raster* object into memory. This function should normally not be used. In most cases \code{\link[raster]{getValues}} or \code{\link[raster]{getValuesBlock}} is more appropriate as \code{readAll} will fail when there is no file associated with the RasterLayer (values may only exist in memory). 13 | } 14 | 15 | 16 | \usage{ 17 | readAll(object) 18 | } 19 | 20 | 21 | \arguments{ 22 | \item{object}{a Raster* object} 23 | } 24 | 25 | 26 | \seealso{ \code{\link[raster]{getValues}}, \code{\link[raster]{getValuesBlock}}, \code{\link[raster]{extract}}} 27 | 28 | 29 | 30 | \examples{ 31 | r <- raster(system.file("external/test.grd", package="raster")) 32 | r <- readAll(r) 33 | } 34 | 35 | \keyword{classes} 36 | \keyword{spatial} 37 | -------------------------------------------------------------------------------- /man/rectify.Rd: -------------------------------------------------------------------------------- 1 | \name{rectify} 2 | 3 | \alias{rectify} 4 | \alias{rectify,Raster-method} 5 | 6 | 7 | \title{rectify a Raster object} 8 | 9 | 10 | \description{ 11 | rectify changes a rotated Raster* object into a non-rotated (rectangular) object. This is wrapper function around \code{\link{resample}}. 12 | } 13 | 14 | 15 | \usage{ 16 | \S4method{rectify}{Raster}(x, ext, res, method='ngb', filename='', ...) 17 | } 18 | 19 | \arguments{ 20 | \item{x}{Raster* object to be rectified} 21 | \item{ext}{Optional. Extent object or object from which an Extent object can be extracted} 22 | \item{res}{Optional. Single or two numbers to set the resolution} 23 | \item{method}{Method used to compute values for the new RasterLayer, should be "bilinear" for bilinear interpolation, or "ngb" for nearest neighbor } 24 | \item{filename}{Character. Output filename } 25 | \item{...}{Additional arguments as for \code{\link{writeRaster}}} 26 | } 27 | 28 | 29 | \value{ 30 | RasterLayer or RasterBrick object 31 | } 32 | 33 | 34 | \keyword{spatial} 35 | 36 | -------------------------------------------------------------------------------- /man/resolution.Rd: -------------------------------------------------------------------------------- 1 | \name{resolution} 2 | 3 | \alias{xres} 4 | \alias{yres} 5 | \alias{res} 6 | \alias{xres,BasicRaster-method} 7 | \alias{yres,BasicRaster-method} 8 | \alias{res,BasicRaster-method} 9 | 10 | \alias{res<-} 11 | \alias{res<-,BasicRaster-method} 12 | 13 | 14 | \title{Resolution} 15 | 16 | \description{ 17 | Get (or set) the x and/or y resolution of a Raster* object 18 | } 19 | 20 | \usage{ 21 | xres(x) 22 | yres(x) 23 | res(x) 24 | res(x) <- value 25 | } 26 | 27 | \arguments{ 28 | \item{x}{Raster* object} 29 | \item{value}{Resolution (single number or vector of two numbers) } 30 | } 31 | 32 | 33 | \value{ 34 | A single numeric value or two numeric values. 35 | } 36 | 37 | \seealso{ \code{\link[raster]{extent}}, \code{\link[raster]{ncell}} } 38 | 39 | 40 | 41 | \examples{ 42 | r <- raster(ncol=18, nrow=18) 43 | xres(r) 44 | yres(r) 45 | res(r) 46 | 47 | res(r) <- 1/120 48 | # set yres differently 49 | res(r) <- c(1/120, 1/60) 50 | 51 | 52 | } 53 | 54 | \keyword{spatial} 55 | -------------------------------------------------------------------------------- /man/rotate.Rd: -------------------------------------------------------------------------------- 1 | \name{rotate} 2 | 3 | \docType{methods} 4 | 5 | \alias{rotate} 6 | \alias{rotate,Raster-method} 7 | 8 | 9 | \title{Rotate} 10 | 11 | \description{ 12 | Rotate a Raster* object that has x coordinates (longitude) from 0 to 360, to standard coordinates between -180 and 180 degrees. Longitude between 0 and 360 is frequently used in global climate models. 13 | } 14 | 15 | \usage{ 16 | \S4method{rotate}{Raster}(x, filename='', ...) 17 | } 18 | 19 | \arguments{ 20 | \item{x}{Raster* object} 21 | \item{filename}{character. Output filename (optional)} 22 | \item{...}{additional arguments as for \code{\link{writeRaster}}} 23 | } 24 | 25 | \value{ 26 | RasterLayer or a RasterBrick object 27 | } 28 | 29 | 30 | \seealso{ \code{\link[raster]{flip}} } 31 | 32 | 33 | \examples{ 34 | 35 | r <- raster(nrow=18, ncol=36) 36 | m <- matrix(1:ncell(r), nrow=18) 37 | values(r) <- as.vector(t(m)) 38 | extent(r) <- extent(0, 360, -90, 90) 39 | rr <- rotate(r) 40 | } 41 | 42 | \keyword{spatial} 43 | -------------------------------------------------------------------------------- /man/rotated.Rd: -------------------------------------------------------------------------------- 1 | \name{rotated} 2 | 3 | \alias{rotated} 4 | 5 | 6 | \title{Do the raster cells have a rotation?} 7 | 8 | \description{ 9 | Do the raster cells have a rotation? 10 | } 11 | 12 | \usage{ 13 | rotated(x) 14 | } 15 | 16 | \arguments{ 17 | \item{x}{A Raster* object} 18 | } 19 | 20 | 21 | \value{ 22 | Logical value 23 | } 24 | 25 | 26 | 27 | \seealso{ \code{\link{rectify}}} 28 | 29 | \examples{ 30 | r <- raster() 31 | rotated(r) 32 | } 33 | 34 | \keyword{spatial} 35 | -------------------------------------------------------------------------------- /man/round.Rd: -------------------------------------------------------------------------------- 1 | \name{round} 2 | 3 | \docType{methods} 4 | 5 | \alias{round,RasterLayer-method} 6 | \alias{trunc,RasterLayer-method} 7 | \alias{ceiling,RasterLayer-method} 8 | \alias{floor,RasterLayer-method} 9 | 10 | \title{Integer values} 11 | 12 | \description{ 13 | These functions take a single RasterLayer argument \code{x} and change its values to integers. 14 | 15 | \code{ceiling} returns a RasterLayer with the smallest integers not less than the corresponding values of x. 16 | 17 | \code{floor} returns a RasterLayer with the largest integers not greater than the corresponding values of x. 18 | 19 | \code{trunc} returns a RasterLayer with the integers formed by truncating the values in x toward 0. 20 | 21 | \code{round} returns a RasterLayer with values rounded to the specified number of digits (decimal places; default 0). 22 | } 23 | 24 | 25 | \section{Methods}{ 26 | \describe{ 27 | ceiling(x) 28 | floor(x) 29 | trunc(x, ...) 30 | round(x, digits = 0) 31 | 32 | 33 | \item{x}{a RasterLayer object} 34 | \item{digits}{integer indicating the precision to be used} 35 | \item{...}{additional arguments} 36 | } 37 | } 38 | 39 | 40 | \details{ 41 | see ?base::round 42 | } 43 | 44 | \value{ 45 | a RasterLayer object 46 | } 47 | 48 | 49 | \examples{ 50 | r <- raster(ncol=10, nrow=10) 51 | values(r) <- runif(ncell(r)) * 10 52 | s <- round(r) 53 | 54 | } 55 | \keyword{spatial} 56 | -------------------------------------------------------------------------------- /man/roundExtent.Rd: -------------------------------------------------------------------------------- 1 | \name{Extent math} 2 | 3 | \alias{floor,Extent-method} 4 | \alias{ceiling,Extent-method} 5 | 6 | \title{round Extent coordinates} 7 | 8 | \description{ 9 | 10 | use \code{round(x, digits=0)} to round the coordinates of an Extent object to the number of digits specified. This can be useful when dealing with a small imprecision in the data (e.g. 179.9999 instead of 180). \code{floor} and \code{ceiling} move the coordiantes to the outer or inner whole integer numbers. 11 | 12 | It is also possible to use Arithmetic functions with Extent objects (but these work perhaps unexpectedly!) 13 | 14 | See \code{\link[raster]{Math-methods}} for these (and many more) methods with Raster* objects. 15 | } 16 | 17 | \usage{ 18 | \S4method{floor}{Extent}(x) 19 | \S4method{ceiling}{Extent}(x) 20 | } 21 | 22 | \arguments{ 23 | \item{x}{Extent object } 24 | } 25 | 26 | 27 | \seealso{\code{\link[raster]{Math-methods}}} 28 | 29 | 30 | \examples{ 31 | e <- extent(c(0.999999, 10.000011, -60.4, 60)) 32 | round(e) 33 | ceiling(e) 34 | floor(e) 35 | } 36 | 37 | \keyword{ spatial } 38 | 39 | 40 | -------------------------------------------------------------------------------- /man/rowFromCell.Rd: -------------------------------------------------------------------------------- 1 | \name{rowFromCell} 2 | 3 | \alias{rowFromCell} 4 | \alias{rowFromCell,BasicRaster,numeric-method} 5 | \alias{colFromCell} 6 | \alias{colFromCell,BasicRaster,numeric-method} 7 | \alias{rowColFromCell} 8 | \alias{rowColFromCell,BasicRaster,numeric-method} 9 | 10 | 11 | \title{Row or column number from a cell number} 12 | 13 | \description{ 14 | These functions get the row and/or column number from a cell number of a Raster* object) 15 | } 16 | 17 | \usage{ 18 | colFromCell(object, cell) 19 | rowFromCell(object, cell) 20 | rowColFromCell(object, cell) 21 | } 22 | 23 | \arguments{ 24 | \item{object}{Raster* object (or a SpatialPixels* or SpatialGrid* object)} 25 | \item{cell}{cell number(s)} 26 | } 27 | 28 | \details{ 29 | The colFromCell and similar functions accept a single value, or a vector or list of these values, 30 | Cell numbers start at 1 in the upper left corner, and increase from left to right, and then from top to bottom. 31 | The last cell number equals the number of cells of the Raster* object. 32 | } 33 | 34 | \value{ 35 | row of column number(s) 36 | } 37 | 38 | \seealso{ \code{\link[raster]{cellFrom}} } 39 | 40 | 41 | 42 | 43 | \examples{ 44 | r <- raster(ncols=10, nrows=10) 45 | colFromCell(r, c(5,15)) 46 | rowFromCell(r, c(5,15)) 47 | rowColFromCell(r, c(5,15)) 48 | } 49 | 50 | \keyword{spatial} 51 | -------------------------------------------------------------------------------- /man/rowSums.Rd: -------------------------------------------------------------------------------- 1 | \name{rowSums} 2 | 3 | \docType{methods} 4 | 5 | \alias{rowSums} 6 | \alias{rowSums,Raster-method} 7 | \alias{colSums} 8 | \alias{colSums,Raster-method} 9 | 10 | \title{rowSums and colSums for Raster objects} 11 | 12 | \description{ 13 | Sum values of Raster objects by row or column. 14 | } 15 | 16 | \usage{ 17 | \S4method{rowSums}{Raster}(x, na.rm=FALSE, dims=1L,...) 18 | \S4method{colSums}{Raster}(x, na.rm=FALSE, dims=1L,...) 19 | } 20 | 21 | \arguments{ 22 | \item{x}{Raster* object} 23 | \item{na.rm}{logical. If \code{TRUE}, \code{NA} values are ignored} 24 | \item{dims}{this argument is ignored} 25 | \item{...}{additional arguments (none implemented)} 26 | } 27 | 28 | \value{ 29 | vector (if \code{x} is a RasterLayer) or matrix 30 | } 31 | 32 | \seealso{ 33 | See \code{\link{cellStats}} for summing all cells values 34 | } 35 | 36 | \examples{ 37 | r <- raster(ncols=2, nrows=5) 38 | values(r) <- 1:10 39 | as.matrix(r) 40 | rowSums(r) 41 | colSums(r) 42 | } 43 | 44 | \keyword{spatial} 45 | 46 | -------------------------------------------------------------------------------- /man/sampleInt.Rd: -------------------------------------------------------------------------------- 1 | \name{SampleInt} 2 | 3 | \alias{sampleInt} 4 | 5 | \title{Sample integer values} 6 | 7 | \description{ 8 | Take a random sample from a range of integer values between 1 and \code{n}. Its purpose is similar to that of \code{\link[base]{sample}}, but that function fails when \code{n} is very large. 9 | } 10 | 11 | \usage{ 12 | sampleInt(n, size, replace=FALSE) 13 | } 14 | 15 | \arguments{ 16 | \item{n}{Positive number (integer); the number of items to choose from } 17 | \item{size}{Non-negative integer; the number of items to choose} 18 | \item{replace}{Logical. Should sampling be with replacement?} 19 | } 20 | 21 | 22 | \value{vector of integer numbers} 23 | 24 | 25 | 26 | \examples{ 27 | sampleInt(1e+12, 10) 28 | 29 | # this may fail: 30 | # sample.int(1e+12, 10) 31 | # sample.int(1e+9, 10) 32 | } 33 | 34 | 35 | \keyword{spatial} 36 | -------------------------------------------------------------------------------- /man/sampleRegular.Rd: -------------------------------------------------------------------------------- 1 | \name{sampleRegular} 2 | 3 | \alias{sampleRegular} 4 | \alias{sampleRegular,Raster-method} 5 | 6 | \title{Regular sample} 7 | 8 | \description{ 9 | Take a systematic sample from a Raster* object. 10 | } 11 | 12 | \usage{ 13 | \S4method{sampleRegular}{Raster}(x, size, ext=NULL, cells=FALSE, xy=FALSE, asRaster=FALSE, 14 | sp=FALSE, ...) 15 | 16 | } 17 | 18 | \arguments{ 19 | \item{x}{Raster object} 20 | \item{size}{positive integer giving the number of items to choose.} 21 | \item{ext}{Extent. To limit regular sampling to the area within that box} 22 | \item{cells}{logical. Also return sampled cell numbers (if asRaster=FALSE) } 23 | \item{xy}{logical. If \code{TRUE}, coordinates of sampled cells are also returned} 24 | \item{asRaster}{logical. If \code{TRUE}, a RasterLayer or RasterBrick is returned, rather than the sampled values} 25 | \item{sp}{logical. If \code{TRUE}, a SpatialPointsDataFrame is returned} 26 | \item{...}{additional arguments. None implemented} 27 | } 28 | 29 | \value{ 30 | A vector (single layer object), matrix (multi-layered object; or if \code{cells=TRUE}, or \code{xy=TRUE}), Raster* object (if \code{asRaster=TRUE}), or SpatialPointsDataFrame (if \code{sp=TRUE}) 31 | } 32 | 33 | \seealso{\code{\link{sampleRandom}}, \link{sampleStratified}} 34 | 35 | 36 | 37 | \examples{ 38 | r <- raster(system.file("external/test.grd", package="raster")) 39 | v <- sampleRegular(r, size=100) 40 | x <- sampleRegular(r, size=100, asRaster=TRUE) 41 | } 42 | 43 | \keyword{spatial} 44 | -------------------------------------------------------------------------------- /man/saveStack.Rd: -------------------------------------------------------------------------------- 1 | \name{stackSave} 2 | 3 | \alias{stackSave} 4 | \alias{stackOpen} 5 | 6 | \title{Save or open a RasterStack file} 7 | 8 | \description{ 9 | A RasterStack is a collection of RasterLayers with the same spatial extent and resolution. They can be created from RasterLayer objects, 10 | or from file names. These two functions allow you to save the references to raster files and recreate a rasterStack object later. They only work if the RasterStack points to layers that have their values on disk. The values are not saved, only the references to the files. 11 | } 12 | 13 | \usage{ 14 | stackOpen(stackfile) 15 | stackSave(x, filename) 16 | } 17 | 18 | \arguments{ 19 | \item{stackfile}{ Filename for the RasterStack (to save it on disk) } 20 | \item{x}{ RasterStack object } 21 | \item{filename}{File name } 22 | } 23 | 24 | \details{ 25 | When a RasterStack is saved to a file, only pointers (filenames) to raster datasets are saved, not the data. 26 | If the name or location of a raster file changes, the RasterStack becomes invalid. 27 | } 28 | 29 | \value{ 30 | RasterStack object 31 | } 32 | 33 | 34 | 35 | \seealso{ \code{\link[raster]{writeRaster}}, \code{\link[raster]{stack}}, \code{\link[raster]{addLayer}}} 36 | 37 | \examples{ 38 | file <- system.file("external/test.grd", package="raster") 39 | s <- stack(c(file, file)) 40 | 41 | \dontrun{ 42 | s <- stackSave(s, "mystack") 43 | # note that filename adds an extension .stk to a stackfile 44 | s2 <- stackOpen("mystack.stk") 45 | s2 46 | } 47 | } 48 | \keyword{ spatial } 49 | 50 | -------------------------------------------------------------------------------- /man/scale.Rd: -------------------------------------------------------------------------------- 1 | \name{scale} 2 | 3 | \alias{scale} 4 | \alias{scale,Raster-method} 5 | 6 | 7 | \title{Scale values} 8 | 9 | \description{ 10 | Center and/or scale raster data 11 | } 12 | 13 | \usage{ 14 | \S4method{scale}{Raster}(x, center=TRUE, scale=TRUE) 15 | } 16 | 17 | 18 | \arguments{ 19 | \item{x}{Raster* object} 20 | \item{center}{logical or numeric. If \code{TRUE}, centering is done by subtracting the layer means (omitting \code{NA}s), and if \code{FALSE}, no centering is done. If \code{center} is a numeric vector with length equal to the \code{nlayers(x)}, then each layer of \code{x} has the corresponding value from center subtracted from it.} 21 | \item{scale}{logical or numeric. If \code{TRUE}, scaling is done by dividing the (centered) layers of \code{x} by their standard deviations if \code{center} is \code{TRUE}, and the root mean square otherwise. If scale is \code{FALSE}, no scaling is done. If \code{scale} is a numeric vector with length equal to \code{nlayers(x)}, each layer of \code{x} is divided by the corresponding value. Scaling is done after centering.} 22 | } 23 | 24 | \value{ 25 | Raster* object 26 | } 27 | 28 | 29 | \seealso{ \code{\link[base]{scale}} } 30 | 31 | \examples{ 32 | b <- brick(system.file("external/rlogo.grd", package="raster")) 33 | bs <- scale(b) 34 | } 35 | 36 | \keyword{ spatial } 37 | -------------------------------------------------------------------------------- /man/scalebar.Rd: -------------------------------------------------------------------------------- 1 | \name{scalebar} 2 | 3 | \alias{scalebar} 4 | 5 | \title{scalebar} 6 | 7 | \description{ 8 | Add a scalebar to a plot 9 | } 10 | 11 | \usage{ 12 | scalebar(d, xy = NULL, type = "line", divs = 2, below = "", 13 | lonlat = NULL, label, adj=c(0.5, -0.5), lwd = 2, ...) 14 | } 15 | 16 | \arguments{ 17 | \item{d}{distance covered by scalebar} 18 | \item{xy}{x and y coordinate to place the plot. Can be NULL. Use \code{xy=click()} to make this interactive } 19 | \item{type}{"line" or "bar"} 20 | \item{divs}{Number of divisions for a bar type. 2 or 4} 21 | \item{below}{Text to go below scalebar (e.g., "kilometers")} 22 | \item{lonlat}{Logical or NULL. If logical, \code{TRUE} indicates if the plot is using longitude/latitude coordinates. If \code{NULL} this is guessed from the plot's coordinates} 23 | \item{adj}{adjustment for text placement} 24 | \item{label}{Vector of three numbers to label the scale bar (beginning, midpoint, end)} 25 | \item{lwd}{line width for the "line" type scalebar} 26 | \item{...}{arguments to be passed to other methods } 27 | } 28 | 29 | 30 | \value{ 31 | None. Use for side effect of a scalebar added to a plot 32 | } 33 | 34 | \seealso{ \code{\link[raster]{plot}} } 35 | 36 | 37 | \author{Robert J. Hijmans; partly based on a function by Josh Gray } 38 | 39 | \examples{ 40 | f <- system.file("external/test.grd", package="raster") 41 | r <- raster(f) 42 | plot(r) 43 | scalebar(1000) 44 | scalebar(1000, xy=c(178000, 333500), type='bar', divs=4) 45 | } 46 | 47 | \keyword{spatial} 48 | -------------------------------------------------------------------------------- /man/setExtent.Rd: -------------------------------------------------------------------------------- 1 | \name{setExtent} 2 | 3 | \alias{setExtent} 4 | \alias{extent<-} 5 | 6 | \title{Set the extent of a RasterLayer} 7 | 8 | \description{ 9 | setExtent sets the extent of a Raster* object. Either by providing a new Extent object or by setting the extreme 10 | coordinates one by one. 11 | } 12 | 13 | \usage{ 14 | setExtent(x, ext, keepres=FALSE, snap=FALSE) 15 | extent(x) <- value 16 | } 17 | 18 | \arguments{ 19 | \item{x}{A Raster* object} 20 | \item{ext}{ An object of class Extent (which you can create with \code{\link[raster]{extent}}, or an object that has an extent (e.g. a Raster* or Spatial* object) ) } 21 | \item{keepres}{logical. If \code{TRUE}, the resolution of the cells will stay the same after adjusting the bounding box (by adjusting the number of rows and columns). 22 | If \code{FALSE}, the number of rows and columns will stay the same, and the resolution will be adjusted. 23 | } 24 | \item{snap}{logical. If \code{TRUE}, the extent is adjusted so that the cells of the input and output RasterLayer are aligned} 25 | \item{value}{An object of class Extent (which you can create with \code{\link[raster]{extent}} )} 26 | } 27 | 28 | 29 | \value{ 30 | a Raster* object 31 | } 32 | 33 | 34 | 35 | \seealso{ \code{\link[raster]{extent}}, \code{\link[raster]{Extent-class}} } 36 | 37 | \examples{ 38 | r <- raster() 39 | bb <- extent(-10, 10, -20, 20) 40 | extent(r) <- bb 41 | r <- setExtent(r, bb, keepres=TRUE) 42 | 43 | } 44 | 45 | \keyword{spatial} 46 | -------------------------------------------------------------------------------- /man/setMinMax.Rd: -------------------------------------------------------------------------------- 1 | \name{setMinMax} 2 | 3 | \alias{setMinMax,RasterLayer-method} 4 | \alias{setMinMax,RasterStack-method} 5 | \alias{setMinMax,RasterBrick-method} 6 | 7 | \alias{setMinMax} 8 | 9 | \title{Compute min and max values} 10 | 11 | \description{ 12 | The minimum and maximum value of a RasterLayer are computed (from a file on disk if necessary) and stored in the returned Raster* object. 13 | } 14 | 15 | \usage{ 16 | setMinMax(x, ...) 17 | } 18 | 19 | \arguments{ 20 | \item{x}{Raster object } 21 | \item{\dots}{additional arguments, none implemented} 22 | } 23 | 24 | \value{ 25 | Raster object 26 | } 27 | 28 | \seealso{ \code{\link[raster]{getValues}} } 29 | 30 | 31 | \examples{ 32 | r <- raster(system.file("external/test.grd", package="raster")) 33 | r 34 | r <- setMinMax(r) 35 | r 36 | } 37 | 38 | \keyword{ spatial } 39 | \keyword{ methods } 40 | 41 | -------------------------------------------------------------------------------- /man/shift.Rd: -------------------------------------------------------------------------------- 1 | \name{shift} 2 | 3 | \docType{methods} 4 | 5 | \alias{shift} 6 | \alias{shift,Raster-method} 7 | \alias{shift,SpatialPolygons-method} 8 | \alias{shift,SpatialLines-method} 9 | \alias{shift,SpatialPoints-method} 10 | 11 | \title{Shift} 12 | 13 | \description{ 14 | Shift the location of a Raster* of vector type Spatial* object in the x and/or y direction 15 | } 16 | 17 | \usage{ 18 | \S4method{shift}{Raster}(x, dx=0, dy=0, filename='', ...) 19 | 20 | \S4method{shift}{SpatialPolygons}(x, dx=0, dy=0, ...) 21 | 22 | \S4method{shift}{SpatialLines}(x, dx=0, dy=0, ...) 23 | 24 | \S4method{shift}{SpatialPoints}(x, dx=0, dy=0, ...) 25 | } 26 | 27 | \arguments{ 28 | \item{x}{Raster* or Spatial* object} 29 | \item{dx}{numeric. The shift in horizontal direction} 30 | \item{dy}{numeric. The shift in vertical direction} 31 | \item{filename}{character file name (optional)} 32 | \item{...}{if \code{x} is a Raster* object: additional arguments as for \code{\link{writeRaster}} } 33 | } 34 | 35 | \value{ 36 | Same object type as \code{x} 37 | } 38 | 39 | 40 | \seealso{ \code{\link{flip}}, \code{\link{rotate}}, and the elide function in the maptools package } 41 | 42 | \examples{ 43 | r <- raster() 44 | r <- shift(r, dx=1, dy=-1) 45 | } 46 | 47 | \keyword{spatial} 48 | -------------------------------------------------------------------------------- /man/slopeAspect.Rd: -------------------------------------------------------------------------------- 1 | \name{Slope and aspect} 2 | 3 | \alias{slopeAspect} 4 | 5 | \title{Slope and aspect} 6 | 7 | \description{ 8 | DEPRACATED. Use \code{\link{terrain}} instead. 9 | } 10 | 11 | \usage{ 12 | slopeAspect(dem, filename='', out=c('slope', 'aspect'), unit='radians', 13 | neighbors=8, flatAspect, ...) 14 | } 15 | 16 | \arguments{ 17 | \item{dem}{DEPRACATED} 18 | \item{filename}{DEPRACATED} 19 | \item{out}{DEPRACATED} 20 | \item{unit}{DEPRACATED} 21 | \item{neighbors}{DEPRACATED} 22 | \item{flatAspect}{DEPRACATED} 23 | \item{...}{DEPRACATED} 24 | } 25 | 26 | \seealso{ \code{\link{terrain}} } 27 | 28 | 29 | \keyword{spatial} 30 | -------------------------------------------------------------------------------- /man/subset.Rd: -------------------------------------------------------------------------------- 1 | \name{subset} 2 | 3 | \alias{subset} 4 | \alias{subset,Raster-method} 5 | \alias{subset,RasterStack-method} 6 | 7 | 8 | \title{Subset layers in a Raster* object} 9 | 10 | \description{ 11 | Extract a set of layers from a RasterStack or RasterBrick object. 12 | } 13 | 14 | \usage{ 15 | \S4method{subset}{Raster}(x, subset, drop=TRUE, filename='', ...) 16 | 17 | \S4method{subset}{RasterStack}(x, subset, drop=TRUE, filename='', ...) 18 | } 19 | 20 | \arguments{ 21 | \item{x}{RasterBrick or RasterStack object} 22 | \item{subset}{integer or character. Should indicate the layers (represented as integer or by their name)} 23 | \item{drop}{If \code{TRUE}, a selection of a single layer will be returned as a RasterLayer} 24 | \item{filename}{character. Output filename (optional)} 25 | \item{...}{additional arguments as for \code{\link{writeRaster}}} 26 | } 27 | 28 | \value{ 29 | Raster* object 30 | } 31 | 32 | 33 | 34 | \seealso{ \code{\link[raster:addLayer]{dropLayer}}} 35 | 36 | \examples{ 37 | s <- stack(system.file("external/rlogo.grd", package="raster")) 38 | sel <- subset(s, 2:3) 39 | 40 | # Note that this is equivalent to 41 | sel2 <- s[[2:3]] 42 | 43 | 44 | # and in this particular case: 45 | sel3 <- dropLayer(s, 1) 46 | 47 | nlayers(s) 48 | nlayers(sel) 49 | 50 | # effect of 'drop=FALSE' when selecting a single layer 51 | sel <- subset(s, 2) 52 | class(sel) 53 | sel <- subset(s, 2, drop=FALSE) 54 | class(sel) 55 | } 56 | 57 | \keyword{ spatial } 58 | -------------------------------------------------------------------------------- /man/summary.Rd: -------------------------------------------------------------------------------- 1 | \name{Summary} 2 | 3 | \docType{methods} 4 | 5 | \alias{summary,RasterLayer-method} 6 | \alias{summary,RasterStackBrick-method} 7 | 8 | \title{Summary} 9 | 10 | \description{ 11 | Summarize a Raster* object. A sample is used for very large files. 12 | } 13 | 14 | \usage{ 15 | \S4method{summary}{RasterLayer}(object, maxsamp=100000, ...) 16 | } 17 | 18 | \arguments{ 19 | \item{object}{Raster* object} 20 | \item{maxsamp}{positive integer. Sample size used for large datasets} 21 | \item{...}{additional arguments. None implemented} 22 | } 23 | 24 | 25 | \value{matrix with (an estimate of) the median, minimum and maximum values, the first and third quartiles, and the number of cells with \code{NA} values} 26 | 27 | 28 | \seealso{ \code{\link{cellStats}, link[raster]{quantile}} } 29 | 30 | 31 | \keyword{methods} 32 | \keyword{spatial} 33 | -------------------------------------------------------------------------------- /man/symdif.Rd: -------------------------------------------------------------------------------- 1 | \name{symdif} 2 | 3 | \docType{methods} 4 | 5 | \alias{symdif} 6 | \alias{symdif,SpatialPolygons,SpatialPolygons-method} 7 | 8 | \title{ 9 | Symetrical difference 10 | } 11 | 12 | \description{ 13 | Symetrical difference of SpatialPolygons* objects 14 | } 15 | 16 | \usage{ 17 | \S4method{symdif}{SpatialPolygons,SpatialPolygons}(x, y, ...) 18 | } 19 | 20 | \arguments{ 21 | \item{x}{SpatialPolygons* object} 22 | \item{y}{SpatialPolygons* object} 23 | \item{...}{Additional SpatialPolygons* object(s)} 24 | } 25 | 26 | \value{ 27 | SpatialPolygons* 28 | } 29 | 30 | \seealso{ 31 | \code{\link{erase}} 32 | } 33 | 34 | \examples{ 35 | #SpatialPolygons 36 | p <- shapefile(system.file("external/lux.shp", package="raster")) 37 | b <- as(extent(6, 6.4, 49.75, 50), 'SpatialPolygons') 38 | crs(b) <- crs(p) 39 | sd <- symdif(p, b) 40 | plot(sd, col='red') 41 | 42 | } 43 | 44 | \keyword{methods} 45 | \keyword{spatial} 46 | 47 | -------------------------------------------------------------------------------- /man/transpose.Rd: -------------------------------------------------------------------------------- 1 | \name{transpose} 2 | 3 | \docType{methods} 4 | 5 | \alias{t} 6 | \alias{t,RasterLayer-method} 7 | \alias{t,RasterStackBrick-method} 8 | 9 | \title{Transpose} 10 | 11 | \description{ 12 | Transpose a Raster* object 13 | } 14 | 15 | \usage{ 16 | t(x) 17 | } 18 | 19 | \arguments{ 20 | \item{x}{a Raster* object} 21 | } 22 | 23 | \value{ 24 | RasterLayer or RasterBrick 25 | } 26 | 27 | \seealso{ transpose: \code{\link{flip}, \link[raster]{rotate}} } 28 | 29 | 30 | 31 | \examples{ 32 | r <- raster(nrow=18, ncol=36) 33 | values(r) <- 1:ncell(r) 34 | rt <- t(r) 35 | } 36 | 37 | \keyword{spatial} 38 | -------------------------------------------------------------------------------- /man/trim.Rd: -------------------------------------------------------------------------------- 1 | \name{trim} 2 | 3 | \alias{trim} 4 | \alias{trim,Raster-method} 5 | \alias{trim,character-method} 6 | \alias{trim,matrix-method} 7 | \alias{trim,data.frame-method} 8 | 9 | \title{Trim} 10 | 11 | \description{ 12 | Trim (shrink) a Raster* object by removing outer rows and columns that all have the same value (e.g. NA). 13 | 14 | Or remove the whitespace before or after a string of characters (or a matrix, or the character values in a data.frame). 15 | } 16 | 17 | \usage{ 18 | \S4method{trim}{Raster}(x, padding=0, values=NA, filename='', ...) 19 | \S4method{trim}{character}(x, internal=FALSE, ...) 20 | } 21 | 22 | \arguments{ 23 | \item{x}{Raster* object or a character string} 24 | \item{values}{numeric. Value(s) based on which a Raster* should be trimmed} 25 | \item{padding}{integer. Number of outer rows/columns to keep} 26 | \item{filename}{character. Optional output filename} 27 | \item{internal}{logical. If \code{TRUE}, sequential internal spaces are replaced by a single space} 28 | \item{...}{If \code{x} is a Raster* object: additional arguments as for \code{\link{writeRaster}}} 29 | } 30 | 31 | 32 | \value{ 33 | A RasterLayer or RasterBrick object (if \code{x} is a Raster* object) or a character string (if \code{x} is a character string). 34 | } 35 | 36 | \author{Robert J. Hijmans and Jacob van Etten} 37 | 38 | \examples{ 39 | r <- raster(ncol=18,nrow=18) 40 | r[39:49] <- 1 41 | r[113:155] <- 2 42 | r[200] <- 6 43 | s <- trim(r) 44 | 45 | 46 | trim(" hi folks ! ") 47 | } 48 | 49 | \keyword{spatial} 50 | 51 | -------------------------------------------------------------------------------- /man/unique.Rd: -------------------------------------------------------------------------------- 1 | \name{unique} 2 | 3 | \docType{methods} 4 | 5 | \alias{unique} 6 | \alias{unique,RasterLayer,missing-method} 7 | \alias{unique,RasterStackBrick,missing-method} 8 | 9 | \title{Unique values} 10 | 11 | \description{ 12 | This function returns the unique values in a RasterLayer object or the unique combinations of the layers in a multilayer object. 13 | } 14 | 15 | \usage{ 16 | \S4method{unique}{RasterLayer,missing}(x, incomparables=FALSE, na.last=NA, progress="", ...) 17 | 18 | \S4method{unique}{RasterStackBrick,missing}(x, incomparables=FALSE, na.last=NA, progress="", ...) 19 | } 20 | 21 | \arguments{ 22 | \item{x}{Raster object} 23 | \item{incomparables}{must be missing. The default value \code{FALSE} is used. See \code{\link[base]{unique}}} 24 | \item{na.last}{logical. for controlling the treatment of \code{NA}s. If \code{TRUE}, missing values in the data are put last; if \code{FALSE}, they are put first; if \code{NA}, they are removed.} 25 | \item{progress}{character. Use "text" or "window" for a progress indicator} 26 | \item{...}{additional arguments. as in \code{\link[base]{unique}}} 27 | } 28 | 29 | \seealso{ \code{\link[base]{unique}} } 30 | 31 | \value{ 32 | vector or matrix 33 | } 34 | 35 | 36 | 37 | \examples{ 38 | r <- raster(ncol=10, nrow=10) 39 | values(r) <- round(runif(ncell(r))*10) 40 | unique(r) 41 | unique(stack(r, round(r/2))) 42 | } 43 | 44 | \keyword{spatial} 45 | -------------------------------------------------------------------------------- /man/unstack.Rd: -------------------------------------------------------------------------------- 1 | \name{unstack} 2 | 3 | \alias{unstack} 4 | \alias{unstack,RasterStack-method} 5 | \alias{unstack,RasterBrick-method} 6 | 7 | \title{ Unstack } 8 | 9 | \description{ 10 | Create a list of RasterLayer objects from a RasterStack or RasterBrick 11 | } 12 | 13 | \usage{ 14 | unstack(x, ...) 15 | } 16 | 17 | \arguments{ 18 | \item{x}{ a RasterStack object } 19 | \item{...}{not used. further arguments passed to or from other methods} 20 | } 21 | 22 | \value{ 23 | A list of RasterLayer objects 24 | } 25 | 26 | 27 | 28 | \seealso{ \code{\link[raster]{stack}}} 29 | 30 | \examples{ 31 | file <- system.file("external/test.grd", package="raster") 32 | s <- stack(file, file) 33 | list1 <- unstack(s) 34 | b <- brick(s) 35 | list2 <- unstack(b) 36 | } 37 | 38 | \keyword{ spatial } 39 | \keyword{ methods } 40 | -------------------------------------------------------------------------------- /man/validCell.Rd: -------------------------------------------------------------------------------- 1 | \name{validCell} 2 | 3 | \alias{validCell} 4 | \alias{validCol} 5 | \alias{validRow} 6 | 7 | 8 | \title{Validity of a cell, column or row number} 9 | 10 | \description{ 11 | Simple helper functions to determine if a row, column or cell number is valid for a certain Raster* object 12 | } 13 | 14 | \usage{ 15 | validCell(object, cell) 16 | validCol(object, colnr) 17 | validRow(object, rownr) 18 | } 19 | 20 | \arguments{ 21 | \item{object}{Raster* object (or a SpatialPixels* or SpatialGrid* object)} 22 | \item{cell}{cell number(s)} 23 | \item{colnr}{column number; or vector of column numbers} 24 | \item{rownr}{row number; or vector of row numbers} 25 | } 26 | 27 | \value{ 28 | logical value 29 | } 30 | 31 | 32 | 33 | \examples{ 34 | #using a new default raster (1 degree global) 35 | r <- raster() 36 | validCell(r, c(-1, 0, 1)) 37 | validRow(r, c(-1, 1, 100, 10000)) 38 | } 39 | 40 | \keyword{spatial} 41 | -------------------------------------------------------------------------------- /man/validNames.Rd: -------------------------------------------------------------------------------- 1 | \name{validNames} 2 | 3 | \alias{validNames} 4 | 5 | \title{Create valid names} 6 | 7 | \description{ 8 | Create a set of valid names (trimmed, no duplicates, not starting with a number). 9 | } 10 | 11 | \usage{ 12 | validNames(x, prefix='layer') 13 | } 14 | 15 | \arguments{ 16 | \item{x}{character} 17 | \item{prefix}{character string used if x is empty} 18 | } 19 | 20 | \value{ 21 | character 22 | } 23 | 24 | 25 | \seealso{ \code{\link{make.names} } } 26 | 27 | 28 | \examples{ 29 | validNames(c('a', 'a', '', '1', NA, 'b', 'a')) 30 | } 31 | 32 | -------------------------------------------------------------------------------- /man/which.Rd: -------------------------------------------------------------------------------- 1 | \name{which} 2 | 3 | \docType{methods} 4 | 5 | \alias{Which} 6 | \alias{Which,RasterLayer-method} 7 | 8 | \title{Which cells are TRUE?} 9 | 10 | \description{ 11 | \code{Which} returns a RasterLayer with \code{TRUE} or \code{FALSE} setting cells that are \code{NA} to \code{FALSE} (unless \code{na.rm=FALSE}). If the RasterLayer has numbers, all values that are 0 become \code{FALSE} and all other values become \code{TRUE}. The function can also return the cell numbers that are \code{TRUE} 12 | } 13 | 14 | 15 | \usage{ 16 | \S4method{Which}{RasterLayer}(x, cells=FALSE, na.rm=TRUE, ...) 17 | } 18 | 19 | \arguments{ 20 | \item{x}{RasterLayer} 21 | \item{cells}{logical. If \code{TRUE}, cell numbers are returned, otherwise a RasterLayer is returned} 22 | \item{na.rm}{logical. If \code{TRUE}, \code{NA} values are treated as \code{FALSE}, otherwise they remain \code{NA} (only when \code{cells=FALSE})} 23 | \item{...}{Additional arguments (none implemented)} 24 | } 25 | 26 | 27 | \seealso{ \code{\link{which.max}}, \code{\link{which.min}} } 28 | 29 | \value{ 30 | RasterLayer 31 | } 32 | 33 | 34 | 35 | \examples{ 36 | r <- raster(ncol=10, nrow=10) 37 | set.seed(0) 38 | values(r) <- runif(ncell(r)) 39 | r[r < 0.2 ] <- 0 40 | r[r > 0.8] <- 1 41 | r[r > 0 & r < 1 ] <- 0.5 42 | 43 | Which(r, cells=TRUE) 44 | Which(r > 0.5, cells=TRUE) 45 | 46 | s1 <- r > 0.5 47 | s2 <- Which(r > 0.5) 48 | s1[1:15] 49 | s2[1:15] 50 | 51 | # this expression 52 | x1 <- Which(r, na.rm=FALSE) 53 | # is the inverse of 54 | x2 <- r==0 55 | } 56 | 57 | \keyword{spatial} 58 | -------------------------------------------------------------------------------- /man/zApply.Rd: -------------------------------------------------------------------------------- 1 | \name{zApply} 2 | 3 | \alias{zApply} 4 | 5 | \title{z (time) apply} 6 | 7 | \description{ 8 | Experimental function to apply a function over a (time) series of layers of a Raster object 9 | } 10 | 11 | \usage{ 12 | zApply(x, by, fun=mean, name='', ...) 13 | } 14 | 15 | \arguments{ 16 | \item{x}{Raster* object} 17 | \item{by}{aggregation indices or function } 18 | \item{fun}{function to compute aggregated values } 19 | \item{name}{character label of the new time series } 20 | \item{...}{additional arguments} 21 | } 22 | 23 | 24 | \value{ 25 | Raster* object 26 | } 27 | 28 | \author{Oscar Perpinan Lamigueiro & Robert J. Hijmans} 29 | 30 | \examples{ 31 | # 12 values of irradiation, 1 for each month 32 | G0dm=c(2.766,3.491,4.494,5.912,6.989,7.742,7.919,7.027,5.369,3.562,2.814,2.179)*1000; 33 | # RasterBrick with 12 layers based on G0dm + noise 34 | r <- raster(nc=10, nr=10) 35 | s <- brick(lapply(1:12, function(x) setValues(r, G0dm[x]+100*rnorm(ncell(r)) ))) 36 | 37 | # time 38 | tm <- seq(as.Date('2010-01-15'), as.Date('2010-12-15'), 'month') 39 | s <- setZ(s, tm, 'months') 40 | 41 | # library(zoo) 42 | # x <- zApply(s, by=as.yearqtr, fun=mean, name='quarters') 43 | } 44 | 45 | \keyword{spatial} 46 | -------------------------------------------------------------------------------- /man/zoom.Rd: -------------------------------------------------------------------------------- 1 | \name{zoom} 2 | 3 | \docType{methods} 4 | 5 | \alias{zoom} 6 | \alias{zoom,Raster-method} 7 | \alias{zoom,Spatial-method} 8 | \alias{zoom,missing-method} 9 | 10 | 11 | \title{Zoom in on a map} 12 | 13 | 14 | \description{ 15 | Zoom in on a map (plot) by providing a new extent, by default this is done by clicking twice on the map. 16 | } 17 | 18 | \usage{ 19 | zoom(x, ...) 20 | \S4method{zoom}{Raster}(x, ext=drawExtent(), maxpixels=100000, layer=1, new=TRUE, useRaster=TRUE, ...) 21 | 22 | \S4method{zoom}{Spatial}(x, ext=drawExtent(), new=TRUE, ...) 23 | 24 | \S4method{zoom}{missing}(x, ext=drawExtent(), new=TRUE, ...) 25 | } 26 | 27 | \arguments{ 28 | \item{x}{Raster* or Spatial* (vector type) object} 29 | \item{ext}{Extent object, or other object from which an extent can be extracted} 30 | \item{maxpixels}{Maximum number of pixels used for the map} 31 | \item{layer}{Positive integer to select the layer to be used if x is a mutilayer Raster object} 32 | \item{new}{Logical. If \code{TRUE}, the zoomed in map will appear on a new device (window)} 33 | \item{useRaster}{Logical. If \code{TRUE}, a bitmap raster is used to plot the image instead of polygons} 34 | \item{...}{additional paramters for base plot} 35 | } 36 | 37 | \value{ 38 | Extent object (invisibly) 39 | } 40 | 41 | 42 | 43 | \seealso{ \code{\link[raster]{drawExtent}}, \code{\link[raster]{plot}}} 44 | 45 | \keyword{spatial} 46 | 47 | -------------------------------------------------------------------------------- /man/zvalues.Rd: -------------------------------------------------------------------------------- 1 | \name{z-values} 2 | 3 | \alias{getZ} 4 | \alias{setZ} 5 | 6 | \title{Get or set z-values} 7 | 8 | \description{ 9 | Initial functions for a somewhat more formal approach to get or set z values (e.g. time) associated with layers of Raster* objects. In development. 10 | } 11 | 12 | \usage{ 13 | setZ(x, z, name='time') 14 | getZ(x) 15 | } 16 | 17 | \arguments{ 18 | \item{x}{Raster* object} 19 | \item{z}{vector of z values of any type (e.g. of class 'Date')} 20 | \item{name}{character label} 21 | } 22 | 23 | 24 | \value{ 25 | setZ: Raster* object 26 | 27 | getZ: vector 28 | } 29 | 30 | 31 | 32 | \examples{ 33 | r <- raster(ncol=10, nrow=10) 34 | s <- stack(lapply(1:3, function(x) setValues(r, runif(ncell(r))))) 35 | s <- setZ(s, as.Date('2000-1-1') + 0:2) 36 | s 37 | getZ(s) 38 | } 39 | 40 | \keyword{spatial} 41 | -------------------------------------------------------------------------------- /src/Makevars.ucrt: -------------------------------------------------------------------------------- 1 | 2 | #PKG_CXXFLAGS += -Wconversion -Wno-sign-conversion 3 | -------------------------------------------------------------------------------- /src/aggregate.h: -------------------------------------------------------------------------------- 1 | std::vector get_dims( std::vector dim); 2 | std::vector > get_aggregates(std::vector > data, std::vector dim); 3 | std::vector > aggregate(std::vector > data, std::vector dim, bool narm, int fun); 4 | -------------------------------------------------------------------------------- /src/bilinear.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | using namespace Rcpp; 4 | 5 | // xy: num[1:n, 1:2] 6 | // x: num[1:2, 1:n] 7 | // y: num[1:2, 1:n] 8 | // v: num[1:n, 1:4] 9 | // columns are: bottom-left, top-left, top-right, bottom-right 10 | 11 | // [[Rcpp::export(name = ".doBilinear")]] 12 | NumericVector doBilinear(NumericMatrix xy, NumericMatrix x, NumericMatrix y, NumericMatrix v) { 13 | size_t len = v.nrow(); 14 | NumericVector result(len); 15 | 16 | for (size_t i = 0; i < len; i++) { 17 | double left = x(0,i); 18 | double right = x(1,i); 19 | double top = y(1,i); 20 | double bottom = y(0,i); 21 | 22 | double horiz = xy(i,0); 23 | double vert = xy(i,1); 24 | 25 | double denom = (right - left) * (top - bottom); 26 | 27 | double bottomLeftValue = v(i,0) / denom; 28 | double topLeftValue = v(i,1) / denom; 29 | double topRightValue = v(i,3) / denom; 30 | double bottomRightValue = v(i,2) / denom; 31 | result[i] = bottomLeftValue*(right-horiz)*(top-vert) + bottomRightValue*(horiz-left)*(top-vert) + 32 | topLeftValue*(right-horiz)*(vert-bottom) + topRightValue*(horiz-left)*(vert-bottom); 33 | } 34 | 35 | return result; 36 | } 37 | 38 | 39 | -------------------------------------------------------------------------------- /src/cellRowCol.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace Rcpp; 3 | 4 | 5 | // [[Rcpp::export(name = ".doCellFromRowCol")]] 6 | NumericVector doCellFromRowCol(IntegerVector nrow, IntegerVector ncol, 7 | IntegerVector rownr, IntegerVector colnr) { 8 | 9 | int nr = nrow[0]; 10 | int nc = ncol[0]; 11 | size_t rownr_size = rownr.size(); 12 | size_t colnr_size = colnr.size(); 13 | 14 | NumericVector result(std::max(rownr_size, colnr_size)); 15 | 16 | // Manually recycle the shorter of rownr/colnr to match the other 17 | size_t len = std::max(rownr.size(), colnr.size()); 18 | 19 | for (size_t i = 0; i < len; i++) { 20 | // The % is to recycle elements if they're not the same length 21 | double r = rownr[i < rownr_size ? i : i % rownr_size]; 22 | double c = colnr[i < colnr_size ? i : i % colnr_size]; 23 | 24 | // Detect out-of-bounds rows/cols and use NA for those 25 | result[i] = (r<1 || r>nr || c<1 || c>nc) ? NA_REAL : (r-1) * nc + c; 26 | } 27 | 28 | return result; 29 | } 30 | -------------------------------------------------------------------------------- /src/clamp.cpp: -------------------------------------------------------------------------------- 1 | /* Robert Hijmans, October 2011 */ 2 | 3 | 4 | #include 5 | 6 | // [[Rcpp::export(name = ".clamp")]] 7 | Rcpp::NumericVector do_clamp(std::vector d, std::vector r, bool usevals) { 8 | 9 | size_t n = d.size(); 10 | Rcpp::NumericVector val(n); 11 | 12 | if (usevals) { 13 | for (size_t i=0; i r[1] ) { 17 | val[i] = r[1]; 18 | } else { 19 | val[i] = d[i]; 20 | } 21 | } 22 | } else { 23 | for (size_t i=0; i r[1])) { 25 | val[i] = NAN; 26 | } else { 27 | val[i] = d[i]; 28 | } 29 | } 30 | } 31 | return(val); 32 | } 33 | 34 | 35 | -------------------------------------------------------------------------------- /src/focal_get.cpp: -------------------------------------------------------------------------------- 1 | /* Robert Hijmans, October 2011 */ 2 | 3 | 4 | #include 5 | 6 | // [[Rcpp::export(name = ".focal_get")]] 7 | std::vector do_focal_get(std::vector d, std::vector dim, std::vector ngb) { 8 | 9 | int nrow = dim[0]; 10 | int ncol = dim[1]; 11 | 12 | int wrows = ngb[0]; 13 | int wcols = ngb[1]; 14 | 15 | size_t n = (nrow-wrows+1) * (ncol-wcols+1) * wrows * wcols; 16 | std::vector val(n); 17 | 18 | if ((wrows % 2 == 0) | (wcols % 2 == 0)) { 19 | Rcpp::Rcerr << "weights matrix must have uneven sides"; 20 | return(val); 21 | } 22 | 23 | int wr = wrows / 2; 24 | int wc = wcols / 2; 25 | wr = std::min(wr, nrow); 26 | wc = std::min(wc, ncol); 27 | 28 | int f = 0; 29 | 30 | for (int i = 0+wr; i < nrow-wr; i++) { 31 | for (int j = 0+wc; j < ncol-wc; j++) { 32 | for (int a=-wr; a <= wr ; a++) { 33 | int aa = (i+a) * ncol; 34 | for (int b=-wc; b <= wc ; b++) { 35 | val[f] = d[aa+j+b]; 36 | f++; 37 | } 38 | } 39 | } 40 | } 41 | 42 | return(val); 43 | } 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /src/layerize.cpp: -------------------------------------------------------------------------------- 1 | /* Robert Hijmans, October 2011 */ 2 | 3 | #include 4 | 5 | // [[Rcpp::export(name = ".layerize")]] 6 | Rcpp::NumericVector layerize(std::vector d, std::vector cls, bool falsena) { 7 | 8 | int vna = falsena ? R_NaInt : 0; 9 | size_t m = d.size(); 10 | size_t n = cls.size(); 11 | Rcpp::NumericVector v(m * n, vna); 12 | 13 | for (size_t i=0; i 7 | #include "Rmath.h" 8 | 9 | #ifndef M_PI 10 | #define M_PI (3.14159265358979323846) 11 | #endif 12 | 13 | 14 | double mod(double x, double n) { 15 | return(x - n * floor(x/n)); 16 | } 17 | 18 | double normalizeLonDeg(double lon) { 19 | return( mod( (lon + 180), 360 ) - 180 ); 20 | } 21 | 22 | double normalizeLonRad(double lon) { 23 | return( mod( (lon + M_PI), M_2PI) - M_PI); 24 | } 25 | 26 | 27 | /* Convert degrees to radians */ 28 | double toRad(double deg) { 29 | return( deg * 0.0174532925199433 ); 30 | } 31 | 32 | double toDeg(double rad) { 33 | return( rad * 57.2957795130823 ); 34 | } 35 | 36 | 37 | 38 | 39 | #endif 40 | 41 | -------------------------------------------------------------------------------- /src/util.h: -------------------------------------------------------------------------------- 1 | /* modulo */ 2 | double mod(double x, double n) ; 3 | 4 | /* Convert degrees to radians */ 5 | double toRad(double deg) ; 6 | 7 | /* Convert radians to degrees */ 8 | double toDeg(double rad) ; 9 | 10 | /* normatlize longitude between -180 .. 180 degrees*/ 11 | double normalizeLonDeg(double lon); 12 | 13 | /* normatlize longitude between -pi .. p1 radians*/ 14 | double normalizeLonRad(double lon); 15 | 16 | -------------------------------------------------------------------------------- /tests/tinytest.R: -------------------------------------------------------------------------------- 1 | 2 | if ( requireNamespace("tinytest", quietly=TRUE) ){ 3 | tinytest::test_package("raster") 4 | } 5 | 6 | --------------------------------------------------------------------------------