57 void clear() {
if (mData)
delete[] mData; mData=0; }
73 int sizeX()
const {
return mSizeX; }
74 int sizeY()
const {
return mSizeY; }
86 int count()
const {
return mCount; }
87 bool isEmpty()
const {
return mData==NULL; }
97 inline T&
operator[](
const int idx)
const {
return mData[idx]; }
104 T&
valueAtIndex(
const int ix,
const int iy) {
return mData[iy*mSizeX + ix]; }
106 inline int index(
const int ix,
const int iy) {
return iy*mSizeX + ix; }
107 inline int index(
const QPoint &pos) {
return pos.y()*mSizeX + pos.x(); }
111 inline const T&
constValueAtIndex(
const int ix,
const int iy)
const {
return mData[iy*mSizeX + ix]; }
122 bool coordValid(
const float x,
const float y)
const {
return x>=mRect.left() && x<mRect.right() && y>=mRect.top() && y<mRect.bottom(); }
125 QPoint
indexAt(
const QPointF& pos)
const {
return QPoint(
int((pos.x()-mRect.left()) / mCellsize),
int((pos.y()-mRect.top())/mCellsize)); }
128 bool isIndexValid(
const QPoint& pos)
const {
return (pos.x()>=0 && pos.x()<mSizeX && pos.y()>=0 && pos.y()<mSizeY); }
129 bool isIndexValid(
const int x,
const int y)
const {
return (x>=0 && x<mSizeX && y>=0 && y<mSizeY); }
132 int index2(
int idx)
const {
return ((idx/mSizeX)/2)*(mSizeX/2) + (idx%mSizeX)/2; }
134 int index5(
int idx)
const {
return ((idx/mSizeX)/5)*(mSizeX/5) + (idx%mSizeX)/5; }
136 int index10(
int idx)
const {
return ((idx/mSizeX)/10)*(mSizeX/10) + (idx%mSizeX)/10; }
139 void validate(QPoint &pos)
const{ pos.setX( qMax(qMin(pos.x(), mSizeX-1), 0) ); pos.setY( qMax(qMin(pos.y(), mSizeY-1), 0) );}
141 QPointF
cellCenterPoint(
const QPoint &pos)
const {
return QPointF( (pos.x()+0.5)*mCellsize+mRect.left(), (pos.y()+0.5)*mCellsize + mRect.top());}
143 QPointF
cellCenterPoint(
const int &
index)
const { QPoint pos=
indexOf(
index);
return QPointF( (pos.x()+0.5)*mCellsize+mRect.left(), (pos.y()+0.5)*mCellsize + mRect.top());}
147 QRectF
cellRect(
const QPoint &pos)
const { QRectF r( QPointF(mRect.left() + mCellsize*pos.x(), mRect.top() + pos.y()*mCellsize),
148 QSizeF(mCellsize, mCellsize));
return r; }
150 inline T*
begin()
const {
return mData; }
151 inline T*
end()
const {
return mEnd; }
152 inline QPoint
indexOf(
const T* element)
const;
159 void add(
const T& summand);
162 void limit(
const T min_value,
const T max_value);
172 T*
ptr(
int x,
int y) {
return &(mData[y*mSizeX + x]); }
173 inline double distance(
const QPoint &p1,
const QPoint &p2);
178 int floodFill(QPoint start, T old_color, T color,
int max_fill=-1);
204 GridRunner(
const Grid<T> &target_grid,
const QRectF &rectangle) {setup(&target_grid, rectangle);}
208 GridRunner(
const Grid<T> &target_grid,
const QRect &rectangle) {setup(&target_grid, rectangle);}
217 T*
last()
const {
return mLast; }
219 bool isValid()
const {
return mCurrent>=mFirst && mCurrent<=mLast; }
223 QPointF
currentCoord()
const {
return mGrid->cellCenterPoint(mGrid->indexOf(mCurrent));}
224 void reset() { mCurrent = mFirst-1; mCurrentCol = -1; }
226 void setPosition(QPoint new_index) {
if (mGrid->isIndexValid(new_index)) mCurrent =
const_cast<Grid<T> *
>(mGrid)->ptr(new_index.x(), new_index.y());
else mCurrent=0; }
234 void setup(
const Grid<T> *target_grid,
const QRectF &rectangle);
235 void setup(
const Grid<T> *target_grid,
const QRect &rectangle);
253 double x()
const {
return mX; }
254 double y()
const {
return mY; }
255 double z()
const {
return mZ; }
271 mRect = toCopy.mRect;
274 const T* end = toCopy.
end();
276 for (T* i= toCopy.
begin(); i!=end; ++i, ++ptr)
288 multiplier = targetvalue / total;
291 for (T* p=target.
begin();p!=target.
end();++p)
301 target.
setup(metricRect(), cellsize()*factor);
306 for (x=offsetx;x<mSizeX;x++)
307 for (y=offsety;y<mSizeY;y++) {
308 target.
valueAtIndex((x-offsetx)/factor, (y-offsety)/factor) += constValueAtIndex(x,y);
311 double fsquare = factor*factor;
312 for (T* p=target.
begin();p!=target.
end();++p)
321 return valueAtIndex( indexAt(QPointF(x,y)) );
327 return constValueAtIndex( indexAt(QPointF(x,y)) );
333 return valueAtIndex( indexAt(posf) );
339 return constValueAtIndex( indexAt(posf) );
345 mData = 0; mCellsize=0.f;
347 mSizeX=0; mSizeY=0; mCount=0;
353 mSizeX=sizex; mSizeY=sizey;
355 mRect.setCoords(0., 0., cellsize*sizex, cellsize*sizey);
359 if (mSizeX*mSizeY > mCount || mCellsize != cellsize) {
361 delete[] mData; mData=NULL;
365 mCount = mSizeX*mSizeY;
369 mData =
new T[mCount];
370 mEnd = &(mData[mCount]);
378 int dx = int(rect.width()/cellsize);
379 if (mRect.left()+cellsize*dx<rect.right())
381 int dy = int(rect.height()/cellsize);
382 if (mRect.top()+cellsize*dy<rect.bottom())
384 return setup(cellsize, dx, dy);
393template <
class T>
inline
397 if (element==NULL || element<mData || element>=end())
398 return QPoint(-1, -1);
399 int idx = element - mData;
400 return QPoint(idx % mSizeX, idx / mSizeX);
409 T maxv = -std::numeric_limits<T>::max();
412 for (p=begin(); p!=pend;++p)
413 maxv = std::max(maxv, *p);
420 T maxv = std::numeric_limits<T>::max();
423 for (p=begin(); p!=pend;++p)
424 maxv = std::min(maxv, *p);
433 for (T *p=begin(); p!=pend;++p)
442 return sum() / T(count());
450 for (T *p=begin(); p!=pend;*p+=summand,++p)
458 for (T *p=begin(); p!=pend;*p*=factor,++p)
466 for (T *p=begin(); p!=pend;++p)
467 *p = *p < min_value ? min_value : (*p>max_value? max_value : *p);
475 memset(mData, 0, mCount*
sizeof(T));
481 if (
sizeof(T)==
sizeof(
int)) {
485 memset(mData, *((
int*)pf), mCount*
sizeof(T));
494 g->
setup(metricRect(), cellsize());
497 double *dp = g->
begin();
498 for (T *p=begin(); p!=end(); ++p, ++dp)
506 QPointF fp1=cellCenterPoint(p1);
507 QPointF fp2=cellCenterPoint(p2);
508 double distance = sqrt( (fp1.x()-fp2.x())*(fp1.x()-fp2.x()) + (fp1.y()-fp2.y())*(fp1.y()-fp2.y()));
526 QQueue<QPoint> pqueue;
527 pqueue.enqueue(start);
529 while (!pqueue.isEmpty()) {
530 QPoint p = pqueue.dequeue();
531 if (!isIndexValid(p))
533 if (valueAtIndex(p)==old_color) {
534 valueAtIndex(p) = color;
535 pqueue.enqueue(p+QPoint(-1,0));
536 pqueue.enqueue(p+QPoint(1,0));
537 pqueue.enqueue(p+QPoint(0,-1));
538 pqueue.enqueue(p+QPoint(0,1));
539 pqueue.enqueue(p+QPoint(1,1));
540 pqueue.enqueue(p+QPoint(1,-1));
541 pqueue.enqueue(p+QPoint(-1,1));
542 pqueue.enqueue(p+QPoint(-1,-1));
544 if (max_fill > 0 && found>=max_fill)
558 QPoint upper_left = rectangle.topLeft();
560 QPoint lower_right = rectangle.bottomRight();
561 mCurrent =
const_cast<Grid<T> *
>(target_grid)->ptr(upper_left.x(), upper_left.y());
564 mLast =
const_cast<Grid<T> *
>(target_grid)->ptr(lower_right.x()-1, lower_right.y()-1);
565 mCols = lower_right.x() - upper_left.x();
566 mLineLength = target_grid->
sizeX() - mCols;
577 QRect rect(target_grid->
indexAt(rectangle_metric.topLeft()),
578 target_grid->
indexAt(rectangle_metric.bottomRight()) );
579 setup (target_grid, rect);
590 if (mCurrentCol >=
int(mCols)) {
591 mCurrent += mLineLength;
607 rArray[0] = mCurrent + mCols + mLineLength > mLast?0: mCurrent + mCols + mLineLength;
609 rArray[3] = mCurrent - (mCols + mLineLength) < mFirst?0: mCurrent - (mCols + mLineLength);
611 rArray[1] = mCurrentCol+1<int(mCols)? mCurrent + 1 : 0;
612 rArray[2] = mCurrentCol>0? mCurrent-1 : 0;
623 rArray[4] = rArray[0] && rArray[1]? rArray[0]+1: 0;
625 rArray[5] = rArray[0] && rArray[2]? rArray[0]-1: 0;
627 rArray[6] = rArray[3] && rArray[1]? rArray[3]+1: 0;
629 rArray[7] = rArray[3] && rArray[2]? rArray[3]-1: 0;
647 bool black_white=
false,
648 double min_value=0.,
double max_value=1.,
664 QTextStream ts(&res);
666 int newl_counter = newline_after;
667 for (
int y=grid.
sizeY()-1;y>=0;--y){
668 for (
int x=0;x<grid.
sizeX();x++){
670 if (--newl_counter==0) {
672 newl_counter = newline_after;
686 QString
gridToString(
const Grid<T> &grid, QString (*valueFunction)(
const T& value),
const QChar sep=QChar(
';'),
const int newline_after=-1 )
689 QTextStream ts(&res);
691 int newl_counter = newline_after;
692 for (
int y=grid.
sizeY()-1;y>=0;--y){
693 for (
int x=0;x<grid.
sizeX();x++){
696 if (--newl_counter==0) {
698 newl_counter = newline_after;
714 QString result = QString(
"ncols %1\r\nnrows %2\r\nxllcorner %3\r\nyllcorner %4\r\ncellsize %5\r\nNODATA_value %6\r\n")
717 .arg(world.
x(),0,
'f').arg(world.
y(),0,
'f')
719 QString line =
gridToString(grid, valueFunction, QChar(
' '));
720 return result + line;
729 QString result = QString(
"ncols %1\r\nnrows %2\r\nxllcorner %3\r\nyllcorner %4\r\ncellsize %5\r\nNODATA_value %6\r\n")
732 .arg(world.
x(),0,
'f').arg(world.
y(),0,
'f')
735 return result + line;
742 double min_value = 1000000000;
743 double max_value = -1000000000;
746 QFile file(fileName);
748 if (!file.open(QIODevice::ReadOnly)) {
749 qDebug() <<
"Grid::loadGridFromFile: " << fileName <<
"does not exist!";
752 QTextStream s(&file);
754 QByteArray file_content=s.readAll().toLatin1();
756 if (file_content.isEmpty()) {
757 qDebug() <<
"GISGrid: file" << fileName <<
"not present or empty.";
760 QList<QByteArray> lines = file_content.split(
'\n');
771 double no_data_val=0.;
773 if (pos>lines.count())
774 throw IException(
"Grid load from ASCII file: unexpected end of file. File: " + fileName);
775 line=lines[pos].simplified();
776 if (line.length()==0 || line.at(0)==
'#') {
780 key=line.left(line.indexOf(
' ')).toLower();
781 if (key.length()>0 && (key.at(0).isNumber() || key.at(0)==
'-')) {
784 value = line.mid(line.indexOf(
' ')).toDouble();
787 else if (key==
"nrows")
789 else if (key==
"xllcorner")
791 else if (key==
"yllcorner")
793 else if (key==
"cellsize")
795 else if (key==
"nodata_value")
798 throw IException( QString(
"GISGrid: invalid key %1.").arg(key));
804 QRectF rect(ox, oy, ncol*cellsize, nrow*cellsize);
805 setup( rect, cellsize );
813 for (i=nrow-1;i>=0;i--)
814 for (j=0;j<ncol;j++) {
818 if (pos>=lines.count())
819 throw std::logic_error(
"GISGrid: Unexpected End of File!");
830 while (*p && strchr(
" \r\n\t", *p))
834 if (value!=no_data_val) {
835 min_value=std::min(min_value, value);
836 max_value=std::max(max_value, value);
838 valueAtIndex(j,i) = value;
840 while (*p && !strchr(
" \r\n\t", *p))
Grid class (template).
Definition: grid.h:44
Grid< double > * toDouble() const
create a double grid (same size as this grid) and convert this grid to double values.
Definition: grid.h:491
QRectF metricRect() const
get the metric rectangle of the grid
Definition: grid.h:79
void setMetricRect(QRectF rect)
set the metric rectangle. Use with care! No further checks are executed!
Definition: grid.h:81
~Grid()
Definition: grid.h:56
T & valueAtIndex(const int index)
get a ref ot value at (one-dimensional) index 'index'.
Definition: grid.h:105
int count() const
returns the number of elements of the grid
Definition: grid.h:86
T min() const
retrieve the minimum value of a grid
Definition: grid.h:418
bool loadGridFromFile(const QString &fileName)
load a grid from an ASCII grid file the coordinates and cell size remain as in the grid file.
Definition: grid.h:740
QPointF cellCenterPoint(const int &index) const
get the metric cell center point of the cell given by index 'index'
Definition: grid.h:143
const T & operator[](const QPoint &p) const
access value of grid with a QPoint
Definition: grid.h:95
T * ptr(int x, int y)
get a pointer to the element indexed by "x" and "y"
Definition: grid.h:172
bool setup(const float cellsize, const int sizex, const int sizey)
Definition: grid.h:351
void validate(QPoint &pos) const
force
Definition: grid.h:139
T * begin() const
get "iterator" pointer
Definition: grid.h:150
QPointF cellCenterPoint(const QPoint &pos) const
get the (metric) centerpoint of cell with index pos
Definition: grid.h:141
Grid< T > averaged(const int factor, const int offsetx=0, const int offsety=0) const
creates a grid with lower resolution and averaged cell values.
Definition: grid.h:298
float metricSizeY() const
Definition: grid.h:77
int index2(int idx) const
returns the index of an aligned grid (with the same size and matching origin) with the double cell si...
Definition: grid.h:132
void multiply(const T &factor)
Definition: grid.h:455
Grid(const Grid< T > &toCopy)
Definition: grid.h:268
T avg() const
retrieve the average value of a grid
Definition: grid.h:439
QRectF cellRect(const QPoint &pos) const
get the metric rectangle of the cell with index @pos
Definition: grid.h:147
const T & operator()(const int ix, const int iy) const
access (const) with index variables. use int.
Definition: grid.h:91
QPoint indexAt(const QPointF &pos) const
get index of value at position pos (metric)
Definition: grid.h:125
Grid(const QRectF rect_metric, const float cellsize)
create from a metric rect
Definition: grid.h:50
const T & constValueAtIndex(const int index) const
value at position defined by the index within the grid
Definition: grid.h:113
void clear()
Definition: grid.h:57
Grid(float cellsize, int sizex, int sizey)
Definition: grid.h:48
Grid()
Definition: grid.h:343
bool setup(const Grid< T > &source)
Definition: grid.h:61
int index10(int idx) const
returns the index of an aligned grid (the same size) with the 10 times bigger cells (e....
Definition: grid.h:136
int sizeX() const
Definition: grid.h:73
const T & constValueAt(const QPointF &posf) const
value at position defined by metric coordinates (QPointF)
Definition: grid.h:337
const T & operator()(const float x, const float y) const
access (const) using metric variables. use float.
Definition: grid.h:93
int index(const QPoint &pos)
get the 0-based index of the cell at 'pos'.
Definition: grid.h:107
void initialize(const T &value)
Definition: grid.h:62
T & valueAtIndex(const int ix, const int iy)
const value at position defined by indices (x,y)
Definition: grid.h:104
void wipe(const T value)
overwrite the whole area with "value" size of T must be the size of "int" ERRORNOUS!...
Definition: grid.h:478
QPoint indexOf(const T *element) const
retrieve index (x/y) of the pointer element. returns -1/-1 if element is not valid.
Definition: grid.h:394
bool coordValid(const float x, const float y) const
Definition: grid.h:122
const T & constValueAtIndex(const QPoint &pos) const
value at position defined by a (integer) QPoint
Definition: grid.h:109
void wipe()
write 0-bytes with memcpy to the whole area
Definition: grid.h:473
T sum() const
retrieve the sum of the grid
Definition: grid.h:429
bool coordValid(const QPointF &pos) const
Definition: grid.h:123
T & valueAt(const QPointF &posf)
value at position defined by metric coordinates (QPointF)
Definition: grid.h:331
int index(const int ix, const int iy)
get the 0-based index of the cell with indices ix and iy.
Definition: grid.h:106
const QPoint randomPosition() const
returns a (valid) random position within the grid
Definition: grid.h:513
const T & constValueAtIndex(const int ix, const int iy) const
value at position defined by a pair of integer coordinates
Definition: grid.h:111
const T & constValueAt(const float x, const float y) const
value at position defined by metric coordinates (x,y)
Definition: grid.h:325
T & operator[](const int idx) const
use the square brackets to access by index
Definition: grid.h:97
T & valueAtIndex(const QPoint &pos)
value at position defined by a QPoint defining the two indices (x,y)
Definition: grid.h:103
int sizeY() const
Definition: grid.h:74
int index5(int idx) const
returns the index of an aligned grid (the same size) with the 5 times bigger cells (e....
Definition: grid.h:134
bool setup(const QRectF &rect, const double cellsize)
Definition: grid.h:375
bool isIndexValid(const QPoint &pos) const
return true, if position is within the grid
Definition: grid.h:128
T * end() const
get iterator end-pointer
Definition: grid.h:151
float cellsize() const
get the length of one pixel of the grid
Definition: grid.h:85
QPoint indexOf(const int index) const
get index (x/y) of the (linear) index 'index' (0..count-1)
Definition: grid.h:127
int floodFill(QPoint start, T old_color, T color, int max_fill=-1)
applies a flood fill algorithm to the grid starting at 'start' fills the contingent area with value '...
Definition: grid.h:523
QRect rectangle() const
get the rectangle of the grid in terms of indices
Definition: grid.h:83
T max() const
retrieve the maximum value of a grid
Definition: grid.h:407
void limit(const T min_value, const T max_value)
limit each cell value to (including) min_value and (including) max_value
Definition: grid.h:463
Grid< T > normalized(const T targetvalue) const
normalized returns a normalized grid, in a way that the sum() =
Definition: grid.h:282
double distance(const QPoint &p1, const QPoint &p2)
distance (metric) between p1 and p2
Definition: grid.h:504
float metricSizeX() const
Definition: grid.h:76
bool isEmpty() const
returns false if the grid was not setup
Definition: grid.h:87
void copy(const Grid< T > &source)
copies the content of the source grid to this grid.
Definition: grid.h:67
void add(const T &summand)
Definition: grid.h:447
QPointF cellCenterPoint(T *ptr)
get the metric cell center point of the cell given py the pointer
Definition: grid.h:145
bool isIndexValid(const int x, const int y) const
return true, if index is within the grid
Definition: grid.h:129
T & valueAt(const float x, const float y)
value at position defined by metric coordinates (x,y)
Definition: grid.h:319
helper class to iterate over a rectangular fraction of a grid
Definition: grid.h:200
GridRunner(Grid< T > *target_grid, const QRectF &rectangle)
Definition: grid.h:205
GridRunner(Grid< T > &target_grid, const QRect &rectangle)
Definition: grid.h:207
GridRunner(Grid< T > *target_grid, const QRect &rectangle)
Definition: grid.h:209
void neighbors8(T **rArray)
get pointers to the 8-neighbor-hood north/east/west/south/NE/NW/SE/SW 0-pointers are returned for edg...
Definition: grid.h:619
T * current() const
return the current element, or NULL
Definition: grid.h:213
T * first() const
return the first element
Definition: grid.h:215
QPoint currentIndex() const
return the (index) - coordinates of the current position in the grid
Definition: grid.h:221
GridRunner(const Grid< T > &target_grid, const QRectF &rectangle)
Definition: grid.h:204
void neighbors4(T **rArray)
fill array with pointers to neighbors (north, east, west, south) or Null-pointers if out of range.
Definition: grid.h:604
GridRunner(Grid< T > &target_grid, const QRectF &rectangle)
Definition: grid.h:203
GridRunner(const Grid< T > &target_grid, const QRect &rectangle)
Definition: grid.h:208
bool isValid() const
checks if the state of the GridRunner is valid, returns false if out of scope
Definition: grid.h:219
T * last() const
return the last element (not one element behind the last element!)
Definition: grid.h:217
void reset()
Definition: grid.h:224
T * next()
to to next element, return NULL if finished
Definition: grid.h:583
void setPosition(QPoint new_index)
set the internal pointer to the pixel at index 'new_index'. The index is relative to the base grid!
Definition: grid.h:226
QPointF currentCoord() const
return the coordinates of the cell center point of the current position in the grid.
Definition: grid.h:223
GridRunner(Grid< T > *target_grid)
Definition: grid.h:210
Exception IException is the iLand model exception class.
Definition: exception.h:27
simple 3d vector.
Definition: grid.h:249
Vector3D()
Definition: grid.h:251
double y() const
get y-coordinate
Definition: grid.h:254
void setZ(const double z)
set value of the z-coordinate
Definition: grid.h:259
Vector3D(const double x, const double y, const double z)
Definition: grid.h:252
void setX(const double x)
set value of the x-coordinate
Definition: grid.h:257
double x() const
get x-coordinate
Definition: grid.h:253
void setY(const double y)
set value of the y-coordinate
Definition: grid.h:258
double z() const
get z-coordinate
Definition: grid.h:255
double distance(const QPointF &a, const QPointF &b)
Definition: mainwindow.cpp:30
QString gridToESRIRaster(const Grid< T > &grid, QString(*valueFunction)(const T &value))
Definition: grid.h:709
void modelToWorld(const Vector3D &From, Vector3D &To)
Definition: gisgrid.cpp:54
bool loadGridFromImage(const QString &fileName, FloatGrid &rGrid)
load into 'rGrid' the content of the image pointed at by 'fileName'.
Definition: grid.cpp:97
GridViewType
Definition: grid.h:193
@ GridViewGrayReverse
Definition: grid.h:193
@ GridViewBrewerDiv
Definition: grid.h:195
@ GridViewGray
Definition: grid.h:193
@ GridViewTerrain
Definition: grid.h:195
@ GridViewRainbow
Definition: grid.h:193
@ GridViewRainbowReverse
Definition: grid.h:193
@ GridViewBlues
Definition: grid.h:194
@ GridViewGreens
Definition: grid.h:194
@ GridViewReds
Definition: grid.h:194
@ GridViewBrewerQual
Definition: grid.h:195
@ GridViewCustom
Definition: grid.h:195
@ GridViewHeat
Definition: grid.h:193
@ GridViewTurbo
Definition: grid.h:194
Grid< float > FloatGrid
Definition: grid.h:191
QImage gridToImage(const FloatGrid &grid, bool black_white=false, double min_value=0., double max_value=1., bool reverse=false)
creates and return a QImage from Grid-Data.
QString gridToString(const FloatGrid &grid, const QChar sep=QChar(';'), const int newline_after=-1)
dumps a FloatGrid to a String.
Definition: grid.cpp:23
int irandom(int from, int to)
return a random number from "from" to "to" (excluding 'to'.), i.e. irandom(3,6) results in 3,...
Definition: randomgenerator.h:81