[gdal-dev] C++ Range and Iterator for GDALDataSet

alex alexhighviz at hotmail.com
Wed Jan 27 03:16:54 PST 2016


>> I found these classes very useful for simple map algebra type applications,
>
>In map algebra you often have two operands. Assuming two raster bands
>have the same size and georeferencing, how would you write a code that
>works on both of them?
>

For that I use a zip range. The following example adds two bands and assigns the result to a third.

auto a = open_gdal_raster<int>("a.tif", GA_ReadOnly);
auto b = open_gdal_raster<int>("b.tif", GA_ReadOnly);
auto out = open_gdal_raster<int>("out.tif", GA_Update);
auto zip = make_zip_range(std::ref(a), std::ref(b), std::ref(out) );
for(auto&& i : zip) {
  std::get<2>(i) =  std::get<0>(i) + std::get<1>(i); 
} 

Alternatively, you could do the same thing using iterators:

auto a = open_gdal_raster<int>("a.tif", GA_ReadOnly);
auto b = open_gdal_raster<int>("b.tif", GA_ReadOnly);
auto out = open_gdal_raster<int>("out.tif", GA_Update);
auto ai = a.begin();
auto bi = b.begin();
auto outi = out.begin();
for( ; ai != a.end(); ++ai, ++bi, ++outi) {
  *outi = *ai + *bi;
}


>> auto input = open_gdal_dataset<int>("input.tif", GA_ReadOnly);
This was a typo; it should have been open_gdal_raster<int>

>> I realize that the primary language of GDAL is not C++.
>
>GDAL is written in C++ but it has a C API, which is used with other
>languages.
>
>Ari
>
That is fortunate!



More information about the gdal-dev mailing list