15 static bool initialized =
false;
16 if (initialized)
return;
20 throw std::runtime_error(
"GDAL support was not enabled at compile time");
25 template<
typename T> GDALDataType get_gdal_datatype() {
throw std::runtime_error(
"get_gdal_datatype called for unsupported type"); }
26 template<> GDALDataType get_gdal_datatype<unsigned char>() {
return GDT_Byte; }
27 template<> GDALDataType get_gdal_datatype<unsigned short>() {
return GDT_UInt16; }
28 template<> GDALDataType get_gdal_datatype<short>() {
return GDT_Int16; }
29 template<> GDALDataType get_gdal_datatype<double>() {
return GDT_Float64; }
30 template<> GDALDataType get_gdal_datatype<int>() {
return GDT_Int32; }
31 template<> GDALDataType get_gdal_datatype<unsigned>() {
return GDT_UInt32; }
35 class MatrixDataset :
public GDALDataset
38 const Matrix2D<T>& image;
40 MatrixDataset(
const Matrix2D<T>& image);
52 class 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));
90 MatrixDataset<T>::MatrixDataset(
const Matrix2D<T>& image)
93 nRasterXSize = image.cols();
94 nRasterYSize = image.rows();
95 SetBand(1,
new MatrixRasterBand<T>(*
this));
99 void 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);
115 void 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");
121 template void write_image(
const Matrix2D<unsigned char>&,
const std::string&,
const std::string&);
122 template void write_image(
const Matrix2D<unsigned short>&,
const std::string&,
const std::string&);
123 template void write_image(
const Matrix2D<double>&,
const std::string&,
const std::string&);
124 template void write_image(
const Matrix2D<int>&,
const std::string&,
const std::string&);
125 template void write_image(
const Matrix2D<unsigned>&,
const std::string&,
const std::string&);
126 template void write_image(
const Matrix2D<short>&,
const std::string&,
const std::string&);
128 std::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...