All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RasterManager.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2001-2009 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/tools/rastermanager/core/RasterManager.cpp
22 
23  \brief An utility class to provide methods to manage rasters
24  */
25 
26 // Terralib Raster Manager
27 #include "RasterManager.h"
28 #include "Utils.h"
29 
30 // Terralib
31 #include <terralib/common.h>
32 #include <terralib/raster.h>
35 
36 // Boost
37 #include <boost/filesystem.hpp>
38 
39 // GDAL
40 #include <gdal_priv.h>
41 
42 namespace fs = boost::filesystem;
43 
45 {
46  m_loadPlugin = true;
47 }
48 
49 bool te::tools::rastermanager::RasterManager::init(std::string input, std::string & errorMessage)
50 {
51 
52  if(!Utils::checkInputPath(input, errorMessage))
53  return false;
54 
55  m_input = input;
56 
57  if(m_loadPlugin)
58  if(!Utils::loadModules(errorMessage))
59  return false;
60 
61  m_loadPlugin = false;
62 
63  return true;
64 
65 }
66 
67 bool te::tools::rastermanager::RasterManager::copyRaster(std::string output, std::vector<int> bandVec, int bandType, std::string & errorMessage)
68 {
69 
70  if(!Utils::checkOutputPath(output, errorMessage))
71  return false;
72 
73  te::rst::Raster* raster = 0;
74  if(!Utils::getRaster(m_input, raster, errorMessage))
75  return false;
76 
77  // Start the copy
78  if(bandVec.size() > raster->getNumberOfBands())
79  {
80  errorMessage = "Error in the number of bands!";
81  return false;
82  }
83 
84  std::vector<te::rst::BandProperty*> bandProps;
85 
86  te::rst::Grid* newGrid = new te::rst::Grid(*raster->getGrid());
87 
88  if(bandVec.empty())
89  {
90  for(int unsigned i = 0; i < raster->getNumberOfBands(); i++)
91  {
92  bandProps.push_back(raster->getBand(i)->getProperty());
93  }
94  }
95  else
96  {
97  for(int unsigned i = 0; i < bandVec.size(); i++)
98  {
99  if(bandVec[i] > raster->getNumberOfBands())
100  {
101  errorMessage = "The band " + te::common::Convert2String(bandVec[i]) + " not exist in the input raster!";
102  return false;
103  }
104  bandProps.push_back(raster->getBand(bandVec[i])->getProperty());
105  }
106  }
107 
108  std::map<std::string, std::string> newRasterInfo;
109  newRasterInfo["URI"] = output;
110 
111  te::rst::Raster* newRaster = te::rst::RasterFactory::make(newGrid, bandProps, newRasterInfo);
112 
113  if(bandVec.empty())
114  {
115  for(int unsigned r = 0; r < raster->getNumberOfRows(); r++)
116  {
117  for(int unsigned c = 0; c < raster->getNumberOfColumns(); c++)
118  {
119  std::vector<std::complex<double>> values;
120  raster->getValues(c, r, values);
121 
122  newRaster->setValues(c, r, values);
123 
124  }
125  }
126  }
127  else
128  {
129  for(int unsigned r = 0; r < raster->getNumberOfRows(); r++)
130  {
131  for(int unsigned c = 0; c < raster->getNumberOfColumns(); c++)
132  {
133  for(int unsigned b = 0; b < bandVec.size(); b++)
134  {
135  std::complex<double> value;
136  raster->getValue(c, r, value, bandVec[b]);
137 
138  newRaster->setValue(c, r, value, bandVec[b]);
139  }
140  }
141  }
142  }
143 
144  delete raster;
145  delete newRaster;
146 
147  return true;
148 
149 }
150 
151 bool te::tools::rastermanager::RasterManager::reproject(std::string output, int srid, std::string & errorMessage)
152 {
153  te::rst::Raster* raster = 0;
154  if(!Utils::getRaster(m_input, raster, errorMessage))
155  return false;
156 
157  // The raster file will be overwriting if output empty
158  if(output.empty())
159  output = m_input;
160  else
161  if(!Utils::checkOutputPath(output, errorMessage))
162  return false;
163 
164  te::rst::Raster* outraster = 0;
165  std::map<std::string, std::string> outInfo;
166 
167  outInfo["URI"] = output;
168  outraster = te::rst::Reproject(raster, srid, outInfo);
169 
170  delete outraster;
171 
172  return true;
173 
174 }
175 
176 bool te::tools::rastermanager::RasterManager::getRasterInfo(std::ostringstream & output, std::string & errorMessage)
177 {
178 
179  te::rst::Raster* raster = 0;
180  if(!Utils::getRaster(m_input, raster, errorMessage))
181  return false;
182 
183  output << raster->toString() << std::endl << std::endl;
184 
185  output << "Bands Type Informations:" << std::endl << std::endl;
186 
187  for(unsigned int i = 0; i < raster->getNumberOfBands(); i++)
188  {
189  te::rst::BandProperty* currPorperty = raster->getBand(i)->getProperty();
190 
191  output << "Band " << te::common::Convert2String(i) << ": " << te::common::Convert2String(currPorperty->getType());
192  output << std::endl;
193  }
194 
195  output << std::endl;
196 
197  output << "Block Informations:" << std::endl << std::endl;
198 
199  te::rst::BandProperty* currPorperty = raster->getBand(0)->getProperty();
200 
201  output << "Block Width: " << te::common::Convert2String(currPorperty->m_blkw) << std::endl;
202  output << "Block Height: " << te::common::Convert2String(currPorperty->m_blkh) << std::endl;
203 
204  output << "Blocks in X: " << te::common::Convert2String(currPorperty->m_nblocksx) << std::endl;
205  output << "Blocks in Y: " << te::common::Convert2String(currPorperty->m_nblocksy) << std::endl;
206 
207  return true;
208 }
209 
210 bool te::tools::rastermanager::RasterManager::convert(std::string output, std::string extension, std::vector<int> bandVec, std::string & errorMessage)
211 {
212  std::string finalOutput = "";
213 
214 
215  if(!output.empty())
216  {
217  if(output == m_input)
218  {
219  errorMessage = "Con not convert the raster for the same path and same extension!";
220  return false;
221  }
222  else
223  {
224  finalOutput = output;
225  }
226  }
227  else if(!extension.empty())
228  {
229  if(extension[0] != '.')
230  extension = "." + extension;
231 
232  fs::path temp(m_input);
233  temp.replace_extension(extension);
234 
235  finalOutput = temp.string();
236 
237  if(finalOutput == m_input)
238  {
239  errorMessage = "Con not convert the raster for the same path and same extension!";
240  return false;
241  }
242  }
243  else
244  {
245  errorMessage = "For this operations you must enter with the output path (\"--output\") or the output extension (\"--extension\")!";
246  return false;
247  }
248 
249  te::rst::Raster* raster = 0;
250  if(!Utils::getRaster(m_input, raster, errorMessage))
251  return false;
252 
253  if(!copyRaster(finalOutput, bandVec, int(),errorMessage))
254  return false;
255 
256  return true;
257 }
258 
259 bool te::tools::rastermanager::RasterManager::changeResolution(std::string output, int method, int scale, std::string & errorMessage)
260 {
261 
262  // The raster file will be overwriting if output empty
263  if(output.empty())
264  output = m_input;
265  else
266  if(!Utils::checkOutputPath(output, errorMessage))
267  return false;
268 
269 
270  if(method < 1 || method > 3)
271  {
272  errorMessage = "Method invalid!";
273  return false;
274  }
275 
276  te::rst::Raster* raster = 0;
277  if(!Utils::getRaster(m_input, raster, errorMessage))
278  return false;
279 
280  std::map<std::string, std::string> newRasterInfo;
281  newRasterInfo["URI"] = output;
282 
283  raster->resample(method, scale, newRasterInfo);
284 
285  delete raster;
286 
287  return true;
288 }
289 
290 bool te::tools::rastermanager::RasterManager::trim(std::string output, std::vector<double> env, std::string & errorMessage)
291 {
292  // The raster file will be overwriting if output empty
293  if(output.empty())
294  output = m_input;
295  else
296  if(!Utils::checkOutputPath(output, errorMessage))
297  return false;
298 
299  te::rst::Raster* raster = 0;
300  if(!Utils::getRaster(m_input, raster, errorMessage))
301  return false;
302 
303  te::gm::Envelope* rasterEnv = raster->getExtent();
304 
305  te::gm::Envelope* newRasterEnv = new te::gm::Envelope(env[0], env[1], env[2], env[3]);
306 
307  std::map<std::string, std::string> outInfo;
308  outInfo["URI"] = output;
309  raster->trim(newRasterEnv, outInfo);
310 
311  delete raster;
312 
313  return true;
314 }
315 
317 {
318 
319  Utils::loadModules(errorMessage);
320 
321  std::map<std::string, std::string> capabilities;
322 
324 
327 
328  std::cout << "Supported Extensions" << std::endl;
329  std::cout << "--------------------" << std::endl << std::endl;
330 
331  while(it != itend)
332  {
333  std::cout << it->first << std::endl;
334 
335  std::map<std::string, std::string> map = static_cast<te::rst::RasterFactory*>(it->second)->getCapabilities();
336 
337  std::cout << " " << map["supported_formats"] << std::endl << std::endl;
338 
339  ++it;
340  }
341 
342  return true;
343 
344 }
345 
346 bool te::tools::rastermanager::RasterManager::getSupportedExtensions(std::vector<std::string> & extents, std::string & errorMessage)
347 {
348  std::map<std::string, std::string> capabilities;
349 
351 
354 
355  std::map<std::string, std::string> map;
356 
357  while(it != itend)
358  {
359  map = static_cast<te::rst::RasterFactory*>(it->second)->getCapabilities();
360 
361  ++it;
362  }
363 
364  te::common::Tokenize(map["supported_formats"], extents, ",");
365 
366  return true;
367 }
static bool checkOutputPath(std::string outputPath, std::string &errorMessage)
Check output raster path.
Definition: Utils.cpp:71
int m_blkh
Block height (pixels).
Definition: BandProperty.h:144
int getType() const
It returns the data type of the elements in the band.
Definition: BandProperty.h:113
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.
Definition: Raster.cpp:233
std::string toString(void) const
It returns the data value in a string notation.
Definition: Raster.cpp:302
unsigned int getNumberOfRows() const
Returns the raster number of rows.
Definition: Raster.cpp:208
Grid * getGrid()
It returns the raster grid.
Definition: Raster.cpp:94
bool copyRaster(std::string output, std::vector< int > bandVec, int bandType, std::string &errorMessage)
Copy a raster.
Terralib Raster Manager Tool Utils.
static Raster * make()
It creates and returns an empty raster with default raster driver.
int m_nblocksy
The number of blocks in y.
Definition: BandProperty.h:146
This class represents a dictionary of factories.
static bool getRaster(std::string path, te::rst::Raster *&raster, std::string &errorMessage)
Get a raster based in the path.
Definition: Utils.cpp:93
static bool loadModules(std::string &errorMessage)
Load Terralib modules.
Definition: Utils.cpp:104
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
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.
Definition: Raster.cpp:228
Terralib Raster Manager Tool.
te::gm::Envelope * getExtent()
Returns the geographic extension of the raster data.
Definition: Raster.cpp:104
A rectified grid is the spatial support for raster data.
Definition: Grid.h:55
int m_blkw
Block width (pixels).
Definition: BandProperty.h:143
static bool checkInputPath(std::string inputPath, std::string &errorMessage)
Check input raster path.
Definition: Utils.cpp:41
It contains the algorithm to reproject raster data.
bool getSupportedExtensions(std::vector< std::string > &extents, std::string &errorMessage)
Get Supported Extensions.
bool changeResolution(std::string output, int method, int scale, std::string &errorMessage)
Change Raster resolution.
static dictionary_type & getDictionary()
It returns a reference to the internal dictionary of concrete factories.
void Tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters=" ")
It tokenizes a given string with a delimiter of your own choice.
Definition: StringUtils.h:216
TERASTEREXPORT te::rst::Raster * Reproject(te::rst::Raster const *const rin, int srid, const std::map< std::string, std::string > &routinfo, int m=te::rst::Interpolator::NearestNeighbor)
Reprojects a raster to another SRS.
std::string Convert2String(boost::int16_t value)
It converts a short integer value to a string.
Definition: StringUtils.h:51
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
Definition: Raster.cpp:213
A raster band description.
Definition: BandProperty.h:61
virtual Raster * trim(const te::gm::Envelope *env, const std::map< std::string, std::string > &rinfo)
Subsetting operation for trimming (cropping) the raster.
Definition: Raster.cpp:390
This file contains include headers for the TerraLib Common Runtime module.
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.
Definition: Raster.cpp:286
bool getRasterInfo(std::ostringstream &output, std::string &errorMessage)
Show raster informations.
int m_nblocksx
The number of blocks in x.
Definition: BandProperty.h:145
An abstract class for raster data strucutures.
Definition: Raster.h:70
virtual Raster * resample(int method, unsigned int drow, unsigned int dcolumn, unsigned int height, unsigned int width, unsigned int newheight, unsigned int newwidth, const std::map< std::string, std::string > &rinfo)
Resample a subset of the raster, given a box.
Definition: Raster.cpp:482
BandProperty * getProperty()
Returns the band property.
Definition: Band.cpp:370
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
An Envelope defines a 2D rectangular region.
std::map< TFACTORYKEY, TFACTORY *, TKEYCOMPARE >::const_iterator const_iterator
bool showSupportedExtensions(std::string &errorMessage)
Show Supported Extensions.
bool reproject(std::string output, int srid, std::string &errorMessage)
Reproject a raster.
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
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.
Definition: Raster.cpp:258
std::map< TFACTORYKEY, TFACTORY *, TKEYCOMPARE >::const_iterator end() const
It returns an iterator to the end of the container.
This is the abstract factory for Rasters.
Definition: RasterFactory.h:50
bool trim(std::string output, std::vector< double > env, std::string &errorMessage)
Trim a raster.
te::da::DataSourceCapabilities capabilities
bool convert(std::string output, std::string extension, std::vector< int > bandVec, std::string &errorMessage)
Convert a raster.
bool init(std::string input, std::string &errorMessage)
Init the application.
std::map< TFACTORYKEY, TFACTORY *, TKEYCOMPARE >::const_iterator begin() const
It returns an iterator to the first stored factory.