OTB  9.0.0
Orfeo Toolbox
otbOGRLayerWrapper.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 #ifndef otbOGRLayerWrapper_h
22 #define otbOGRLayerWrapper_h
23 
24 // #include <iosfwd> // std::ostream&
25 #if defined(__GNUC__) || defined(__clang__)
26 #pragma GCC diagnostic push
27 #pragma GCC diagnostic ignored "-Wshadow"
28 #include <boost/iterator/iterator_facade.hpp>
29 #include <boost/utility/enable_if.hpp>
30 #pragma GCC diagnostic pop
31 #else
32 #include <boost/iterator/iterator_facade.hpp>
33 #include <boost/utility/enable_if.hpp>
34 #endif
35 // #include "itkIndent.h", included from field
36 #include "otbOGRFeatureWrapper.h"
37 #include <memory>
38 #include <string>
39 
40 // #include "ogr_core.h" // OGRwkbGeometryType, included from feature -> field
41 // Forward declarations
42 class OGRLayer;
43 class OGRGeometry;
44 class OGRFeatureDefn;
45 
46 namespace otb
47 {
48 namespace ogr
49 {
50 class DataSource;
51 class Layer;
52 
57 OTBGdalAdapters_EXPORT bool operator==(Layer const& lhs, Layer const& rhs);
58 
80 class OTBGdalAdapters_EXPORT Layer
81 {
82 public:
83 
86  typedef Layer Self;
87  const char* GetNameOfClass() const
88  {
89  return "Layer";
90  }
92 
93 #if 0
94  typedef itk::SmartPointer<DataSource> DataSourcePtr;
95 #endif
96 
99 
111  Layer(OGRLayer* layer, bool modifiable);
112 
127  Layer(OGRLayer* layer, GDALDataset& sourceInChargeOfLifeTime, bool modifiable);
129 
132 
141  int GetFeatureCount(bool doForceComputation) const;
142 
156  void CreateFeature(Feature feature);
157 
167  void DeleteFeature(long nFID);
168 
185  Feature GetFeature(long nFID);
186 
203  void SetFeature(Feature feature);
205 
208  std::string GetName() const;
209 
216  OGREnvelope GetExtent(bool force = false) const;
217 
227  void GetExtent(double& ulx, double& uly, double& lrx, double& lry, bool force = false) const;
228 
230  void PrintSelf(std::ostream& os, itk::Indent indent) const;
231 
232 
235  struct boolean
236  {
237  int i;
238  };
239 
246  operator int boolean::*() const
247  {
248  return m_Layer ? &boolean::i : nullptr;
249  }
250 
259  OGRLayer& ogr();
260 
269 
279  OGRGeometry const* GetSpatialFilter() const;
280 
295  void SetSpatialFilter(OGRGeometry const* spatialFilter);
296 
304  void SetSpatialFilterRect(double dfMinX, double dfMinY, double dfMaxX, double dfMaxY);
306 
312  OGRSpatialReference const* GetSpatialRef() const;
313 
317  std::string GetProjectionRef() const;
318 
321 
347  template <class Value>
348  class feature_iter : public boost::iterator_facade<feature_iter<Value>, Value, boost::single_pass_traversal_tag>
349  {
350  struct enabler
351  {
352  };
354 
355  public:
356  feature_iter() : m_Layer(nullptr), m_Crt(nullptr)
357  {
358  }
359  explicit feature_iter(otb::ogr::Layer& layer) : m_Layer(&layer), m_Crt(layer.GetNextFeature())
360  {
361  }
362  template <class OtherValue>
363  feature_iter(feature_iter<OtherValue> const& other, typename boost::enable_if<boost::is_convertible<OtherValue*, Value*>>::type* = nullptr)
364  : m_Layer(other.m_Layer), m_Crt(other.m_Crt)
365  {
366  }
367 
368  private:
369  friend class boost::iterator_core_access;
370  template <class>
371  friend class feature_iter;
372 
373  template <class OtherValue>
374  bool equal(feature_iter<OtherValue> const& other) const
375  {
376  return other.m_Crt == m_Crt;
377  }
378  void increment()
379  {
380  assert(m_Layer && "cannot increment end()");
381  m_Crt = m_Layer->GetNextFeature();
382  }
383  Value& dereference() const
384  {
385  return m_Crt;
386  }
387 
390  };
391 
392  template <class>
393  friend class feature_iter;
398 
402  const_iterator cbegin() const;
403 
407  {
408  return iterator();
409  }
410 
413  {
414  return cbegin();
415  }
416 
419  {
420  return cend();
421  }
422 
424  iterator begin();
425 
428  {
429  return iterator();
430  }
431 
437  const_iterator cstart_at(GIntBig index) const;
438 
440  const_iterator start_at(GIntBig index) const
441  {
442  return cstart_at(index);
443  }
444 
446  iterator start_at(GIntBig index);
448 
454 
463  OGRFeatureDefn& GetLayerDefn() const;
464 
479  void CreateField(FieldDefn const& field, bool bApproxOK = true);
480 
492  void DeleteField(int fieldIndex);
493 
511  void AlterFieldDefn(size_t fieldIndex, FieldDefn const& newFieldDefn, int nFlags);
512 
525  void ReorderField(size_t oldPos, size_t newPos);
526 
538  void ReorderFields(int* map);
539 
554  void SetIgnoredFields(char const** fieldNames);
556 
560  OGRwkbGeometryType GetGeomType() const;
561 
562  friend bool operator==(Layer const& lhs, Layer const& rhs)
563  {
564  const bool equal = lhs.m_Layer.get() == rhs.m_Layer.get();
565  return equal;
566  }
567 
568 private:
578  Feature GetNextFeature();
579 
586  std::shared_ptr<OGRLayer> m_Layer;
587 
589 
590 #if 0
591 
594  DataSourcePtr m_DataSource;
595 #endif
596 };
597 
598 inline bool operator!=(Layer const& lhs, Layer const& rhs)
599 {
600  return !(lhs == rhs);
601 }
602 }
603 } // end namespace otb::ogr
604 
605 #ifndef OTB_MANUAL_INSTANTIATION
606 // #include "otbLayerWrapper.hxx"
607 #endif
608 
609 #endif // otbOGRLayerWrapper_h
otb::ogr::Layer::GetNameOfClass
const char * GetNameOfClass() const
Definition: otbOGRLayerWrapper.h:87
otb::ogr::FieldDefn
Encapsulation of OGRFieldDefn: field definition.
Definition: otbOGRFieldWrapper.h:60
otb::ogr::Layer::m_Layer
std::shared_ptr< OGRLayer > m_Layer
Definition: otbOGRLayerWrapper.h:586
otb::ogr::Layer::feature_iter::feature_iter
feature_iter(otb::ogr::Layer &layer)
Definition: otbOGRLayerWrapper.h:359
otb::ogr::Layer::iterator
feature_iter< Feature > iterator
Features iterator.
Definition: otbOGRLayerWrapper.h:395
otb::ogr::Layer::end
iterator end()
Definition: otbOGRLayerWrapper.h:427
otb::ogr::Layer::feature_iter::m_Layer
otb::ogr::Layer * m_Layer
Definition: otbOGRLayerWrapper.h:388
otb::ogr::Layer::Self
Layer Self
Definition: otbOGRLayerWrapper.h:86
otb::ogr::operator==
OTBGdalAdapters_EXPORT bool operator==(Feature const &lhs, Feature const &rhs)
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::ogr::Layer::feature_iter::feature_iter
feature_iter()
Definition: otbOGRLayerWrapper.h:356
otb::ogr::Layer::start_at
const_iterator start_at(GIntBig index) const
Definition: otbOGRLayerWrapper.h:440
otb::ogr::Layer::feature_iter::feature_iter
feature_iter(feature_iter< OtherValue > const &other, typename boost::enable_if< boost::is_convertible< OtherValue *, Value * >>::type *=nullptr)
Definition: otbOGRLayerWrapper.h:363
otb::ogr::Layer::begin
const_iterator begin() const
Definition: otbOGRLayerWrapper.h:412
otb::ogr::Layer::end
const_iterator end() const
Definition: otbOGRLayerWrapper.h:418
otb::ogr::operator!=
bool operator!=(Layer const &lhs, Layer const &rhs)
Definition: otbOGRLayerWrapper.h:598
otb::ogr::Layer::cend
const_iterator cend() const
Definition: otbOGRLayerWrapper.h:406
otb::ogr::Layer::feature_iter
Implementation class for Feature iterator. This iterator is a single pass iterator....
Definition: otbOGRLayerWrapper.h:348
otb::ogr::Layer::feature_iter::m_Crt
otb::ogr::Feature m_Crt
Definition: otbOGRLayerWrapper.h:389
otb::ogr::Layer::boolean
int boolean ::* () const
Definition: otbOGRLayerWrapper.h:235
otb::ogr::Layer::feature_iter::dereference
Value & dereference() const
Definition: otbOGRLayerWrapper.h:383
otb::ogr::Layer::m_Modifiable
bool m_Modifiable
Definition: otbOGRLayerWrapper.h:588
otb::ogr::Feature
Geometric object with descriptive fields.
Definition: otbOGRFeatureWrapper.h:63
otb::ogr::Layer::feature_iter::enabler
Definition: otbOGRLayerWrapper.h:350
otb::ogr::Layer::boolean::i
int i
Definition: otbOGRLayerWrapper.h:237
otb::ogr::Layer::feature_iter::increment
void increment()
Definition: otbOGRLayerWrapper.h:378
otb::ogr::Layer
Layer of geometric objects.
Definition: otbOGRLayerWrapper.h:80
otb::ogr::Layer::const_iterator
feature_iter< Feature const > const_iterator
Features const iterator.
Definition: otbOGRLayerWrapper.h:397
otb::ogr::Layer::feature_iter::equal
bool equal(feature_iter< OtherValue > const &other) const
Definition: otbOGRLayerWrapper.h:374
otbOGRFeatureWrapper.h