All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 
72  {
73  m_currentOffset = 0;
74  }
75 
77  {
78  return (unsigned int) m_rasters.size();
79  }
80 
82  {
83  return m_currentOffset;
84  }
85 
86  // -----------------------------------------------------------------------
87 
89  const std::vector< std::string >& rTypes,
90  const std::vector< std::map< std::string, std::string > >& rInfos )
91  : m_currentOffset( 0 ), m_rTypes( rTypes ), m_rInfos( rInfos )
92  {
93  TERP_TRUE_OR_THROW( rTypes.size() == rInfos.size(),
94  "Invalid rasters info" )
95  reset();
96  };
97 
99  : m_currentOffset( 0 )
100  {
101  };
102 
104  {
105  };
106 
108  {
109  return m_currentRasterPtr.get();
110  };
111 
113  {
114  if( m_currentOffset == m_rTypes.size() )
115  {
116  return false;
117  }
118  else
119  {
120  ++m_currentOffset;
121 
122  if( m_currentOffset == m_rTypes.size() )
123  {
124  m_currentRasterPtr.reset();
125  return false;
126  }
127  else
128  {
130  m_rTypes[ m_currentOffset ], m_rInfos[ m_currentOffset ],
131  te::common::RAccess ) );
132 
133  if( m_currentRasterPtr.get() )
134  {
135  return true;
136  }
137  else
138  {
139  return false;
140  }
141  }
142  }
143  }
144 
146  {
147  m_currentRasterPtr.reset();
148 
149  if( ! m_rTypes.empty() )
150  {
152  m_rInfos[ 0 ], te::common::RAccess ) );
153 
154  if( m_currentRasterPtr.get() )
155  {
156  m_currentOffset = 0;
157  }
158  else
159  {
160  m_currentOffset = m_rTypes.size();
161  }
162  }
163  }
164 
166  {
167  return (unsigned int) m_rTypes.size();
168  }
169 
171  {
172  return m_currentOffset;
173  }
174 
175  // -----------------------------------------------------------------------
176 
178  const std::string& directoryName,
179  const bool recursive,
180  const std::string& rType,
181  const bool sortFileNames,
182  const std::vector< std::string >& fileExtensions )
183  : m_rType( rType )
184  {
185  const std::vector< std::string >::size_type fileExtensionsSize =
186  fileExtensions.size();
187  std::vector< std::string >::size_type fileExtensionsIdx = 0;
188 
189  boost::filesystem::path directoryPath( directoryName );
190  if( boost::filesystem::is_directory( directoryName ) )
191  {
192  if( recursive )
193  {
194  boost::filesystem::recursive_directory_iterator dirIt( directoryPath );
195  const boost::filesystem::recursive_directory_iterator dirItE;
196  std::string auxString;
197 
198  while( dirIt != dirItE )
199  {
200  if( boost::filesystem::is_regular_file( *dirIt ) )
201  {
202  if( fileExtensionsSize )
203  {
204  for( fileExtensionsIdx = 0 ; fileExtensionsIdx < fileExtensionsSize ;
205  ++fileExtensionsIdx )
206  {
207  if( dirIt->path().extension().generic_string()
208  == fileExtensions[ fileExtensionsIdx ] )
209  {
210  m_filesNames.push_back( dirIt->path().generic_string() );
211  }
212  }
213  }
214  else
215  {
216  m_filesNames.push_back( dirIt->path().generic_string() );
217  }
218  }
219 
220  ++dirIt;
221  }
222  }
223  else
224  {
225  boost::filesystem::directory_iterator dirIt( directoryPath );
226  const boost::filesystem::directory_iterator dirItE;
227 
228  while( dirIt != dirItE )
229  {
230  if( boost::filesystem::is_regular_file( *dirIt ) )
231  {
232  if( fileExtensionsSize )
233  {
234  for( fileExtensionsIdx = 0 ; fileExtensionsIdx < fileExtensionsSize ;
235  ++fileExtensionsIdx )
236  {
237  if( dirIt->path().extension().generic_string()
238  == fileExtensions[ fileExtensionsIdx ] )
239  {
240  m_filesNames.push_back( dirIt->path().generic_string() );
241  }
242  }
243  }
244  else
245  {
246  m_filesNames.push_back( dirIt->path().generic_string() );
247  }
248  }
249 
250  ++dirIt;
251  }
252  }
253  }
254 
255  if( sortFileNames ) std::sort( m_filesNames.begin(), m_filesNames.end() );
256 
257  m_currentOffset = m_filesNames.size();
258 
259  reset();
260  };
261 
263  : m_currentOffset( 0 )
264  {
265  };
266 
268  {
269  };
270 
272  {
273  return m_currentRasterPtr.get();
274  };
275 
277  {
278  if( m_currentOffset == m_filesNames.size() )
279  {
280  return false;
281  }
282  else
283  {
284  ++m_currentOffset;
285 
286  if( m_currentOffset == m_filesNames.size() )
287  {
288  m_currentRasterPtr.reset();
289  return false;
290  }
291  else
292  {
293  std::map< std::string, std::string > mInfo;
294  mInfo[ "URI" ] = m_filesNames[ m_currentOffset ];
295 
297  m_rType, mInfo,
298  te::common::RAccess ) );
299 
300  if( m_currentRasterPtr.get() )
301  {
302  return true;
303  }
304  else
305  {
306  return false;
307  }
308  }
309  }
310  }
311 
313  {
314  m_currentRasterPtr.reset();
315 
316  if( ! m_filesNames.empty() )
317  {
318  std::map< std::string, std::string > mInfo;
319  mInfo[ "URI" ] = m_filesNames[ 0 ];
320 
322  mInfo, te::common::RAccess ) );
323 
324  if( m_currentRasterPtr.get() )
325  {
326  m_currentOffset = 0;
327  }
328  else
329  {
330  m_currentOffset = m_filesNames.size();
331  }
332  }
333  }
334 
336  {
337  return (unsigned int) m_filesNames.size();
338  }
339 
341  {
342  return m_currentOffset;
343  }
344 
345  } // end namespace rp
346 } // end namespace te
347 
std::vector< std::string > m_rTypes
std::vector< std::string > m_filesNames
bool moveNext()
Advances to the next sequence obeject.
std::auto_ptr< te::rst::Raster > m_currentRasterPtr
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...
std::vector< std::string >::size_type m_currentOffset
unsigned int getCurrentOffset() const
Return the index of the current object.
std::vector< std::map< std::string, std::string > > m_rInfos
std::vector< std::string >::size_type m_currentOffset
te::rst::Raster const * getCurrentObj() const
Return the current sequence object.
Raster objects feeders.
std::vector< const te::rst::Raster * > m_rasters
Definition: FeedersRaster.h:91
unsigned int getCurrentOffset() const
Return the index of the current object.
unsigned int getObjsCount() const
Return the total number of feeder objects.
bool moveNext()
Advances to the next sequence obeject.
unsigned int getObjsCount() const
Return the total number of feeder objects.
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.
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.
#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
te::rst::Raster const * getCurrentObj() const
Return the current sequence object.
bool moveNext()
Advances to the next sequence obeject.
An abstract class for raster data strucutures.
Definition: Raster.h:70
te::rst::Raster const * getCurrentObj() const
Return the current sequence object.
unsigned int getCurrentOffset() const
Return the index of the current object.
std::vector< const te::rst::Raster * >::size_type m_currentOffset
Definition: FeedersRaster.h:90