# Implement r.cross / ArcGIS Combine with GDAL

I am using GDAL to process a lot of rasters. I then need to combine all the arrays to create a single output raster that represents all the unique combinations of values. I.E the same thing as r.cross in GRASS or Combine_sa in arcgis.

I'd like to program a similar thing using the GDAL library. What is the algorithm? How would I handle rasters that are too large to fit in RAM? I.e., when chunking them into smaller arrays, how do I ensure that a combination in the nth chunk is given the same value if it appeared in a different chunk?

I'm struggling to come up with anything that will perform well.

• When you say "a lot of rasters", what kind of number are we looking at? Also, how many unique numbers are in each raster? Doesn't have to be precise, just a rough estimate. – Mikkel Lydholm Rasmussen Apr 23 '15 at 16:18
• @MikkelLydholmRasmussen There will be between 12-20 input rasters depending on configuration. For each raster, before combining a number of additional values (for each cell) are calculated, meaning that for a single cell, there will be about 30-40 values to combine. Unique numbers in each raster (to begin with) is not that many - usually 6. But with 30 - 40 values for a single cell, that is a lot of possible combinations. I have started programming a possible solution based on a btree to look up values, which I believe r.cross uses. – James Apr 24 '15 at 6:21
• I think that you may run into problems with there simply being too many possible combinations for a raster to handle. Assuming 20 rasters, each with 6 unique values, that ends up as 6^20 = 3.6561584e+15 combinations, which is to say that you need to operate with 64-bit integer rasters, which are uncommon. Furthermore, the reverse look-up is going to be a pain to work through. – Mikkel Lydholm Rasmussen Apr 24 '15 at 6:54

\sum_i {(n^i) * c_i }