isce3 0.25.0
Loading...
Searching...
No Matches
Raster.icc
1#ifndef ISCE_IO_GDAL_RASTER_ICC
2#error "Raster.icc is an implementation detail of Raster.h"
3#endif
4
5#include <isce3/except/Error.h>
6
7#include "Buffer.h"
8#include "detail/GDALDataTypeUtil.h"
9
10namespace isce3 { namespace io { namespace gdal {
11
12namespace detail {
13
14inline
15void checkSingleBand(const Dataset & dataset)
16{
17 if (dataset.bands() == 0) {
18 throw isce3::except::RuntimeError(ISCE_SRCINFO(), "dataset contains no raster bands");
19 }
20 if (dataset.bands() > 1) {
21 std::string errmsg = "dataset contains multiple raster bands - "
22 "please specify raster band index";
23 throw isce3::except::RuntimeError(ISCE_SRCINFO(), errmsg);
24 }
25}
26
27inline
28void checkValidBand(const Dataset & dataset, int band)
29{
30 if (band < 1 || band > dataset.bands()) {
31 std::string errmsg = "raster band index (" + std::to_string(band) + ") is out of range";
32 throw isce3::except::OutOfRange(ISCE_SRCINFO(), errmsg);
33 }
34}
35
36}
37
38inline
39Raster::Raster(const std::string & path, GDALAccess access)
40:
41 _dataset(path, access)
42{
43 detail::checkSingleBand(_dataset);
44}
45
46inline
48:
49 _dataset(dataset, access)
50{
51 detail::checkSingleBand(_dataset);
52}
53
54inline
55Raster::Raster(const std::string & path, int band, GDALAccess access)
56:
57 _dataset(path, access),
58 _band(band)
59{
60 detail::checkValidBand(_dataset, band);
61}
62
63inline
64Raster::Raster(const IDataSet & dataset, int band, GDALAccess access)
65:
66 _dataset(dataset, access),
67 _band(band)
68{
69 detail::checkValidBand(_dataset, band);
70}
71
72inline
73Raster::Raster(const std::string & path,
74 int width,
75 int length,
76 GDALDataType datatype,
77 const std::string & driver)
78:
79 _dataset(path, width, length, 1, datatype, driver)
80{}
81
82template<typename T>
83inline
84Raster::Raster(const T * data, int width, int length)
85:
86 _dataset(data, width, length, 1)
87{}
88
89template<typename T>
90inline
91Raster::Raster(T * data, int width, int length, GDALAccess access)
92:
93 _dataset(data, width, length, 1, access)
94{}
95
96template<typename T>
97inline
98Raster::Raster(const T * data,
99 int width,
100 int length,
101 std::size_t colstride,
102 std::size_t rowstride)
103:
104 _dataset(data, width, length, 1, colstride, rowstride, 0)
105{}
106
107template<typename T>
108inline
110 int width,
111 int length,
112 std::size_t colstride,
113 std::size_t rowstride,
114 GDALAccess access)
115:
116 _dataset(data, width, length, 1, colstride, rowstride, 0, access)
117{}
118
119inline
120GDALDataType Raster::datatype() const
121{
122 return _dataset._dataset->GetRasterBand(_band)->GetRasterDataType();
123}
124
125template<typename T>
126inline
127void Raster::readPixel(T * dst, int col, int row) const
128{
129 return readBlock(dst, col, row, 1, 1);
130}
131
132template<typename T>
133inline
134void Raster::writePixel(const T * src, int col, int row)
135{
136 return writeBlock(src, col, row, 1, 1);
137}
138
139template<typename T>
140inline
141void Raster::readLine(T * dst, int row) const
142{
143 return readLines(dst, row, 1);
144}
145
146template<typename T>
147inline
148void Raster::writeLine(const T * src, int row)
149{
150 return writeLines(src, row, 1);
151}
152
153template<typename T>
154inline
155void Raster::readLines(T * dst, int first_row, int num_rows) const
156{
157 return readBlock(dst, 0, first_row, width(), num_rows);
158}
159
160template<typename T>
161inline
162void Raster::writeLines(const T * src, int first_row, int num_rows)
163{
164 return writeBlock(src, 0, first_row, width(), num_rows);
165}
166
167template<typename T>
168inline
169void Raster::readBlock(T * dst, int first_col, int first_row, int num_cols, int num_rows) const
170{
171 if (!dst) {
172 throw isce3::except::InvalidArgument(ISCE_SRCINFO(), "destination address may not be null");
173 }
174
175 CPLErr status = readwriteBlock(dst, first_col, first_row, num_cols, num_rows, GF_Read);
176 if (status != CE_None) {
177 throw isce3::except::GDALError(ISCE_SRCINFO(), "error while reading from raster");
178 }
179}
180
181template<typename T>
182inline
183void Raster::writeBlock(const T * src, int first_col, int first_row, int num_cols, int num_rows)
184{
185 if (access() == GA_ReadOnly) {
186 throw isce3::except::RuntimeError(ISCE_SRCINFO(), "attempted to write to read-only raster");
187 }
188
189 if (!src) {
190 throw isce3::except::InvalidArgument(ISCE_SRCINFO(), "source address may not be null");
191 }
192
193 CPLErr status = readwriteBlock(const_cast<T *>(src), first_col, first_row, num_cols, num_rows, GF_Write);
194 if (status != CE_None) {
195 throw isce3::except::GDALError(ISCE_SRCINFO(), "error while writing to raster");
196 }
197}
198
199template<typename T>
200inline
201void Raster::readAll(T * dst) const
202{
203 return readBlock(dst, 0, 0, width(), length());
204}
205
206template<typename T>
207inline
208void Raster::writeAll(const T * src)
209{
210 return writeBlock(src, 0, 0, width(), length());
211}
212
213inline
215{
216 // create the virtual memory mapping if not already mapped
217 if (!_mmap) {
218 _mmap = detail::MemoryMap(get(), access());
219 }
220
221 std::array<int, 2> shape = { length(), width() };
222 std::array<std::size_t, 2> strides = { _mmap.rowstride(), _mmap.colstride() };
223
224 return Buffer(_mmap.data(), datatype(), shape, strides, access());
225}
226
227template<typename T>
228inline
230{
231 Buffer buffer = memmap();
232 return buffer.cast<T>();
233}
234
235inline
236Raster::Raster(const Dataset & dataset, int band)
237:
238 _dataset(dataset),
239 _band(band)
240{}
241
242template<typename T>
243inline
244GDALDataType Raster::getIODataType() const
245{
246 return detail::Type2GDALDataType<T>::datatype;
247}
248
249template<>
250inline
251GDALDataType Raster::getIODataType<void>() const
252{
253 return datatype();
254}
255
256template<typename T>
257inline
258CPLErr Raster::readwriteBlock(T * buf,
259 int first_col,
260 int first_row,
261 int num_cols,
262 int num_rows,
263 GDALRWFlag rwflag) const
264{
265 GDALDataType gdt = getIODataType<T>();
266 if (gdt == GDT_Unknown) {
267 throw isce3::except::RuntimeError(ISCE_SRCINFO(), "type is not mappable to GDALDataType");
268 }
269
270 if (num_cols <= 0) {
271 throw isce3::except::InvalidArgument(ISCE_SRCINFO(), "block width must be > 0");
272 }
273
274 if (num_rows <= 0) {
275 throw isce3::except::InvalidArgument(ISCE_SRCINFO(), "block length must be > 0");
276 }
277
278 if (first_col < 0 || first_row < 0 || first_col + num_cols > width() || first_row + num_rows > length()) {
279 throw isce3::except::OutOfRange(ISCE_SRCINFO(), "out of bounds raster access");
280 }
281
282 return _dataset._dataset->GetRasterBand(_band)->RasterIO(
283 rwflag, first_col, first_row, num_cols, num_rows, buf, num_cols, num_rows, gdt, 0, 0);
284}
285
286}}}
Our derived dataset structure that includes utility functions.
Definition IH5.h:41
Interface to 2-D memory array.
Definition Buffer.h:13
constexpr TypedBuffer< T > cast() const
Cast to typed buffer.
Definition Buffer.icc:63
Wrapper for GDALDataset representing a collection of associated Raster bands.
Definition Dataset.h:17
GDALDataType datatype() const
Datatype identifier.
Definition Raster.icc:120
const Dataset & dataset() const
Get the dataset containing the raster.
Definition Raster.h:134
void readPixel(T *dst, int col, int row) const
Read a single pixel value from the raster.
Definition Raster.icc:127
Raster(const std::string &path, GDALAccess access=GA_ReadOnly)
Open an existing file containing a single raster band as a GDAL raster.
Definition Raster.icc:39
void writeBlock(const T *src, int first_col, int first_row, int num_cols, int num_rows)
Write a block of pixel data to the raster.
Definition Raster.icc:183
int length() const
Number of rows.
Definition Raster.h:152
std::string driver() const
Driver name.
Definition Raster.h:155
int width() const
Number of columns.
Definition Raster.h:149
int band() const
Band index (1-based)
Definition Raster.h:140
void readBlock(T *dst, int first_col, int first_row, int num_cols, int num_rows) const
Read a block of pixel data from the raster.
Definition Raster.icc:169
void writePixel(const T *src, int col, int row)
Write a single pixel value to the raster.
Definition Raster.icc:134
Buffer memmap()
Create a virtual memory mapping of the raster.
Definition Raster.icc:214
void writeAll(const T *src)
Write all pixel data to the raster.
Definition Raster.icc:208
void writeLine(const T *src, int row)
Write a line of pixel data to the raster.
Definition Raster.icc:148
void writeLines(const T *src, int first_row, int num_rows)
Write one or more lines of pixel data to the raster.
Definition Raster.icc:162
void readLine(T *dst, int row) const
Read a line of pixel data from the raster.
Definition Raster.icc:141
GDALRasterBand * get()
Get the underlying GDALRasterBand pointer.
Definition Raster.h:327
void readAll(T *dst) const
Read all pixel data from the raster.
Definition Raster.icc:201
void readLines(T *dst, int first_row, int num_rows) const
Read one or more lines of pixel data from the raster.
Definition Raster.icc:155
GDALAccess access() const
Access mode.
Definition Raster.h:146
Buffer with static type information.
Definition Buffer.h:136
The isce3::io namespace.
Definition Constants.h:14
base interpolator is an abstract base class
Definition BinarySearchFunc.cpp:5

Generated for ISCE3.0 by doxygen 1.13.2.