OTB  9.0.0
Orfeo Toolbox
otbOGRDataSourceWrapper.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 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
56 {
57 namespace ogr
58 {
81 #include "OTBGdalAdaptersExport.h"
82 
83 class OTBGdalAdapters_EXPORT DataSource : public itk::DataObject, public boost::noncopyable
84 {
85 public:
88  typedef DataSource Self;
89  typedef itk::DataObject Superclass;
90  typedef itk::SmartPointer<Self> Pointer;
91  typedef itk::SmartPointer<const Self> ConstPointer;
93 
97 
109  itkNewMacro(Self);
110  itkTypeMacro(DataSource, DataObject);
112 
115 
121  struct Modes
122  {
123  enum type
124  {
127  Overwrite,
128  Update_LayerOverwrite,
134  Update_LayerUpdate,
139  Update_LayerCreateOnly,
143  MAX__
149  };
150  };
151 
162  static Pointer New(std::string const& datasourcename, Modes::type mode = Modes::Read);
163 
175  static Pointer New(GDALDataset* sourcemode, Modes::type mode = Modes::Read, const std::vector<std::string>& layerOptions = std::vector<std::string>());
177 
180 #if 0
181  void SetProjectionRef(const std::string& projectionRef);
182  std::string GetProjectionRef() const;
183 #endif
184 
185 
186 
192  bool Clear();
193 
196 
211  template <class Value>
212  class layer_iter : public boost::iterator_facade<layer_iter<Value>, Value, boost::random_access_traversal_tag, Value>
213  {
214  struct enabler
215  {
216  };
217 
231  typedef typename boost::mpl::if_<boost::is_const<Value>, otb::ogr::DataSource const, otb::ogr::DataSource>::type container_type;
232 
233  public:
234  layer_iter(container_type& datasource, size_t index);
235  layer_iter();
236 
237  template <class OtherValue>
238  layer_iter(layer_iter<OtherValue> const& other, typename boost::enable_if<boost::is_convertible<OtherValue*, Value*>, enabler>::type = enabler());
239 
240  private:
241  friend class boost::iterator_core_access;
242  template <class>
243  friend class layer_iter;
244 
245  template <class OtherValue>
246  bool equal(layer_iter<OtherValue> const& other) const;
247  void increment();
248  Value dereference() const;
249 
251  size_t m_index;
252  };
253 
254  template <class>
255  friend class layer_iter;
258 
260  {
261  return cbegin();
262  }
264  {
265  return cend();
266  }
267  const_iterator cbegin() const;
268  const_iterator cend() const;
269  iterator begin();
270  iterator end();
272 
281  int Size(bool doForceComputation) const;
282 
294  std::string GetGlobalExtent(double& ulx, double& uly, double& lrx, double& lry, bool force = false) const;
295 
304  OGREnvelope GetGlobalExtent(bool force = false, std::string* outwkt = nullptr) const;
306 
315  void Graft(const itk::DataObject* data) override;
316 
323  void Reset(GDALDataset* source);
324 
327 
351  Layer CreateLayer(std::string const& name, OGRSpatialReference* poSpatialRef = nullptr, OGRwkbGeometryType eGType = wkbUnknown,
352  std::vector<std::string> const& papszOptions = std::vector<std::string>());
353 
366  void DeleteLayer(size_t i);
367 
387  Layer CopyLayer(Layer& srcLayer, std::string const& newName, std::vector<std::string> const& papszOptions = std::vector<std::string>());
389 
394 
398  int GetLayersCount() const;
399 
410  Layer GetLayer(size_t i);
411 
414  Layer const GetLayer(size_t i) const;
415 
424  Layer GetLayer(std::string const& name);
425 
428  Layer const GetLayer(std::string const& name) const;
429 
440  Layer GetLayerChecked(size_t i);
441 
444  Layer const GetLayerChecked(size_t i) const;
445 
454  Layer GetLayerChecked(std::string const& name);
455 
458  Layer const GetLayerChecked(std::string const& name) const;
459 
479  Layer ExecuteSQL(std::string const& statement, OGRGeometry* poSpatialFilter, char const* pszDialect);
481 
483 
484 
485  struct boolean
486  {
487  int i;
488  };
495  operator int boolean::*() const
496  {
497  return m_DataSource ? &boolean::i : nullptr;
498  }
499 
502  void SyncToDisk();
503 
509  bool HasCapability(std::string const& capabilityName) const;
510 
519  GDALDataset& ogr();
520 
521  void SetLayerCreationOptions(const std::vector<std::string>& options);
522  void AddLayerCreationOptions(std::vector<std::string> options);
523  const std::vector<std::string>& GetLayerCreationOptions() const;
524 
525 protected:
535  DataSource();
536 
540  DataSource(GDALDataset* source, Modes::type mode, const std::vector<std::string>& layerOption = std::vector<std::string>());
541 
545  ~DataSource() override;
546 
547  static Pointer OpenDataSource(std::string const& datasourceName, Modes::type mode);
548 
550  void PrintSelf(std::ostream& os, itk::Indent indent) const override;
551 
552 private:
563  OGRLayer* GetLayerUnchecked(size_t i);
564 
567  OGRLayer* GetLayerUnchecked(size_t i) const;
568 
569  bool IsLayerModifiable(size_t i) const;
570  bool IsLayerModifiable(std::string const& name) const;
571 
572  size_t GetLayerID(std::string const& name) const;
573  int GetLayerIDUnchecked(std::string const& name) const;
574 
576  std::string GetDatasetDescription() const;
577 
578 private:
579  GDALDataset* m_DataSource;
580  std::vector<std::string> m_LayerOptions;
583 }; // end class DataSource
584 }
585 } // end namespace otb::ogr
586 
587 #ifndef OTB_MANUAL_INSTANTIATION
589 #endif
590 
591 #endif // otbOGRDataSourceWrapper_h
otb::OGRExtendedFilenameToOptions
This class aim at processing GDAL option that can be pass through extended filename.
Definition: otbOGRExtendedFilenameToOptions.h:39
otbOGRExtendedFilenameToOptions.h
otb::ogr::DataSource::boolean
Definition: otbOGRDataSourceWrapper.h:485
otb::ogr::DataSource::layer_iter
Implementation class for Layer iterator.
Definition: otbOGRDataSourceWrapper.h:212
otb::ogr::DataSource::ConstPointer
itk::SmartPointer< const Self > ConstPointer
Definition: otbOGRDataSourceWrapper.h:91
otb::ogr::DataSource::Self
DataSource Self
Definition: otbOGRDataSourceWrapper.h:88
otbOGRLayerWrapper.h
otb::ogr::DataSource::layer_iter::enabler
Definition: otbOGRDataSourceWrapper.h:214
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::ogr::DataSource::layer_iter::container_type
boost::mpl::if_< boost::is_const< Value >, otb::ogr::DataSource const, otb::ogr::DataSource >::type container_type
Definition: otbOGRDataSourceWrapper.h:231
otb::ogr::DataSource
Collection of geometric objects.
Definition: otbOGRDataSourceWrapper.h:83
otb::ogr::DataSource::m_DataSource
GDALDataset * m_DataSource
Definition: otbOGRDataSourceWrapper.h:579
otb::ogr::DataSource::boolean::i
int i
Definition: otbOGRDataSourceWrapper.h:487
otb::ogr::DataSource::end
const_iterator end() const
Definition: otbOGRDataSourceWrapper.h:263
otb::ogr::DataSource::const_iterator
layer_iter< Layer const > const_iterator
Definition: otbOGRDataSourceWrapper.h:257
otb::ogr::DataSource::layer_iter::m_DataSource
container_type * m_DataSource
Definition: otbOGRDataSourceWrapper.h:250
otbOGRDataSourceWrapper.hxx
otb::ogr::DataSource::FileNameHelperType
OGRExtendedFilenameToOptions FileNameHelperType
Definition: otbOGRDataSourceWrapper.h:94
otb::ogr::DataSource::m_OpenMode
Modes::type m_OpenMode
Definition: otbOGRDataSourceWrapper.h:581
otb::ogr::DataSource::m_FirstModifiableLayerID
int m_FirstModifiableLayerID
Definition: otbOGRDataSourceWrapper.h:582
otb::ogr::DataSource::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbOGRDataSourceWrapper.h:90
otb::ogr::DataSource::Modes
Definition: otbOGRDataSourceWrapper.h:121
otb::ogr::DataSource::Superclass
itk::DataObject Superclass
Definition: otbOGRDataSourceWrapper.h:89
otb::ogr::DataSource::begin
const_iterator begin() const
Definition: otbOGRDataSourceWrapper.h:259
otb::Wrapper::XML::Read
OTBApplicationEngine_EXPORT int Read(const std::string &filename, Application::Pointer application)
otb::ogr::DataSource::Modes::Read
@ Read
Open data source in read-only mode.
Definition: otbOGRDataSourceWrapper.h:126
otb::ogr::DataSource::layer_iter::m_index
vcl_size_t m_index
Definition: otbOGRDataSourceWrapper.h:251
otb::ogr::DataSource::iterator
layer_iter< Layer > iterator
Definition: otbOGRDataSourceWrapper.h:256
otb::ogr::DataSource::m_LayerOptions
std::vector< std::string > m_LayerOptions
Definition: otbOGRDataSourceWrapper.h:580
otb::ogr::DataSource::Modes::type
type
Definition: otbOGRDataSourceWrapper.h:123
otb::ogr::Layer
Layer of geometric objects.
Definition: otbOGRLayerWrapper.h:80
otb::ogr::DataSource::Modes::Invalid
@ Invalid
Definition: otbOGRDataSourceWrapper.h:125