otbOGRLayerWrapper.cxx 10.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*=========================================================================

  Program:   ORFEO Toolbox
  Language:  C++
  Date:      $Date$
  Version:   $Revision$


  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
  See OTBCopyright.txt for details.


     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

/*===========================================================================*/
/*===============================[ Includes ]================================*/
/*===========================================================================*/
#include "otbOGRLayerWrapper.h"
23

24
#include <cassert>
OTB Bot's avatar
STYLE  
OTB Bot committed
25
#include <boost/bind.hpp>
26
#include <boost/foreach.hpp>
27 28 29
#include "ogrsf_frmts.h" // OGRDataSource & OGRLayer

/*===========================================================================*/
30
/*======================[ Construction & Destruction ]=======================*/
31 32
/*===========================================================================*/
namespace  { // Anonymous namespace
Luc Hermitte's avatar
Luc Hermitte committed
33
  /**\ingroup GeometryInternals
34 35
   * Deleter for \c boost::shared_ptr<> that doesn't delete.
   * This is required for \c OGRLayer s that belong to \c OGRDataSource.
Luc Hermitte's avatar
Luc Hermitte committed
36
   * \internal Unlike OGR, works as a no-op on null geometries.
37 38 39 40 41
   */
  struct LeaveAloneDeleter
    {
    void operator()(OGRLayer*) const {}
    };
OTB Bot's avatar
STYLE  
OTB Bot committed
42
} // Anonymous namespace
43 44 45 46 47 48 49


otb::ogr::Layer::Layer(OGRLayer* layer)
: m_Layer(layer, LeaveAloneDeleter())
{
}

Luc Hermitte's avatar
Luc Hermitte committed
50 51
otb::ogr::Layer::Layer(OGRLayer* layer, OGRDataSource& sourceInChargeOfLifeTime)
: m_Layer(layer,  boost::bind(&OGRDataSource::ReleaseResultSet, boost::ref(sourceInChargeOfLifeTime), _1))
52 53 54 55 56
{
  assert(layer && "A null OGRlayer cannot belong to an OGRDataSource" );
  // OGR always refuses "delete 0". *sigh*
}

57 58 59
/*===========================================================================*/
/*===============================[ Features ]================================*/
/*===========================================================================*/
60 61 62 63 64 65
int otb::ogr::Layer::GetFeatureCount(bool doForceComputation) const
{
  assert(m_Layer);
  return m_Layer->GetFeatureCount(doForceComputation);
}

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
otb::ogr::Feature otb::ogr::Layer::GetNextFeature()
{
  assert(m_Layer && "OGRLayer not initialized");
  return m_Layer->GetNextFeature();
}

otb::ogr::Layer::iterator otb::ogr::Layer::begin()
{
  assert(m_Layer && "OGRLayer not initialized");
  m_Layer->ResetReading();
  return iterator(*this);
}

otb::ogr::Layer::const_iterator otb::ogr::Layer::cbegin() const
{
  assert(m_Layer && "OGRLayer not initialized");
  m_Layer->ResetReading();
  return const_iterator(*const_cast <Layer*>(this));
}

Luc Hermitte's avatar
Luc Hermitte committed
86
otb::ogr::Layer::iterator otb::ogr::Layer::start_at(size_t index)
87 88 89 90 91 92
{
  assert(m_Layer && "OGRLayer not initialized");
  m_Layer->SetNextByIndex(index);
  return iterator(*this);
}

Luc Hermitte's avatar
Luc Hermitte committed
93
otb::ogr::Layer::const_iterator otb::ogr::Layer::cstart_at(size_t index) const
94 95 96 97 98 99
{
  assert(m_Layer && "OGRLayer not initialized");
  m_Layer->SetNextByIndex(index);
  return const_iterator(*const_cast <Layer*>(this));
}

100 101 102
void otb::ogr::Layer::CreateFeature(Feature feature)
{
  assert(m_Layer && "OGRLayer not initialized");
Luc Hermitte's avatar
Luc Hermitte committed
103 104 105
  const OGRErr res = m_Layer->CreateFeature(&feature.ogr());
  if (res != OGRERR_NONE)
    {
Luc Hermitte's avatar
Luc Hermitte committed
106 107
    itkGenericExceptionMacro(<< "Cannot create a new feature in the layer <"
      <<GetName()<<">:" << CPLGetLastErrorMsg());
Luc Hermitte's avatar
Luc Hermitte committed
108
    }
109 110 111 112 113
}

void otb::ogr::Layer::DeleteFeature(long nFID)
{
  assert(m_Layer && "OGRLayer not initialized");
Luc Hermitte's avatar
Luc Hermitte committed
114 115 116
  const OGRErr res = m_Layer->DeleteFeature(nFID);
  if (res != OGRERR_NONE)
    {
Luc Hermitte's avatar
Luc Hermitte committed
117 118
    itkGenericExceptionMacro(<< "Cannot delete the feature <"<<nFID<<"> in the layer <"
      <<GetName()<<">:" << CPLGetLastErrorMsg());
Luc Hermitte's avatar
Luc Hermitte committed
119
    }
120 121 122 123 124
}

