15 static bool initialized =
false;
16 if (initialized)
return;
20 throw std::runtime_error(
"GDAL support was not enabled at compile time");
25template<
typename T> GDALDataType get_gdal_datatype() {
throw std::runtime_error(
"get_gdal_datatype called for unsupported type"); }
26template<> GDALDataType get_gdal_datatype<unsigned char>() {
return GDT_Byte; }
27template<> GDALDataType get_gdal_datatype<unsigned short>() {
return GDT_UInt16; }
28template<> GDALDataType get_gdal_datatype<short>() {
return GDT_Int16; }
29template<> GDALDataType get_gdal_datatype<double>() {
return GDT_Float64; }
30template<> GDALDataType get_gdal_datatype<int>() {
return GDT_Int32; }
31template<> GDALDataType get_gdal_datatype<unsigned>() {
return GDT_UInt32; }
35class MatrixDataset :
public GDALDataset
38 const Matrix2D<T>& image;
40 MatrixDataset(
const Matrix2D<T>& image);
52class MatrixRasterBand :
public GDALRasterBand
55 const Matrix2D<T>& image;
57 MatrixRasterBand(MatrixDataset<T>& ds)
62 nBlockXSize = image.cols();
63 nBlockYSize = image.rows();
66 eDataType = get_gdal_datatype<T>();
71 CPLErr IReadBlock(
int xblock,
int yblock,
void *buf)
override
73 if (xblock != 0 || yblock != 0)
75 CPLError(CE_Failure, CPLE_AppDefined,
"Invalid block number");
79 memcpy(buf, image.data(), image.size() *
sizeof(T));
90MatrixDataset<T>::MatrixDataset(
const Matrix2D<T>& image)
93 nRasterXSize = image.cols();
94 nRasterYSize = image.rows();
95 SetBand(1,
new MatrixRasterBand<T>(*
this));
99void write_image(
const Matrix2D<T>& image,
const std::string& fname,
const std::string& format)
101 unique_ptr<MatrixDataset<T>> src(
new MatrixDataset<T>(image));
102 GDALDriver *driver = GetGDALDriverManager()->GetDriverByName(format.c_str());
104 throw std::runtime_error(
"driver not found for " + format);
106 GDALDataset* dst = driver->CreateCopy(fname.c_str(), src.get(),
false, NULL, NULL, NULL);
108 throw std::runtime_error(
"cannot create " + fname);
115void write_image(
const Matrix2D<T>& image,
const std::string& fname,
const std::string& format)
117 throw std::runtime_error(
"GDAL support was not enabled at compile time");
121template void write_image(
const Matrix2D<unsigned char>&,
const std::string&,
const std::string&);
122template void write_image(
const Matrix2D<unsigned short>&,
const std::string&,
const std::string&);
123template void write_image(
const Matrix2D<double>&,
const std::string&,
const std::string&);
124template void write_image(
const Matrix2D<int>&,
const std::string&,
const std::string&);
125template void write_image(
const Matrix2D<unsigned>&,
const std::string&,
const std::string&);
126template void write_image(
const Matrix2D<short>&,
const std::string&,
const std::string&);
128std::string gdal_extension_for_format(
const std::string& format)
131 GDALDriver *driver = GetGDALDriverManager()->GetDriverByName(format.c_str());
133 throw std::runtime_error(
"driver not found for " + format);
135 const char* ext = driver->GetMetadataItem(GDAL_DMD_EXTENSION, NULL);
137 throw std::runtime_error(
"extension not found for format " + format);
140 throw std::runtime_error(
"GDAL support was not enabled at compile time");
void gdal_init_once()
Initialize the GDAL library when called for the first time; does nothing all other times.