isce3 0.25.0
Loading...
Searching...
No Matches
Raster.h
1//-*- C++ -*-
2//-*- coding: utf-8 -*-
3//
4// Author: Marco Lavalle
5// Original code: Joshua Cohen
6// Copyright 2018
7
8#pragma once
9
10#include "forward.h"
11
12#include <complex>
13#include <cstdint>
14#include <string>
15#include <typeindex>
16#include <unordered_map>
17#include <vector>
18#include <valarray>
19#include <gdal_priv.h>
20#include <gdal_vrt.h>
21#include <ogr_spatialref.h>
22#include "Constants.h"
23#include <isce3/core/Matrix.h>
24
25#include <isce3/io/gdal/Raster.h>
26
33
34 public:
35
36 Raster() {_dataset = nullptr;};
37
39 Raster(const std::string& fname, GDALAccess access = GA_ReadOnly);
40
42 Raster(const std::string& fname, size_t width, size_t length,
43 size_t numBands, GDALDataType dtype = isce3::io::defaultGDALDataType,
44 const std::string& driverName = isce3::io::defaultGDALDriver);
45
47 Raster(const std::string& fname, size_t width, size_t length, GDALDataType dtype = isce3::io::defaultGDALDataType);
48
49 // Constructor for a 1 band dataset from isce3::core::Matrix<T>::view_type
50 template<typename T> Raster(Eigen::PlainObjectBase<T> &view);
51
52 // Constructor for a 1 band dataset from isce3::core::Matrix<T> view type
53 template <typename Derived>
54 Raster(Eigen::Block<Derived>& view)
55 {
56 using Scalar = typename Eigen::PlainObjectBase<Derived>::value_type;
57
58 //Get the packing. Update with pyre error logging.
59 if (!view.IsRowMajor)
60 {
61 throw std::runtime_error("Input view is not packed in row major order");
62 }
63
64 //Size of each element
65 size_t bytesperunit = sizeof(Scalar);
66
67 //Pointer and offset math
68 size_t pixeloffset = (const char*) &view(0, 1) - (const char*) &view(0, 0);
69 size_t lineoffset = (const char*) &view(1, 0) - (const char*) &view(0, 0);
70
71 //Update with pyre error logging
72 if ((pixeloffset < bytesperunit) || (lineoffset < bytesperunit))
73 {
74 throw std::runtime_error("Invalid pixel/line offset");
75 }
76
77 initFromPointer(view.data(),
78 asGDT<Scalar>,
79 view.cols(),
80 view.rows(),
81 pixeloffset,
82 lineoffset);
83 }
84
85
86
88 Raster(const std::string& fname, const Raster& rast);
89 template<typename T> Raster(const std::string& fname, const std::vector<T>& buffer, size_t length) :
90 Raster(fname, buffer.size(), length, 1, asGDT<T>) {}
91 template<typename T> Raster(const std::string& fname, const std::valarray<T>& buffer, size_t length) :
92 Raster(fname, buffer.size(), length, 1, asGDT<T>) {}
93
95 Raster(const std::string& fname, const std::vector<Raster>& rastVec);
96
99
101 Raster(const Raster&);
102
104 Raster(GDALDataset *inputDataset, bool owner=true);
105
107 inline void initFromPointer(void* ptr, GDALDataType dtype, size_t width, size_t length, size_t pixeloffset, size_t lineoffset);
108
110 ~Raster();
111
113 inline Raster& operator=(const Raster&);
114
116 inline size_t length() const { return _dataset->GetRasterYSize(); }
117
119 inline size_t width() const { return _dataset->GetRasterXSize(); }
120
122 inline size_t numBands() const { return _dataset->GetRasterCount(); }
123
125 inline GDALAccess access() const { return _dataset->GetAccess(); }
126
128 inline GDALDataset* dataset() const { return _dataset; }
129
133 inline void dataset(GDALDataset* ds) { _dataset=ds; }
134
136 inline bool dataset_owner() const { return _owner; }
137
141 inline GDALDataType dtype(const size_t band=1) const { return _dataset->GetRasterBand(band)->GetRasterDataType(); }
142
146 inline bool match(const Raster & rast) const { return width()==rast.width() && length()==rast.length(); }
147
149 inline void open(const std::string &fname, GDALAccess access);
150
152 inline void addRasterToVRT(const isce3::io::Raster& rast);
153
155 inline void addBandToVRT(GDALRasterBand *inBand);
156
158 inline void addRawBandToVRT(const std::string &fname, GDALDataType dtype);
159 //void close() { GDALClose( _dataset ); } // todo: fix segfault conflict with destructor
160
161 //Pixel read/write with buffer passed by reference, optional band index
163 template<typename T> void getSetValue(T& buffer, size_t xidz, size_t yidx, size_t band, GDALRWFlag);
164 template<typename T> void getValue(T& buffer, size_t xidx, size_t yidx, size_t band = 1);
165 template<typename T> void setValue(T& buffer, size_t xidx, size_t yidx, size_t band = 1);
166
167 // Line read/write with raw pointer and width or STL container, optional band index
169 template<typename T> void getSetLine(T* buffer, size_t yidx, size_t iowidth, size_t band, GDALRWFlag iodir);
171 template<typename T> void getLine(T* buffer, size_t yidx, size_t iowidth, size_t band = 1);
172 template<typename T> void getLine(std::vector<T>& vec, size_t yidx, size_t band = 1);
173 template<typename T> void getLine(std::valarray<T>& arr, size_t yidx, size_t band = 1);
175 template<typename T> void setLine(T* buffer, size_t yidx, size_t iowidth, size_t band = 1);
176 template<typename T> void setLine(std::vector<T>& vec, size_t yidx, size_t band = 1);
177 template<typename T> void setLine(std::valarray<T>& arr, size_t yidx, size_t band = 1);
178
179 // 2D block read/write, generic container w/ width or STL container, optional band index
181 template<typename T> void getSetBlock(T* buffer, size_t xidx, size_t yidx, size_t iowidth, size_t iolength, size_t band, GDALRWFlag iodir);
183 template<typename T> void getBlock(T* buffer, size_t xidx, size_t yidx, size_t iowidth, size_t iolength, size_t band = 1);
184 template<typename T> void getBlock(std::vector<T>& vec, size_t xidx, size_t yidx, size_t iowidth, size_t iolength, size_t band = 1);
185 template<typename T> void getBlock(std::valarray<T>& arr, size_t xidx, size_t yidx, size_t iowidth, size_t iolength, size_t band = 1);
187 template<typename T> void setBlock(T* buffer, size_t xidx, size_t yidx, size_t iowidth, size_t iolength, size_t band = 1);
188 template<typename T> void setBlock(std::vector<T>& vec, size_t xidx, size_t yidx, size_t iowidth, size_t iolength, size_t band = 1);
189 template<typename T> void setBlock(std::valarray<T>& arr, size_t xidx, size_t yidx, size_t iowidth, size_t iolength, size_t band = 1);
190
191 //2D block read/write for Matrix<T>, optional band index
193 template<typename T> void getBlock(isce3::core::Matrix<T>& mat, size_t xidx, size_t yidx, size_t band = 1);
194 template<typename T> void setBlock(isce3::core::Matrix<T>& mat, size_t xidx, size_t yidx, size_t band = 1);
195 //2D block read/write for Matrix<T>, optional band index
196
198 template<typename T> void getBlock(isce3::core::EArray2D<T>& mat, size_t xidx, size_t yidx, size_t band = 1);
199 template<typename T> void setBlock(isce3::core::EArray2D<T>& mat, size_t xidx, size_t yidx, size_t band = 1);
200
202 template<typename T> void getBlock(isce3::core::EMatrix2D<T>& mat, size_t xidx, size_t yidx, size_t band = 1);
203 template<typename T> void setBlock(isce3::core::EMatrix2D<T>& mat, size_t xidx, size_t yidx, size_t band = 1);
204
205 template<typename Derived>
206 void setBlock(const Eigen::Block<Derived>& block, int xoff, int yoff, int band = 1) {
207
208 using T = typename Eigen::Block<Derived>::value_type;
209
210 const int nxsize = block.cols();
211 const int nysize = block.rows();
212
213 const size_t line_spacing = (char*) &block(1, 0) - (char*) &block(0, 0);
214
215 auto iodir = GF_Write;
216 auto iostat = _dataset->GetRasterBand(band)->RasterIO(
217 iodir, xoff, yoff, nxsize, nysize,
218 (void*) &block(0, 0), nxsize, nysize, asGDT<T>,
219 sizeof(T), line_spacing);
220
221 if (iostat != CPLE_None) { // RasterIO returned errors
222 throw std::runtime_error(
223 "Raster::getBlock(Eigen::Block): error in RasterIO");
224 }
225 }
226
227 template<typename Derived>
228 void getBlock(Eigen::Block<Derived>& block, int xoff, int yoff, int band = 1) const {
229
230 using T = typename Eigen::Block<Derived>::value_type;
231
232 const int nxsize = block.cols();
233 const int nysize = block.rows();
234
235 const size_t line_spacing = (char*) &block(1, 0) - (char*) &block(0, 0);
236
237 auto iodir = GF_Read;
238 auto iostat = _dataset->GetRasterBand(band)->RasterIO(
239 iodir, xoff, yoff, nxsize, nysize,
240 (void*) &block(0, 0), nxsize, nysize, asGDT<T>,
241 sizeof(T), line_spacing);
242
243 if (iostat != CPLE_None) { // RasterIO returned errors
244 throw std::runtime_error(
245 "Raster::getBlock(Eigen::Block): error in RasterIO");
246 }
247 }
248
249 //Functions to deal with projections and geotransform information
251 int getEPSG() const;
253 int setEPSG(int code);
255 inline void setGeoTransform(double *arr);
256 inline void setGeoTransform(std::vector<double>&);
257 inline void setGeoTransform(std::valarray<double>&);
259 inline void getGeoTransform(double *) const;
260 inline void getGeoTransform(std::vector<double>&) const;
261 inline void getGeoTransform(std::valarray<double>&) const;
262 //Read only functions for specific elements of GeoTransform
264 inline double x0() const;
266 inline double y0() const;
268 inline double dx() const;
270 inline double dy() const;
271
272private:
273 GDALDataset * _dataset;
274 bool _owner = true;
275};
276
277#define ISCE_IO_RASTER_ICC
278#include "Raster.icc"
279#undef ISCE_IO_RASTER_ICC
Data structure for a 2D row-major matrix.
Definition Matrix.h:23
size_t width() const
Width getter.
Definition Raster.h:119
GDALAccess access() const
Access mode getter.
Definition Raster.h:125
GDALDataType dtype(const size_t band=1) const
Return GDALDatatype of specified band.
Definition Raster.h:141
size_t numBands() const
Number of bands getter.
Definition Raster.h:122
size_t length() const
Length getter.
Definition Raster.h:116
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 dataset(GDALDataset *ds)
GDALDataset pointer setter.
Definition Raster.h:133
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
int getEPSG() const
Return EPSG code corresponding to raster.
Definition Raster.cpp:142
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
~Raster()
Destructor.
Definition Raster.cpp:235
bool dataset_owner() const
GDALDataset owner getter.
Definition Raster.h:136
int setEPSG(int code)
Set EPSG code.
Definition Raster.cpp:203
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
Wrapper for GDALRasterBand representing a single raster.
Definition Raster.h:14
const GDALDataType defaultGDALDataType
Default GDAL data type used by Raster for creation.
Definition Constants.h:20
const std::string defaultGDALDriver
Default GDAL driver used by Raster for creation.
Definition Constants.h:18

Generated for ISCE3.0 by doxygen 1.13.2.