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 // TerraLib
26 #include "FeedersRaster.h"
27 #include "Macros.h"
28 #include "../core/filesystem/FileSystem.h"
29 #include "../raster/RasterFactory.h"
30 #include "../raster/Grid.h"
31 #include "../geometry/LinearRing.h"
32 #include "../common/Exception.h"
33 
34 //Boost
35 #include <boost/filesystem.hpp>
36 
37 namespace te
38 {
39  namespace rp
40  {
41  // -----------------------------------------------------------------------
42 
44  const std::vector< const te::rst::Raster* > rasters )
45  : m_currentOffset( 0 ), m_rasters( rasters )
46  {
47  }
48 
50  : m_currentOffset( 0 )
51  {
52  }
53 
55 
57  {
58  return ( m_currentOffset < m_rasters.size() ) ?
59  m_rasters[ m_currentOffset ] : nullptr;
60  }
61 
63  {
64  if( m_currentOffset == m_rasters.size() )
65  {
66  return false;
67  }
68  else
69  {
71  return ( m_currentOffset != m_rasters.size() );
72  }
73  }
74 
75  bool FeederConstRasterVector::moveTo( const unsigned int index )
76  {
77  if( index >= m_rasters.size() )
78  {
79  return false;
80  }
81  else
82  {
83  m_currentOffset = index;
84  return true;
85  }
86  }
87 
89  {
90  m_currentOffset = 0;
91  }
92 
94  {
95  return (unsigned int) m_rasters.size();
96  }
97 
99  {
100  return static_cast<unsigned int>(m_currentOffset);
101  }
102 
103  // -----------------------------------------------------------------------
104 
106  const std::vector< std::string >& rTypes,
107  const std::vector< std::map< std::string, std::string > >& rInfos )
108  : m_currentOffset( 0 ), m_rTypes( rTypes ), m_rInfos( rInfos )
109  {
110  TERP_TRUE_OR_THROW( rTypes.size() == rInfos.size(),
111  "Invalid rasters info" )
112  reset();
113  }
114 
116  : m_currentOffset( 0 )
117  {
118  }
119 
121 
123  {
124  return m_currentRasterPtr.get();
125  }
126 
128  {
129  if( m_currentOffset == m_rTypes.size() )
130  {
131  return false;
132  }
133  else
134  {
135  ++m_currentOffset;
136 
137  if( m_currentOffset == m_rTypes.size() )
138  {
139  m_currentRasterPtr.reset();
140  return false;
141  }
142  else
143  {
145  m_rTypes[ m_currentOffset ], m_rInfos[ m_currentOffset ],
146  te::common::RAccess ) );
147 
148  if( m_currentRasterPtr.get() )
149  {
150  return true;
151  }
152  else
153  {
154  return false;
155  }
156  }
157  }
158  }
159 
160  bool FeederConstRasterInfo::moveTo( const unsigned int index )
161  {
162  if( index >= m_rInfos.size() )
163  {
164  return false;
165  }
166  else if( index == m_currentOffset )
167  {
168  return true;
169  }
170  else
171  {
172  m_currentOffset = index;
173 
175  m_rTypes[ m_currentOffset ], m_rInfos[ m_currentOffset ],
176  te::common::RAccess ) );
177 
178  if( m_currentRasterPtr.get() )
179  {
180  return true;
181  }
182  else
183  {
184  return false;
185  }
186  }
187  }
188 
190  {
191  m_currentRasterPtr.reset();
192 
193  if( ! m_rTypes.empty() )
194  {
196  m_rInfos[ 0 ], te::common::RAccess ) );
197 
198  if( m_currentRasterPtr.get() )
199  {
200  m_currentOffset = 0;
201  }
202  else
203  {
204  m_currentOffset = m_rTypes.size();
205  }
206  }
207  }
208 
210  {
211  return (unsigned int) m_rTypes.size();
212  }
213 
215  {
216  return static_cast<unsigned int>(m_currentOffset);
217  }
218 
219  // -----------------------------------------------------------------------
220 
222  const std::vector< const te::rst::Raster* > rastersPtrsVec,
223  const std::vector< std::string >& rTypes,
224  const std::vector< std::map< std::string, std::string > >& rInfos )
225  :
226  m_CurrentRasterOffset( 0 ),
227  m_currentRasterNakedPtr( nullptr ),
228  m_rasters( rastersPtrsVec ),
229  m_rTypes( rTypes ),
230  m_rInfos( rInfos )
231  {
232  TERP_TRUE_OR_THROW( rTypes.size() == rInfos.size(),
233  "Invalid rasters info" )
234  reset();
235  }
236 
238  :
239  m_CurrentRasterOffset( 0 ),
240  m_currentRasterNakedPtr( nullptr )
241  {
242  }
243 
245 
247  {
249  }
250 
252  {
253  return moveTo(static_cast<unsigned int>(m_CurrentRasterOffset) + 1);
254  }
255 
256  bool FeederConstRasterInfoAndVector::moveTo( const unsigned int index )
257  {
258  if( index < m_rasters.size() )
259  {
260  m_CurrentRasterOffset = index;
262  m_currentRasterHandler.reset();
263  return true;
264  }
265  else
266  {
267  if( ( index - m_rasters.size() ) < m_rTypes.size() )
268  {
269  try
270  {
272  m_rTypes[ index - m_rasters.size() ], m_rInfos[ index - m_rasters.size() ],
273  te::common::RAccess ) );
274  }
275  catch(...)
276  {
277  return false;
278  }
279 
280  m_CurrentRasterOffset = index;
282  return true;
283  }
284  else
285  {
287  m_currentRasterNakedPtr = nullptr;
288  m_currentRasterHandler.reset();
289  return false;
290  }
291  }
292  }
293 
295  {
296  m_currentRasterHandler.reset();
298  m_currentRasterNakedPtr = nullptr;
299 
300  moveTo( 0 );
301  }
302 
304  {
305  return (unsigned int)( m_rasters.size() + m_rTypes.size() );
306  }
307 
309  {
310  return static_cast<unsigned int>(m_CurrentRasterOffset);
311  }
312 
313  // -----------------------------------------------------------------------
314 
316  const std::string& directoryName,
317  const bool recursive,
318  const std::string& rType,
319  const bool sortFileNames,
320  const std::vector< std::string >& fileExtensions )
321  {
322  TERP_TRUE_OR_THROW( initialize( directoryName, recursive, rType,
323  sortFileNames, fileExtensions, nullptr, false ), "Feeder initialization error" );
324  }
325 
327  const std::string& directoryName,
328  const bool recursive,
329  const std::string& rType,
330  const bool sortFileNames,
331  const std::vector< std::string >& fileExtensions,
332  te::gm::Geometry const * const restrictionGeomPtr,
333  const bool ignoreInvalidRasterFiles )
334  {
335  TERP_TRUE_OR_THROW( initialize( directoryName, recursive, rType,
336  sortFileNames, fileExtensions, restrictionGeomPtr, ignoreInvalidRasterFiles ),
337  "Feeder initialization error" );
338  }
339 
341  : m_selectedRasterIndexesOffset( 0 )
342  {
343  }
344 
346 
348  {
350  {
352  }
353  else
354  {
355  static std::string emptyStr;
356  return emptyStr;
357  }
358  }
359 
361  {
362  return m_currentRasterPtr.get();
363  }
364 
366  {
367  m_currentRasterPtr.reset();
368 
369  std::map< std::string, std::string > mInfo;
370 
371  std::size_t nextSelectedRasterIndexesOffset = m_selectedRasterIndexesOffset + 1;
372 
373  while( nextSelectedRasterIndexesOffset < m_selectedRastersIndexes.size() )
374  {
376  nextSelectedRasterIndexesOffset ] ];
377 
378  try
379  {
381  m_rType, mInfo,
382  te::common::RAccess ) );
383  }
384  catch( const te::common::Exception& excep )
385  {
387  {
388  m_selectedRasterIndexesOffset = m_selectedRastersIndexes.size();
389  m_currentRasterPtr.reset();
390  TERP_LOG_AND_RETURN_FALSE( "Error:" + std::string( excep.what() ) );
391  }
392  }
393 
394  if( m_currentRasterPtr.get() )
395  {
396  break;
397  }
398 
399  ++nextSelectedRasterIndexesOffset;
400  }
401 
402  m_selectedRasterIndexesOffset = nextSelectedRasterIndexesOffset;
403 
404  if( m_currentRasterPtr.get() )
405  {
406  return true;
407  }
408  else
409  {
410  return false;
411  }
412  }
413 
414  bool FeederConstRasterDirectory::moveTo( const unsigned int index )
415  {
416  if( index >= m_selectedRastersIndexes.size() )
417  {
418  m_currentRasterPtr.reset();
419  return false;
420  }
421  else
422  {
423  std::map< std::string, std::string > mInfo;
425  index ] ];
426 
427  try
428  {
430  m_rType, mInfo,
431  te::common::RAccess ) );
432  }
433  catch( const te::common::Exception& excep )
434  {
435  TERP_LOGWARN( "Error:" + std::string( excep.what() ) );
436  m_currentRasterPtr.reset();
437  }
438 
439 
440  if( m_currentRasterPtr.get() )
441  {
443  return true;
444  }
445  else
446  {
447  return false;
448  }
449  }
450  }
451 
453  {
454  m_currentRasterPtr.reset();
455 
456  std::map< std::string, std::string > mInfo;
457  std::size_t nextSelectedRasterIndexesOffset = 0;
458 
459  while( nextSelectedRasterIndexesOffset < m_selectedRastersIndexes.size() )
460  {
462  nextSelectedRasterIndexesOffset ] ];
463 
464  try
465  {
467  mInfo, te::common::RAccess ) );
468  }
469  catch( const te::common::Exception& excep )
470  {
472  {
473  TERP_LOGWARN( "Error:" + std::string( excep.what() ) );
474  m_currentRasterPtr.reset();
475  m_selectedRasterIndexesOffset = m_selectedRastersIndexes.size();
476  return;
477  }
478  }
479 
480  if( m_currentRasterPtr.get() )
481  {
482  break;
483  }
484 
485  ++nextSelectedRasterIndexesOffset;
486  }
487 
488  m_selectedRasterIndexesOffset = nextSelectedRasterIndexesOffset;
489  }
490 
492  {
493  return (unsigned int) m_selectedRastersIndexes.size();
494  }
495 
497  {
498  return static_cast<unsigned int>(m_selectedRasterIndexesOffset);
499  }
500 
502  {
503  std::unique_ptr< te::dt::AbstractData > cloneResult( restrictionGeom.clone() );
504  TERP_TRUE_OR_THROW( dynamic_cast< te::gm::Geometry* >( cloneResult.get() ),
505  "Invalid geometry" );
506 
507  m_restrictionGeomPtr.reset( (te::gm::Geometry*)cloneResult.release() );
508 
509  // reading all bounding boxes
510 
511  if( m_allRastersBoundingBoxes.size() != m_allRasterFileNames.size() )
512  {
514 
515  te::gm::Polygon auxPolygon( 0, te::gm::PolygonType, 0 );
516  te::gm::LinearRing* auxLinearRingPtr = nullptr;
517  std::map< std::string, std::string > mInfo;
518  std::unique_ptr< te::rst::Raster > rasterPtr;
519  te::gm::Envelope const* rasterExtPtr = nullptr;
520 
521  for( unsigned int allRasterFileNamesIdx = 0 ; allRasterFileNamesIdx <
522  m_allRasterFileNames.size() ; ++allRasterFileNamesIdx )
523  {
524  mInfo[ "URI" ] = m_allRasterFileNames[ allRasterFileNamesIdx ];
525 
526  try
527  {
528  rasterPtr.reset( te::rst::RasterFactory::open( m_rType,
529  mInfo, te::common::RAccess ) );
530  TERP_TRUE_OR_THROW( rasterPtr.get(), "Invalid raster" );
531  }
532  catch( const te::common::Exception& excep )
533  {
534  rasterPtr.reset();
535 
537  {
538  TERP_LOGWARN( "Error:" + std::string( excep.what() ) );
539  }
540  else
541  {
542  TERP_LOG_AND_THROW( "Error:" + std::string( excep.what() ) );
543  }
544  }
545 
546  if( rasterPtr.get() )
547  {
548  rasterExtPtr = rasterPtr->getGrid()->getExtent();
549 
550  auxLinearRingPtr = new te::gm::LinearRing(5, te::gm::LineStringType);
551  auxLinearRingPtr->setPoint( 0, rasterExtPtr->m_llx,
552  rasterExtPtr->m_ury );
553  auxLinearRingPtr->setPoint( 1, rasterExtPtr->m_urx,
554  rasterExtPtr->m_ury );
555  auxLinearRingPtr->setPoint( 2, rasterExtPtr->m_urx,
556  rasterExtPtr->m_lly );
557  auxLinearRingPtr->setPoint( 3, rasterExtPtr->m_llx,
558  rasterExtPtr->m_lly );
559  auxLinearRingPtr->setPoint( 4, rasterExtPtr->m_llx,
560  rasterExtPtr->m_ury );
561  auxLinearRingPtr->setSRID( rasterPtr->getSRID() );
562 
563  auxPolygon.clear();
564  auxPolygon.push_back( auxLinearRingPtr );
565 
566  auxPolygon.setSRID( rasterPtr->getSRID() );
567 
568  m_allRastersBoundingBoxes.push_back( auxPolygon );
569  }
570  }
571  }
572 
573  // Selecting the intersected bounding boxes
574 
575  m_selectedRastersIndexes.clear();
576 
577  for( unsigned int allRastersBoundingBoxesIdx = 0 ; allRastersBoundingBoxesIdx <
578  m_allRastersBoundingBoxes.size() ; ++allRastersBoundingBoxesIdx )
579  {
581  allRastersBoundingBoxesIdx ].getSRID() )
582  {
584  allRastersBoundingBoxesIdx ].getSRID() );
585  }
586 
588  allRastersBoundingBoxesIdx ] ) ) )
589  {
590  m_selectedRastersIndexes.push_back( allRastersBoundingBoxesIdx );
591  }
592  }
593 
595 
596  reset();
597  }
598 
600  const std::string& directoryName,
601  const bool recursive,
602  const std::string& rType,
603  const bool sortFileNames,
604  const std::vector< std::string >& fileExtensions,
605  te::gm::Geometry const * const restrictionGeomPtr,
606  const bool ignoreInvalidRasterFiles )
607  {
608  m_ignoreInvalidRasterFiles = ignoreInvalidRasterFiles;
609 
610  // updating m_allRasterFileNames
611 
612  {
613  m_allRasterFileNames.clear();
614 
615  m_rType = rType;
616  const std::vector< std::string >::size_type fileExtensionsSize =
617  fileExtensions.size();
618  std::vector< std::string >::size_type fileExtensionsIdx = 0;
619 
620  boost::filesystem::path directoryPath( directoryName );
621  if( te::core::FileSystem::isDirectory( directoryName ) )
622  {
623  if( recursive )
624  {
625  boost::filesystem::recursive_directory_iterator dirIt( directoryPath );
626  const boost::filesystem::recursive_directory_iterator dirItE;
627  std::string auxString;
628 
629  while( dirIt != dirItE )
630  {
631  if( te::core::FileSystem::isRegularFile( dirIt->path().string() ) )
632  {
633  if( fileExtensionsSize )
634  {
635  for( fileExtensionsIdx = 0 ; fileExtensionsIdx < fileExtensionsSize ;
636  ++fileExtensionsIdx )
637  {
638  if( dirIt->path().extension().generic_string()
639  == fileExtensions[ fileExtensionsIdx ] )
640  {
641  m_allRasterFileNames.push_back( dirIt->path().generic_string() );
642  }
643  }
644  }
645  else
646  {
647  m_allRasterFileNames.push_back( dirIt->path().generic_string() );
648  }
649  }
650 
651  ++dirIt;
652  }
653  }
654  else
655  {
656  boost::filesystem::directory_iterator dirIt( directoryPath );
657  const boost::filesystem::directory_iterator dirItE;
658 
659  while( dirIt != dirItE )
660  {
661  if( te::core::FileSystem::isRegularFile( dirIt->path().string() ) )
662  {
663  if( fileExtensionsSize )
664  {
665  for( fileExtensionsIdx = 0 ; fileExtensionsIdx < fileExtensionsSize ;
666  ++fileExtensionsIdx )
667  {
668  if( dirIt->path().extension().generic_string()
669  == fileExtensions[ fileExtensionsIdx ] )
670  {
671  m_allRasterFileNames.push_back( dirIt->path().generic_string() );
672  }
673  }
674  }
675  else
676  {
677  m_allRasterFileNames.push_back( dirIt->path().generic_string() );
678  }
679  }
680 
681  ++dirIt;
682  }
683  }
684  }
685 
686  if( sortFileNames ) std::sort( m_allRasterFileNames.begin(), m_allRasterFileNames.end() );
687  }
688 
689  // updating m_selectedRastersIndexes
690 
691  {
693 
694  for( std::vector< std::string >::size_type allRasterFileNamesIdx = 0 ;
695  allRasterFileNamesIdx < m_allRasterFileNames.size() ;
696  ++allRasterFileNamesIdx )
697  {
698  m_selectedRastersIndexes[allRasterFileNamesIdx] = static_cast<unsigned int>(allRasterFileNamesIdx);
699  }
700  }
701 
703 
704  // applying the restriction geometry
705 
706  if( restrictionGeomPtr )
707  {
708  applyGeometryRestriction( *restrictionGeomPtr );
709  }
710  else
711  {
712  reset();
713  }
714 
715  return true;
716  }
717 
718  } // end namespace rp
719 } // end namespace te
720 
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.
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
te::rst::Raster const * getCurrentObj() const
Return the current sequence object.
bool moveTo(const unsigned int index)
Jump to the given object index.
static bool isDirectory(const std::string &path)
Checks if a given path in UTF-8 is a directory.
Definition: FileSystem.cpp:87
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.
std::vector< const te::rst::Raster * > m_rasters
#define TERP_LOGWARN(message)
Logs a warning message.
Definition: Macros.h:128
std::unique_ptr< te::rst::Raster > m_currentRasterPtr
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.
void reset()
Reset the feeder to the first position (subsequent accesses will start from the first sequence obejct...
te::rst::Raster const * m_currentRasterNakedPtr
std::vector< const te::rst::Raster * >::size_type m_currentOffset
Definition: FeedersRaster.h:93
std::vector< unsigned int >::size_type m_selectedRasterIndexesOffset
std::unique_ptr< te::gm::Geometry > m_restrictionGeomPtr
std::vector< const te::rst::Raster * > m_rasters
Definition: FeedersRaster.h:94
std::vector< const te::rst::Raster * >::size_type m_CurrentRasterOffset
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.
unsigned int getObjsCount() const
Return the total number of feeder objects.
A LinearRing is a LineString that is both closed and simple.
Definition: LinearRing.h:53
double m_llx
Lower left corner x-coordinate.
void setPoint(std::size_t i, const double &x, const double &y)
It sets the value of the specified point.
An Envelope defines a 2D rectangular region.
An abstract class for raster data strucutures.
#define TERP_LOG_AND_THROW(message)
Logs a error message and throws.
Definition: Macros.h:139
bool moveTo(const unsigned int index)
Jump to the given object index.
URI C++ Library.
Definition: Attributes.h:37
virtual AbstractData * clone() const =0
It returns a clone of this object.
std::vector< std::map< std::string, std::string > > m_rInfos
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. ...
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
std::unique_ptr< te::rst::Raster > m_currentRasterHandler
double m_lly
Lower left corner y-coordinate.
unsigned int getObjsCount() const
Return the total number of feeder objects.
std::vector< std::string > m_rTypes
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:269
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.
void clear()
It deletes all the rings of the CurvePolygon and clear it.
unsigned int getCurrentOffset() const
Return the index of the current object.
void setSRID(int srid)
It sets the Spatial Reference System ID of the linestring.
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.
list rasters
Definition: compose.py:3
static bool isRegularFile(const std::string &path)
Checks if a given path in UTF-8 is a regular file.
Definition: FileSystem.cpp:98
std::unique_ptr< te::rst::Raster > m_currentRasterPtr
std::vector< unsigned int > m_selectedRastersIndexes
std::vector< std::string >::size_type m_currentOffset
bool moveNext()
Advances to the next sequence obeject.
#define TERP_TRUE_OR_THROW(value, message)
Checks if value is true and throws an exception if not.
Definition: Macros.h:150
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