0

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[])
6
  • So far couldn't find a way to do that either... Why not read the values into R and calculate the histogram there? Memory limitation? Commented Sep 13, 2020 at 7:47
  • 1
    Yes, Michael. Extremely big files Commented Sep 13, 2020 at 13:38
  • If there is no other solution, you can always read the raster in chunks. First figure out the dimensions (this can be done without reading the entire file), then run a loop that goes over 1000 rows each time, for instance. The 'stars' package can be used to do that: cran.r-project.org/web/packages/stars/vignettes/… Commented Sep 13, 2020 at 17:10
  • 1
    You might also want to check out 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 to raster::freq(). Commented Sep 14, 2020 at 21:13
  • 1
    @gonzalez.ivan90 Yes, the functions in rasterDT are all written to handle rasters that are too large to process in RAM. Let me know how freqDT() ends up working for you in the end. Commented Sep 19, 2020 at 15:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.