otb::ogr::Feature otb::ogr::Layer::GetFeature(long nFID)
{
  assert(m_Layer && "OGRLayer not initialized");
Luc Hermitte's avatar
Luc Hermitte committed
125 126 127 128
  if (nFID == OGRNullFID)
    {
    itkGenericExceptionMacro(<< "Invalid feature null id GetFeature() in the layer <"<<GetName()<<">.");
    }
129 130
  const Feature feat = m_Layer->GetFeature(nFID);
  return feat;
131 132 133 134 135
}

void otb::ogr::Layer::SetFeature(Feature feature)
{
  assert(m_Layer && "OGRLayer not initialized");
Luc Hermitte's avatar
Luc Hermitte committed
136 137 138
  const OGRErr res = m_Layer->SetFeature(&feature.ogr());
  if (res != OGRERR_NONE)
    {
Luc Hermitte's avatar
Luc Hermitte committed
139 140
    itkGenericExceptionMacro(<< "Cannot update a feature in the layer <"
      <<GetName()<<">:" << CPLGetLastErrorMsg());
Luc Hermitte's avatar
Luc Hermitte committed
141
    }
142 143 144 145 146
}

/*===========================================================================*/
/*=================================[ Misc ]==================================*/
/*===========================================================================*/
147 148 149
std::string otb::ogr::Layer::GetName() const
{
  assert(m_Layer && "null layer");
150
#if GDAL_VERSION_NUM >= 1800
151
  return m_Layer->GetName();
OTB Bot's avatar
STYLE  
OTB Bot committed
152
#else
153 154
  return GetLayerDefn().GetName();
#endif
155 156 157 158 159 160 161 162 163 164 165 166 167
}

OGRLayer & otb::ogr::Layer::ogr()
{
  assert(m_Layer && "OGRLayer not initialized");
  return *m_Layer;
}

void otb::ogr::Layer::PrintSelf(std::ostream& os, itk::Indent indent) const
{
  os << indent << "+";
  if (m_Layer) // in case for odd reason the layer that should exist can't be found
    {
Luc Hermitte's avatar
Luc Hermitte committed
168
    os << "Layer <" << GetName() << "> of "<< OGRGeometryTypeToName(GetGeomType()) <<"\n";
169 170 171 172 173
    indent = indent.GetNextIndent();
    BOOST_FOREACH(Feature f, *this)
      {
      f.PrintSelf(os, indent);
      }
174 175 176 177 178 179 180
    }
  else
    {
    os << "null Layer\n";
    }
}

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
/*===========================================================================*/
/*============================[ Spatial Filter ]=============================*/
/*===========================================================================*/

OGRGeometry const* otb::ogr::Layer::GetSpatialFilter() const
{
  assert(m_Layer && "OGRLayer not initialized");
  OGRGeometry* spatialFilter = m_Layer->GetSpatialFilter();
  return spatialFilter;
}

void otb::ogr::Layer::SetSpatialFilter(OGRGeometry const* spatialFilter)
{
  assert(m_Layer && "OGRLayer not initialized");
  // const_cast because OGR is not 100% const-correct
  m_Layer->SetSpatialFilter(const_cast <OGRGeometry*>(spatialFilter));
}

199 200 201 202 203 204 205 206 207 208 209 210 211
void otb::ogr::Layer::SetSpatialFilterRect(
  double dfMinX, double dfMinY, double dfMaxX, double dfMaxY)
{
  assert(m_Layer && "OGRLayer not initialized");
  m_Layer->SetSpatialFilterRect(dfMinX, dfMinY, dfMaxX, dfMaxY);
}

OGRSpatialReference const* otb::ogr::Layer::GetSpatialRef() const
{
  assert(m_Layer && "OGRLayer not initialized");
  return m_Layer->GetSpatialRef();
}

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
/*===========================================================================*/
/*==========================[ Feature Definition ]===========================*/
/*===========================================================================*/
OGRFeatureDefn & otb::ogr::Layer::GetLayerDefn() const
{
  assert(m_Layer && "OGRLayer not initialized");
  return *const_cast <OGRLayer*>(m_Layer.get())->GetLayerDefn();
}

void otb::ogr::Layer::CreateField(
  OGRFieldDefn const& field, bool bApproxOK/* = true */)
{
  assert(m_Layer && "OGRLayer not initialized");
  const OGRErr res = m_Layer->CreateField(const_cast <OGRFieldDefn*>(&field), bApproxOK);
  if (res != OGRERR_NONE)
    {
Luc Hermitte's avatar
Luc Hermitte committed
228 229
    itkGenericExceptionMacro(<< "Cannot create a field in the layer <"
      <<GetName()<<">:" << CPLGetLastErrorMsg());
230 231 232 233 234 235
    }
}

