openModeller  Version 1.5.0
TerralibRaster.cpp
Go to the documentation of this file.
1 
31 
34 #include <TeRaster.h>
35 #include <TeDatabase.h>
36 #include <TeLayer.h>
37 
39 #include <openmodeller/Log.hh>
42 
43 #include <TeProjection.h>
44 
45 #include <string>
46 using std::string;
47 
48 #include <vector>
49 using std::vector;
50 
51 #include <utility>
52 using std::pair;
53 
54 #include <stdio.h>
55 /****************************************************************/
56 /************************** Te Raster ***************************/
60 Raster*
62 {
63  return new TerralibRaster();
64 }
65 
71 // Open an existing file -- read only.
72 void
73 TerralibRaster::createRaster( const string& str, int categ )
74 {
76  te_str_parser_->str_ = str;
77  f_scalefactor = 1.0;
78  f_hdr.minmax = 0;
79 
80  // Open an existing Raster
81  openTeRaster();
82 
83  if ( raster_ )
84  {
85 
86  raster_->init();
87  params_ = &raster_->params();
88 
89  if (params_->status_ != TeRasterParams::TeReadyToRead)
90  {
91  std::string msg = "TerralibRaster::createRaster - Raster cannot be opened: ";
92  msg += raster_->errorMessage().c_str();
93  Log::instance()->error( msg.c_str() );
94  throw RasterException( msg );
95  }
96  else
97  {
98  // Number of bands (channels).
99  f_hdr.nband = raster_->nBands();
100 
101  // Projection.
102  //string teste = TeGetWKTFromTeProjection( raster_->projection() );
103  f_hdr.setProj( TeGetWKTFromTeProjection( raster_->projection() ) );
104  if ( ! f_hdr.hasProj() )
105  {
106  Log::instance()->warn( "Raster %s is not georeferenced. Assuming LatLong WGS84\n", f_file.c_str() );
108  }
109 
110  // Assumes that all bands have the same georeference
111  // characteristics, ie, the same header.
112  f_hdr.xdim = params_->ncols_;
113  f_hdr.ydim = params_->nlines_;
114 
115  f_hdr.noval = params_->dummy_[0];
116 
117  // Map limits.
118  f_hdr.xmin = (Scalar) params_->boundingBox().x1_;
119  f_hdr.xmax = (Scalar) params_->boundingBox().x2_;
120  f_hdr.ymax = (Scalar) params_->boundingBox().y2_;
121  f_hdr.ymin = (Scalar) params_->boundingBox().y1_;
122 
124 
125  f_hdr.gt[0] = params_->boundingBox().x1_; // top left x
126  f_hdr.gt[1] = params_->resx_; // w-e pixel resolution
127  f_hdr.gt[2] = 0; // rotation, 0 if image is "north up"
128  f_hdr.gt[3] = params_->boundingBox().y2_; // top left y
129  f_hdr.gt[4] = 0; // rotation, 0 if image is "north up"
130  f_hdr.gt[5] = - params_->resy_; // n-s pixel resolution
131 
132  // Minimum and maximum values.
133  f_hdr.min = params_->vmin_[0];
134  f_hdr.max = params_->vmax_[0];
135 
136  // If not zero 'min' and 'max' are valid values.
137  f_hdr.minmax = 1; //true
138 
139  // Assumes grid (in place of 'pixel').
140  // todo: how can I read this with TerraLib?
141  f_hdr.grid = 0;
142 
143  // set categorical status. It's not stored as part of
144  // the gdal file's header.
145  f_hdr.categ = categ;
146  }
147  }
148 }
149 
155 void
156 TerralibRaster::createRaster( const std::string& str, const MapFormat& format )
157 {
159  te_str_parser_->str_ = str;
160 
161  Scalar nv;
162 
163  TeDataType rtype;
164  switch( format.getFormat() )
165  {
166  case MapFormat::GreyBMP:
167  f_scalefactor = 255.0;
168  nv = 0.0;
169  rtype = TeUNSIGNEDCHAR;
170  break;
171  case MapFormat::GreyTiff:
172  f_scalefactor = 254.0;
173  nv = 255.0;
174  rtype = TeUNSIGNEDCHAR;
175  break;
177  f_scalefactor = 1.0;
178  nv = -1.0;
179  rtype = TeFLOAT;
180  break;
181  default:
182  Log::instance()->error( "Unsupported output format.\n" );
183  throw InvalidParameterException( "Unsupported output format" );
184  }
185 
186  f_hdr = Header( format.getWidth(),
187  format.getHeight(),
188  format.getXMin(),
189  format.getYMin(),
190  format.getXMax(),
191  format.getYMax(),
192  nv,
193  1,
194  0 );
195 
196  f_hdr.setProj( format.getProjection() );
197 
198  // Create a new TeRaster.
199  createTeRaster();
200 
201  if ( raster_ )
202  {
203  params_ = &raster_->params();
204 
205  params_->nBands( 1 );
206  params_->setDataType( rtype );
207  params_->setDummy(nv, 0);
208 
209  params_->boundingBoxLinesColumns(format.getXMin(), format.getYMin(),
210  format.getXMax(), format.getYMax(),
211  format.getHeight(), format.getWidth());
212 
213  // Block Size.
214  /*if(format.getHeight() > 128 || format.getWidth() > 128)
215  {
216  params_->blockHeight_ = 128;
217  params_->blockWidth_ = 128;
218  }
219  else
220  {
221  params_->blockHeight_ = format.getHeight();
222  params_->blockWidth_ = format.getWidth();
223  }*/
224  params_->blockHeight_ = 1;
225  params_->blockWidth_ = format.getWidth();
226 
227  raster_->init();
228 
229  if (params_->status_ != TeRasterParams::TeReadyToWrite)
230  {
231  Log::instance()->warn( "TerraLib raster not ready: %s.\n", raster_->errorMessage() );
232  }
233  }
234 }
235 
240 {
241  if( layer_ )
242  {
243  layer_->addRasterGeometry( raster_ );
244  }
245 
246  delete te_str_parser_;
247 }
248 
253 int
255 {
256  pair<int,int> xy = f_hdr.convertLonLat2XY( px, py );
257  int x = xy.first;
258  int y = xy.second;
259 
260  // If the point is out of range, returns 0.
261  if ( x < 0 || x >= f_hdr.xdim || y < 0 || y >= f_hdr.ydim )
262  {
263  //Log::instance()->debug( "Raster::get() Pixel (%d,%d) is not in extent\n",x,y);
264  return 0;
265  }
266 
267  if( raster_->getElement(x, y, *val , 0) )
268  return 1;
269 
270  return 0;
271 };
272 
278 int
280 {
281  pair<int,int> xy = f_hdr.convertLonLat2XY( px, py );
282  int x = xy.first;
283  int y = xy.second;
284 
285  if ( x < 0 || x >= f_hdr.xdim || y < 0 || y >= f_hdr.ydim )
286  return 0;
287 
288  if( raster_->setElement(x, y, f_scalefactor*val, 0) )
289  return 1;
290 
291  return 0;
292 }
293 
299 int
301 {
302  pair<int,int> xy = f_hdr.convertLonLat2XY( px, py );
303  int x = xy.first;
304  int y = xy.second;
305 
306  if ( x < 0 || x >= f_hdr.xdim || y < 0 || y >= f_hdr.ydim )
307  return 0;
308 
309  if( raster_->setElement(x, y, f_hdr.noval, 0) )
310  return 1;
311 
312  return 0;
313 }
314 
318 void
320 {
321  if( te_str_parser_->parse() )
322  {
324 
325  if ( !db_->isConnected() )
326  {
327  //delete db_;
328  std::string msg = "TerralibRaster::openTeRaster - Cannot connect to database: ";
329  msg += db_->errorMessage().c_str();
330  Log::instance()->error( msg.c_str() );
331  throw RasterException( msg );
332  }
333  else
334  {
335  if (db_->layerExist( te_str_parser_->layerName_ ))
336  {
337  layer_ = new TeLayer(te_str_parser_->layerName_, db_);
338  raster_ = layer_->raster();
339  }
340  }
341  }
342  else
343  {
344  // Disk file.
345  raster_ = new TeRaster( te_str_parser_->str_, 'r');
346  }
347 }
348 
352 void
354 {
355  if( te_str_parser_->parse() )
356  {
358 
359  if ( !db_->isConnected() )
360  {
361  //delete db_;
362  std::string msg = "TerralibRaster::createTeRaster - Cannot connect to database: ";
363  msg += db_->errorMessage().c_str();
364  Log::instance()->error( msg.c_str() );
365  throw RasterException( msg );
366  }
367  else
368  {
369  TeProjection* proj = TeGetTeProjectionFromWKT( f_hdr.proj );
370 
371  layer_ = new TeLayer(te_str_parser_->layerName_, db_, proj);
372 
373  // create a raster geometry in a TerraLib database
374  TeRasterParams params;
375  params.fileName_ = te_str_parser_->layerName_;
376  params.mode_ = 'c';
377  // parameters specific of the database decoder
378  params.decoderIdentifier_ = "DB"; // a database decoder
379  params.database_ = db_; // pointer to the database
380  // we do not intent to mosaic any raster data to this
381  params.tiling_type_ = TeRasterParams::TeNoExpansible;
382 
383  // the photometric interpretation of the raster
384  params.setPhotometric(TeRasterParams::TeMultiBand);
385 
386  raster_ = new TeRaster( params );
387  }
388  }
389  else
390  {
391  // Disk file.
392  raster_ = new TeRaster( te_str_parser_->str_, 'c');
393  }
394 }
395 
396 /*******************/
397 /*** get Min Max ***/
398 int
400 {
401  *min = f_hdr.min;
402 
403  *max = f_hdr.max;
404 
405  return 1;
406 }
407 
408 
409 /*********************/
410 /*** delete Raster ***/
411 int
413 {
415 
416  if ( !db_->isConnected() )
417  {
418  //delete db_;
419  std::string msg = "TerralibRaster::openTeRaster - Cannot connect to database: ";
420  msg += db_->errorMessage().c_str();
421  Log::instance()->error( msg.c_str() );
422  throw RasterException( msg );
423  return 0;
424  }
425  else
426  {
427  if ( db_->layerExist( layer_->name() ) )
428  {
429  db_->deleteLayer( layer_->id() );
430  }
431 
432  if( raster_ )
433  {
434  delete raster_;
435  }
436 
437  if( params_ )
438  {
439  delete params_;
440  }
441 
442  if( te_str_parser_ )
443  {
444  delete te_str_parser_;
445  }
446 
447  return 1;
448  }
449 
450  // If raster is a file disk.
451  if( raster_ )
452  {
453  delete raster_;
454  }
455 
456  if( params_ )
457  {
458  delete params_;
459  }
460 
461  if( te_str_parser_ )
462  {
463  delete te_str_parser_;
464  }
465 
466  return 1;
467 }
int categ
Definition: Header.hh:93
void warn(const char *format,...)
'Warn' level.
Definition: Log.cpp:273
Coord getYMin() const
Definition: MapFormat.cpp:305
std::string getProjection() const
Definition: MapFormat.cpp:361
int xdim
Definition: Header.hh:72
double Scalar
Type of map values.
Definition: om_defs.hh:39
void calculateCell()
Definition: Header.cpp:130
int ydim
Definition: Header.hh:73
int nband
Definition: Header.hh:83
Coord xmin
Definition: Header.hh:74
Coord xmax
Definition: Header.hh:76
static Raster * CreateRasterCallback()
Return a new instance of TerralibRaster.
static Log * instance()
Returns the instance pointer, creating the object on the first call.
Definition: Log.cpp:45
int hasProj() const
Definition: Header.hh:65
A common interface to rasters.
Definition: Raster.hh:44
TeRasterParams * params_
std::pair< int, int > convertLonLat2XY(Coord lon, Coord lat) const
Definition: Header.cpp:160
Coord getXMax() const
Definition: MapFormat.cpp:319
Scalar noval
Definition: Header.hh:82
Definition: Header.hh:45
void error(const char *format,...)
'Error' level.
Definition: Log.cpp:290
std::string proj
Definition: Header.hh:104
Coord gt[6]
Definition: Header.hh:80
Coord ymin
Definition: Header.hh:75
int getFormat() const
Definition: MapFormat.hh:97
Coord getYMax() const
Definition: MapFormat.cpp:333
Scalar f_scalefactor
Definition: Raster.hh:184
int get(Coord x, Coord y, Scalar *val)
Fills '*val' with the map value at (x,y).
int grid
Definition: Header.hh:86
Coord ymax
Definition: Header.hh:77
static char const * getDefaultCS()
TeDatabase * db_
Pointer to database connection.
Coord getXMin() const
Definition: MapFormat.cpp:291
bool parse()
Parser the url.
TeDatabase * create(const TeDatabaseFactoryParams &params)
Return a TeDatabase pointer if it exists or create a new one.
Header f_hdr
Definition: Raster.hh:192
void createTeRaster()
Create a new TeRaster in a database or file disk.
int put(Coord x, Coord y, Scalar val)
Put '*val' at the (x,y) coordinate.
void createRaster(const std::string &str, int categ=0)
Open an existing raster file or a raster in a TerraLib database (read only).
static TeDatabaseManager & instance()
Singleton pattern.
int getWidth() const
Definition: MapFormat.cpp:245
int getHeight() const
Definition: MapFormat.cpp:256
string str_
Terralib string.
void openTeRaster()
Open an existing TeRaster in a database or file disk.
int minmax
Definition: Header.hh:99
int getMinMax(Scalar *min, Scalar *max)
TerralibRaster()
Empty Constructor.
TeRaster * raster_
TeStringParser * te_str_parser_
TerraLib DataBase string parser.
double Coord
Type of map coordinates.
Definition: om_defs.hh:38
string layerName_
Layer in Database.
void setProj(const std::string &projection)
Definition: Header.cpp:142
int min(int v1, int v2)
Definition: rules_base.cpp:56
std::string f_file
Definition: Raster.hh:186