otbImageSampleExtractorFilterTest.cxx 5.37 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*=========================================================================

  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 "otbImageSampleExtractorFilter.h"
#include "otbVectorImage.h"
#include "otbImage.h"
22
#include "itkPhysicalPointImageSource.h"
Guillaume Pasero's avatar
Guillaume Pasero committed
23
#include "itkTimeProbe.h"
24
25
26
27
28
29
#include <fstream>

int otbImageSampleExtractorFilterNew(int itkNotUsed(argc), char* itkNotUsed(argv) [])
{
  typedef otb::VectorImage<float> InputImageType;
  typedef otb::ImageSampleExtractorFilter<InputImageType> FilterType;
30

31
32
33
34
35
36
37
38
39
  FilterType::Pointer filter = FilterType::New();
  std::cout << filter << std::endl;
  return EXIT_SUCCESS;
}

int otbImageSampleExtractorFilter(int argc, char* argv[])
{
  typedef otb::VectorImage<float> InputImageType;
  typedef otb::ImageSampleExtractorFilter<InputImageType> FilterType;
40

41
42
  if (argc < 3)
    {
43
    std::cout << "Usage : "<<argv[0]<< "  input_vector  output" << std::endl;
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

  std::string vectorPath(argv[1]);
  std::string outputPath(argv[2]);

  otb::ogr::DataSource::Pointer vectors = otb::ogr::DataSource::New(vectorPath);
  otb::ogr::DataSource::Pointer output =
    otb::ogr::DataSource::New(outputPath,otb::ogr::DataSource::Modes::Overwrite);

  InputImageType::RegionType region;
  region.SetSize(0,99);
  region.SetSize(1,50);

  InputImageType::PointType origin;
  origin.Fill(0.5);

  InputImageType::SpacingType spacing;
  spacing[0] = 1.0;
  spacing[1] = -1.0;

  std::string classFieldName("label");
  std::string outputPrefix("measure_");

  typedef itk::PhysicalPointImageSource<InputImageType> ImageSourceType;
  ImageSourceType::Pointer imgSource = ImageSourceType::New();
  imgSource->SetSize(region.GetSize());
  imgSource->SetSpacing(spacing);
  imgSource->SetOrigin(origin);

  FilterType::Pointer filter = FilterType::New();
  filter->SetInput(imgSource->GetOutput());
  filter->SetLayerIndex(2);
  filter->SetSamplePositions(vectors);
77
  filter->SetOutputSamples(output);
78
79
80
  filter->SetClassFieldName(classFieldName);
  filter->SetOutputFieldPrefix(outputPrefix);

Guillaume Pasero's avatar
Guillaume Pasero committed
81
82
83
  itk::TimeProbe chrono;
  chrono.Start();

84
  filter->Update();
Guillaume Pasero's avatar
Guillaume Pasero committed
85
86
87
88

  chrono.Stop();
  std::cout << "Extraction took "<< chrono.GetTotal() << " sec" << std::endl;

89
90
91
  return EXIT_SUCCESS;
}

92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
int otbImageSampleExtractorFilterUpdate(int argc, char* argv[])
{
  typedef otb::VectorImage<float> InputImageType;
  typedef otb::ImageSampleExtractorFilter<InputImageType> FilterType;

  if (argc < 3)
    {
    std::cout << "Usage : "<<argv[0]<< "  input_vector  output" << std::endl;
    }

  std::string vectorPath(argv[1]);
  std::string outputPath(argv[2]);

  otb::ogr::DataSource::Pointer vectors = otb::ogr::DataSource::New(vectorPath);
  otb::ogr::DataSource::Pointer output =
    otb::ogr::DataSource::New(outputPath,otb::ogr::DataSource::Modes::Overwrite);

  InputImageType::RegionType region;
  region.SetSize(0,99);
  region.SetSize(1,50);

  InputImageType::PointType origin;
  origin.Fill(0.5);

  InputImageType::SpacingType spacing;
  spacing[0] = 1.0;
  spacing[1] = -1.0;

  std::string classFieldName("label");
  std::string outputPrefix("measure_");

  // first copy the input layer #3 to output
  otb::ogr::Layer inLayer = vectors->GetLayer(2);
  OGRSpatialReference * oSRS = inLayer.GetSpatialRef()->Clone();
  output->CreateLayer( inLayer.GetName(),
                       oSRS,
                       inLayer.GetLayerDefn().GetGeomType());
  otb::ogr::Layer dstLayer = output->GetLayer(0);
  OGRFieldDefn labelField(classFieldName.c_str(),OFTString);
  dstLayer.CreateField(labelField, true);
132
133
134
135
136
137
  OGRErr err = dstLayer.ogr().StartTransaction();

  if (err != OGRERR_NONE) {
    itkExceptionMacro(<< "Unable to start transaction for OGR layer " << dstLayer.ogr().GetName() << ".");
  }

138
139
140
141
142
143
144
  otb::ogr::Layer::const_iterator featIt = inLayer.begin();
  for(; featIt!=inLayer.end(); ++featIt)
    {
    otb::ogr::Feature dstFeature(dstLayer.GetLayerDefn());
    dstFeature.SetFrom( *featIt, TRUE );
    dstLayer.CreateFeature( dstFeature );
    }
145
146
147
148
149
150
151

  err = dstLayer.ogr().CommitTransaction();
  if (err != OGRERR_NONE)
    {
    itkExceptionMacro(<< "Unable to commit transaction for OGR layer " << dstLayer.ogr().GetName() << ".");
    }

152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
  output->Clear();

  otb::ogr::DataSource::Pointer outputUpdate =
    otb::ogr::DataSource::New(outputPath,otb::ogr::DataSource::Modes::Update_LayerUpdate);

  typedef itk::PhysicalPointImageSource<InputImageType> ImageSourceType;
  ImageSourceType::Pointer imgSource = ImageSourceType::New();
  imgSource->SetSize(region.GetSize());
  imgSource->SetSpacing(spacing);
  imgSource->SetOrigin(origin);

  FilterType::Pointer filter = FilterType::New();
  filter->SetInput(imgSource->GetOutput());
  filter->SetLayerIndex(0);
  filter->SetSamplePositions(outputUpdate);
167
  filter->SetOutputSamples(outputUpdate);
168
169
170
  filter->SetClassFieldName(classFieldName);
  filter->SetOutputFieldPrefix(outputPrefix);

Guillaume Pasero's avatar
Guillaume Pasero committed
171
172
173
  itk::TimeProbe chrono;
  chrono.Start();

174
175
  filter->Update();

Guillaume Pasero's avatar
Guillaume Pasero committed
176
177
178
  chrono.Stop();
  std::cout << "Extraction took "<< chrono.GetTotal() << " sec" << std::endl;

179
180
  return EXIT_SUCCESS;
}