VectorDataExtractROIExample.cxx 4.91 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 31

// Software Guide : BeginLatex
//
// There is some vector data sets widely available on the internet. These data
// sets can be huge, covering an entire country, with hundreds of thousands
// objects.
//
// Most of the time, you won't be interested in the whole area and would like
// to focuss only on the area corresponding to your satellite image.
//
32 33 34
// The \doxygen{otb}{VectorDataExtractROI} is able to extract the area corresponding
// to your satellite image, even if the image is still in sensor geometry (provided
// the sensor model is supported by OTB). Let's see how we can do that.
35 36 37 38 39 40 41 42 43 44 45
//
// This example demonstrates the use of the
// \doxygen{otb}{VectorDataExtractROI}.
//
// Software Guide : EndLatex

#include "otbVectorData.h"
#include "otbVectorDataExtractROI.h"

#include "otbVectorDataFileReader.h"
#include "otbVectorDataFileWriter.h"
46
#include "otbImageMetadataInterfaceFactory.h"
47 48 49 50

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

OTB Bot's avatar
STYLE  
OTB Bot committed
51
int main(int argc, char* argv[])
52
{
OTB Bot's avatar
STYLE  
OTB Bot committed
53 54 55 56 57 58
  if (argc < 4)
    {
    std::cout << argv[0] <<
    " <input vector filename> <input image name> <output vector filename>  "
              <<
    std::endl;
59 60

    return EXIT_FAILURE;
OTB Bot's avatar
STYLE  
OTB Bot committed
61
    }
62 63 64 65 66

  const char * inVectorName = argv[1];
  const char * inImageName = argv[2];
  const char * outVectorName = argv[3];

OTB Bot's avatar
STYLE  
OTB Bot committed
67 68
  typedef double            Type;
  typedef otb::VectorData<> VectorDataType;
69

OTB Bot's avatar
STYLE  
OTB Bot committed
70 71
  typedef otb::VectorDataFileReader<VectorDataType> VectorDataFileReaderType;
  typedef otb::VectorDataFileWriter<VectorDataType> VectorDataWriterType;
72

OTB Bot's avatar
STYLE  
OTB Bot committed
73
  typedef   otb::RemoteSensingRegion<Type> TypedRegion;
74

OTB Bot's avatar
STYLE  
OTB Bot committed
75 76
  typedef otb::Image<unsigned char, 2>    ImageType;
  typedef otb::ImageFileReader<ImageType> ImageReaderType;
77 78 79 80
  ImageReaderType::Pointer imageReader = ImageReaderType::New();
  imageReader->SetFileName(inImageName);
  imageReader->UpdateOutputInformation();

81 82 83 84 85 86
  // Software Guide : BeginLatex
  //
  // After the usual declaration (you can check the source file for the details),
  // we can declare the \doxygen{otb}{VectorDataExtractROI}:
  //
  // Software Guide : EndLatex
87

88
  // Software Guide : BeginCodeSnippet
OTB Bot's avatar
STYLE  
OTB Bot committed
89 90
  typedef otb::VectorDataExtractROI<VectorDataType> FilterType;
  FilterType::Pointer filter = FilterType::New();
91 92 93 94 95 96 97 98 99 100 101 102
  // Software Guide : EndCodeSnippet

  // Software Guide : BeginLatex
  //
  // Then, we need to specify the region to extract. This region is a bit special as
  // it contains also information related to its reference system (cartographic projection
  // or sensor model projection). We retrieve all these information from the image
  // we gave as an input.
  //
  // Software Guide : EndLatex

  // Software Guide : BeginCodeSnippet
OTB Bot's avatar
STYLE  
OTB Bot committed
103 104 105
  TypedRegion            region;
  TypedRegion::SizeType  size;
  TypedRegion::IndexType index;
106 107

  size[0]  = imageReader->GetOutput()->GetLargestPossibleRegion().GetSize()[0]
108
             * imageReader->GetOutput()->GetSignedSpacing()[0];
109
  size[1]  = imageReader->GetOutput()->GetLargestPossibleRegion().GetSize()[1]
110
             * imageReader->GetOutput()->GetSignedSpacing()[1];
111
  index[0] = imageReader->GetOutput()->GetOrigin()[0]
112
             - 0.5 * imageReader->GetOutput()->GetSignedSpacing()[0];
113
  index[1] = imageReader->GetOutput()->GetOrigin()[1]
114
             - 0.5 * imageReader->GetOutput()->GetSignedSpacing()[1];
115 116 117
  region.SetSize(size);
  region.SetOrigin(index);

118
  otb::ImageMetadataInterfaceBase::Pointer imageMetadataInterface
OTB Bot's avatar
STYLE  
OTB Bot committed
119 120
    = otb::ImageMetadataInterfaceFactory::CreateIMI(
    imageReader->GetOutput()->GetMetaDataDictionary());
121
  region.SetRegionProjection(
122
    imageMetadataInterface->GetProjectionRef());
123

124 125
  region.SetKeywordList(imageReader->GetOutput()->GetImageKeywordlist());

126
  filter->SetRegion(region);
127
  // Software Guide : EndCodeSnippet
128

129 130 131
  VectorDataFileReaderType::Pointer reader = VectorDataFileReaderType::New();
  VectorDataWriterType::Pointer     writer = VectorDataWriterType::New();
  reader->SetFileName(inVectorName);
132
  writer->SetFileName(outVectorName);
133 134 135 136 137 138 139 140 141

  // Software Guide : BeginLatex
  //
  // And finally, we can plug the filter in the pipeline:
  //
  // Software Guide : EndLatex

  // Software Guide : BeginCodeSnippet
  filter->SetInput(reader->GetOutput());
142
  writer->SetInput(filter->GetOutput());
143 144
  // Software Guide : EndCodeSnippet

145 146 147 148
  writer->Update();

  return EXIT_SUCCESS;
}