All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
FeedersRaster.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 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/rp/FeedersRaster.cpp
22  \brief Raster objects feeders.
23 */
24 
25 #include "FeedersRaster.h"
26 #include "Macros.h"
27 #include "../raster/RasterFactory.h"
28 #include "../raster/Grid.h"
29 #include "../geometry/LinearRing.h"
30 #include "../common/Exception.h"
31 
32 #include <boost/filesystem.hpp>
33 
34 namespace te
35 {
36  namespace rp
37  {
38  // -----------------------------------------------------------------------
39 
41  const std::vector< const te::rst::Raster* > rasters )
42  : m_currentOffset( 0 ), m_rasters( rasters )
43  {
44  }
45 
47  : m_currentOffset( 0 )
48  {
49  }
50 
52  {
53  }
54 
56  {
57  return ( m_currentOffset < m_rasters.size() ) ?
58  m_rasters[ m_currentOffset ] : 0;
59  }
60 
62  {
63  if( m_currentOffset == m_rasters.size() )
64  {
65  return false;
66  }
67  else
68  {
70  return ( m_currentOffset != m_rasters.size() );
71  }
72  }
73 
74  bool FeederConstRasterVector::moveTo( const unsigned int index )
75  {
76  if( index >= m_rasters.size() )
77  {
78  return false;
79  }
80  else
81  {
82  m_currentOffset = index;
83  return true;
84  }
85  }
86 
88  {
89  m_currentOffset = 0;
90  }
91 
93  {
94  return (unsigned int) m_rasters.size();
95  }
96 
98  {
99  return m_currentOffset;
100  }
101 
102  // -----------------------------------------------------------------------
103 
105  const std::vector< std::string >& rTypes,
106  const std::vector< std::map< std::string, std::string > >& rInfos )
107  : m_currentOffset( 0 ), m_rTypes( rTypes ), m_rInfos( rInfos )
108  {
109  TERP_TRUE_OR_THROW( rTypes.size() == rInfos.size(),
110  "Invalid rasters info" )
111  reset();
112  }
113 
115  : m_currentOffset( 0 )
116  {
117  }
118 
120  {
121  }
122 
124  {
125  return m_currentRasterPtr.get();
126  }
127 
129  {
130  if( m_currentOffset == m_rTypes.size() )
131  {
132  return false;
133  }
134  else
135  {
136  ++m_currentOffset;
137 
138  if( m_currentOffset == m_rTypes.size() )
139  {
140  m_currentRasterPtr.reset();
141  return false;
142  }
143  else
144  {
146  m_rTypes[ m_currentOffset ], m_rInfos[ m_currentOffset ],
147  te::common::RAccess ) );
148 
149  if( m_currentRasterPtr.get() )
150  {
151  return true;
152  }
153  else
154  {
155  return false;
156  }
157  }
158  }
159  }
160 
161  bool FeederConstRasterInfo::moveTo( const unsigned int index )
162  {
163  if( index >= m_rInfos.size() )
164  {
165  return false;
166  }
167  else if( index == m_currentOffset )
168  {
169  return true;
170  }
171  else
172  {
173  m_currentOffset = index;
174 
176  m_rTypes[ m_currentOffset ], m_rInfos[ m_currentOffset ],
177  te::common::RAccess ) );
178 
179  if( m_currentRasterPtr.get() )
180  {
181  return true;
182  }
183  else
184  {
185  return false;
186  }
187  }
188  }
189 
191  {
192  m_currentRasterPtr.reset();
193 
194  if( ! m_rTypes.empty() )
195  {
197  m_rInfos[ 0 ], te::common::RAccess ) );
198 
199  if( m_currentRasterPtr.get() )
200  {
201  m_currentOffset = 0;
202  }
203  else
204  {
205  m_currentOffset = m_rTypes.size();
206  }
207  }
208  }
209 
211  {
212  return (unsigned int) m_rTypes.size();
213  }
214 
216  {
217  return m_currentOffset;
218  }
219 
220  // -----------------------------------------------------------------------
221 
223  const std::string& directoryName,
224  const bool recursive,
225  const std::string& rType,
226  const bool sortFileNames,
227  const std::vector< std::string >& fileExtensions )
228  {
229  TERP_TRUE_OR_THROW( initialize( directoryName, recursive, rType,
230  sortFileNames, fileExtensions, 0, false ), "Feeder initialization error" );
231  }
232 
234  const std::string& directoryName,
235  const bool recursive,
236  const std::string& rType,
237  const bool sortFileNames,
238  const std::vector< std::string >& fileExtensions,
239  te::gm::Geometry const * const restrictionGeomPtr,
240  const bool ignoreInvalidRasterFiles )
241  {
242  TERP_TRUE_OR_THROW( initialize( directoryName, recursive, rType,
243  sortFileNames, fileExtensions, restrictionGeomPtr, ignoreInvalidRasterFiles ),
244  "Feeder initialization error" );
245  }
246 
248  : m_selectedRasterIndexesOffset( 0 )
249  {
250  }
251 
253  {
254  }
255 
257  {
259  {
261  }
262  else
263  {
264  static std::string emptyStr;
265  return emptyStr;
266  }
267  }
268 
270  {
271  return m_currentRasterPtr.get();
272  }
273 
275  {
276  m_currentRasterPtr.reset();
277 
278  std::map< std::string, std::string > mInfo;
279 
280  std::size_t nextSelectedRasterIndexesOffset = m_selectedRasterIndexesOffset + 1;
281 
282  while( nextSelectedRasterIndexesOffset < m_selectedRastersIndexes.size() )
283  {
285  nextSelectedRasterIndexesOffset ] ];
286 
287  try
288  {
290  m_rType, mInfo,
291  te::common::RAccess ) );
292  }
293  catch( const te::common::Exception& excep )
294  {
296  {
297  m_selectedRasterIndexesOffset = m_selectedRastersIndexes.size();
298  m_currentRasterPtr.reset();
299  TERP_LOG_AND_RETURN_FALSE( "Error:" + std::string( excep.what() ) );
300  }
301  }
302 
303  if( m_currentRasterPtr.get() )
304  {
305  break;
306  }
307 
308  ++nextSelectedRasterIndexesOffset;
309  }
310 
311  m_selectedRasterIndexesOffset = nextSelectedRasterIndexesOffset;
312 
313  if( m_currentRasterPtr.get() )
314  {
315  return true;
316  }
317  else
318  {
319  return false;
320  }
321  }
322 
323  bool FeederConstRasterDirectory::moveTo( const unsigned int index )
324  {
325  if( index >= m_selectedRastersIndexes.size() )
326  {
327  m_currentRasterPtr.reset();
328  return false;
329  }
330  else
331  {
332  std::map< std::string, std::string > mInfo;
334  index ] ];
335 
336  try
337  {
339  m_rType, mInfo,
340  te::common::RAccess ) );
341  }
342  catch( const te::common::Exception& excep )
343  {
344  TERP_LOGWARN( "Error:" + std::string( excep.what() ) );
345  m_currentRasterPtr.reset();
346  }
347 
348 
349  if( m_currentRasterPtr.get() )
350  {
352  return true;
353  }
354  else
355  {
356  return false;
357  }
358  }
359  }
360 
362  {
363  m_currentRasterPtr.reset();
364 
365  std::map< std::string, std::string > mInfo;
366  std::size_t nextSelectedRasterIndexesOffset = 0;
367 
368  while( nextSelectedRasterIndexesOffset < m_selectedRastersIndexes.size() )
369  {
371  nextSelectedRasterIndexesOffset ] ];
372 
373  try
374  {
376  mInfo, te::common::RAccess ) );
377  }
378  catch( const te::common::Exception& excep )
379  {
381  {
382  TERP_LOGWARN( "Error:" + std::string( excep.what() ) );
383  m_currentRasterPtr.reset();
384  m_selectedRasterIndexesOffset = m_selectedRastersIndexes.size();
385  return;
386  }
387  }
388 
389  if( m_currentRasterPtr.get() )
390  {
391  break;
392  }
393 
394  ++nextSelectedRasterIndexesOffset;
395  }
396 
397  m_selectedRasterIndexesOffset = nextSelectedRasterIndexesOffset;
398  }
399 
401  {
402  return (unsigned int) m_selectedRastersIndexes.size();
403  }
404 
406  {
408  }
409 
411  {
412  std::auto_ptr< te::dt::AbstractData > cloneResult( restrictionGeom.clone() );
413  TERP_TRUE_OR_THROW( dynamic_cast< te::gm::Geometry* >( cloneResult.get() ),
414  "Invalid geometry" );
415 
416  m_restrictionGeomPtr.reset( (te::gm::Geometry*)cloneResult.release() );
417 
418  // reading all bounding boxes
419 
420  if( m_allRastersBoundingBoxes.size() != m_allRasterFileNames.size() )
421  {
423 
424  te::gm::Polygon auxPolygon( 0, te::gm::PolygonType, 0 );
425  te::gm::LinearRing* auxLinearRingPtr = 0;
426  std::map< std::string, std::string > mInfo;
427  std::auto_ptr< te::rst::Raster > rasterPtr;
428  te::gm::Envelope const* rasterExtPtr = 0;
429 
430  for( unsigned int allRasterFileNamesIdx = 0 ; allRasterFileNamesIdx <
431  m_allRasterFileNames.size() ; ++allRasterFileNamesIdx )
432  {
433  mInfo[ "URI" ] = m_allRasterFileNames[ allRasterFileNamesIdx ];
434 
435  try
436  {
437  rasterPtr.reset( te::rst::RasterFactory::open( m_rType,
438  mInfo, te::common::RAccess ) );
439  TERP_TRUE_OR_THROW( rasterPtr.get(), "Invalid raster" );
440  }
441  catch( const te::common::Exception& excep )
442  {
443  rasterPtr.reset();
444 
446  {
447  TERP_LOGWARN( "Error:" + std::string( excep.what() ) );
448  }
449  else
450  {
451  TERP_LOG_AND_THROW( "Error:" + std::string( excep.what() ) );
452  }
453  }
454 
455  if( rasterPtr.get() )
456  {
457  rasterExtPtr = rasterPtr->getGrid()->getExtent();
458 
459  auxLinearRingPtr = new te::gm::LinearRing(5, te::gm::LineStringType);
460  auxLinearRingPtr->setPoint( 0, rasterExtPtr->m_llx,
461  rasterExtPtr->m_ury );
462  auxLinearRingPtr->setPoint( 1, rasterExtPtr->m_urx,
463  rasterExtPtr->m_ury );
464  auxLinearRingPtr->setPoint( 2, rasterExtPtr->m_urx,
465  rasterExtPtr->m_lly );
466  auxLinearRingPtr->setPoint( 3, rasterExtPtr->m_llx,
467  rasterExtPtr->m_lly );
468  auxLinearRingPtr->setPoint( 4, rasterExtPtr->m_llx,
469  rasterExtPtr->m_ury );
470  auxLinearRingPtr->setSRID( rasterPtr->getSRID() );
471 
472  auxPolygon.clear();
473  auxPolygon.push_back( auxLinearRingPtr );
474 
475  auxPolygon.setSRID( rasterPtr->getSRID() );
476 
477  m_allRastersBoundingBoxes.push_back( auxPolygon );
478  }
479  }
480  }
481 
482  // Selecting the intersected bounding boxes
483 
484  m_selectedRastersIndexes.clear();
485 
486  for( unsigned int allRastersBoundingBoxesIdx = 0 ; allRastersBoundingBoxesIdx <
487  m_allRastersBoundingBoxes.size() ; ++allRastersBoundingBoxesIdx )
488  {
490  allRastersBoundingBoxesIdx ].getSRID() )
491  {
493  allRastersBoundingBoxesIdx ].getSRID() );
494  }
495 
497  allRastersBoundingBoxesIdx ] ) ) )
498  {
499  m_selectedRastersIndexes.push_back( allRastersBoundingBoxesIdx );
500  }
501  }
502 
504 
505  reset();
506  }
507 
509  const std::string& directoryName,
510  const bool recursive,
511  const std::string& rType,
512  const bool sortFileNames,
513  const std::vector< std::string >& fileExtensions,
514  te::gm::Geometry const * const restrictionGeomPtr,
515  const bool ignoreInvalidRasterFiles )
516  {
517  m_ignoreInvalidRasterFiles = ignoreInvalidRasterFiles;
518 
519  // updating m_allRasterFileNames
520 
521  {
522  m_allRasterFileNames.clear();
523 
524  m_rType = rType;
525  const std::vector< std::string >::size_type fileExtensionsSize =
526  fileExtensions.size();
527  std::vector< std::string >::size_type fileExtensionsIdx = 0;
528 
529  boost::filesystem::path directoryPath( directoryName );
530  if( boost::filesystem::is_directory( directoryName ) )
531  {
532  if( recursive )
533  {
534  boost::filesystem::recursive_directory_iterator dirIt( directoryPath );
535  const boost::filesystem::recursive_directory_iterator dirItE;
536  std::string auxString;
537 
538  while( dirIt != dirItE )
539  {
540  if( boost::filesystem::is_regular_file( *dirIt ) )
541  {
542  if( fileExtensionsSize )
543  {
544  for( fileExtensionsIdx = 0 ; fileExtensionsIdx < fileExtensionsSize ;
545  ++fileExtensionsIdx )
546  {
547  if( dirIt->path().extension().generic_string()
548  == fileExtensions[ fileExtensionsIdx ] )
549  {
550  m_allRasterFileNames.push_back( dirIt->path().generic_string() );
551  }
552  }
553  }
554  else
555  {
556  m_allRasterFileNames.push_back( dirIt->path().generic_string() );
557  }
558  }
559 
560  ++dirIt;
561  }
562  }
563  else
564  {
565  boost::filesystem::directory_iterator dirIt( directoryPath );
566  const boost::filesystem::directory_iterator dirItE;
567 
568  while( dirIt != dirItE )
569  {
570  if( boost::filesystem::is_regular_file( *dirIt ) )
571  {
572  if( fileExtensionsSize )
573  {
574  for( fileExtensionsIdx = 0 ; fileExtensionsIdx < fileExtensionsSize ;
575  ++fileExtensionsIdx )
576  {
577  if( dirIt->path().extension().generic_string()
578  == fileExtensions[ fileExtensionsIdx ] )
579  {
580  m_allRasterFileNames.push_back( dirIt->path().generic_string() );
581  }
582  }
583  }
584  else
585  {
586  m_allRasterFileNames.push_back( dirIt->path().generic_string() );
587  }
588  }
589 
590  ++dirIt;
591  }
592  }
593  }
594 
595  if( sortFileNames ) std::sort( m_allRasterFileNames.begin(), m_allRasterFileNames.end() );
596  }
597 
598  // updating m_selectedRastersIndexes
599 
600  {
602 
603  for( std::vector< std::string >::size_type allRasterFileNamesIdx = 0 ;
604  allRasterFileNamesIdx < m_allRasterFileNames.size() ;
605  ++allRasterFileNamesIdx )
606  {
607  m_selectedRastersIndexes[ allRasterFileNamesIdx ] = allRasterFileNamesIdx;
608  }
609  }
610 
612 
613  // applying the restriction geometry
614 
615  if( restrictionGeomPtr )
616  {
617  applyGeometryRestriction( *restrictionGeomPtr );
618  }
619  else
620  {
621  reset();
622  }
623 
624  return true;
625  }
626 
627  } // end namespace rp
628 } // end namespace te
629 
bool moveTo(const unsigned int index)
Jump to the given object index.
void push_back(Curve *ring)
It adds the curve to the curve polygon.
Definition: CurvePolygon.h:279
te::rst::Raster const * getCurrentObj() const
Return the current sequence object.
bool moveNext()
Advances to the next sequence obeject.
std::vector< te::gm::Polygon > m_allRastersBoundingBoxes
bool moveTo(const unsigned int index)
Jump to the given object index.
unsigned int getCurrentOffset() const
Return the index of the current object.
void setSRID(int srid)
It sets the Spatial Reference System ID of the geometry and all its parts if it is a GeometryCollecti...
unsigned int getCurrentOffset() const
Return the index of the current object.
bool moveNext()
Advances to the next sequence obeject.
void reset()
Reset the feeder to the first position (subsequent accesses will start from the first sequence obejct...
virtual const char * what() const
It outputs the exception message.
Definition: Exception.cpp:58
#define TERP_LOGWARN(message)
Logs a warning message.
Definition: Macros.h:127
unsigned int getObjsCount() const
Return the total number of feeder objects.
unsigned int getObjsCount() const
Return the total number of feeder objects.
double m_urx
Upper right corner x-coordinate.
Definition: Envelope.h:346
std::auto_ptr< te::gm::Geometry > m_restrictionGeomPtr
std::vector< const te::rst::Raster * >::size_type m_currentOffset
Definition: FeedersRaster.h:93
std::vector< unsigned int >::size_type m_selectedRasterIndexesOffset
std::vector< const te::rst::Raster * > m_rasters
Definition: FeedersRaster.h:94
void applyGeometryRestriction(const te::gm::Geometry &restrictionGeom)
Apply a new geometry intersect restriction selecting only those rasters intersecting the given geomet...
te::rst::Raster const * getCurrentObj() const
Return the current sequence object.
A LinearRing is a LineString that is both closed and simple.
Definition: LinearRing.h:53
double m_llx
Lower left corner x-coordinate.
Definition: Envelope.h:344
void setPoint(std::size_t i, const double &x, const double &y)
It sets the value of the specified point.
Definition: LineString.cpp:353
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
An abstract class for raster data strucutures.
Definition: Raster.h:71
#define TERP_LOG_AND_THROW(message)
Logs a error message and throws.
Definition: Macros.h:138
std::auto_ptr< te::rst::Raster > m_currentRasterPtr
te::rst::Raster const * getCurrentObj() const
Return the current sequence object.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Definition: Exception.h:58
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
Definition: Geometry.h:73
double m_lly
Lower left corner y-coordinate.
Definition: Envelope.h:345
unsigned int getObjsCount() const
Return the total number of feeder objects.
bool moveTo(const unsigned int index)
Jump to the given object index.
#define TERP_LOG_AND_RETURN_FALSE(message)
Logs a warning message will and return false.
Definition: Macros.h:236
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
Definition: Polygon.h:50
bool moveNext()
Advances to the next sequence obeject.
double m_ury
Upper right corner y-coordinate.
Definition: Envelope.h:347
void clear()
It deletes all the rings of the CurvePolygon and clear it.
void setSRID(int srid)
It sets the Spatial Reference System ID of the linestring.
Definition: LineString.cpp:176
void reset()
Reset the feeder to the first position (subsequent accesses will start from the first sequence obejct...
void reset()
Reset the feeder to the first position (subsequent accesses will start from the first sequence obejct...
unsigned int getCurrentOffset() const
Return the index of the current object.
std::vector< std::map< std::string, std::string > > m_rInfos
Raster objects feeders.
bool initialize(const std::string &directoryName, const bool recursive, const std::string &rType, const bool sortFileNames, const std::vector< std::string > &fileExtensions, te::gm::Geometry const *const restrictionGeomPtr, const bool ignoreInvalidRasterFiles)
Initialize this instance.
virtual AbstractData * clone() const =0
It returns a clone of this object.
std::vector< unsigned int > m_selectedRastersIndexes
std::vector< std::string >::size_type m_currentOffset
#define TERP_TRUE_OR_THROW(value, message)
Checks if value is true and throws an exception if not.
Definition: Macros.h:149
std::auto_ptr< te::rst::Raster > m_currentRasterPtr
bool m_ignoreInvalidRasterFiles
If true, invalid raster files will be ignored.
const std::string & getCurrentRasterFileName()
Returns the current raster file name.
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.
std::vector< std::string > m_rTypes
std::vector< std::string > m_allRasterFileNames