OTB  10.0.0
Orfeo Toolbox
otbOGRDataSourceWrapper.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2024 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,
133  Update_LayerOverwrite,
138  Update_LayerUpdate,
142  Update_LayerCreateOnly,
148  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
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  }
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 
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:
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
This class aim at processing GDAL option that can be pass through extended filename.
Implementation class for Layer iterator.
boost::mpl::if_< boost::is_const< Value >, otb::ogr::DataSource const, otb::ogr::DataSource >::type container_type
Collection of geometric objects.
bool IsLayerModifiable(vcl_size_t i) const
itk::SmartPointer< const Self > ConstPointer
Layer GetLayerChecked(vcl_size_t i)
Layer const GetLayerChecked(std::string const &name) const
int Size(bool doForceComputation) const
int GetLayersCount() const
const_iterator end() const
Layer CreateLayer(std::string const &name, OGRSpatialReference *poSpatialRef=nullptr, OGRwkbGeometryType eGType=wkbUnknown, std::vector< std::string > const &papszOptions=std::vector< std::string >())
Layer ExecuteSQL(std::string const &statement, OGRGeometry *poSpatialFilter, char const *pszDialect)
static Pointer New(GDALDataset *sourcemode, Modes::type mode=Modes::Read, const std::vector< std::string > &layerOptions=std::vector< std::string >())
OGREnvelope GetGlobalExtent(bool force=false, std::string *outwkt=nullptr) const
std::vector< std::string > m_LayerOptions
static Pointer OpenDataSource(std::string const &datasourceName, Modes::type mode)
const_iterator cend() const
static Pointer New(std::string const &datasourcename, Modes::type mode=Modes::Read)
layer_iter< Layer > iterator
OGRExtendedFilenameToOptions FileNameHelperType
std::string GetGlobalExtent(double &ulx, double &uly, double &lrx, double &lry, bool force=false) const
Layer CopyLayer(Layer &srcLayer, std::string const &newName, std::vector< std::string > const &papszOptions=std::vector< std::string >())
const_iterator begin() const
void AddLayerCreationOptions(std::vector< std::string > options)
~DataSource() override
void Reset(GDALDataset *source)
vcl_size_t GetLayerID(std::string const &name) const
const_iterator cbegin() const
layer_iter< Layer const > const_iterator
void Graft(const itk::DataObject *data) override
DataSource(GDALDataset *source, Modes::type mode, const std::vector< std::string > &layerOption=std::vector< std::string >())
void DeleteLayer(vcl_size_t i)
void PrintSelf(std::ostream &os, itk::Indent indent) const override
itk::SmartPointer< Self > Pointer
bool HasCapability(std::string const &capabilityName) const
int GetLayerIDUnchecked(std::string const &name) const
std::string GetDatasetDescription() const
const std::vector< std::string > & GetLayerCreationOptions() const
OGRLayer * GetLayerUnchecked(vcl_size_t i)
Layer GetLayer(std::string const &name)
Layer GetLayerChecked(std::string const &name)
void SetLayerCreationOptions(const std::vector< std::string > &options)
bool IsLayerModifiable(std::string const &name) const
Layer of geometric objects.
OTBApplicationEngine_EXPORT int Read(const std::string &filename, Application::Pointer application)
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
@ Read
Open data source in read-only mode.