OTB  6.7.0
Orfeo Toolbox
otbOGRDataSourceWrapper.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 
22 #ifndef otbOGRDataSourceWrapper_h
23 #define otbOGRDataSourceWrapper_h
24 
25 #include <string>
26 
27 // to implement copy_const
28 #if defined(__GNUC__) || defined(__clang__)
29 #pragma GCC diagnostic push
30 #pragma GCC diagnostic ignored "-Wshadow"
31 #include <boost/mpl/if.hpp>
32 #include <boost/type_traits/add_const.hpp>
33 #include <boost/type_traits/is_const.hpp>
34 #include <boost/noncopyable.hpp>
35 #pragma GCC diagnostic pop
36 #else
37 #include <boost/mpl/if.hpp>
38 #include <boost/type_traits/add_const.hpp>
39 #include <boost/type_traits/is_const.hpp>
40 #include <boost/noncopyable.hpp>
41 #endif
42 
43 #include "itkDataObject.h"
44 #include "itkMacro.h" // itkNewMacro
45 #include "itkObjectFactory.h" // that should have been included by itkMacro.h
46 
47 #include "otbOGRLayerWrapper.h"
49 
50 class OGRLayer;
51 class OGRSpatialReference;
52 class OGRGeometry;
53 // #include "ogr_core.h" // OGRwkbGeometryType, included from Layer
54 
55 namespace otb { namespace ogr {
78  #include "OTBGdalAdaptersExport.h"
79 
80 class OTBGdalAdapters_EXPORT DataSource : public itk::DataObject , public boost::noncopyable
81  {
82 public:
85  typedef DataSource Self;
90 
94 
106  itkNewMacro(Self);
107  itkTypeMacro(DataSource, DataObject);
109 
112 
118  struct Modes {
119  enum type
120  {
123  Overwrite,
124  Update_LayerOverwrite,
130  Update_LayerUpdate,
135  Update_LayerCreateOnly,
139  MAX__ };
145  };
146 
157  static Pointer New(std::string const& datasourcename, Modes::type mode = Modes::Read);
158 
170  static Pointer New(GDALDataset * sourcemode, Modes::type mode = Modes::Read , const std::vector< std::string > & layerOptions = std::vector< std::string >() );
172 
175 #if 0
176  void SetProjectionRef(const std::string& projectionRef);
177  std::string GetProjectionRef() const;
178 #endif
179 
180 
181 
187  bool Clear();
188 
191 
206  template <class Value> class layer_iter
207  : public boost::iterator_facade<layer_iter<Value>, Value, boost::random_access_traversal_tag, Value>
208  {
209  struct enabler {};
210 
224  typedef typename boost::mpl::if_ <boost::is_const<Value> , otb::ogr::DataSource const , otb::ogr::DataSource >::type
226  public:
227  layer_iter(container_type & datasource, size_t index);
228  layer_iter();
230 
231  template <class OtherValue> layer_iter(
232  layer_iter<OtherValue> const& other,
233  typename boost::enable_if<boost::is_convertible<OtherValue*,Value*>
234  , enabler
235  >::type = enabler()
236  );
237  private:
238  friend class boost::iterator_core_access;
239  template <class> friend class layer_iter;
240 
241  template <class OtherValue> bool equal(layer_iter<OtherValue> const& other) const;
242  void increment();
243  Value dereference() const;
244 
246  size_t m_index;
247  };
248 
249  template <class> friend class layer_iter;
252 
253  const_iterator begin () const { return cbegin(); }
254  const_iterator end () const { return cend (); }
255  const_iterator cbegin() const;
256  const_iterator cend () const;
257  iterator begin ();
258  iterator end ();
260 
269  int Size(bool doForceComputation) const;
270 
282  std::string GetGlobalExtent(double & ulx, double & uly, double & lrx, double & lry, bool force = false) const;
283 
292  OGREnvelope GetGlobalExtent(bool force = false, std::string * outwkt=nullptr) const;
294 
303  void Graft(const itk::DataObject *data) override;
304 
311  void Reset(GDALDataset * source);
312 
315 
339  Layer CreateLayer(
340  std::string const& name,
341  OGRSpatialReference * poSpatialRef = nullptr,
342  OGRwkbGeometryType eGType = wkbUnknown,
343  std::vector<std::string> const& papszOptions = std::vector<std::string>());
344 
357  void DeleteLayer(size_t i);
358 
378  Layer CopyLayer(
379  Layer & srcLayer,
380  std::string const& newName,
381  std::vector<std::string> const& papszOptions = std::vector<std::string>() );
383 
388 
392  int GetLayersCount() const;
393 
404  Layer GetLayer(size_t i);
405 
408  Layer const GetLayer(size_t i) const;
409 
418  Layer GetLayer(std::string const& name);
419 
422  Layer const GetLayer(std::string const& name) const;
423 
434  Layer GetLayerChecked(size_t i);
435 
438  Layer const GetLayerChecked(size_t i) const;
439 
448  Layer GetLayerChecked(std::string const& name);
449 
452  Layer const GetLayerChecked(std::string const& name) const;
453 
473  Layer ExecuteSQL(
474  std::string const& statement,
475  OGRGeometry * poSpatialFilter,
476  char const* pszDialect);
478 
480 
481 
482  struct boolean{ int i; };
489  operator int boolean ::* () const {
490  return m_DataSource ? &boolean::i : nullptr;
491  }
492 
495  void SyncToDisk();
496 
502  bool HasCapability(std::string const& capabilityName) const;
503 
512  GDALDataset & ogr();
513 
514  void SetLayerCreationOptions( const std::vector< std::string > & options );
515  void AddLayerCreationOptions( std::vector< std::string > options );
516  const std::vector< std::string > & GetLayerCreationOptions() const ;
517 
518 protected:
528  DataSource();
529 
533  DataSource(GDALDataset * source, Modes::type mode , const std::vector< std::string > & layerOption = std::vector< std::string >() );
534 
538  ~DataSource() override;
539 
540  static Pointer OpenDataSource(std::string const& datasourceName, Modes::type mode);
541 
543  void PrintSelf(std::ostream& os, itk::Indent indent) const override;
544 
545 private:
556  OGRLayer* GetLayerUnchecked(size_t i);
557 
560  OGRLayer* GetLayerUnchecked(size_t i) const;
561 
562  bool IsLayerModifiable(size_t i) const;
563  bool IsLayerModifiable(std::string const& name) const;
564 
565  size_t GetLayerID(std::string const& name) const;
566  int GetLayerIDUnchecked(std::string const& name) const;
567 
569  std::string GetDatasetDescription() const;
570 
571 private:
572  GDALDataset *m_DataSource;
573  std::vector< std::string > m_LayerOptions;
576  }; // end class DataSource
577 } } // end namespace otb::ogr
578 
579 #if 0
580 // Either this, or inheriting from noncopyable is required for DataSource to be
581 // compatible with BOOST_FOREACH
582 namespace boost { namespace foreach {
583  template<typename T> struct is_noncopyable; // forward declaration
584  template <>
585  struct is_noncopyable<otb::ogr::DataSource> : mpl::true_ {};
586 }}
587 #endif
588 
589 #ifndef OTB_MANUAL_INSTANTIATION
591 #endif
592 
593 #endif // otbOGRDataSourceWrapper_h
Collection of geometric objects.
OTBApplicationEngine_EXPORT int Read(const std::string &filename, Application::Pointer application)
itk::SmartPointer< Self > Pointer
Layer of geometric objets.
layer_iter< Layer > iterator
layer_iter< Layer const > const_iterator
boost::mpl::if_< boost::is_const< Value >, otb::ogr::DataSource const , otb::ogr::DataSource >::type container_type
const_iterator end() const
OGRExtendedFilenameToOptions FileNameHelperType
const_iterator begin() const
This class aim at processing GDAL option that can be pass through extended filename.
itk::SmartPointer< const Self > ConstPointer
Implementation class for Layer iterator.
std::vector< std::string > m_LayerOptions
Open data source in read-only mode.