ExemplifyIterators.cpp
Go to the documentation of this file.
1 #include "RasterExamples.h"
2 
3 // TerraLib
6 #include <terralib/raster/Grid.h>
10 #include <terralib/raster/Utils.h>
11 
12 // STL
13 #include <ctime>
14 #include <iostream>
15 #include <limits>
16 #include <map>
17 #include <set>
18 
19 // Boost
20 #include <boost/random/mersenne_twister.hpp>
21 #include <boost/random/uniform_int_distribution.hpp>
22 
24 {
25  std::cout << "Example of Band Iterator" << std::endl;
26 
27 // define raster info and load
28  std::map<std::string, std::string> rinfo;
29  rinfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_rgb342_crop.tif";
31 
32  te::rst::Band* band = inraster->getBand(0);
33 
34 // create iterators for band 0
36 
38 
39  double max = std::numeric_limits<unsigned char>::min();
40 
41  double value;
42 
43  while (it != itend)
44  {
45 // using iterator
46  value = *it;
47 
48  if (value > max)
49  max = value;
50 
51  ++it;
52  }
53 
54  std::cout << std::endl;
55  std::cout << " using iterator" << std::endl;
56  std::cout << " the maximum value for band 0 is " << max << std::endl;
57 
58  max = std::numeric_limits<double>::min();
59 
60  for (unsigned r = 0; r < inraster->getNumberOfRows(); r++)
61  for (unsigned c = 0; c < inraster->getNumberOfColumns(); c++)
62  {
63  inraster->getValue(c, r, value, 0);
64 
65  if (value > max)
66  max = value;
67  }
68 
69  std::cout << " not using iterator" << std::endl;
70  std::cout << " the maximum value for band 0 is " << max << std::endl << std::endl;
71 
72 // clean up
73  delete inraster;
74 }
75 
77 {
78  std::cout << "Example of Band Iterator Mask" << std::endl;
79 
80 // define raster info and load
81  std::map<std::string, std::string> rinfo;
82  rinfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_rgb342_crop.tif";
84 
85  te::rst::Band* band = inraster->getBand(0);
86 
87 // define raster mask and load
88  std::map<std::string, std::string> minfo;
89  minfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_rgb342_crop_mask_1bit.tif";
91 
92 // define output raster and create (only one band will be created)
93  std::map<std::string, std::string> orinfo;
94  orinfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_rgb342_crop_mask_8bits.tif";
95  te::rst::Grid* ogrid = new te::rst::Grid(*inraster->getGrid());
96  std::vector<te::rst::BandProperty*> obprops;
97  for (unsigned int i = 0; i < inraster->getNumberOfBands(); i++)
98  obprops.push_back(new te::rst::BandProperty(i, te::dt::UCHAR_TYPE, "masked band"));
99  te::rst::Raster* outraster = te::rst::RasterFactory::make(ogrid, obprops, orinfo);
100  te::rst::FillRaster(outraster, 0.0);
101 
102 // create iterators for band
105 
106  std::vector<std::complex<double> > pixels;
107  while (it != itend)
108  {
109  inraster->getValues(it.getColumn(), it.getRow(), pixels);
110  outraster->setValues(it.getColumn(), it.getRow(), pixels);
111 
112  ++it;
113  }
114 
115 // clean up
116  delete inraster;
117  delete mraster;
118  delete outraster;
119 }
120 
122 {
123  std::cout << "Example of Raster Iterator" << std::endl;
124 
125 // define raster info and load
126  std::map<std::string, std::string> rinfo;
127  rinfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_rgb342_crop.tif";
129 
130 // create iterators for bands 0 and 1
131  std::vector<unsigned int> bands;
132 
133  bands.push_back(0);
134  bands.push_back(1);
135 
137 
139 
140 // this example will compute the mean values for bands 0 and 1
141  double mean0 = 0.0;
142  double mean1 = 0.0;
143 
144  int N = 0;
145 
146  while (it != itend)
147  {
148 // using iterator
149  mean0 += (*it)[0];
150  mean1 += (*it)[1];
151  ++N;
152 
153  ++it;
154  }
155 
156  std::cout << std::endl;
157  std::cout << " the mean for band " << bands[0] << " is " << mean0 / N << std::endl;
158  std::cout << " the mean for band " << bands[1] << " is " << mean1 / N << std::endl << std::endl;
159 
160 // clean up
161  delete inraster;
162 }
163 
165 {
166  std::cout << "Example of Band Iterator Window" << std::endl;
167 
168 // define raster info and load
169  std::map<std::string, std::string> rinfo;
170  rinfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_rgb342_crop.tif";
172 
173 // define output raster info
174  std::map<std::string, std::string> orinfo;
175  orinfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_band3_crop_median_5x5.tif";
176 
177  te::rst::Grid* grid = new te::rst::Grid(*inraster->getGrid());
178 
179  std::vector<te::rst::BandProperty*> bands;
180  bands.push_back(new te::rst::BandProperty(0, te::dt::UCHAR_TYPE, "median in 5x5 window of band 3"));
181 
182 // create output raster in disk
183  te::rst::Raster* outraster = te::rst::RasterFactory::make(grid, bands, orinfo);
184 
185  te::rst::Band* band = inraster->getBand(0);
186 
187 // create iterator for band 0 with a window of 5x5
188  int W = 5;
189  int H = 5;
191 
193 
194  while (it != itend)
195  {
196  unsigned R = it.getRow();
197  unsigned C = it.getColumn();
198 
199  std::set<unsigned char> values;
200 
201 // "walk" through window around iterator
202  if ((R > (unsigned)(H / 2) && R < inraster->getNumberOfRows() - (H / 2)) &&
203  (C > (unsigned)(W / 2) && C < inraster->getNumberOfColumns() - (W / 2)))
204  {
205  for (int r = -(H / 2); r <= (H / 2); r++)
206  for (int c = -(W / 2); c <= (W / 2); c++)
207  values.insert(it.getValue(c, r));
208  }
209  else
210  values.insert(it.getValue());
211 
212  std::set<unsigned char>::iterator vit = values.begin();
213 
214 // the middle position of a set will have the median value
215  for (std::size_t i = 0; i < values.size()/2; i++)
216  ++vit;
217 
218  outraster->setValue(C, R, (double) *vit);
219 
220  ++it;
221  }
222 
223  std::cout << std::endl << " Check the file " << orinfo["URI"] << std::endl << std::endl;
224 
225 // clean up
226  delete inraster;
227  delete outraster;
228 }
229 
231 {
232  std::cout << "Example of Polygon Iterator" << std::endl;
233 
234 // define raster info and load
235  std::map<std::string, std::string> rinfo;
236  rinfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_rgb342_crop.tif";
238 
239  unsigned int nvalues = 0;
240  double sum_pixels = 0.0;
241  double mean = 0.0;
242 
243 // create a polygon to use the iterator (corresponds to the upper left quadrant of the image)
244  double xc = (inraster->getExtent()->getUpperRightX() + inraster->getExtent()->getLowerLeftX())/2;
245  double yc = (inraster->getExtent()->getUpperRightY() + inraster->getExtent()->getLowerLeftY())/2;
247  lr->setPoint(0, inraster->getExtent()->getLowerLeftX(), inraster->getExtent()->getLowerLeftY()); // lower left
248  lr->setPoint(1, inraster->getExtent()->getLowerLeftX(), yc); // upper left
249  lr->setPoint(2, xc, yc); // upper rigth
250  lr->setPoint(3, xc, inraster->getExtent()->getLowerLeftY()); // lower rigth
251  lr->setPoint(4, inraster->getExtent()->getLowerLeftX(), inraster->getExtent()->getLowerLeftY()); // closing
252 
254  polygon->push_back(lr);
255 
256 // assuming we have a te::gm::Polygon* polygon
259 
260  while (it != itend)
261  {
262  sum_pixels += (*it)[0];
263  nvalues++;
264 
265  ++it;
266  }
267 
268 // compute mean value dividing the sum of pixels by the amount of pixels
269  if (nvalues > 0)
270  mean = sum_pixels / nvalues;
271 
272  std::cout << "The average pixel value (mean) inside the upper left region of band 0 is: " << mean << std::endl << std::endl;
273 
274  std::cout << "Done!" << std::endl << std::endl;
275 
276 // clean up
277  delete inraster;
278 }
279 
281 {
282  std::cout << "Example of Line Iterator" << std::endl;
283 
284 // define raster info and load
285  std::map<std::string, std::string> rinfo;
286  rinfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_rgb342_crop.tif";
288 
289 // create a line to use the iterator, corresponds to a horizontal line in the middle of the raster
290  double yc = (inraster->getExtent()->getUpperRightY() + inraster->getExtent()->getLowerLeftY())/2;
291  te::gm::Point p1(inraster->getExtent()->getLowerLeftX(), yc, inraster->getSRID());
292  te::gm::Point p2(inraster->getExtent()->getUpperRightX(), yc, inraster->getSRID());
293  te::gm::Line* line = new te::gm::Line(p1, p2, te::gm::LineStringType, inraster->getSRID());
294 
295  std::vector<double> pixels_in_line;
296 
297 // assuming we have a te::gm::Line* line
300 
301  while (it != itend)
302  {
303  pixels_in_line.push_back((*it)[0]);
304 
305  ++it;
306  }
307 
308  std::cout << "Pixels in line: ";
309  for (unsigned int i = 0; i < pixels_in_line.size(); i++)
310  std::cout << pixels_in_line[i] << " ";
311  std::cout << std::endl << std::endl;
312 
313  std::cout << "Done!" << std::endl << std::endl;
314 
315 // clean up
316  delete inraster;
317 }
318 
320 {
321  std::cout << "Example of Point Set Iterator" << std::endl;
322 
323 // define raster info and load
324  std::map<std::string, std::string> rinfo;
325  rinfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_rgb342_crop.tif";
327 
328 // create a vector of random points inside the band's envelope
329  int srid = inraster->getSRID();
330  std::vector<te::gm::Point*> points;
331  double x, y;
332  boost::random::mt19937 gen(static_cast<boost::uint32_t>(std::time(0)));
333  boost::random::uniform_int_distribution<> distX((int)(inraster->getExtent()->getLowerLeftX()), (int)(inraster->getExtent()->getUpperRightX()));
334  boost::random::uniform_int_distribution<> distY((int)(inraster->getExtent()->getLowerLeftY()), (int)(inraster->getExtent()->getUpperRightY()));
335  for (unsigned int i = 0; i < 50; i++)
336  {
337  x = distX(gen);
338  y = distY(gen);
339  points.push_back(new te::gm::Point(x, y, srid));
340  }
341 
342  std::vector<double> pixels_in_points;
343 
344 // assuming we have a std::vector<te::gm::Point*> points
347 
348  while (it != itend)
349  {
350  pixels_in_points.push_back((*it)[0]);
351 
352  ++it;
353  }
354 
355  std::cout << "Pixels in point set: ";
356  for (unsigned int i = 0; i < pixels_in_points.size(); i++)
357  std::cout << pixels_in_points[i] << " ";
358  std::cout << std::endl << std::endl;
359 
360  std::cout << "Done!" << std::endl << std::endl;
361 
362 // clean up
363  delete inraster;
364 }
365 
367 {
368  try
369  {
370  std::cout << "This test creates iterators over Bands, Windows, and Rasters." << std::endl << std::endl;
371 
379 
380  std::cout << "Done!" << std::endl << std::endl;
381  }
382  catch(const std::exception& e)
383  {
384  std::cout << std::endl << "An exception has occurred in ExemplifyIterators(): " << e.what() << std::endl;
385  }
386  catch(...)
387  {
388  std::cout << std::endl << "An unexpected exception has occurred in ExemplifyIterators()!" << std::endl;
389  }
390 
391 }
This class implements an iterator to "navigate" over a single band, with a spatial restriction given ...
Definition: BandIterator.h:280
virtual void setValue(unsigned int c, unsigned int r, const double value, std::size_t b=0)
Sets the attribute value in a band of a cell.
virtual void setValues(unsigned int c, unsigned int r, const std::vector< double > &values)
Sets the imaginary attribute values in all complex bands of a cell.
unsigned int getRow() const
Returns the current row in iterator.
Definition: BandIterator.h:378
te::gm::Envelope * getExtent()
Returns the geographic extension of the raster data.
void push_back(Curve *ring)
It adds the curve to the curve polygon.
unsigned int band
A Line is LineString with 2 points.
Definition: Line.h:50
unsigned int getColumn() const
Returns the current column in iterator.
Definition: BandIterator.h:383
void ExemplifyIterators()
An example to test iterators over Bands, Windows, and Rasters.
A raster band description.
Definition: BandProperty.h:61
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
unsigned int getColumn() const
Returns the current column in iterator.
This class implements and iterator to "navigate" over a raster, with a predefined number of bands...
virtual void getValues(unsigned int c, unsigned int r, std::vector< double > &values) const
Returns the imaginary attribute values in all complex bands of a cell.
const double & getUpperRightX() const
It returns a constant refernce to the x coordinate of the upper right corner.
This class implements the strategy to iterate with spatial restriction, the iteration occurs inside a...
This class implements the strategy to iterate with spatial restriction, the iteration occurs inside a...
void ExemplifyPointSetIterator()
const double & getLowerLeftY() const
It returns a constant refernce to the y coordinate of the lower left corner.
static PointSetIterator end(const te::rst::Raster *r, const std::vector< te::gm::Point * > p)
Returns an iterator referring to after the end of the iterator.
TERASTEREXPORT void FillRaster(te::rst::Raster *rin, const std::complex< double > &value)
Fill a Raster with provided value (all bands).
const double & getUpperRightY() const
It returns a constant refernce to the x coordinate of the upper right corner.
unsigned int getRow() const
Returns the current row in iterator.
It implements and iterator to "navigate" over a single band, optimized by a window structure (e...
This class implements the strategy to iterate with spatial restriction, the iteration occurs inside a...
static BandIterator begin(Band *b)
Returns an iterator referring to the first value of the band.
Definition: BandIterator.h:563
unsigned int line
Utility functions for the raster module.
A LinearRing is a LineString that is both closed and simple.
Definition: LinearRing.h:53
This is the abstract factory for Rasters.
static BandIteratorWithMask end(Band *b, Raster *m)
Returns an iterator with the mask referring to after the end of the iterator.
Definition: BandIterator.h:732
static PolygonIterator end(const te::rst::Raster *r, const te::gm::Polygon *p)
Returns an iterator referring to after the end of the iterator.
A point with x and y coordinate values.
Definition: Point.h:50
void setPoint(std::size_t i, const double &x, const double &y)
It sets the value of the specified point.
An abstract class for raster data strucutures.
static RasterIterator begin(Raster *r, const std::vector< unsigned int > &bands)
Returns an iterator referring to the first value.
unsigned int getNumberOfRows() const
Returns the raster number of rows.
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
It implements an iterator to "navigate" over a single band (const or not const).
list bands
Definition: compose.py:2
void ExemplifyPolygonIterator()
void ExemplifyBandIteratorMask()
A raster band description.
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
Grid * getGrid()
It returns the raster grid.
void ExemplifyBandIteratorWindow()
static BandIteratorWindow end(Band const *const b, std::size_t w, const std::size_t h)
Returns an iterator referring to after the end of the iterator.
virtual void getValue(unsigned int c, unsigned int r, double &value, std::size_t b=0) const
Returns the attribute value of a band of a cell.
static PolygonIterator begin(const te::rst::Raster *r, const te::gm::Polygon *p, const IterationType iterationType)
Returns an iterator referring to the first value of the band.
It implements and iterator to "navigate" over a raster, with a predefined number of bands...
int getSRID() const
Returns the raster spatial reference system identifier.
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
A rectified grid is the spatial support for raster data.
These routines show how to use the raster module and the GDAL data source module. ...
This class implements an iterator to "navigate" over a single band.
Definition: BandIterator.h:153
static Raster * make()
It creates and returns an empty raster with default raster driver.
static LineIterator end(const te::rst::Raster *r, const te::gm::Line *l)
Returns an iterator referring to after the end of the iterator.
const double & getLowerLeftX() const
It returns a constant reference to the x coordinate of the lower left corner.
static RasterIterator end(Raster *r, const std::vector< unsigned int > &bands)
Returns an iterator referring to after the end of the iterator.
void ExemplifyRasterIterator()
static BandIteratorWithMask begin(Band *b, Raster *m)
Returns an iterator with the mask referring to the first value of the band.
Definition: BandIterator.h:727
A rectified grid is the spatial support for raster data.
Definition: raster/Grid.h:68
It implements and iterator to "navigate" over a single band, optimized by a window structure (e...
static BandIterator end(Band *b)
Returns an iterator referring to after the end of the iterator.
Definition: BandIterator.h:568
void ExemplifyBandIterator()
static BandIteratorWindow begin(Band const *const b, std::size_t w, const std::size_t h)
Returns an iterator referring to the first value of the band.
T getValue() const
Returns the value in current position (column, row) from iterator.
void ExemplifyLineIterator()
static PointSetIterator begin(const te::rst::Raster *r, const std::vector< te::gm::Point * > p)
Returns an iterator referring to the first value of the band.
static Raster * open(const std::map< std::string, std::string > &rinfo, te::common::AccessPolicy p=te::common::RAccess)
It opens a raster with the given parameters and default raster driver.
static LineIterator begin(const te::rst::Raster *r, const te::gm::Line *l)
Returns an iterator referring to the first value of the band.