OTB  9.0.0
Orfeo Toolbox
otbImageSampleExtractorFilter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otbImageSampleExtractorFilter_hxx
22 #define otbImageSampleExtractorFilter_hxx
23 
25 #include "itkDefaultConvertPixelTraits.h"
26 #include "itkProgressReporter.h"
27 
28 namespace otb
29 {
30 // --------- otb::PersistentImageSampleExtractorFilter ---------------------
31 
32 template <class TInputImage>
34 {
35  this->SetNumberOfRequiredOutputs(2);
36  this->SetNthOutput(0, TInputImage::New());
37 }
38 
39 template <class TInputImage>
41 {
42  this->SetNthOutput(1, data);
43 }
44 
45 template <class TInputImage>
47 {
48  if (this->GetNumberOfOutputs() < 2)
49  {
50  return 0;
51  }
52  return static_cast<ogr::DataSource*>(this->itk::ProcessObject::GetOutput(1));
53 }
54 
55 template <class TInputImage>
57 {
58  // Check output field names
59  TInputImage* inputImage = const_cast<TInputImage*>(this->GetInput());
60  inputImage->UpdateOutputInformation();
61  unsigned int nbBand = inputImage->GetNumberOfComponentsPerPixel();
62  if (m_SampleFieldNames.size())
63  {
64  if (m_SampleFieldNames.size() != nbBand)
65  {
66  itkExceptionMacro(<< "Wrong number of field names given, got " << m_SampleFieldNames.size() << ", expected " << nbBand);
67  }
68  }
69  else
70  {
71  // use the prefix to create field names
72  std::ostringstream oss;
73  for (unsigned int i = 0; i < nbBand; ++i)
74  {
75  oss.str("");
76  oss << this->GetSampleFieldPrefix() << i;
77  m_SampleFieldNames.push_back(oss.str());
78  }
79  }
80  // initialize additional fields for output
81  this->InitializeFields();
82 
83  // initialize output DataSource
84  ogr::DataSource* inputDS = const_cast<ogr::DataSource*>(this->GetOGRData());
85  ogr::DataSource* output = this->GetOutputSamples();
86  this->InitializeOutputDataSource(inputDS, output);
87 }
88 
89 template <class TInputImage>
91 {
92  m_SampleFieldNames.clear();
93  for (unsigned int i = 0; i < names.size(); i++)
94  {
95  m_SampleFieldNames.push_back(names[i]);
96  }
97 }
98 
99 template <class TInputImage>
101 {
102  return m_SampleFieldNames;
103 }
104 
105 template <class TInputImage>
107 {
108  Superclass::GenerateOutputInformation();
109 
110  // Check SRS of input image and samples
111  std::string projectionRefWkt = this->GetInput()->GetProjectionRef();
112  bool projectionInformationAvailable = !projectionRefWkt.empty();
113  if (projectionInformationAvailable)
114  {
115  OGRSpatialReference imgSRS;
116 
117 #if GDAL_VERSION_NUM >= 3000000 // importFromWkt is const-correct in GDAL 3
118  const char* projWktCstr = projectionRefWkt.c_str();
119  OGRErr err = imgSRS.importFromWkt(&projWktCstr);
120  // Use the same mapping strategy as the one in the datasource.
121  imgSRS.SetAxisMappingStrategy(
122  this->GetOGRData()->GetLayer(this->GetLayerIndex()).GetSpatialRef()->GetAxisMappingStrategy ());
123 #else
124  const char* projWktCstr = projectionRefWkt.c_str();
125  char** projWktPointer = const_cast<char**>(&projWktCstr);
126  OGRErr err = imgSRS.importFromWkt(projWktPointer);
127 #endif
128 
129  if (err == OGRERR_NONE)
130  {
131  // get input layer
132  ogr::Layer inLayer = this->GetOGRData()->GetLayer(this->GetLayerIndex());
133  if (!imgSRS.IsSame(inLayer.GetSpatialRef()))
134  {
135  char* layerSrsWkt = NULL;
136  inLayer.GetSpatialRef()->exportToPrettyWkt(&layerSrsWkt);
137  itkExceptionMacro(<< "Spatial reference of input image and samples don't match: \n" << projectionRefWkt << "\nvs\n" << layerSrsWkt);
138  }
139  }
140  }
141 }
142 
143 template <class TInputImage>
145 {
146  InputImageType* input = const_cast<InputImageType*>(this->GetInput());
147  RegionType requested = this->GetOutput()->GetRequestedRegion();
148  input->SetRequestedRegion(requested);
149 }
150 
151 
152 template <class TInputImage>
154 {
155  // Retrieve inputs
156  TInputImage* inputImage = const_cast<TInputImage*>(this->GetInput());
157  unsigned int nbBand = inputImage->GetNumberOfComponentsPerPixel();
158 
159  ogr::Layer outputLayer = this->GetInMemoryOutput(threadid);
160 
161  itk::ProgressReporter progress(this, threadid, layerForThread.GetFeatureCount(true));
162 
163  // Loop across the features in the layer (filtered by requested region in BeforeTGD already)
164  OGRGeometry* geom;
165  PointType imgPoint;
166  IndexType imgIndex;
167  PixelType imgPixel;
168  double imgComp;
169 
170  ogr::Layer::const_iterator featIt = layerForThread.begin();
171  for (; featIt != layerForThread.end(); ++featIt)
172  {
173  geom = featIt->ogr().GetGeometryRef();
174  switch (geom->getGeometryType())
175  {
176  case wkbPoint:
177  case wkbPoint25D:
178  {
179  OGRPoint* castPoint = dynamic_cast<OGRPoint*>(geom);
180  if (castPoint == NULL)
181  {
182  // Wrong Type !
183  break;
184  }
185  imgPoint[0] = castPoint->getX();
186  imgPoint[1] = castPoint->getY();
187  inputImage->TransformPhysicalPointToIndex(imgPoint, imgIndex);
188  imgPixel = inputImage->GetPixel(imgIndex);
189 
190  ogr::Feature dstFeature(outputLayer.GetLayerDefn());
191  dstFeature.SetFrom(*featIt, TRUE);
192  dstFeature.SetFID(featIt->GetFID());
193  for (unsigned int i = 0; i < nbBand; ++i)
194  {
195  imgComp = static_cast<double>(itk::DefaultConvertPixelTraits<PixelType>::GetNthComponent(i, imgPixel));
196  // Fill the output OGRDataSource
197  dstFeature[m_SampleFieldNames[i]].SetValue(imgComp);
198  }
199  outputLayer.CreateFeature(dstFeature);
200  break;
201  }
202  default:
203  {
204  otbWarningMacro("Geometry not handled: " << geom->getGeometryName());
205  break;
206  }
207  }
208  progress.CompletedPixel();
209  }
210 }
211 
212 
213 template <class TInputImage>
215 {
216  this->ClearAdditionalFields();
217  for (unsigned int i = 0; i < m_SampleFieldNames.size(); ++i)
218  {
219  this->CreateAdditionalField(m_SampleFieldNames[i], OFTReal, 24, 15);
220  }
221 }
222 
223 // -------------- otb::ImageSampleExtractorFilter --------------------------
224 
225 template <class TInputImage>
227 {
228  this->GetFilter()->SetInput(image);
229 }
230 
231 template <class TInputImage>
233 {
234  return this->GetFilter()->GetInput();
235 }
236 
237 template <class TInputImage>
239 {
240  this->GetFilter()->SetOGRData(data);
241 }
242 
243 template <class TInputImage>
245 {
246  return this->GetFilter()->GetOGRData();
247 }
248 
249 template <class TInputImage>
251 {
252  this->GetFilter()->SetOutputSamples(data);
253 }
254 
255 template <class TInputImage>
257 {
258  return this->GetFilter()->GetOutputSamples();
259 }
260 
261 template <class TInputImage>
263 {
264  this->GetFilter()->SetSampleFieldPrefix(key);
265 }
266 
267 template <class TInputImage>
269 {
270  return this->GetFilter()->GetSampleFieldPrefix();
271 }
272 
273 template <class TInputImage>
275 {
276  this->GetFilter()->SetSampleFieldNames(names);
277 }
278 
279 template <class TInputImage>
281 {
282  return this->GetFilter()->GetSampleFieldNames();
283 }
284 
285 
286 template <class TInputImage>
288 {
289  this->GetFilter()->SetLayerIndex(index);
290 }
291 
292 template <class TInputImage>
294 {
295  return this->GetFilter()->GetLayerIndex();
296 }
297 
298 template <class TInputImage>
300 {
301  this->GetFilter()->SetFieldName(name);
302 }
303 
304 template <class TInputImage>
306 {
307  return this->GetFilter()->GetFieldName();
308 }
309 
310 } // end of namespace otb
311 
312 #endif
otb::ogr::Layer::GetSpatialRef
OGRSpatialReference const * GetSpatialRef() const
otb::PersistentImageSampleExtractorFilter::GenerateOutputInformation
void GenerateOutputInformation() override
Definition: otbImageSampleExtractorFilter.hxx:106
otb::PersistentImageSampleExtractorFilter::PersistentImageSampleExtractorFilter
PersistentImageSampleExtractorFilter()
Definition: otbImageSampleExtractorFilter.hxx:33
otb::ImageSampleExtractorFilter::SetInput
virtual void SetInput(const TInputImage *image)
Definition: otbImageSampleExtractorFilter.hxx:226
otb::ogr::Feature::SetFrom
void SetFrom(Feature const &rhs, int *map, bool mustForgive=true)
Definition: otbOGRFeatureWrapper.hxx:51
otb::PersistentImageSampleExtractorFilter::PixelType
InputImageType::PixelType PixelType
Definition: otbImageSampleExtractorFilter.h:56
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::ImageSampleExtractorFilter::SetOutputFieldNames
void SetOutputFieldNames(std::vector< std::string > &names)
Definition: otbImageSampleExtractorFilter.hxx:274
otb::ogr::DataSource
Collection of geometric objects.
Definition: otbOGRDataSourceWrapper.h:83
otb::ogr::Layer::CreateFeature
void CreateFeature(Feature feature)
otb::ogr::Layer::begin
const_iterator begin() const
Definition: otbOGRLayerWrapper.h:412
otb::ogr::Layer::end
const_iterator end() const
Definition: otbOGRLayerWrapper.h:418
otb::ImageSampleExtractorFilter::SetLayerIndex
void SetLayerIndex(int index)
Definition: otbImageSampleExtractorFilter.hxx:287
otb::PersistentImageSampleExtractorFilter::SetOutputSamples
void SetOutputSamples(ogr::DataSource *data)
Definition: otbImageSampleExtractorFilter.hxx:40
otb::PersistentImageSampleExtractorFilter::GetSampleFieldNames
const std::vector< std::string > & GetSampleFieldNames()
Definition: otbImageSampleExtractorFilter.hxx:100
otb::PersistentImageSampleExtractorFilter::Reset
void Reset(void) override
Definition: otbImageSampleExtractorFilter.hxx:56
otb::ImageSampleExtractorFilter::SetClassFieldName
void SetClassFieldName(const std::string &name)
Definition: otbImageSampleExtractorFilter.hxx:299
otb::PersistentImageSampleExtractorFilter::SetSampleFieldNames
void SetSampleFieldNames(std::vector< std::string > &names)
Definition: otbImageSampleExtractorFilter.hxx:90
otb::ImageSampleExtractorFilter::GetInput
const TInputImage * GetInput()
Definition: otbImageSampleExtractorFilter.hxx:232
otb::ImageSampleExtractorFilter::GetOutputSamples
const otb::ogr::DataSource * GetOutputSamples()
Definition: otbImageSampleExtractorFilter.hxx:256
otb::PersistentImageSampleExtractorFilter::InitializeFields
void InitializeFields()
Definition: otbImageSampleExtractorFilter.hxx:214
otb::PersistentImageSampleExtractorFilter::GetOutputSamples
ogr::DataSource * GetOutputSamples()
Definition: otbImageSampleExtractorFilter.hxx:46
otbWarningMacro
#define otbWarningMacro(x)
Definition: otbMacro.h:65
otb::ogr::Layer::feature_iter
Implementation class for Feature iterator. This iterator is a single pass iterator....
Definition: otbOGRLayerWrapper.h:348
otb::PersistentImageSampleExtractorFilter::RegionType
InputImageType::RegionType RegionType
Definition: otbImageSampleExtractorFilter.h:53
otb::PersistentImageSampleExtractorFilter::InputImageType
TInputImage InputImageType
Definition: otbImageSampleExtractorFilter.h:51
otb::ImageSampleExtractorFilter::SetSamplePositions
void SetSamplePositions(const otb::ogr::DataSource *data)
Definition: otbImageSampleExtractorFilter.hxx:238
otb::ImageSampleExtractorFilter::GetLayerIndex
int GetLayerIndex()
Definition: otbImageSampleExtractorFilter.hxx:293
otb::ogr::DataSource::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbOGRDataSourceWrapper.h:90
otb::PersistentImageSampleExtractorFilter::ThreadedGenerateVectorData
void ThreadedGenerateVectorData(const ogr::Layer &layerForThread, itk::ThreadIdType threadid) override
Definition: otbImageSampleExtractorFilter.hxx:153
otb::ImageSampleExtractorFilter::SetOutputSamples
void SetOutputSamples(OGRDataType::Pointer data)
Definition: otbImageSampleExtractorFilter.hxx:250
otb::ImageSampleExtractorFilter::GetOutputFieldPrefix
std::string GetOutputFieldPrefix()
Definition: otbImageSampleExtractorFilter.hxx:268
otb::ogr::Feature::SetFID
void SetFID(long fid)
Definition: otbOGRFeatureWrapper.hxx:109
otb::ImageSampleExtractorFilter::SetOutputFieldPrefix
void SetOutputFieldPrefix(const std::string &key)
Definition: otbImageSampleExtractorFilter.hxx:262
otb::PersistentImageSampleExtractorFilter::IndexType
InputImageType::IndexType IndexType
Definition: otbImageSampleExtractorFilter.h:55
otb::PersistentImageSampleExtractorFilter::PointType
InputImageType::PointType PointType
Definition: otbImageSampleExtractorFilter.h:54
otb::ogr::Feature
Geometric object with descriptive fields.
Definition: otbOGRFeatureWrapper.h:63
otb::ImageSampleExtractorFilter::GetSamplePositions
const otb::ogr::DataSource * GetSamplePositions()
Definition: otbImageSampleExtractorFilter.hxx:244
otb::ImageSampleExtractorFilter::GetOutputFieldNames
const std::vector< std::string > & GetOutputFieldNames()
Definition: otbImageSampleExtractorFilter.hxx:280
otb::ogr::Layer::GetFeatureCount
int GetFeatureCount(bool doForceComputation) const
otb::ImageSampleExtractorFilter::GetClassFieldName
std::string GetClassFieldName(void)
Definition: otbImageSampleExtractorFilter.hxx:305
otb::ogr::Layer
Layer of geometric objects.
Definition: otbOGRLayerWrapper.h:80
otbImageSampleExtractorFilter.h
otb::PersistentImageSampleExtractorFilter::GenerateInputRequestedRegion
void GenerateInputRequestedRegion() override
Definition: otbImageSampleExtractorFilter.hxx:144
otb::ogr::Layer::GetLayerDefn
OGRFeatureDefn & GetLayerDefn() const