isce3  0.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
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 
27 //#include <pyre/journal.h>
28 
35 
36  public:
37 
38  Raster() {_dataset = nullptr;};
39 
41  Raster(const std::string& fname, GDALAccess access = GA_ReadOnly);
42 
44  Raster(const std::string& fname, size_t width, size_t length,
45  size_t numBands, GDALDataType dtype = isce3::io::defaultGDALDataType,
46  const std::string& driverName = isce3::io::defaultGDALDriver);
47 
49  Raster(const std::string& fname, size_t width, size_t length, GDALDataType dtype = isce3::io::defaultGDALDataType);
50 
52  template<typename T> Raster(isce3::core::Matrix<T> &matrix);
53 
54  // Constructor for a 1 band dataset from isce3::core::Matrix<T>::view_type
55  template<typename T> Raster(pyre::grid::View<T> &view);
56 
58  Raster(const std::string& fname, const Raster& rast);
59  template<typename T> Raster(const std::string& fname, const std::vector<T>& buffer, size_t length) :
60  Raster(fname, buffer.size(), length, 1, asGDT<T>) {}
61  template<typename T> Raster(const std::string& fname, const std::valarray<T>& buffer, size_t length) :
62  Raster(fname, buffer.size(), length, 1, asGDT<T>) {}
63 
65  Raster(const std::string& fname, const std::vector<Raster>& rastVec);
66 
69 
71  Raster(const Raster&);
72 
74  Raster(GDALDataset *inputDataset, bool owner=true);
75 
77  inline void initFromPointer(void* ptr, GDALDataType dtype, size_t width, size_t length, size_t pixeloffset, size_t lineoffset);
78 
80  ~Raster();
81 
83  inline Raster& operator=(const Raster&);
84 
86  inline size_t length() const { return _dataset->GetRasterYSize(); }
87 
89  inline size_t width() const { return _dataset->GetRasterXSize(); }
90 
92  inline size_t numBands() const { return _dataset->GetRasterCount(); }
93 
95  inline GDALAccess access() const { return _dataset->GetAccess(); }
96 
98  inline GDALDataset* dataset() const { return _dataset; }
99 
103  inline void dataset(GDALDataset* ds) { _dataset=ds; }
104 
108  inline GDALDataType dtype(const size_t band=1) const { return _dataset->GetRasterBand(band)->GetRasterDataType(); }
109 
113  inline bool match(const Raster & rast) const { return width()==rast.width() && length()==rast.length(); }
114 
116  inline void open(const std::string &fname, GDALAccess access);
117 
119  inline void addRasterToVRT(const isce3::io::Raster& rast);
120 
122  inline void addBandToVRT(GDALRasterBand *inBand);
123 
125  inline void addRawBandToVRT(const std::string &fname, GDALDataType dtype);
126  //void close() { GDALClose( _dataset ); } // todo: fix segfault conflict with destructor
127 
128  //Pixel read/write with buffer passed by reference, optional band index
130  template<typename T> void getSetValue(T& buffer, size_t xidz, size_t yidx, size_t band, GDALRWFlag);
131  template<typename T> void getValue(T& buffer, size_t xidx, size_t yidx, size_t band = 1);
132  template<typename T> void setValue(T& buffer, size_t xidx, size_t yidx, size_t band = 1);
133 
134  // Line read/write with raw pointer and width or STL container, optional band index
136  template<typename T> void getSetLine(T* buffer, size_t yidx, size_t iowidth, size_t band, GDALRWFlag iodir);
138  template<typename T> void getLine(T* buffer, size_t yidx, size_t iowidth, size_t band = 1);
139  template<typename T> void getLine(std::vector<T>& vec, size_t yidx, size_t band = 1);
140  template<typename T> void getLine(std::valarray<T>& arr, size_t yidx, size_t band = 1);
142  template<typename T> void setLine(T* buffer, size_t yidx, size_t iowidth, size_t band = 1);
143  template<typename T> void setLine(std::vector<T>& vec, size_t yidx, size_t band = 1);
144  template<typename T> void setLine(std::valarray<T>& arr, size_t yidx, size_t band = 1);
145 
146  // 2D block read/write, generic container w/ width or STL container, optional band index
148  template<typename T> void getSetBlock(T* buffer, size_t xidx, size_t yidx, size_t iowidth, size_t iolength, size_t band, GDALRWFlag iodir);
150  template<typename T> void getBlock(T* buffer, size_t xidx, size_t yidx, size_t iowidth, size_t iolength, size_t band = 1);
151  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);
152  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);
154  template<typename T> void setBlock(T* buffer, size_t xidx, size_t yidx, size_t iowidth, size_t iolength, size_t band = 1);
155  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);
156  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);
157 
158  //2D block read/write for Matrix<T>, optional band index
160  template<typename T> void getBlock(isce3::core::Matrix<T>& mat, size_t xidx, size_t yidx, size_t band = 1);
161  template<typename T> void setBlock(isce3::core::Matrix<T>& mat, size_t xidx, size_t yidx, size_t band = 1);
162  //2D block read/write for Matrix<T>, optional band index
164  template<typename T> void getSetBlock(pyre::grid::View<T>& view, size_t xidx, size_t yidx, size_t band, GDALRWFlag iodir);
165  template<typename T> void getBlock(pyre::grid::View<T>& view, size_t xidx, size_t yidx, size_t band = 1);
166  template<typename T> void setBlock(pyre::grid::View<T>& view, size_t xidx, size_t yidx, size_t band = 1);
167 
168  //Functions to deal with projections and geotransform information
170  int getEPSG();
172  int setEPSG(int code);
174  inline void setGeoTransform(double *arr);
175  inline void setGeoTransform(std::vector<double>&);
176  inline void setGeoTransform(std::valarray<double>&);
178  inline void getGeoTransform(double *) const;
179  inline void getGeoTransform(std::vector<double>&) const;
180  inline void getGeoTransform(std::valarray<double>&) const;
181  //Read only functions for specific elements of GeoTransform
183  inline double x0() const;
185  inline double y0() const;
187  inline double dx() const;
189  inline double dy() const;
190 
191 private:
192  GDALDataset * _dataset;
193  bool _owner = true;
194 };
195 
196 #define ISCE_IO_RASTER_ICC
197 #include "Raster.icc"
198 #undef ISCE_IO_RASTER_ICC
void addBandToVRT(GDALRasterBand *inBand)
Add a GDALRasterBand to VRT.
Definition: Raster.icc:173
void dataset(GDALDataset *ds)
GDALDataset pointer setter.
Definition: Raster.h:103
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:424
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:288
void setGeoTransform(double *arr)
Set Raster GeoTransform from buffer, vector, or valarray.
Definition: Raster.icc:554
int getEPSG()
Return EPSG code corresponding to raster.
Definition: Raster.cpp:142
const std::string defaultGDALDriver
Default GDAL driver used by Raster for creation.
Definition: Constants.h:18
bool match(const Raster &rast) const
Check dimensions compatibility with another raster.
Definition: Raster.h:113
double dx() const
Return EW pixel spacing of Raster.
Definition: Raster.icc:631
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:267
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:313
GDALAccess access() const
Access mode getter.
Definition: Raster.h:95
size_t length() const
Length getter.
Definition: Raster.h:86
const GDALDataType defaultGDALDataType
Default GDAL data type used by Raster for creation.
Definition: Constants.h:20
void addRawBandToVRT(const std::string &fname, GDALDataType dtype)
Add a raw data band to VRT.
Definition: Raster.icc:189
void open(const std::string &fname, GDALAccess access)
Open file with GDAL.
Definition: Raster.icc:153
double y0() const
Return Northern Limit of Raster.
Definition: Raster.icc:623
size_t width() const
Width getter.
Definition: Raster.h:89
double dy() const
Return NS pixel spacing of Raster.
Definition: Raster.icc:639
void getSetValue(T &buffer, size_t xidz, size_t yidx, size_t band, GDALRWFlag)
Get/Set single value for given band.
Definition: Raster.icc:218
void getValue(T &buffer, size_t xidx, size_t yidx, size_t band=1)
Definition: Raster.icc:238
int setEPSG(int code)
Set EPSG code.
Definition: Raster.cpp:203
void setValue(T &buffer, size_t xidx, size_t yidx, size_t band=1)
Definition: Raster.icc:249
~Raster()
Destructor.
Definition: Raster.cpp:235
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:347
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
void getGeoTransform(double *) const
Copy Raster GeoTransform into a buffer, vector, or valarray.
Definition: Raster.icc:586
Wrapper for GDALRasterBand representing a single raster.
Definition: Raster.h:14
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:373
Raster & operator=(const Raster &)
Assignment operator.
Definition: Raster.icc:133
GDALDataset * dataset() const
GDALDataset pointer getter.
Definition: Raster.h:98
size_t numBands() const
Number of bands getter.
Definition: Raster.h:92
void addRasterToVRT(const isce3::io::Raster &rast)
Add a raster to VRT.
Definition: Raster.icc:162
GDALDataType dtype(const size_t band=1) const
Return GDALDatatype of specified band.
Definition: Raster.h:108
Data structure meant to handle Raster I/O operations.
Definition: Raster.h:34
double x0() const
Return Western Limit of Raster.
Definition: Raster.icc:615

Generated for ISCE3.0 by doxygen 1.8.5.