9#if !defined(ISCE_IO_RASTER_ICC)
10#error "Raster.icc is an implementation detail of class Raster"
14#include <isce3/except/Error.h>
25 GDALDriver* memDriver = (GDALDriver*) GDALGetDriverByName(
"MEM");
32 char** memdsOptions =
nullptr;
38 memset(szTmp, 0,
sizeof(szTmp));
39 CPLPrintPointer(szTmp, ptr,
sizeof(szTmp));
40 memdsOptions = CSLSetNameValue(memdsOptions,
"DATAPOINTER", szTmp);
43 memset(szTmp, 0,
sizeof(szTmp));
44 CPLPrintUIntBig(szTmp, lineoffset,
sizeof(szTmp));
45 memdsOptions = CSLSetNameValue(memdsOptions,
"LINEOFFSET", szTmp);
48 memset(szTmp, 0,
sizeof(szTmp));
49 CPLPrintUIntBig(szTmp, pixeloffset,
sizeof(szTmp));
50 memdsOptions = CSLSetNameValue(memdsOptions,
"PIXELOFFSET", szTmp);
58template <
typename Derived>
59isce3::io::Raster::Raster(Eigen::PlainObjectBase<Derived>& view)
61 using Scalar =
typename Eigen::PlainObjectBase<Derived>::value_type;
66 throw std::runtime_error(
"Input view is not packed in row major order");
70 size_t bytesperunit =
sizeof(Scalar);
73 size_t pixeloffset = (
char*) &view(0, 1) - (
char*) &view(0, 0);
74 size_t lineoffset = (
char*) &view(1, 0) - (
char*) &view(0, 0);
77 if ((pixeloffset < bytesperunit) || (lineoffset < bytesperunit))
79 throw std::runtime_error(
"Something looks fishy in the offset estimation");
82 initFromPointer(view.data(),
95 throw isce3::except::RuntimeError(ISCE_SRCINFO(),
"cannot copy non-owning raster");
113 GDALAccess
access=GA_ReadOnly) {
114 GDALClose( _dataset );
115 dataset(
static_cast<GDALDataset*
>(GDALOpenShared( fname.c_str(),
access )) );
123 throw isce3::except::LengthError(ISCE_SRCINFO(),
"Rasters have different sizes.");
125 for (
size_t b = 1; b <= rast.
numBands(); ++b)
133 if (strcmp(
dataset()->GetDriverName(),
"VRT"))
134 throw isce3::except::RuntimeError(ISCE_SRCINFO(),
"GDAL driver must be VRT.");
136 dataset()->AddBand(inBand->GetRasterDataType(),
nullptr);
138 VRTAddSimpleSource(rastBand, inBand,
139 0, 0, inBand->GetXSize(), inBand->GetYSize(),
140 0, 0, inBand->GetXSize(), inBand->GetYSize(),
141 nullptr, VRT_NODATA_UNSET);
149 if (strcmp(
dataset()->GetDriverName(),
"VRT"))
150 throw isce3::except::LengthError(ISCE_SRCINFO(),
"GDAL driver must be VRT.");
152 char** papszOptions =
nullptr;
153 std::string srcName = fname.substr(0, fname.find_last_of(
"."));
154 papszOptions = CSLAddNameValue(papszOptions,
"subclass",
"VRTRawRasterBand");
155 papszOptions = CSLAddNameValue(papszOptions,
"SourceFilename", srcName.c_str());
156 papszOptions = CSLAddNameValue(papszOptions,
"RelativeToVRT",
"true");
158 CSLDestroy(papszOptions);
183 auto iostat = _dataset->GetRasterBand(band)->RasterIO(
184 iodir, xidx, yidx, 1, 1, &buffer, 1, 1, asGDT<T>, 0, 0);
186 if (iostat != CPLE_None)
187 std::cout <<
"In isce3::io::Raster::getSetValue() - error in RasterIO." << std::endl;
232 size_t rdwidth = std::min(iowidth,
width());
233 auto iostat = _dataset->GetRasterBand(band)->RasterIO(iodir, 0, yidx, rdwidth, 1, buffer,
234 rdwidth, 1, asGDT<T>, 0, 0);
236 if (iostat != CPLE_None)
237 std::cout <<
"In isce3::io::Raster::get/setLine() - error in RasterIO." << std::endl;
248 getSetLine(buffer, yidx, iowidth, band, GF_Read);
255 getLine(buffer.data(), yidx, buffer.size(), band);
262 getLine(&buffer[0], yidx, buffer.size(), band);
273 getSetLine(buffer, yidx, iowidth, band, GF_Write);
280 setLine(buffer.data(), yidx, buffer.size(), band);
287 setLine(&buffer[0], yidx, buffer.size(), band);
314 auto iostat = _dataset->GetRasterBand(band)->RasterIO(iodir, xidx, yidx, iowidth,
315 iolength, buffer, iowidth,
319 if (iostat != CPLE_None)
320 std::cout <<
"In isce3::io::Raster::get/setValue() - error in RasterIO." << std::endl;
333 size_t iowidth,
size_t iolength,
size_t band) {
334 getSetBlock(buffer, xidx, yidx, iowidth, iolength, band, GF_Read);
340 size_t iowidth,
size_t iolength,
size_t band) {
342 if ((iolength * iowidth) > buffer.size())
343 throw isce3::except::LengthError(ISCE_SRCINFO(),
"Requested more elements than buffer size.");
345 getBlock(buffer.data(), xidx, yidx, iowidth, iolength, band);
347 if ((iolength * iowidth) < buffer.size())
348 std::cout <<
"WARNING: In isce3::io::Raster::getBlock() - Requested fewer elements than buffer size. " << std::endl;
355 size_t iowidth,
size_t iolength,
size_t band) {
357 if ((iolength * iowidth) > buffer.size())
358 throw isce3::except::LengthError(ISCE_SRCINFO(),
"Requested more elements than buffer size.");
360 getBlock(&buffer[0], xidx, yidx, iowidth, iolength, band);
362 if ((iolength * iowidth) < buffer.size())
363 std::cout <<
"WARNING: In isce3::io::Raster::getBlock() - Requested fewer elements than buffer size. " << std::endl;
378 getBlock(mat.data(), xidx, yidx, mat.cols(), mat.rows(), band);
385 getBlock(mat.data(), xidx, yidx, mat.cols(), mat.rows(), band);
397 size_t iowidth,
size_t iolength,
size_t band) {
398 getSetBlock(buffer, xidx, yidx, iowidth, iolength, band, GF_Write);
405 size_t iowidth,
size_t iolength,
size_t band) {
407 if ((iolength * iowidth) > buffer.size())
408 throw isce3::except::LengthError(ISCE_SRCINFO(),
"Requested more elements than buffer size.");
410 setBlock(buffer.data(), xidx, yidx, iowidth, iolength, band);
412 if ((iolength * iowidth) < buffer.size())
413 std::cout <<
"WARNING: In isce3::io::Raster::getBlock() - Requested fewer elements than buffer size. " << std::endl;
420 size_t iowidth,
size_t iolength,
size_t band) {
422 if ((iolength * iowidth) > buffer.size())
423 throw isce3::except::LengthError(ISCE_SRCINFO(),
"Requested more elements than buffer size.");
425 setBlock(&buffer[0], xidx, yidx, iowidth, iolength, band);
427 if ((iolength * iowidth) < buffer.size())
428 std::cout <<
"WARNING: In isce3::io::Raster::getBlock() - Requested fewer elements than buffer size. " << std::endl;
436 setBlock(mat.data(), xidx, yidx, mat.
width(), mat.
length(), band);
443 setBlock(mat.data(), xidx, yidx, mat.cols(), mat.rows(), band);
450 setBlock(mat.data(), xidx, yidx, mat.cols(), mat.rows(), band);
459 int status = _dataset->SetGeoTransform(arr);
461 throw isce3::except::RuntimeError(ISCE_SRCINFO(),
"Could not set GDAL GeoTransform");
469 if (arr.size() != 6) {
470 throw isce3::except::LengthError(ISCE_SRCINFO(),
"vector is not of size 6");
479 if (arr.size() != 6) {
480 throw isce3::except::LengthError(ISCE_SRCINFO(),
"valarray is not of size 6");
491 int status = _dataset->GetGeoTransform(arr);
493 throw isce3::except::RuntimeError(ISCE_SRCINFO(),
"Could not fetch GDAL GeoTransform");
501 if (arr.size() != 6) {
502 throw isce3::except::LengthError(ISCE_SRCINFO(),
"vector is not of size 6");
511 if (arr.size() != 6) {
512 throw isce3::except::LengthError(ISCE_SRCINFO(),
"valarray is not of size 6");
Data structure for a 2D row-major matrix.
Definition Matrix.h:23
size_t length() const
Get matrix length.
Definition Matrix.h:77
size_t width() const
Get matrix width.
Definition Matrix.h:74
Data structure meant to handle Raster I/O operations.
Definition Raster.h:32
double y0() const
Return Northern Limit of Raster.
Definition Raster.icc:526
void getLine(T *buffer, size_t yidx, size_t iowidth, size_t band=1)
Read one line of data from given band to buffer, vector, or valarray.
Definition Raster.icc:247
void getSetValue(T &buffer, size_t xidz, size_t yidx, size_t band, GDALRWFlag)
Get/Set single value for given band.
Definition Raster.icc:177
void setValue(T &buffer, size_t xidx, size_t yidx, size_t band=1)
Definition Raster.icc:208
size_t width() const
Width getter.
Definition Raster.h:119
GDALAccess access() const
Access mode getter.
Definition Raster.h:125
void getValue(T &buffer, size_t xidx, size_t yidx, size_t band=1)
Definition Raster.icc:197
void addRasterToVRT(const isce3::io::Raster &rast)
Add a raster to VRT.
Definition Raster.icc:121
void getGeoTransform(double *) const
Copy Raster GeoTransform into a buffer, vector, or valarray.
Definition Raster.icc:489
GDALDataType dtype(const size_t band=1) const
Return GDALDatatype of specified band.
Definition Raster.h:141
GDALDataset * dataset() const
GDALDataset pointer getter.
Definition Raster.h:128
size_t numBands() const
Number of bands getter.
Definition Raster.h:122
double dx() const
Return EW pixel spacing of Raster.
Definition Raster.icc:534
void addBandToVRT(GDALRasterBand *inBand)
Add a GDALRasterBand to VRT.
Definition Raster.icc:132
void setLine(T *buffer, size_t yidx, size_t iowidth, size_t band=1)
Write one line of data from buffer, vector, or valarray to given band.
Definition Raster.icc:272
void setBlock(T *buffer, size_t xidx, size_t yidx, size_t iowidth, size_t iolength, size_t band=1)
Write block of data to given band from buffer, vector, or valarray.
Definition Raster.icc:396
void getSetLine(T *buffer, size_t yidx, size_t iowidth, size_t band, GDALRWFlag iodir)
Get/Set line in a band from raw pointer.
Definition Raster.icc:226
double x0() const
Return Western Limit of Raster.
Definition Raster.icc:518
void initFromPointer(void *ptr, GDALDataType dtype, size_t width, size_t length, size_t pixeloffset, size_t lineoffset)
Construct dataset for a 1 band dataset with raw pointer, dimensions and offsets.
Definition Raster.icc:17
Raster & operator=(const Raster &)
Assignment operator.
Definition Raster.icc:92
double dy() const
Return NS pixel spacing of Raster.
Definition Raster.icc:542
bool match(const Raster &rast) const
Check dimensions compatibility with another raster.
Definition Raster.h:146
void setGeoTransform(double *arr)
Set Raster GeoTransform from buffer, vector, or valarray.
Definition Raster.icc:457
size_t length() const
Length getter.
Definition Raster.h:116
void getSetBlock(T *buffer, size_t xidx, size_t yidx, size_t iowidth, size_t iolength, size_t band, GDALRWFlag iodir)
Get/Set block in band from raw pointer.
Definition Raster.icc:306
void open(const std::string &fname, GDALAccess access)
Open file with GDAL.
Definition Raster.icc:112
void getBlock(T *buffer, size_t xidx, size_t yidx, size_t iowidth, size_t iolength, size_t band=1)
Read block of data from given band to buffer, vector, or valarray.
Definition Raster.icc:332
void addRawBandToVRT(const std::string &fname, GDALDataType dtype)
Add a raw data band to VRT.
Definition Raster.icc:148