otbOGRDataSourceWrapper.cxx 9.78 KB
Newer Older
Luc Hermitte's avatar
Luc Hermitte committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
/*=========================================================================

  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.

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

#include "otbOGRDataSourceWrapper.h"
// standard includes
#include <cassert>
#include <boost/bind.hpp>
// ITK includes
#include "itkMacro.h" // itkExceptionMacro
#include "itkMetaDataObject.h"
#include "itkExceptionObject.h"
// OTB includes
#include "otbMacro.h"
#include "otbMetaDataKey.h"
#include "otbOGRDriversInit.h"
// OGR includes
#include "ogrsf_frmts.h"

void
otb::ogr::DataSource
::SetProjectionRef(const std::string& projectionRef)
{
  itk::MetaDataDictionary& dict = this->GetMetaDataDictionary();
  itk::EncapsulateMetaData<std::string>(
    dict, MetaDataKey::ProjectionRefKey, projectionRef);
  this->Modified();
}

std::string
otb::ogr::DataSource
::GetProjectionRef() const
{
  const itk::MetaDataDictionary& dict = this->GetMetaDataDictionary();

  std::string projectionRef;
  itk::ExposeMetaData<std::string>(dict, MetaDataKey::ProjectionRefKey, projectionRef);

  return projectionRef;
}

/*===========================================================================*/
/*=======================[ construction/destruction ]========================*/
/*===========================================================================*/
bool otb::ogr::DataSource::Clear()
{
  Reset(0);
  return true;
}

void otb::ogr::DataSource::Reset(OGRDataSource * source)
{
  if (m_DataSource) {
    // OGR makes a pointless check for non-nullity in
    // OGRDataSource::DestroyDataSource (pointless because "delete 0" is
    // perfectly valid -> it's a no-op)
    OGRDataSource::DestroyDataSource(m_DataSource); // void, noexcept
  }
  m_DataSource = source;
}

otb::ogr::DataSource::DataSource()
: m_DataSource(0)
{
  Drivers::Init();

OTB Bot's avatar
STYLE  
OTB Bot committed
81
  OGRSFDriver * d = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName("Memory");
Luc Hermitte's avatar
Luc Hermitte committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
  assert(d && "OGR Memory driver not found");
  m_DataSource = d->CreateDataSource("in-memory");
  if (!m_DataSource) {
    itkExceptionMacro(<< "Failed to create OGRMemDataSource");
  }
  m_DataSource->SetDriver(d);
}

otb::ogr::DataSource::DataSource(OGRDataSource * source)
: m_DataSource(source)
{
}

otb::ogr::DataSource::Pointer
otb::ogr::DataSource::New(std::string const& filename, Modes::type mode)
{
  Drivers::Init();

  const bool update = mode & Modes::write;
  OGRDataSource * source = OGRSFDriverRegistrar::Open(filename.c_str(), update);
  if (!source) {
    itkGenericExceptionMacro(<< "Failed to open OGRDataSource file " << filename);
  }
  Pointer res = new DataSource(source);
  return res;
}

OTB Bot's avatar
STYLE  
OTB Bot committed
109
/*static*/
Luc Hermitte's avatar
Luc Hermitte committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
otb::ogr::DataSource::Pointer
otb::ogr::DataSource::New(OGRDataSource * source)
{
  Pointer res = new DataSource(source);
  return res;
}

/*virtual*/  otb::ogr::DataSource::~DataSource()
{
  Clear();
}

/*===========================================================================*/
/*================================[ layers ]=================================*/
/*===========================================================================*/
125 126
otb::ogr::DataSource::const_iterator otb::ogr::DataSource::cbegin() const
{
127 128 129
  return const_iterator(*this, 0);
  // assert(!"not-ready");
  // return const_iterator();
130 131 132 133
}

otb::ogr::DataSource::const_iterator otb::ogr::DataSource::cend() const
{
134 135 136
  return const_iterator(*this, GetLayersCount());
  // assert(!"not-ready");
  // return const_iterator();
137 138 139 140 141 142 143 144 145 146 147 148
}

otb::ogr::DataSource::iterator otb::ogr::DataSource::begin()
{
  return iterator(*this, 0);
}

otb::ogr::DataSource::iterator otb::ogr::DataSource::end()
{
  return iterator(*this, GetLayersCount());
}