void otb::ogr::Layer::DeleteField(size_t fieldIndex)
{
  assert(m_Layer && "OGRLayer not initialized");
236
#if GDAL_VERSION_NUM < 1900
Luc Hermitte's avatar
Luc Hermitte committed
237
  itkGenericExceptionMacro("OGRLayer::DeleteField is not supported by OGR v"
238
    << GDAL_VERSION_NUM << ". Upgrade to a version >= 1.9.0, and recompile OTB.")
239 240 241 242 243
#else
  const OGRErr res = m_Layer->DeleteField(int(fieldIndex));
  if (res != OGRERR_NONE)
    {
    itkGenericExceptionMacro(<< "Cannot delete the "<<fieldIndex << "th field in the layer <"
Luc Hermitte's avatar
Luc Hermitte committed
244
      <<GetName() <<">:" << CPLGetLastErrorMsg());
245 246 247 248 249
    }
#endif
}

void otb::ogr::Layer::AlterFieldDefn(
Luc Hermitte's avatar
Luc Hermitte committed
250
  size_t fieldIndex, OGRFieldDefn const& newFieldDefn, int nFlags)
251 252
{
  assert(m_Layer && "OGRLayer not initialized");
253
#if GDAL_VERSION_NUM < 1900
254
  itkGenericExceptionMacro("OGRLayer::AlterFieldDefn is not supported by OGR v"
255
    << GDAL_VERSION_NUM << ". Upgrade to a version >= 1.9.0, and recompile OTB.")
256
#else
Luc Hermitte's avatar
Luc Hermitte committed
257 258 259 260
  const OGRErr res = m_Layer->AlterFieldDefn(
    int(fieldIndex),
    const_cast <OGRFieldDefn*>(&newFieldDefn),
    nFlags);
261 262 263
  if (res != OGRERR_NONE)
    {
    itkGenericExceptionMacro(<< "Cannot alter the "<<fieldIndex << "th field in the layer <"
Luc Hermitte's avatar
Luc Hermitte committed
264
      <<GetName() <<">:" << CPLGetLastErrorMsg());
265 266 267 268 269 270 271
    }
#endif
}

void otb::ogr::Layer::ReorderField(size_t oldPos, size_t newPos)
{
  assert(m_Layer && "OGRLayer not initialized");
272
#if GDAL_VERSION_NUM < 1900
273
  itkGenericExceptionMacro("OGRLayer::ReorderField is not supported by OGR v"
274
    << GDAL_VERSION_NUM << ". Upgrade to a version >= 1.9.0, and recompile OTB.")
275 276 277 278 279
#else
  const OGRErr res = m_Layer->ReorderField(int(oldPos), int(newPos));
  if (res != OGRERR_NONE)
    {
    itkGenericExceptionMacro(<< "Cannot move the "<<oldPos << "th field to the "
Luc Hermitte's avatar
Luc Hermitte committed
280
      << newPos << "th position in the layer <" <<GetName() <<">:" << CPLGetLastErrorMsg());
281 282 283 284 285 286 287
    }
#endif
}

void otb::ogr::Layer::ReorderFields(int * map)
{
  assert(m_Layer && "OGRLayer not initialized");
288
#if GDAL_VERSION_NUM < 1900
289
  itkGenericExceptionMacro("OGRLayer::ReorderField is not supported by OGR v"
290
    << GDAL_VERSION_NUM << ". Upgrade to a version >= 1.9.0, and recompile OTB.")
291 292 293 294 295
#else
  const OGRErr res = m_Layer->ReorderFields(map);
  if (res != OGRERR_NONE)
    {
    itkGenericExceptionMacro(<< "Cannot reorder the fields of the layer <"
Luc Hermitte's avatar
Luc Hermitte committed
296
      <<GetName() <<">:" << CPLGetLastErrorMsg());
297 298 299
    }
#endif
}
Luc Hermitte's avatar
Luc Hermitte committed
300

Luc Hermitte's avatar
Luc Hermitte committed
301 302 303
void otb::ogr::Layer::SetIgnoredFields(char const** fieldNames)
{
  assert(m_Layer && "OGRLayer not initialized");
304
#if GDAL_VERSION_NUM >= 1900
Luc Hermitte's avatar
Luc Hermitte committed
305 306 307 308 309 310
  const OGRErr res = m_Layer->SetIgnoredFields(fieldNames);
  if (res != OGRERR_NONE)
    {
    itkGenericExceptionMacro(<< "Cannot set fields to ignore on the layer <"
      <<GetName() <<">:" << CPLGetLastErrorMsg());
    }
311 312 313 314
#else
  itkGenericExceptionMacro("OGRLayer::SetIgnoredFields is not supported by OGR v"
    << GDAL_VERSION_NUM << ". Upgrade to a version >= 1.9.0, and recompile OTB.")
#endif
Luc Hermitte's avatar
Luc Hermitte committed
315 316
}

Luc Hermitte's avatar
Luc Hermitte committed
317 318 319 320 321 322 323 324 325
OGRwkbGeometryType otb::ogr::Layer::GetGeomType() const
{
  assert(m_Layer && "OGRLayer not initialized");
#if GDAL_VERSION_NUM < 1800
  return GetLayerDefn().GetGeomType();
#else
  return m_Layer->GetGeomType();
#endif
}