Ilwis-Objects  1.0
GIS and Remote Sensing framework for data access and processing
 All Classes Functions Enumerations Pages
grid.h
1 #ifndef Grid_H
2 #define Grid_H
3 
4 #include "Kernel_global.h"
5 #include <list>
6 #include <mutex>
7 
8 
9 namespace Ilwis {
10 
12 public:
13  GridBlockInternal(quint32 lines , quint32 width);
15 
16 
17  Size<> size() const ;
18  GridBlockInternal *clone();
19 
20  double& at(quint32 index) {
21  prepare();
22  if ( index < _blockSize){
23  return _data[index];
24  }
25  //throw ErrorObject(TR("Grid index out of bounds"));
26  if (_undef != rUNDEF)
27  _undef = rUNDEF;
28  return _undef;
29  }
30 
31  char *blockAsMemory();
32 
33  void fill(const std::vector<double>& values);
34 
35  quint32 blockSize();
36  bool isLoaded() const { return _inMemory; }
37  inline bool unload() ;
38  bool load();
39 
40 private:
41  void prepare() {
42  if (!_initialized) {
43  Locker lock(_mutex);
44  if ( _initialized) // may happen due to multithreading
45  return;
46  try{
47  _data.resize(blockSize());
48  std::fill(_data.begin(), _data.end(), _undef);
49  _initialized = true;
50  } catch(const std::bad_alloc& ){
51  throw ErrorObject( TR("Couldnt allocate memory for raster")) ;
52  }
53 
54 
55  }
56  }
57  std::mutex _mutex;
58  std::vector<double> _data;
59  double _undef;
60  quint32 _index;
61  Size<> _size;
62  quint64 _id;
63  bool _initialized;
64  bool _inMemory;
65  static quint64 _blockid;
66  QString _tempName = sUNDEF;
67  QScopedPointer<QTemporaryFile> _swapFile;
68  quint64 _blockSize;
69 };
70 
71 class KERNELSHARED_EXPORT Grid
72 
73 {
74 public:
75  friend class GridInterpolator;
76 
77  Grid(const Size<>& sz, int maxLines=500);
78  virtual ~Grid();
79 
80  void clear();
81 
82  //double value(const Point3D<double> &pix, int method=0);
83  double& value(quint32 block, int offset );
84  double value(const Pixel& pix) ;
85  void setValue(quint32 block, int offset, double v );
86 
87  quint32 blocks() const;
88  quint32 blocksPerBand() const;
89 
90  void setBlockData(quint32 block, const std::vector<double>& data, bool creation);
91  char *blockAsMemory(quint32 block, bool creation);
92  void setSize(const Size<>& sz);
93  bool prepare() ;
94  quint32 blockSize(quint32 index) const;
95  Size<> size() const;
96  int maxLines() const;
97  Grid * clone(quint32 index1=iUNDEF, quint32 index2=iUNDEF) ;
98  void unload();
99 private:
100  double bilinear(const Pixeld &pix) const;
101  double bicubic(const Pixeld &pix) const;
102  int numberOfBlocks();
103  inline bool update(quint32 block, bool creation=false);
104 
105  std::mutex _mutex;
106  std::vector< GridBlockInternal *> _blocks;
107  QList<quint32> _cache;
108  quint32 _inMemoryIndex;
109  qint64 _memUsed;
110  //quint64 _bandSize;
111  quint32 _blocksPerBand;
112  std::vector<quint32> _blockSizes;
113  Size<> _size;
114  quint32 _maxLines;
115  std::vector<std::vector<quint32>> _offsets;
116  std::vector<quint32> _blockOffsets;
117  bool _allInMemory = false;
118 };
119 }
120 
121 
122 
123 #endif // Grid_H