<commit_before>/*
 * Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES)
 *
 * This file is part of Orfeo Toolbox
 *
 *     https://www.orfeo-toolbox.org/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "otbOGRIOHelper.h"

#include "otbMacro.h"
#include "ogrsf_frmts.h"
#include "otbOGR.h"
#include "otbStopwatch.h"

namespace otb
{

void
OGRIOHelper
::ConvertGeometryToPointNode(const OGRGeometry * ogrGeometry, DataNodePointerType node) const
{
  OGRPoint * ogrPoint = (OGRPoint *) ogrGeometry;

  if (ogrPoint == nullptr)
    {
    itkGenericExceptionMacro(<< "Failed to convert OGRGeometry to OGRPoint");
    }

  PointType otbPoint;
  otbPoint.Fill(0);
  otbPoint[0] = static_cast<DataNodeType::PrecisionType>(ogrPoint->getX());
  otbPoint[1] = static_cast<DataNodeType::PrecisionType>(ogrPoint->getY());

  if (DataNodeType::Dimension > 2)
    {
    if (PointType::PointDimension != 3)
      {
      itkGenericExceptionMacro(<< "OTB vector data can't contain the OGR information (2D instead of 2.5D)");
      }
    otbPoint[2] = static_cast<DataNodeType::PrecisionType>(ogrPoint->getZ());
    }

  node->SetPoint(otbPoint);
}


void
OGRIOHelper
::ConvertGeometryToLineNode(const OGRGeometry * ogrGeometry, DataNodePointerType node) const
{
  OGRLineString * ogrLine = (OGRLineString *) ogrGeometry;

  if (ogrLine == nullptr)
    {
    itkGenericExceptionMacro(<< "Failed to convert OGRGeometry to OGRLine");
    }

  LinePointerType line = LineType::New();

  OGRPoint * ogrTmpPoint = (OGRPoint *) OGRGeometryFactory::createGeometry(wkbPoint);

  for (int pIndex = 0; pIndex < ogrLine->getNumPoints(); ++pIndex)
    {

    ogrLine->getPoint(pIndex, ogrTmpPoint);

    LineType::VertexType vertex;

    vertex[0] = ogrTmpPoint->getX();
    vertex[1] = ogrTmpPoint->getY();

    if (DataNodeType::Dimension > 2)
      {
      if (LineType::VertexType::PointDimension != 3)
        {
        itkGenericExceptionMacro(<< "OTB vector data can't contain the OGR information (2D instead of 2.5D)");
        }
      vertex[2] = ogrTmpPoint->getZ();
      }

    line->AddVertex(vertex);
    }
  OGRGeometryFactory::destroyGeometry(ogrTmpPoint);

  node->SetLine(line);
}


void
OGRIOHelper
::ConvertGeometryToPolygonNode(const OGRGeometry * ogrGeometry, DataNodePointerType node) const
{
  OGRPolygon * ogrPolygon = (OGRPolygon *) ogrGeometry;

  if (ogrPolygon == nullptr)
    {
    itkGenericExceptionMacro(<< "Failed to convert OGRGeometry to OGRPolygon");
    }

  OGRPoint *      ogrTmpPoint = (OGRPoint *) OGRGeometryFactory::createGeometry(wkbPoint);
  OGRLinearRing * ogrRing = ogrPolygon->getExteriorRing();

  PolygonPointerType extRing = PolygonType::New();

  for (int pIndex = 0; pIndex < ogrRing->getNumPoints(); ++pIndex)
    {
    ogrRing->getPoint(pIndex, ogrTmpPoint);
    PolygonType::VertexType vertex;
    vertex[0] = ogrTmpPoint->getX();
    vertex[1] = ogrTmpPoint->getY();

    if (DataNodeType::Dimension > 2)
      {
      if (PolygonType::VertexType::PointDimension != 3)
        {
        itkGenericExceptionMacro(<< "OTB vector data can't contain the OGR information (2D instead of 2.5D)");
        }
      vertex[2] = ogrTmpPoint->getZ();
      }

    extRing->AddVertex(vertex);
    }

  PolygonListPointerType intRings = PolygonListType::New();

  for (int intRingIndex = 0; intRingIndex < ogrPolygon->getNumInteriorRings(); ++intRingIndex)
    {
    PolygonPointerType ring = PolygonType::New();
    ogrRing = ogrPolygon->getInteriorRing(intRingIndex);
    for (int pIndex = 0; pIndex < ogrRing->getNumPoints(); ++pIndex)
      {
      ogrRing->getPoint(pIndex, ogrTmpPoint);
      PolygonType::VertexType vertex;

      vertex[0] = ogrTmpPoint->getX();
      vertex[1] = ogrTmpPoint->getY();
      if (DataNodeType::Dimension > 2)
        {
        if (PolygonType::VertexType::PointDimension != 3)
          {
          itkGenericExceptionMacro(<< "OTB vector data can't contain the OGR information (2D instead of 2.5D)");
          }
        vertex[2] = ogrTmpPoint->getZ();
        }
      ring->AddVertex(vertex);
      }
    intRings->PushBack(ring);
    }

  OGRGeometryFactory::destroyGeometry(ogrTmpPoint);

  node->SetPolygonExteriorRing(extRing);
  node->SetPolygonInteriorRings(intRings);
}


OGRIOHelper
::OGRIOHelper()
{
  otb::ogr::Drivers::Init();
}


OGRIOHelper
::~OGRIOHelper()
{}


void OGRIOHelper
::ConvertOGRLayerToDataTreeNode(OGRLayer * layer, InternalTreeNodeType * documentPtr) const
{
  /** Temporary pointer to store the feature */
  OGRFeature * feature;

  layer->ResetReading();

  unsigned int   counter = 0;
  otb::Stopwatch chrono = otb::Stopwatch::StartNew();

  while ((feature = layer->GetNextFeature()) != nullptr)
    {

    // A pointer to the current multi-geometry
    InternalTreeNodeType::Pointer multiPtr;

    /** Temporary geometry container */
    OGRGeometry * geometry = feature->GetGeometryRef();

    if (geometry == nullptr)
      {
      OGRFeature::DestroyFeature(feature);
      ++counter;
      continue;
      }<commit_msg>DOC: Add a warning when using 3D vector data along with VectorDataIO<commit_after>/*
 * Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES)
 *
 * This file is part of Orfeo Toolbox
 *
 *     https://www.orfeo-toolbox.org/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "otbOGRIOHelper.h"

#include "otbMacro.h"
#include "ogrsf_frmts.h"
#include "otbOGR.h"
#include "otbStopwatch.h"

namespace otb
{

void
OGRIOHelper
::ConvertGeometryToPointNode(const OGRGeometry * ogrGeometry, DataNodePointerType node) const
{
  OGRPoint * ogrPoint = (OGRPoint *) ogrGeometry;

  if (ogrPoint == nullptr)
    {
    itkGenericExceptionMacro(<< "Failed to convert OGRGeometry to OGRPoint");
    }

  PointType otbPoint;
  otbPoint.Fill(0);
  otbPoint[0] = static_cast<DataNodeType::PrecisionType>(ogrPoint->getX());
  otbPoint[1] = static_cast<DataNodeType::PrecisionType>(ogrPoint->getY());

  if (DataNodeType::Dimension > 2)
    {
    if (PointType::PointDimension != 3)
      {
      itkGenericExceptionMacro(<< "OTB vector data can't contain the OGR information (2D instead of 2.5D)");
      }
    otbPoint[2] = static_cast<DataNodeType::PrecisionType>(ogrPoint->getZ());
    }

  node->SetPoint(otbPoint);
}


void
OGRIOHelper
::ConvertGeometryToLineNode(const OGRGeometry * ogrGeometry, DataNodePointerType node) const
{
  OGRLineString * ogrLine = (OGRLineString *) ogrGeometry;

  if (ogrLine == nullptr)
    {
    itkGenericExceptionMacro(<< "Failed to convert OGRGeometry to OGRLine");
    }

  LinePointerType line = LineType::New();

  OGRPoint * ogrTmpPoint = (OGRPoint *) OGRGeometryFactory::createGeometry(wkbPoint);

  for (int pIndex = 0; pIndex < ogrLine->getNumPoints(); ++pIndex)
    {

    ogrLine->getPoint(pIndex, ogrTmpPoint);

    LineType::VertexType vertex;

    vertex[0] = ogrTmpPoint->getX();
    vertex[1] = ogrTmpPoint->getY();

    if (DataNodeType::Dimension > 2)
      {
      if (LineType::VertexType::PointDimension != 3)
        {
        itkGenericExceptionMacro(<< "OTB vector data can't contain the OGR information (2D instead of 2.5D)");
        }
      vertex[2] = ogrTmpPoint->getZ();
      }

    line->AddVertex(vertex);
    }
  OGRGeometryFactory::destroyGeometry(ogrTmpPoint);

  node->SetLine(line);
}


void
OGRIOHelper
::ConvertGeometryToPolygonNode(const OGRGeometry * ogrGeometry, DataNodePointerType node) const
{
  OGRPolygon * ogrPolygon = (OGRPolygon *) ogrGeometry;

  if (ogrPolygon == nullptr)
    {
    itkGenericExceptionMacro(<< "Failed to convert OGRGeometry to OGRPolygon");
    }

  OGRPoint *      ogrTmpPoint = (OGRPoint *) OGRGeometryFactory::createGeometry(wkbPoint);
  OGRLinearRing * ogrRing = ogrPolygon->getExteriorRing();

  PolygonPointerType extRing = PolygonType::New();

  for (int pIndex = 0; pIndex < ogrRing->getNumPoints(); ++pIndex)
    {
    ogrRing->getPoint(pIndex, ogrTmpPoint);
    PolygonType::VertexType vertex;
    vertex[0] = ogrTmpPoint->getX();
    vertex[1] = ogrTmpPoint->getY();

    if (DataNodeType::Dimension > 2)
      {
      if (PolygonType::VertexType::PointDimension != 3)
        {
        itkGenericExceptionMacro(<< "OTB vector data can't contain the OGR information (2D instead of 2.5D)");
        }
      vertex[2] = ogrTmpPoint->getZ();
      }

    extRing->AddVertex(vertex);
    }

  PolygonListPointerType intRings = PolygonListType::New();

  for (int intRingIndex = 0; intRingIndex < ogrPolygon->getNumInteriorRings(); ++intRingIndex)
    {
    PolygonPointerType ring = PolygonType::New();
    ogrRing = ogrPolygon->getInteriorRing(intRingIndex);
    for (int pIndex = 0; pIndex < ogrRing->getNumPoints(); ++pIndex)
      {
      ogrRing->getPoint(pIndex, ogrTmpPoint);
      PolygonType::VertexType vertex;

      vertex[0] = ogrTmpPoint->getX();
      vertex[1] = ogrTmpPoint->getY();
      if (DataNodeType::Dimension > 2)
        {
        if (PolygonType::VertexType::PointDimension != 3)
          {
          itkGenericExceptionMacro(<< "OTB vector data can't contain the OGR information (2D instead of 2.5D)");
          }
        vertex[2] = ogrTmpPoint->getZ();
        }
      ring->AddVertex(vertex);
      }
    intRings->PushBack(ring);
    }

  OGRGeometryFactory::destroyGeometry(ogrTmpPoint);

  node->SetPolygonExteriorRing(extRing);
  node->SetPolygonInteriorRings(intRings);
}


OGRIOHelper
::OGRIOHelper()
{
  otb::ogr::Drivers::Init();
}


OGRIOHelper
::~OGRIOHelper()
{}


void OGRIOHelper
::ConvertOGRLayerToDataTreeNode(OGRLayer * layer, InternalTreeNodeType * documentPtr) const
{
  /** Temporary pointer to store the feature */
  OGRFeature * feature;

  layer->ResetReading();

  // Warn user that 3D data are not supported for reading/writing
  auto geomType = layer->GetGeomType();

  if(geomType == wkbPoint25D 
     || geomType == wkbLineString25D 
     || geomType == wkbPolygon25D 
     || geomType == wkbMultiPoint25D 
     || geomType == wkbMultiLineString25D 
     || geomType == wkbMultiPolygon25D 
     || geomType == wkbGeometryCollection25D)
    {
    otbLogMacro(Warning,<<"OGRVectorDataIO does not support 3D data. "<<OGRGeometryTypeToName(geomType)<<" will be converted to 2D upon reading."); 
    }


  unsigned int   counter = 0;
  otb::Stopwatch chrono = otb::Stopwatch::StartNew();

  while ((feature = layer->GetNextFeature()) != nullptr)
    {

    // A pointer to the current multi-geometry
    InternalTreeNodeType::Pointer multiPtr;

    /** Temporary geometry container */
    OGRGeometry * geometry = feature->GetGeometryRef();

    if (geometry == nullptr)
      {
      OGRFeature::DestroyFeature(feature);
      ++counter;
      continue;
      }
{'fraction_non_alphanumeric': 0.12137789554662792, 'fraction_numerical': 0.01008633216514232, 'mean_word_length': 3.740680713128039, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 82, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5701163', 'n_tokens_mistral': 4005, 'n_tokens_neox': 3814, 'n_words': 858}