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) 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/rp/FeedersRaster.cpp
22  \brief Raster objects feeders.
23 */
24 
25 #include "FeedersRaster.h"
26 #include "Macros.h"
27 #include "../raster/RasterFactory.h"
28 
29 #include <boost/filesystem.hpp>
30 
31 namespace te
32 {
33  namespace rp
34  {
35  // -----------------------------------------------------------------------
36 
38  const std::vector< const te::rst::Raster* > rasters )
39  : m_currentOffset( 0 ), m_rasters( rasters )
40  {
41  }
42 
44  : m_currentOffset( 0 )
45  {
46  }
47 
49  {
50  }
51 
53  {
54  return ( m_currentOffset < m_rasters.size() ) ?
55  m_rasters[ m_currentOffset ] : 0;
56  }
57 
59  {
60  if( m_currentOffset == m_rasters.size() )
61  {
62  return false;
63  }
64  else
65  {
67  return ( m_currentOffset != m_rasters.size() );
68  }
69  }
70 
71  bool FeederConstRasterVector::moveTo( const unsigned int index )
72  {
73  if( index >= m_rasters.size() )
74  {
75  return false;
76  }
77  else
78  {
79  m_currentOffset = index;
80  return true;
81  }
82  }
83 
85  {
86  m_currentOffset = 0;
87  }
88 
90  {
91  return (unsigned int) m_rasters.size();
92  }
93 
95  {
96  return m_currentOffset;
97  }
98 
99  // -----------------------------------------------------------------------
100 
102  const std::vector< std::string >& rTypes,
103  const std::vector< std::map< std::string, std::string > >& rInfos )
104  : m_currentOffset( 0 ), m_rTypes( rTypes ), m_rInfos( rInfos )
105  {
106  TERP_TRUE_OR_THROW( rTypes.size() == rInfos.size(),
107  "Invalid rasters info" )
108  reset();
109  }
110 
112  : m_currentOffset( 0 )
113  {
114  }
115 
117  {
118  }
119 
121  {
122  return m_currentRasterPtr.get();
123  }
124 
126  {
127  if( m_currentOffset == m_rTypes.size() )
128  {
129  return false;
130  }
131  else
132  {
133  ++m_currentOffset;
134 
135  if( m_currentOffset == m_rTypes.size() )
136  {
137  m_currentRasterPtr.reset();
138  return false;
139  }
140  else
141  {
143  m_rTypes[ m_currentOffset ], m_rInfos[ m_currentOffset ],
144  te::common::RAccess ) );
145 
146  if( m_currentRasterPtr.get() )
147  {
148  return true;
149  }
150  else
151  {
152  return false;
153  }
154  }
155  }
156  }
157 
158  bool FeederConstRasterInfo::moveTo( const unsigned int index )
159  {
160  if( index >= m_rInfos.size() )
161  {
162  return false;
163  }
164  else if( index == m_currentOffset )
165  {
166  return true;
167  }
168  else
169  {
170  m_currentOffset = index;
171 
173  m_rTypes[ m_currentOffset ], m_rInfos[ m_currentOffset ],
174  te::common::RAccess ) );
175 
176  if( m_currentRasterPtr.get() )
177  {
178  return true;
179  }
180  else
181  {
182  return false;
183  }
184  }
185  }
186 
188  {
189  m_currentRasterPtr.reset();
190 
191  if( ! m_rTypes.empty() )
192  {
194  m_rInfos[ 0 ], te::common::RAccess ) );
195 
196  if( m_currentRasterPtr.get() )
197  {
198  m_currentOffset = 0;
199  }
200  else
201  {
202  m_currentOffset = m_rTypes.size();
203  }
204  }
205  }
206 
208  {
209  return (unsigned int) m_rTypes.size();
210  }
211 
213  {
214  return m_currentOffset;
215  }
216 
217  // -----------------------------------------------------------------------
218 
220  const std::string& directoryName,
221  const bool recursive,
222  const std::string& rType,
223  const bool sortFileNames,
224  const std::vector< std::string >& fileExtensions )
225  : m_rType( rType )
226  {
227  const std::vector< std::string >::size_type fileExtensionsSize =
228  fileExtensions.size();
229  std::vector< std::string >::size_type fileExtensionsIdx = 0;
230 
231  boost::filesystem::path directoryPath( directoryName );
232  if( boost::filesystem::is_directory( directoryName ) )
233  {
234  if( recursive )
235  {
236  boost::filesystem::recursive_directory_iterator dirIt( directoryPath );
237  const boost::filesystem::recursive_directory_iterator dirItE;
238  std::string auxString;
239 
240  while( dirIt != dirItE )
241  {
242  if( boost::filesystem::is_regular_file( *dirIt ) )
243  {
244  if( fileExtensionsSize )
245  {
246  for( fileExtensionsIdx = 0 ; fileExtensionsIdx < fileExtensionsSize ;
247  ++fileExtensionsIdx )
248  {
249  if( dirIt->path().extension().generic_string()
250  == fileExtensions[ fileExtensionsIdx ] )
251  {
252  m_filesNames.push_back( dirIt->path().generic_string() );
253  }
254  }
255  }
256  else
257  {
258  m_filesNames.push_back( dirIt->path().generic_string() );
259  }
260  }
261 
262  ++dirIt;
263  }
264  }
265  else
266  {
267  boost::filesystem::directory_iterator dirIt( directoryPath );
268  const boost::filesystem::directory_iterator dirItE;
269 
270  while( dirIt != dirItE )
271  {
272  if( boost::filesystem::is_regular_file( *dirIt ) )
273  {
274  if( fileExtensionsSize )
275  {
276  for( fileExtensionsIdx = 0 ; fileExtensionsIdx < fileExtensionsSize ;
277  ++fileExtensionsIdx )
278  {
279  if( dirIt->path().extension().generic_string()
280  == fileExtensions[ fileExtensionsIdx ] )
281  {
282  m_filesNames.push_back( dirIt->path().generic_string() );
283  }
284  }
285  }
286  else
287  {
288  m_filesNames.push_back( dirIt->path().generic_string() );
289  }
290  }
291 
292  ++dirIt;
293  }
294  }
295  }
296 
297  if( sortFileNames ) std::sort( m_filesNames.begin(), m_filesNames.end() );
298 
299  m_currentOffset = m_filesNames.size();
300 
301  reset();
302  }
303 
305  : m_currentOffset( 0 )
306  {
307  }
308 
310  {
311  }
312 
314  {
315  return m_currentRasterPtr.get();
316  }
317 
319  {
320  if( m_currentOffset == m_filesNames.size() )
321  {
322  return false;
323  }
324  else
325  {
326  ++m_currentOffset;
327 
328  if( m_currentOffset == m_filesNames.size() )
329  {
330  m_currentRasterPtr.reset();
331  return false;
332  }
333  else
334  {
335  std::map< std::string, std::string > mInfo;
336  mInfo[ "URI" ] = m_filesNames[ m_currentOffset ];
337 
339  m_rType, mInfo,
340  te::common::RAccess ) );
341 
342  if( m_currentRasterPtr.get() )
343  {
344  return true;
345  }
346  else
347  {
348  return false;
349  }
350  }
351  }
352  }
353 
354  bool FeederConstRasterDirectory::moveTo( const unsigned int index )
355  {
356  if( index >= m_filesNames.size() )
357  {
358  return false;
359  }
360  else
361  {
362  m_currentOffset = index;
363 
364  std::map< std::string, std::string > mInfo;
365  mInfo[ "URI" ] = m_filesNames[ m_currentOffset ];
366 
368  m_rType, mInfo,
369  te::common::RAccess ) );
370 
371  if( m_currentRasterPtr.get() )
372  {
373  return true;
374  }
375  else
376  {
377  return false;
378  }
379  }
380  }
381 
383  {
384  m_currentRasterPtr.reset();
385 
386  if( ! m_filesNames.empty() )
387  {
388  std::map< std::string, std::string > mInfo;
389  mInfo[ "URI" ] = m_filesNames[ 0 ];
390 
392  mInfo, te::common::RAccess ) );
393 
394  if( m_currentRasterPtr.get() )
395  {
396  m_currentOffset = 0;
397  }
398  else
399  {
400  m_currentOffset = m_filesNames.size();
401  }
402  }
403  }
404 
406  {
407  return (unsigned int) m_filesNames.size();
408  }
409 
411  {
412  return m_currentOffset;
413  }
414 
415  } // end namespace rp
416 } // end namespace te
417 
bool moveTo(const unsigned int index)
Jump to the given object index.
te::rst::Raster const * getCurrentObj() const
Return the current sequence object.
bool moveNext()
Advances to the next sequence obeject.
bool moveTo(const unsigned int index)
Jump to the given object index.
unsigned int getCurrentOffset() const
Return the index of the current object.
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...
unsigned int getObjsCount() const
Return the total number of feeder objects.
unsigned int getObjsCount() const
Return the total number of feeder objects.
std::vector< const te::rst::Raster * >::size_type m_currentOffset
Definition: FeedersRaster.h:92
std::vector< const te::rst::Raster * > m_rasters
Definition: FeedersRaster.h:93
te::rst::Raster const * getCurrentObj() const
Return the current sequence object.
std::vector< std::string >::size_type m_currentOffset
An abstract class for raster data strucutures.
Definition: Raster.h:71
std::auto_ptr< te::rst::Raster > m_currentRasterPtr
std::vector< std::string > m_filesNames
te::rst::Raster const * getCurrentObj() const
Return the current sequence object.
unsigned int getObjsCount() const
Return the total number of feeder objects.
bool moveTo(const unsigned int index)
Jump to the given object index.
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...
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.
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
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