ArithmeticWithRaster.cpp
Go to the documentation of this file.
1 #include "RasterExamples.h"
2 
3 // TerraLib
4 #include <terralib/common.h>
5 #include <terralib/dataaccess.h>
7 #include <terralib/gdal.h>
8 #include <terralib/geometry.h>
9 #include <terralib/raster.h>
11 
12 // STL
13 #include <complex>
14 #include <iostream>
15 #include <string>
16 #include <vector>
17 
18 // GDAL
19 #include <gdal_priv.h>
20 
22 {
23  try
24  {
25  std::cout << "This is a test to create a new raster (NDVI) based on raster arithmetic operations." << std::endl << std::endl;
26 
27 // set raster names
28  std::map<std::string, std::string> sri, srndvi, srndvin, srden;
29  sri["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_rgb342_crop.tif";
30  srndvi["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_ndvi.tif";
31  srndvin["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_ndvi_normalized.tif";
32  std::string base_path = TERRALIB_DATA_DIR "/geotiff/";
33 
34 // open input raster (band 0 = Red, band 1 = IRed, band 2 = Blue)
36 
37 // create temporary raster using in memory driver
38  te::rst::Grid* gden = new te::rst::Grid(*ri->getGrid());
39  std::vector<te::rst::BandProperty*> bdsden;
40  bdsden.push_back(new te::rst::BandProperty(0, te::dt::FLOAT_TYPE, "ndvi den"));
41  bdsden[0]->m_blkh = gden->getNumberOfRows();
42  bdsden[0]->m_blkw = gden->getNumberOfColumns();
43  bdsden[0]->m_nblocksx = 1;
44  bdsden[0]->m_nblocksy = 1;
45  std::map<std::string, std::string> deninfo;
46  deninfo["FORCE_MEM_DRIVER"] = "TRUE";
47  te::rst::Raster* rden = te::rst::RasterFactory::make(gden, bdsden, deninfo);
48 
49 // access a raster datasource to create temporary raster
50  std::string connInfoRaster("file://");
51  connInfoRaster += base_path;
52  std::unique_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("GDAL", connInfoRaster);
53  ds->open();
54  std::unique_ptr<te::da::DataSourceTransactor> tr = ds->getTransactor();
55 
56 // create ndvi raster
57  te::rst::Grid* gndvi = new te::rst::Grid(*ri->getGrid());
58  std::vector<te::rst::BandProperty*> bdsndvi;
59  bdsndvi.push_back(new te::rst::BandProperty(0, te::dt::FLOAT_TYPE));
60  te::rst::Raster* rndvi = te::rst::RasterFactory::make(gndvi, bdsndvi, srndvi);
61 
62 // get GDAL band pointers
63  te::gdal::Band* ndviBand = static_cast<te::gdal::Band*> (rndvi->getBand(0));
64  te::gdal::Band* redBand = static_cast<te::gdal::Band*> (ri->getBand(0));
65  te::gdal::Band* iredBand = static_cast<te::gdal::Band*> (ri->getBand(1));
66  te::gdal::Band* ndenBand = static_cast<te::gdal::Band*> (rden->getBand(0));
67 
68 // calculate vegetation index (IRed - Red)
69  std::complex<double> value;
70  for (unsigned r = 0; r < rndvi->getNumberOfRows(); r++)
71  for (unsigned c = 0; c < rndvi->getNumberOfColumns(); c++)
72  {
73  iredBand->getValue(c, r, value);
74  ndviBand->setValue(c, r, value);
75  }
76  *ndviBand -= *redBand;
77 
78 // derive denominator (den = IRed + Red)
79  for (unsigned r = 0; r < rndvi->getNumberOfRows(); r++)
80  for (unsigned c = 0; c < rndvi->getNumberOfColumns(); c++)
81  {
82  iredBand->getValue(c, r, value);
83  ndenBand->setValue(c, r, value);
84  }
85  *ndenBand += *redBand;
86 
87 // calculate ndvi [-1, +1] = (IRed - Red) / (IRed + Red)
88  *rndvi /= *rden;
89 
90 // show ndvi status
91  std::cout << "NDVI created:" << std::endl;
92  std::cout << rndvi->toString();
93 
94 // create normalized ndvi [-1, +1] -> [0,255] for visualization
95  te::rst::Grid* gndvin = new te::rst::Grid(*ri->getGrid());
96  std::vector<te::rst::BandProperty*> bdsndvin;
97  bdsndvin.push_back(new te::rst::BandProperty(0, te::dt::DOUBLE_TYPE));
98  te::rst::Raster* rndvin = te::rst::RasterFactory::make(gndvin, bdsndvin, srndvin);
99  te::rst::Copy(*rndvi, *rndvin);
100  std::complex<double> ndvimin = rndvi->getBand(0)->getMinValue();
101  std::complex<double> ndvimax = 255.0 / (rndvi->getBand(0)->getMaxValue() - ndvimin);
102 
103  *rndvin -= ndvimin;
104  *rndvin *= ndvimax;
105 
106 // show normalized ndvi status
107  std::cout << "Normalized NDVI created:" << std::endl;
108  std::cout << rndvin->toString();
109 
110 // remove temporary raster using data set type persistence
111  delete rden;
112 
113 // clean up
114  delete ri;
115  delete rndvi;
116  delete rndvin;
117 
118  std::cout << "Done!" << std::endl << std::endl;
119  }
120  catch(const std::exception& e)
121  {
122  std::cout << std::endl << "An exception has occurred in ArithmetichWithRaster(): " << e.what() << std::endl;
123  }
124  catch(...)
125  {
126  std::cout << std::endl << "An unexpected exception has occurred in ArithmetichWithRaster()!" << std::endl;
127  }
128 }
unsigned int getNumberOfRows() const
Returns the grid number of rows.
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
A raster band description.
Definition: BandProperty.h:61
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
TERASTEREXPORT void Copy(const Raster &rin, Raster &rout)
Copies the pixel values from one raster to another.
static te::dt::Date ds(2010, 01, 01)
void ArithmeticWithRaster()
Creates a new raster based on raster arithmetic operations.
This class represents raster band description.
This is the abstract factory for Rasters.
virtual std::complex< double > getMaxValue(bool readall=false, unsigned int rs=0, unsigned int cs=0, unsigned int rf=0, unsigned int cf=0) const
It computes and returns the maximum occurring value in a window of the band.
void setValue(unsigned int c, unsigned int r, const double value)
Sets the cell attribute value.
An abstract class for raster data strucutures.
unsigned int getNumberOfRows() const
Returns the raster number of rows.
This file contains include headers for the TerraLib GDAL driver.
unsigned int getNumberOfColumns() const
Returns the grid number of columns.
A factory for data sources.
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
Grid * getGrid()
It returns the raster grid.
void getValue(unsigned int c, unsigned int r, double &value) const
Returns the cell attribute value.
std::string toString(void) const
It returns the data value in a string notation.
These routines show how to use the raster module and the GDAL data source module. ...
virtual std::complex< double > getMinValue(bool readall=false, unsigned int rs=0, unsigned int cs=0, unsigned int rf=0, unsigned int cf=0) const
It computes and returns the minimum occurring value in a window of the band.
static Raster * make()
It creates and returns an empty raster with default raster driver.
This file contains include headers for the TerraLib Common Runtime module.
This file contains include headers for the Vector Geometry model of TerraLib.
A rectified grid is the spatial support for raster data.
Definition: raster/Grid.h:68
This file contains include headers for the Data Access module of TerraLib.
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.