I need to calculate a histogram (pixel-count by each unique value, actually) for more than 256 values , the default number of buckets in gdalinfo. I understand that GDALGetRasterHistogram can deal with this, but I can't find an option to call/use that function/tool from R o command-line.
Any idea?
Here is an example:
## Raster with more than 256 values
r <- raster::raster(matrix(1:10000, ncol = 100))
raster::writeRaster(r, 'C:/temp/10000.tif')
## Fast GDAL implementation, but limited to 256 values
currentHist <- capture.output(gdalUtilities::gdalinfo('C:/temp/10000.tif', hist = TRUE ) )
## R option to calulate the values, but very slow in big files
desiredAndVerySlowHist <- table(r[])
freqDT()
in the rasterDT package. It processes large rasters in chunks and will likely get you at least a 6-fold speedup (see here for example) relative toraster::freq()
.freqDT()
ends up working for you in the end.