VectorDataProjectionExample.cxx 4.49 KB
Newer Older
1
/*
Julien Michel's avatar
Julien Michel committed
2
 * Copyright (C) 2005-2019 Centre National d'Etudes Spatiales (CNES)
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * 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
// Let's assume that you have a KML file (hence in geographical coordinates)
23 24 25 26
// that you would like to superpose to some image with a specific map projection.
// Of course, you could use the handy ogr2ogr tool to do that, but it won't
// integrate so seamlessly into your OTB application.
//
27 28 29 30
// You can also suppose that the image on which you want to superpose
// the data is not in a specific map projection but a raw image from a
// particular sensor.  Thanks to OTB, the same code below will be able
// to do the appropriate conversion.
31 32 33 34 35 36 37 38 39 40 41 42
//
// This example demonstrates the use of the
// \doxygen{otb}{VectorDataProjectionFilter}.

#include "otbVectorDataProjectionFilter.h"
#include "otbVectorData.h"
#include "otbVectorDataFileReader.h"
#include "otbVectorDataFileWriter.h"

#include "otbImage.h"
#include "otbImageFileReader.h"

OTB Bot's avatar
OTB Bot committed
43
int main(int argc, char* argv[])
44
{
OTB Bot's avatar
OTB Bot committed
45
  if (argc < 4)
46 47
  {
    std::cout << argv[0] << " <input vector filename> <input image name> <output vector filename> " << std::endl;
48
    return EXIT_FAILURE;
49
  }
50

51
  // Declare the vector data type that you would like to use in your
52
  // application.
53

54 55
  using InputVectorDataType  = otb::VectorData<double>;
  using OutputVectorDataType = otb::VectorData<double>;
56

57
  // Declare and instantiate the vector data reader:
58 59 60
  // \doxygen{otb}{VectorDataFileReader}. The call to the
  // \code{UpdateOutputInformation()} method fill up the header information.

61 62
  using VectorDataFileReaderType           = otb::VectorDataFileReader<InputVectorDataType>;
  VectorDataFileReaderType::Pointer reader = VectorDataFileReaderType::New();
63 64 65 66

  reader->SetFileName(argv[1]);
  reader->UpdateOutputInformation();

67 68 69 70
  // We need the image only to retrieve its projection information,
  // i.e. map projection or sensor model parameters. Hence, the image
  // pixels won't be read, only the header information using the
  // \code{UpdateOutputInformation()} method.
71

72 73 74
  using ImageType                      = otb::Image<unsigned short int, 2>;
  using ImageReaderType                = otb::ImageFileReader<ImageType>;
  ImageReaderType::Pointer imageReader = ImageReaderType::New();
75 76 77
  imageReader->SetFileName(argv[2]);
  imageReader->UpdateOutputInformation();

78 79 80 81
  // The \doxygen{otb}{VectorDataProjectionFilter} will do the work of
  // converting the vector data coordinates. It is usually a good idea
  // to use it when you design applications reading or saving vector
  // data.
82

83 84
  using VectorDataFilterType                         = otb::VectorDataProjectionFilter<InputVectorDataType, OutputVectorDataType>;
  VectorDataFilterType::Pointer vectorDataProjection = VectorDataFilterType::New();
85

86 87 88
  // Information concerning the original projection of the vector data
  // will be automatically retrieved from the metadata. Nothing else
  // is needed from you:
89 90 91

  vectorDataProjection->SetInput(reader->GetOutput());

92
  // Information about the target projection is retrieved directly from
93
  // the image:
94 95 96 97 98 99

  vectorDataProjection->SetOutputKeywordList(imageReader->GetOutput()->GetImageKeywordlist());
  vectorDataProjection->SetOutputOrigin(imageReader->GetOutput()->GetOrigin());
  vectorDataProjection->SetOutputSpacing(imageReader->GetOutput()->GetSignedSpacing());
  vectorDataProjection->SetOutputProjectionRef(imageReader->GetOutput()->GetProjectionRef());

100
  // Finally, the result is saved into a new vector file.
101

102 103
  using VectorDataFileWriterType           = otb::VectorDataFileWriter<OutputVectorDataType>;
  VectorDataFileWriterType::Pointer writer = VectorDataFileWriterType::New();
104 105 106 107
  writer->SetFileName(argv[3]);
  writer->SetInput(vectorDataProjection->GetOutput());
  writer->Update();

108 109 110
  // It is worth noting that none of this code is specific to the
  // vector data format. Whether you pass a shapefile, or a KML file,
  // the correct driver will be automatically instantiated.
111 112

  return EXIT_SUCCESS;
113
}