OTB  6.7.0
Orfeo Toolbox
otbOGRLayerWrapper.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 #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/shared_ptr.hpp>
29 #include <boost/iterator/iterator_facade.hpp>
30 #include <boost/utility/enable_if.hpp>
31 #pragma GCC diagnostic pop
32 #else
33 #include <boost/shared_ptr.hpp>
34 #include <boost/iterator/iterator_facade.hpp>
35 #include <boost/utility/enable_if.hpp>
36 #endif
37 // #include "itkIndent.h", included from field
38 #include "otbOGRFeatureWrapper.h"
39 #include <string>
40 
41 // #include "ogr_core.h" // OGRwkbGeometryType, included from feature -> field
42 // Forward declarations
43 class OGRLayer;
44 class OGRGeometry;
45 class OGRFeatureDefn;
46 
47 namespace otb { namespace ogr {
48 class DataSource;
49 class Layer;
50 
55  OTBGdalAdapters_EXPORT
56 bool operator==(Layer const& lhs, Layer const& rhs);
57 
79 class OTBGdalAdapters_EXPORT Layer
80  {
81 public:
82 
85  typedef Layer Self;
86  const char *GetNameOfClass() const {return "Layer"; }
88 
89 #if 0
90  typedef itk::SmartPointer<DataSource> DataSourcePtr;
91 #endif
92 
95 
107  Layer(OGRLayer* layer, bool modifiable);
108 
123  Layer(OGRLayer* layer, GDALDataset& sourceInChargeOfLifeTime, bool modifiable);
125 
128 
137  int GetFeatureCount(bool doForceComputation) const;
138 
152  void CreateFeature(Feature feature);
153 
163  void DeleteFeature(long nFID);
164 
181  Feature GetFeature(long nFID);
182 
199  void SetFeature(Feature feature);
201 
204  std::string GetName() const;
205 
212  OGREnvelope GetExtent(bool force = false) const;
213 
223  void GetExtent(double & ulx, double & uly, double & lrx, double & lry, bool force = false) const;
224 
226  void PrintSelf(std::ostream& os, itk::Indent indent) const;
227 
228 
231  struct boolean{ int i; };
232 
239  operator int boolean ::* () const
240  {
241  return m_Layer ? &boolean::i : nullptr;
242  }
243 
252  OGRLayer & ogr();
253 
262 
272  OGRGeometry const* GetSpatialFilter() const;
273 
288  void SetSpatialFilter(OGRGeometry const* spatialFilter);
289 
297  void SetSpatialFilterRect(double dfMinX, double dfMinY, double dfMaxX, double dfMaxY);
299 
305  OGRSpatialReference const* GetSpatialRef() const;
306 
310  std::string GetProjectionRef() const;
311 
314 
340  template <class Value> class feature_iter
341  : public boost::iterator_facade<feature_iter<Value>, Value, boost::single_pass_traversal_tag>
342  {
343  struct enabler {};
344  public:
346  : m_Layer(nullptr), m_Crt(nullptr) {}
347  explicit feature_iter(otb::ogr::Layer & layer)
348  : m_Layer(&layer), m_Crt(layer.GetNextFeature()) {}
349  template <class OtherValue> feature_iter(
350  feature_iter<OtherValue> const& other,
351  typename boost::enable_if<boost::is_convertible<OtherValue*,Value*> >::type* = nullptr
352  )
353  : m_Layer(other.m_Layer), m_Crt(other.m_Crt)
354  {}
355  private:
356  friend class boost::iterator_core_access;
357  template <class> friend class feature_iter;
359 
360  template <class OtherValue> bool equal(feature_iter<OtherValue> const& other) const
361  { return other.m_Crt == m_Crt; }
362  void increment()
363  {
364  assert(m_Layer && "cannot increment end()");
365  m_Crt = m_Layer->GetNextFeature();
366  }
367  Value & dereference() const
368  { return m_Crt; }
369 
372  };
373 
374  template <class> friend class feature_iter;
379 
383  const_iterator cbegin() const;
384 
387  const_iterator cend () const { return iterator(); }
388 
390  const_iterator begin () const { return cbegin(); }
391 
393  const_iterator end () const { return cend (); }
394 
396  iterator begin ();
397 
399  iterator end () { return iterator(); }
400 
406  const_iterator cstart_at(GIntBig index) const;
407 
409  const_iterator start_at (GIntBig index) const { return cstart_at(index); }
410 
412  iterator start_at (GIntBig index);
414 
420 
429  OGRFeatureDefn & GetLayerDefn() const;
430 
445  void CreateField(FieldDefn const& field, bool bApproxOK = true);
446 
458  void DeleteField(int fieldIndex);
459 
477  void AlterFieldDefn(size_t fieldIndex, FieldDefn const& newFieldDefn, int nFlags);
478 
491  void ReorderField(size_t oldPos, size_t newPos);
492 
504  void ReorderFields(int *map);
505 
520  void SetIgnoredFields(char const** fieldNames);
522 
526  OGRwkbGeometryType GetGeomType() const;
527 
528  friend bool operator==(Layer const& lhs, Layer const& rhs)
529  {
530  const bool equal = lhs.m_Layer.get() == rhs.m_Layer.get();
531  return equal;
532  }
533 
534 private:
544  Feature GetNextFeature();
545 
552  boost::shared_ptr<OGRLayer> m_Layer;
553 
555 
556 #if 0
557 
560  DataSourcePtr m_DataSource;
561 #endif
562  };
563 
564 inline bool operator!=(Layer const& lhs, Layer const& rhs)
565  {
566  return ! (lhs == rhs);
567  }
568 
569 } } // end namespace otb::ogr
570 
571 #ifndef OTB_MANUAL_INSTANTIATION
572 // #include "otbLayerWrapper.hxx"
573 #endif
574 
575 #endif // otbOGRLayerWrapper_h
feature_iter< Feature > iterator
Features iterator.
int boolean ::* () const
bool equal(feature_iter< OtherValue > const &other) const
Layer of geometric objets.
boost::shared_ptr< OGRLayer > m_Layer
const_iterator start_at(GIntBig index) const
const_iterator cend() const
Implementation class for Feature iterator. This iterator is a single pass iterator. We may fetch the Feature referenced by an iterator previously stored, but never resume the iteration after a call to Layer::begin(), Layer::start_at(), Layer::CreateFeature(), Layer::DeleteFeature(), Layer::GetFeature(), Layer::SetFeature(), nor fork the iteration.
OTBGdalAdapters_EXPORT bool operator==(Feature const &lhs, Feature const &rhs)
const_iterator begin() const
const char * GetNameOfClass() const
feature_iter(feature_iter< OtherValue > const &other, typename boost::enable_if< boost::is_convertible< OtherValue *, Value * > >::type *=nullptr)
Geometric objet with descriptive fields.
feature_iter(otb::ogr::Layer &layer)
Encapsulation of OGRFieldDefn: field definition.
feature_iter< Feature const > const_iterator
Features const iterator.
const_iterator end() const
bool operator!=(Layer const &lhs, Layer const &rhs)