149
otb::ogr::Layer otb::ogr::DataSource::CreateLayer(
OTB Bot's avatar
STYLE  
OTB Bot committed
150 151 152
  std::string const& name,
  OGRSpatialReference * poSpatialRef/* = NULL */,
  OGRwkbGeometryType eGType/* = wkbUnknown */,
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
  char ** papszOptions/* = NULL */)
{
  assert(m_DataSource && "Datasource not initialized");
  OGRLayer * ol = m_DataSource->CreateLayer(
    name.c_str(), poSpatialRef, eGType, papszOptions);
  if (!ol)
    {
    itkGenericExceptionMacro(<< "Failed to create the layer <"<<name
      << "> in the OGRDataSource file " << m_DataSource->GetName());
    }
  Layer l(ol);
  return l;
}

otb::ogr::Layer otb::ogr::DataSource::CopyLayer(
OTB Bot's avatar
STYLE  
OTB Bot committed
168 169
  Layer & srcLayer,
  std::string const& newName,
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
  char ** papszOptions/* = NULL */)
{
  assert(m_DataSource && "Datasource not initialized");
  OGRLayer * l0 = &srcLayer.ogr();
  OGRLayer * ol = m_DataSource->CopyLayer(l0, newName.c_str(), papszOptions);
  if (!ol)
    {
    itkGenericExceptionMacro(<< "Failed to copy the layer <"
      << srcLayer.GetName() << "> into the new layer <" <<newName
      << "> in the OGRDataSource file " << m_DataSource->GetName());
    }
  Layer l(ol);
  return l;
}

void otb::ogr::DataSource::DeleteLayer(size_t i)
Luc Hermitte's avatar
Luc Hermitte committed
186 187 188 189
{
  const int nb_layers = GetLayersCount();
  if (int(i) >= nb_layers)
    {
190 191
    itkExceptionMacro(<< "Cannot delete " << i << "th layer in the OGRDataSource <"
      << m_DataSource->GetName() << "> as it contains only " << nb_layers << "layers.");
Luc Hermitte's avatar
Luc Hermitte committed
192
    }
193 194
  const OGRErr err = m_DataSource->DeleteLayer(int(i));
  if (err != OGRERR_NONE)
Luc Hermitte's avatar
Luc Hermitte committed
195
    {
196 197
    itkExceptionMacro(<< "Cannot delete " << i << "th layer in the OGRDataSource <"
      << m_DataSource->GetName() << ">.");
Luc Hermitte's avatar
Luc Hermitte committed
198 199 200
    }
}

201 202

otb::ogr::Layer otb::ogr::DataSource::GetLayerChecked(size_t i)
Luc Hermitte's avatar
Luc Hermitte committed
203
{
204 205 206 207 208 209 210 211 212 213 214 215 216
  const int nb_layers = GetLayersCount();
  if (int(i) >= nb_layers)
    {
    itkExceptionMacro(<< "Cannot fetch " << i << "th layer in the OGRDataSource <"
      << m_DataSource->GetName() << "> as it contains only " << nb_layers << "layers.");
    }
  OGRLayer * layer_ptr = m_DataSource->GetLayer(int(i));
  if (!layer_ptr)
    {
    itkExceptionMacro( << "Unexpected error: cannot fetch " << i << "th layer in the OGRDataSource <"
      << m_DataSource->GetName() << ">.");
    }
    return otb::ogr::Layer(layer_ptr);
Luc Hermitte's avatar
Luc Hermitte committed
217 218 219 220
}

OGRLayer* otb::ogr::DataSource::GetLayerUnchecked(size_t i)
{
221
  assert(m_DataSource && "Datasource not initialized");
Luc Hermitte's avatar
Luc Hermitte committed
222
  OGRLayer * layer_ptr = m_DataSource->GetLayer(int(i));
Luc Hermitte's avatar
Luc Hermitte committed
223
  return layer_ptr;
Luc Hermitte's avatar
Luc Hermitte committed
224 225
}

226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
otb::ogr::Layer otb::ogr::DataSource::GetLayer(std::string const& name)
{
  assert(m_DataSource && "Datasource not initialized");
  OGRLayer * layer_ptr = m_DataSource->GetLayerByName(name.c_str());
  return otb::ogr::Layer(layer_ptr);
}


