Raster.C
Go to the documentation of this file.
1 #include "Raster.H"
3 {
4  nrows=0;
5  ncols=0;
6  xll=0.0;
7  yll=0.0;
8  xur=0.0;
9  yur=0.0;
10  nodata=-9999;
11  cellsize=1000;
12 }
13 Raster::Raster(int NROWS, int NCOLS, double XLL, double YLL,double CELLSIZE ,int NODATA,double VALUE)
14 {
15  nrows=NROWS;
16  ncols=NCOLS;
17  xll=XLL;
18  yll=YLL;
19  xur=XLL+NCOLS*CELLSIZE;
20  yur=YLL+NROWS*CELLSIZE;
21  nodata=NODATA;
22  cellsize=CELLSIZE;
23  std::vector<double> rasterRow(NCOLS,VALUE);
24  std::vector< std::vector<double> > newRaster(NROWS, rasterRow);
25  data=newRaster;
26 }
27 
28 
29 Raster::Raster(const Raster &rast, double value)
30 {
31  ncols=rast.ncols;
32  nrows=rast.nrows;
33  nodata=rast.nodata;
34  cellsize=rast.cellsize;
35  xll=rast.xll;
36  yll=rast.yll;
37  yur=rast.yur;
38  xur=rast.xur;
39  std::vector<double> rasterRow(ncols,value);
40  std::vector< std::vector<double> > newRaster(nrows, rasterRow);
41  data=newRaster;
42 }
43 
44 Raster::Raster(const Raster &rast)
45 {
46  ncols=rast.ncols;
47  nrows=rast.nrows;
48  nodata=rast.nodata;
49  cellsize=rast.cellsize;
50  xll=rast.xll;
51  yll=rast.yll;
52  yur=rast.yur;
53  xur=rast.xur;
54  std::vector<double> rasterRow(ncols,nodata);
55  std::vector< std::vector<double> > newRaster(nrows, rasterRow);
56  data=newRaster;
57 }
58 
60 {
61 }
62 
63 int Raster::write(const char fileName[])
64 {
65  std::ofstream fout;
66  fout.open(fileName);
67  fout << "nrows "<< nrows <<"\n";
68  fout << "ncols "<< ncols << "\n";
69  fout << "xllcorner "<< xll << "\n";
70  fout << "yllcorner "<< yll << "\n";
71  fout << "cellsize "<< cellsize << "\n";
72  fout << "NODATA_value -9999"<<"\n";
73 
74  std::vector< std::vector<double> >::iterator row;
75  std::vector<double>::iterator col;
76 
77  for(row=data.begin();row!=data.end();row++)
78  {
79  for(col=row->begin();col!=row->end();col++)
80  {
81  if(col!=row->begin())
82  fout<<" ";
83  fout<<*col;
84  }
85  fout<<"\n";
86  }
87  fout.close();
88  return 1;
89 }
90 
91 int Raster::read(const char fileName[])
92  {
93  try
94  {
95  std::string tmp;
96  std::ifstream fin(fileName);
97 
98  fin>>tmp; fin >> ncols;
99  fin>>tmp; fin >> nrows;
100  fin>>tmp; fin >> xll;
101  fin>>tmp; fin >> yll;
102  fin>>tmp; fin >> cellsize;
103  fin>>tmp; fin >> nodata;
104 
105  if(ncols==0 or nrows==0)
106  return 0;
107 
110 
111  double value;
112  for (int row=0;row<nrows;row++)
113  {
114  std::vector<double> rowVector;
115  for(int col=0;col<ncols;col++)
116  {
117  fin >> value;
118  rowVector.push_back(value);
119  }
120  data.push_back(rowVector);
121  }
122  return 1;
123  }
124  catch(...)
125  {
126  return 0;
127  }
128  }
129 
131 {
132  nrows=rhs.nrows;
133  ncols=rhs.ncols;
134  xll=rhs.xll;
135  yll=rhs.yll;
136  nodata=rhs.nodata;
137  cellsize=rhs.cellsize;
138  yur=rhs.yur;
139  xur=rhs.xur;
140  std::vector<double> rasterRow(ncols,nodata);
141  std::vector< std::vector<double> > newRaster(nrows, rasterRow);
142  data=newRaster;
143  for(int row=0;row<this->nrows;row++)
144  for(int col=0;col<this->ncols;col++)
145  data[row][col]=rhs.data[row][col];
146  return *this;
147 }
148 
149 int Raster::getIndex(double x, double y,int& row, int& col)
150 {
151  if((x<xll or y<yll) or (x > xur or y > yur))
152  return 0;
153  else
154  {
155  col=int((x-xll)/cellsize);
156  row=nrows-int(ceil((y-yll)/cellsize));
157  if(row == nrows)
158  row--;
159  if(col == ncols)
160  col--;
161  return 1;
162  }
163 }
164 
165 bool Raster::inside(double x, double y)
166 {
167  if((x<xll or y<yll) or (x > xur or y > yur))
168  return false;
169  else
170  return true;
171 }
172 
173 double Raster::getValue(double x, double y)
174 {
175  int row,col;
176  if(!getIndex(x,y,row,col))
177  {
178  std::cout<<"Value in "<<x<<","<<y<<" is outside of raster boundaries";
179  exit(1);
180  }
181  else
182  return data[row][col];
183 
184 }
185 
186 double Raster::getX(int col)
187 {
188  return xll+col*cellsize+0.5*cellsize;
189 }
190 
191 double Raster::getY(int row)
192 {
193  return yll+row*cellsize+0.5*cellsize;
194 }
195 
196 
197 double Raster::max()
198 {
199  double maxValue=-9e99;
200  for (int row=0;row<nrows;row++)
201  {
202  for(int col=0;col<ncols;col++)
203  {
204  if(data[row][col]>maxValue and data[row][col]!=nodata)
205  maxValue=data[row][col];
206  }
207  }
208  if(maxValue==-9e99)
209  return nodata;
210  else
211  return maxValue;
212 }
213 
214 double Raster::min()
215 {
216  double minValue=9e99;
217  for (int row=0;row<nrows;row++)
218  {
219  for(int col=0;col<ncols;col++)
220  {
221  if(data[row][col]<minValue and data[row][col]!=nodata)
222  minValue=data[row][col];
223  }
224  }
225  if(minValue==9e99)
226  return nodata;
227  else
228  return minValue;
229 }
230 
231 double Raster::sum()
232 {
233  double sumValue=0;
234  for (int row=0;row<nrows;row++)
235  {
236  for(int col=0;col<ncols;col++)
237  {
238  if(data[row][col]!=nodata)
239  sumValue+=data[row][col];
240  }
241  }
242  return sumValue;
243 }
244 
245 void Raster::scale(double value)
246 {
247  for(int row=0;row<nrows;row++)
248  for(int col=0;col<ncols;col++)
249  {
250  if(data[row][col]!=nodata)
251  data[row][col]=value*data[row][col];
252  }
253 }
254 
255 
256 double Raster::interpValue(double x, double y)
257 {
258  int row,col;
259  std::vector<double> vals,dists;
260  double d=0;
261 
262  if(!getIndex(x,y,row,col))
263  {
264  std::cout<<"Value is outside of raster boundaries";
265  exit(1);
266  }
267 
268 
269  int kvadrant=0;
270  if(x>=this->getX(col) and y>=this->getY(row))
271  kvadrant=1;
272  else if(x>=this->getX(col) and y<this->getY(row))
273  kvadrant=2;
274  else if(y>=this->getY(row))
275  kvadrant=4;
276  else
277  kvadrant=3;
278 
279 
280  int row1,row2,row3,row4,col1,col2,col3,col4;
281 
282  switch (kvadrant)
283  {
284  case 1:
285  row1=row;
286  col1=col;
287  row2=row-1;
288  col2=col;
289  row3=row-1;
290  col3=col+1;
291  row4=row;
292  col4=col+1;
293 
294 
295  if (row2<0 or col4>=this->ncols)
296  {
297  std::cout<<"Value is outside of raster boundaries";
298  exit(1);
299  }
300  break;
301 
302  case 2:
303  row1=row;
304  col1=col;
305  row2=row+1;
306  col2=col;
307  row3=row+1;
308  col3=col+1;
309  row4=row;
310  col4=col+1;
311  if (row2>=this->nrows or col3>=this->ncols)
312  {
313  std::cout<<"Value is outside of raster boundaries";
314  exit(1);
315  }
316 
317  break;
318 
319  case 3:
320  row1=row;
321  col1=col;
322  row2=row+1;
323  col2=col;
324  row3=row+1;
325  col3=col-1;
326  row4=row;
327  col4=col-1;
328  if (row2>=this->nrows or col3<0)
329  {
330  std::cout<<"Value is outside of raster boundaries";
331  exit(1);
332  }
333 
334  break;
335 
336  case 4:
337  row1=row;
338  col1=col;
339  row2=row-1;
340  col2=col;
341  row3=row-1;
342  col3=col-1;
343  row4=row;
344  col4=col-1;
345 
346  if (row2<0 or col3<0)
347  {
348  std::cout<<"Value is outside of raster boundaries";
349  exit(1);
350  }
351 
352  break;
353  }
354 
355  d=sqrt(pow(this->getX(col1)-x,2)+pow(this->getY(row1)-y,2));
356  dists.push_back(d);
357  vals.push_back(data[row1][col1]);
358 
359  d=sqrt(pow(this->getX(col2)-x,2)+pow(this->getY(row2)-y,2));
360  dists.push_back(d);
361  vals.push_back(data[row2][col2]);
362 
363  d=sqrt(pow(this->getX(col3)-x,2)+pow(this->getY(row3)-y,2));
364  dists.push_back(d);
365  vals.push_back(data[row3][col3]);
366 
367 
368  d=sqrt(pow(this->getX(col4)-x,2)+pow(this->getY(row4)-y,2));
369  dists.push_back(d);
370  vals.push_back(data[row4][col4]);
371 
372 
373 
374  double a=0;
375  double b=0;
376  for(int j=0;j<4;j++)
377  {
378  if (dists[j]>0)
379  {
380  a=a+vals[j]/pow(dists[j],2);
381  b=b+1./pow(dists[j],2);
382  }
383  else
384  return vals[j];
385  }
386  return a/b;
387 
388 }
389 
390 bool Raster::conformal(const Raster &rast)
391 {
392  if (cellsize!=rast.cellsize)
393  return false;
394  double xsteps=(xll-rast.xll)/double(cellsize);
395  double ysteps=(yll-rast.yll)/double(cellsize);
396  double xrest=xsteps-floor(xsteps);
397  double yrest=ysteps-floor(ysteps);
398  if(xrest!=0 or yrest!=0)
399  return false;
400  else
401  return true;
402 }
403 
404 bool Raster::match(const Raster &rast)
405 {
406  if(this->conformal(rast) and xll==rast.xll
407  and yll==rast.yll and ncols==rast.ncols
408  and nrows==rast.nrows)
409  return true;
410  else
411  return false;
412 }
413 
414 void Raster::whereAdd(const Raster &rast,double value)
415 {
416  for(int row=0;row<nrows;row++)
417  {
418  for(int col=0;col<ncols;col++)
419  {
420  if (rast.data[row][col]>0 and rast.data[row][col]!=rast.nodata)
421  data[row][col]=data[row][col]+value;
422  if (rast.data[row][col]==rast.nodata)
423  data[row][col]=rast.nodata;
424  }
425  }
426 }
427 
428 void Raster::translate(const double &x, const double &y) {
429  xll += x;
430  xur += x;
431  yll += y;
432  yur += y;
433 }
434 
435 Raster where(const Raster &rast,const double &val1, const double &val2)
436 {
437  Raster temp(rast,0);
438  for(int row=0;row<temp.nrows;row++)
439  {
440  for(int col=0;col<temp.ncols;col++)
441  {
442  if (rast.data[row][col]>0 and rast.data[row][col]!=rast.nodata)
443  temp.data[row][col]=val1;
444  else
445  temp.data[row][col]=val2;
446  }
447  }
448  return temp;
449 }
450 
451 Raster where(const Raster &rast,const Raster &rast1, const Raster &rast2)
452 {
453  Raster temp(rast,0);
454  for(int row=0;row<temp.nrows;row++)
455  {
456  for(int col=0;col<temp.ncols;col++)
457  {
458  if (rast.data[row][col]>0 and rast.data[row][col]!=rast.nodata)
459  temp.data[row][col]= rast1.data[row][col];
460  else
461  temp.data[row][col]= rast2.data[row][col];
462  }
463  }
464  return temp;
465 }
466 
467 
468 Raster operator /(const Raster &rast1, const Raster &rast2)
469 {
470  Raster temp(rast1);
471 
472  for(int row=0;row<temp.nrows;row++)
473  for(int col=0;col<temp.ncols;col++)
474  {
475  if(rast1.data[row][col]!=rast1.nodata and rast2.data[row][col]!=rast2.nodata)
476  temp.data[row][col]=rast1.data[row][col]/rast2.data[row][col];
477  else
478  temp.data[row][col]=temp.nodata;
479  }
480  return temp;
481 }
482 
483 Raster operator *(const Raster &rast1, const Raster &rast2)
484 {
485  Raster temp(rast1);
486 
487  for(int row=0;row<temp.nrows;row++)
488  for(int col=0;col<temp.ncols;col++)
489  {
490  if(rast1.data[row][col]!=rast1.nodata and rast2.data[row][col]!=rast2.nodata)
491  temp.data[row][col]=rast1.data[row][col]*rast2.data[row][col];
492  else
493  temp.data[row][col]=temp.nodata;
494  }
495  return temp;
496 }
497 Raster operator +(const Raster &rast1, const Raster &rast2)
498 {
499  Raster temp(rast1);
500 
501  for(int row=0;row<temp.nrows;row++)
502  for(int col=0;col<temp.ncols;col++)
503  {
504  if(rast1.data[row][col]!=rast1.nodata and rast2.data[row][col]!=rast2.nodata)
505  temp.data[row][col]=rast1.data[row][col]+rast2.data[row][col];
506  else
507  temp.data[row][col]=temp.nodata;
508  }
509 return temp;
510 }
511 Raster operator -(const Raster &rast1, const Raster &rast2)
512 {
513  Raster temp(rast1);
514 
515  for(int row=0;row<temp.nrows;row++)
516  for(int col=0;col<temp.ncols;col++)
517  {
518  if(rast1.data[row][col]!=rast1.nodata and rast2.data[row][col]!=rast2.nodata)
519  temp.data[row][col]=rast1.data[row][col]-rast2.data[row][col];
520  else
521  temp.data[row][col]=temp.nodata;
522  }
523  return temp;
524 }
525 
526 Raster operator *(const double value, const Raster &rast)
527 {
528  Raster temp(rast);
529  for(int row=0;row<temp.nrows;row++)
530  for(int col=0;col<temp.ncols;col++)
531  {
532  if(rast.data[row][col]!=rast.nodata)
533  temp.data[row][col]=value*rast.data[row][col];
534  else
535  temp.data[row][col]=temp.nodata;
536  }
537  return temp;
538 }
539 
540 Raster operator *(const Raster &rast, const double value)
541 {
542  Raster temp(rast);
543  for(int row=0;row<temp.nrows;row++)
544  for(int col=0;col<temp.ncols;col++)
545  {
546  if(rast.data[row][col]!=rast.nodata)
547  temp.data[row][col]=value*rast.data[row][col];
548  else
549  temp.data[row][col]=temp.nodata;
550  }
551 return temp;
552 }
553 
554 Raster operator +(const Raster &rast, const double value)
555 {
556  Raster temp(rast);
557  for(int row=0;row<temp.nrows;row++)
558  for(int col=0;col<temp.ncols;col++)
559  {
560  if(rast.data[row][col]!=rast.nodata)
561  temp.data[row][col]=value+rast.data[row][col];
562  else
563  temp.data[row][col]=temp.nodata;
564  }
565  return temp;
566 }
567 
568 Raster operator -(const Raster &rast, const double value)
569 {
570  Raster temp(rast);
571  for(int row=0;row<temp.nrows;row++)
572  for(int col=0;col<temp.ncols;col++)
573  {
574  if(rast.data[row][col]!=rast.nodata)
575  temp.data[row][col]=rast.data[row][col]-value;
576  else
577  temp.data[row][col]=temp.nodata;
578  }
579  return temp;
580 }
581 
582 Raster operator /(const Raster &rast, const double value)
583 {
584  Raster temp(rast);
585  for(int row=0;row<temp.nrows;row++)
586  for(int col=0;col<temp.ncols;col++)
587  {
588  if(rast.data[row][col]!=rast.nodata)
589  temp.data[row][col]=rast.data[row][col]/value;
590  else
591  temp.data[row][col]=temp.nodata;
592  }
593  return temp;
594 }
595 
596 Raster operator ==(const Raster &rast,const double value)
597 {
598  Raster temp(rast,0);
599  for(int row=0;row<temp.nrows;row++)
600  {
601  for(int col=0;col<temp.ncols;col++)
602  {
603  if (rast.data[row][col]==value and rast.data[row][col]!=rast.nodata)
604  temp.data[row][col]=1;
605  }
606  }
607  return temp;
608 }
609 
610 Raster operator <(const Raster &rast,const double value)
611 {
612  Raster temp(rast,0);
613  for(int row=0;row<temp.nrows;row++)
614  {
615  for(int col=0;col<temp.ncols;col++)
616  {
617  if (rast.data[row][col]<value and rast.data[row][col]!=rast.nodata)
618  temp.data[row][col]=1;
619  }
620  }
621  return temp;
622 }
623 
624 Raster operator >(const Raster &rast,const double value)
625 {
626  Raster temp(rast,0);
627  for(int row=0;row<temp.nrows;row++)
628  {
629  for(int col=0;col<temp.ncols;col++)
630  {
631  if (rast.data[row][col]>value and rast.data[row][col]!=rast.nodata)
632  temp.data[row][col]=1;
633  }
634  }
635  return temp;
636 }
637 
638 Raster operator <=(const Raster &rast,const double value)
639 {
640  Raster temp(rast,0);
641  for(int row=0;row<temp.nrows;row++)
642  {
643  for(int col=0;col<temp.ncols;col++)
644  {
645  if (rast.data[row][col]<=value and rast.data[row][col]!=rast.nodata)
646  temp.data[row][col]=1;
647  }
648  }
649  return temp;
650 }
651 
652 Raster operator >=(const Raster &rast,const double value)
653 {
654  Raster temp(rast,0);
655  for(int row=0;row<temp.nrows;row++)
656  {
657  for(int col=0;col<temp.ncols;col++)
658  {
659  if (rast.data[row][col]>=value and rast.data[row][col]!=rast.nodata)
660  temp.data[row][col]=1;
661  }
662  }
663  return temp;
664 }
665 
666 Raster intersect(const Raster &rast1,const Raster &rast2)
667 {
668  Raster temp(rast1,0);
669  for(int row=0;row<temp.nrows;row++)
670  {
671  for(int col=0;col<temp.ncols;col++)
672  {
673  if (rast1.data[row][col]>0 and rast2.data[row][col]>0)
674  temp.data[row][col]=1;
675  }
676  }
677  return temp;
678 }
Raster::conformal
bool conformal(const Raster &rast)
Definition: Raster.C:390
Raster
Definition: Raster.H:13
Raster::inside
bool inside(double x, double y)
Definition: Raster.C:165
Raster::Raster
Raster()
Definition: Raster.C:2
Raster::write
int write(const char fileName[])
Definition: Raster.C:63
Raster::getValue
double getValue(double x, double y)
Definition: Raster.C:173
operator<=
Raster operator<=(const Raster &rast, const double value)
Definition: Raster.C:638
Raster::data
std::vector< std::vector< double > > data
Definition: Raster.H:23
Raster::~Raster
~Raster()
Definition: Raster.C:59
Raster::ncols
int ncols
Definition: Raster.H:16
Foam::row
graphRow< VRWGraph > row
Definition: graphRow.H:135
operator-
Raster operator-(const Raster &rast1, const Raster &rast2)
Definition: Raster.C:511
Raster::translate
void translate(const double &x, const double &y)
Definition: Raster.C:428
Raster::getY
double getY(int row)
Definition: Raster.C:191
operator+
Raster operator+(const Raster &rast1, const Raster &rast2)
Definition: Raster.C:497
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:28
Raster::yur
double yur
Definition: Raster.H:20
Raster::read
int read(const char fileName[])
Definition: Raster.C:91
Raster::interpValue
double interpValue(double x, double y)
Definition: Raster.C:256
Foam::pow
dimensionedScalar pow(const dimensionedScalar &ds, const dimensionedScalar &expt)
Definition: dimensionedScalar.C:73
intersect
Raster intersect(const Raster &rast1, const Raster &rast2)
Definition: Raster.C:666
Raster::xll
double xll
Definition: Raster.H:17
operator>
Raster operator>(const Raster &rast, const double value)
Definition: Raster.C:624
Raster::whereAdd
void whereAdd(const Raster &rast, double value)
Definition: Raster.C:414
Raster::cellsize
double cellsize
Definition: Raster.H:21
Raster::operator=
Raster & operator=(const Raster &rast)
Definition: Raster.C:130
Raster::nrows
int nrows
Definition: Raster.H:15
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Raster::min
double min()
Definition: Raster.C:214
operator/
Raster operator/(const Raster &rast1, const Raster &rast2)
Definition: Raster.C:468
Raster::getX
double getX(int col)
Definition: Raster.C:186
operator<
Raster operator<(const Raster &rast, const double value)
Definition: Raster.C:610
Raster::max
double max()
Definition: Raster.C:197
Raster::sum
double sum()
Definition: Raster.C:231
Foam::sqrt
dimensionedScalar sqrt(const dimensionedScalar &ds)
Definition: dimensionedScalar.C:142
Raster.H
Raster::yll
double yll
Definition: Raster.H:18
Raster::nodata
double nodata
Definition: Raster.H:22
operator>=
Raster operator>=(const Raster &rast, const double value)
Definition: Raster.C:652
x
x
Definition: LISASMDCalcMethod2.H:52
Raster::scale
void scale(double value)
Definition: Raster.C:245
minValue
scalar minValue
Definition: LISASMDCalcMethod2.H:12
Raster::xur
double xur
Definition: Raster.H:19
where
Raster where(const Raster &rast, const double &val1, const double &val2)
Definition: Raster.C:435
operator*
Raster operator*(const Raster &rast1, const Raster &rast2)
Definition: Raster.C:483
Raster::getIndex
int getIndex(double x, double y, int &row, int &col)
Definition: Raster.C:149
operator==
Raster operator==(const Raster &rast, const double value)
Definition: Raster.C:596
Raster::match
bool match(const Raster &rast)
Definition: Raster.C:404
y
scalar y
Definition: LISASMDCalcMethod1.H:14
maxValue
scalar maxValue
Definition: LISASMDCalcMethod1.H:5