GeometriesFilter.cxx 3.81 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 2005-2017 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.
 */
20 21 22 23 24 25 26 27 28 29 30


/*===========================================================================*/
/*===============================[ Includes ]================================*/
/*===========================================================================*/
#include "otbGeometriesToGeometriesFilter.h"
#include <string>
#include <iostream>
#include "otbGeometriesSet.h"

/*===========================================================================*/
31
/*=============================[ SwapXYFunctor ]=============================*/
32 33 34 35 36 37
/*===========================================================================*/
struct SwapXYFunctor
{
  typedef OGRGeometry TransformedElementType;
  otb::ogr::UniqueGeometryPtr operator()(OGRGeometry const* in) const
    {
38
    otb::ogr::UniqueGeometryPtr out(in ? in->clone() : nullptr);
39 40
    if (out)
      {
41
#if GDAL_VERSION_NUM >= 1900
42
      out->swapXY();
43 44 45
#else
      assert(!"Sorry, This example filter requires GDAL v1.9.0 or later");
#endif
46
      }
47
    return otb::move(out);
48 49 50
    }
};

51 52 53 54 55
/*===========================================================================*/
/*=================================[ main ]==================================*/
/*===========================================================================*/
typedef otb::DefaultGeometriesToGeometriesFilter<SwapXYFunctor> FilterType;

56 57 58 59
int main (int argc, char **argv)
{
  if (argc < 2)
    {
60
    std::cerr << argv[0] << " inputGeometriesFile [outputGeometriesFile]" << std::endl;
61 62 63 64
    return EXIT_FAILURE;
    }
  try
    {
65
    const bool workingInplace = argc == 2;
66
    const bool outputIsStdout = !workingInplace && !strcmp(argv[2], "-");
67 68 69 70 71 72

    const std::string inputFile = argv[1];
    const std::string outputFile = workingInplace ? argv[1] : argv[2];

    otb::ogr::DataSource::Pointer input = otb::ogr::DataSource::New(
      inputFile,
73
      workingInplace ? otb::ogr::DataSource::Modes::Update_LayerOverwrite : otb::ogr::DataSource::Modes::Read);
74 75 76

    otb::ogr::DataSource::Pointer output
      = workingInplace ? input
77
      : outputIsStdout ? nullptr
78
      :                  otb::ogr::DataSource::New( outputFile, otb::ogr::DataSource::Modes::Update_LayerCreateOnly);
79
    std::cout << "input: " << otb::ogr::version_proxy::GetFileListAsStringVector(&input->ogr())[0] << " should be: " << inputFile << "\n";
80 81
    if (output)
      {
82
      std::cout << "output: " << otb::ogr::version_proxy::GetFileListAsStringVector(&output->ogr())[0] << " should be: " << outputFile << "\n";
83 84
      }
    // std::cout << "\n";
85

86
    FilterType::Pointer filter = FilterType::New();
87 88 89 90 91 92 93 94 95 96 97 98 99
    if (!workingInplace)
      {
      otb::GeometriesSet::Pointer in_set = otb::GeometriesSet::New(input);
      filter->SetInput(in_set);
      }
    if (output)
      {
      otb::GeometriesSet::Pointer out_set = otb::GeometriesSet::New(output);
      filter->SetOutput(out_set);
      out_set->Update();
      }
    else
      {
100
      filter->UpdateOutputData(nullptr);
101 102 103
      assert(filter->GetOutput() && "output not set");
      filter->GetOutput()->Print(std::cout, 0);
      }
104 105 106 107 108 109 110 111 112

    return EXIT_SUCCESS;
    }
  catch (std::exception const& e)
    {
    std::cerr << e.what() << "\n";
    }
  return EXIT_FAILURE;
}