otb::ogr::Layer otb::ogr::DataSource::GetLayerChecked(std::string const& name)
{
  assert(m_DataSource && "Datasource not initialized");
  OGRLayer * layer_ptr = m_DataSource->GetLayerByName(name.c_str());
  if (!layer_ptr)
    {
    itkExceptionMacro( << "Cannot fetch any layer named <" << name
      << "> in the OGRDataSource <" << m_DataSource->GetName() << ">.");
    }
  return otb::ogr::Layer(layer_ptr);
}

Luc Hermitte's avatar
Luc Hermitte committed
246 247
int otb::ogr::DataSource::GetLayersCount() const
{
248
  assert(m_DataSource && "Datasource not initialized");
Luc Hermitte's avatar
Luc Hermitte committed
249 250 251
  return m_DataSource->GetLayerCount();
}

252
otb::ogr::Layer otb::ogr::DataSource::ExecuteSQL(
OTB Bot's avatar
STYLE  
OTB Bot committed
253 254
  std::string const& statement,
  OGRGeometry * poSpatialFilter,
255 256 257 258 259 260 261 262 263 264
  char        const* pszDialect)
{
  assert(m_DataSource && "Datasource not initialized");
  OGRLayer * layer_ptr = m_DataSource->ExecuteSQL(
    statement.c_str(), poSpatialFilter, pszDialect);
  if (!layer_ptr)
    {
#if defined(PREFER_EXCEPTION)
    itkExceptionMacro( << "Unexpected error: cannot execute the SQL request <" << statement
      << "> in the OGRDataSource <" << m_DataSource->GetName() << ">.");
OTB Bot's avatar
STYLE  
OTB Bot committed
265
#else
266 267 268 269 270 271 272 273 274 275 276
    // Cannot use the deleter made for result sets obtained from
    // OGRDataSource::ExecuteSQL because it checks for non-nullity....
    // *sigh*
    return otb::ogr::Layer(0);
#endif
    }
  return otb::ogr::Layer(layer_ptr, m_DataSource);

}


Luc Hermitte's avatar
Luc Hermitte committed
277 278 279 280 281 282 283 284
/*===========================================================================*/
/*===============================[ features ]================================*/
/*===========================================================================*/
namespace  { // Anonymous namespace
  struct AccuLayersSizes
    {
    AccuLayersSizes(bool doForceComputation)
      : m_doForceComputation(doForceComputation) { }
285
    int operator()(otb::ogr::Layer const& layer, int accumulated) const
Luc Hermitte's avatar
Luc Hermitte committed
286 287 288 289 290 291 292
      {
      const int loc_size = layer.GetFeatureCount(m_doForceComputation);
      return loc_size < 0 ? loc_size : loc_size+accumulated;
      }
  private:
    bool m_doForceComputation;
    };
OTB Bot's avatar
STYLE  
OTB Bot committed
293
} // Anonymous namespace
Luc Hermitte's avatar
Luc Hermitte committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307

int otb::ogr::DataSource::Size(bool doForceComputation) const
{
  return AccumulateOnLayers(AccuLayersSizes(doForceComputation), 0);
}

/*===========================================================================*/
/*=================================[ Misc ]==================================*/
/*===========================================================================*/

/*virtual*/
void otb::ogr::DataSource::PrintSelf(
  std::ostream& os, itk::Indent indent) const
{
308 309
  assert(m_DataSource && "Datasource not initialized");
  ForEachLayer(boost::bind(&Layer::PrintSelf, _1, boost::ref(os), indent.GetNextIndent()));
Luc Hermitte's avatar
Luc Hermitte committed
310 311 312 313 314 315
}

/*virtual*/ void otb::ogr::DataSource::Graft(const itk::DataObject * data)
{
  assert(! "Disabled to check if it makes sense...");
}
316

317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
bool otb::ogr::DataSource::HasCapability(std::string const& capabilityName)
{
  assert(m_DataSource && "Datasource not initialized");
  return m_DataSource->TestCapability(capabilityName.c_str());
}

void otb::ogr::DataSource::SyncToDisk()
{
  assert(m_DataSource && "Datasource not initialized");
  const OGRErr res= m_DataSource->SyncToDisk();
  if (res != OGRERR_NONE)
    {
    itkExceptionMacro( << "Cannot flush the pending of the OGRDataSource <"
      << m_DataSource->GetName() << ">.");
    }
}