21 #ifndef otbImageSampleExtractorFilter_hxx
22 #define otbImageSampleExtractorFilter_hxx
25 #include "itkDefaultConvertPixelTraits.h"
26 #include "itkProgressReporter.h"
32 template <
class TInputImage>
35 this->SetNumberOfRequiredOutputs(2);
36 this->SetNthOutput(0, TInputImage::New());
39 template <
class TInputImage>
42 this->SetNthOutput(1, data);
45 template <
class TInputImage>
48 if (this->GetNumberOfOutputs() < 2)
52 return static_cast<ogr::DataSource*
>(this->itk::ProcessObject::GetOutput(1));
55 template <
class TInputImage>
59 TInputImage* inputImage =
const_cast<TInputImage*
>(this->GetInput());
60 inputImage->UpdateOutputInformation();
61 unsigned int nbBand = inputImage->GetNumberOfComponentsPerPixel();
62 if (m_SampleFieldNames.size())
64 if (m_SampleFieldNames.size() != nbBand)
66 itkExceptionMacro(<<
"Wrong number of field names given, got " << m_SampleFieldNames.size() <<
", expected " << nbBand);
72 std::ostringstream oss;
73 for (
unsigned int i = 0; i < nbBand; ++i)
76 oss << this->GetSampleFieldPrefix() << i;
77 m_SampleFieldNames.push_back(oss.str());
81 this->InitializeFields();
86 this->InitializeOutputDataSource(inputDS, output);
89 template <
class TInputImage>
92 m_SampleFieldNames.clear();
93 for (
unsigned int i = 0; i < names.size(); i++)
95 m_SampleFieldNames.push_back(names[i]);
99 template <
class TInputImage>
102 return m_SampleFieldNames;
105 template <
class TInputImage>
108 Superclass::GenerateOutputInformation();
111 std::string projectionRefWkt = this->GetInput()->GetProjectionRef();
112 bool projectionInformationAvailable = !projectionRefWkt.empty();
113 if (projectionInformationAvailable)
115 OGRSpatialReference imgSRS;
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);
121 imgSRS.SetAxisMappingStrategy(
122 this->GetOGRData()->GetLayer(this->GetLayerIndex()).GetSpatialRef()->GetAxisMappingStrategy ());
124 const char* projWktCstr = projectionRefWkt.c_str();
125 char** projWktPointer =
const_cast<char**
>(&projWktCstr);
126 OGRErr err = imgSRS.importFromWkt(projWktPointer);
129 if (err == OGRERR_NONE)
132 ogr::Layer inLayer = this->GetOGRData()->GetLayer(this->GetLayerIndex());
135 char* layerSrsWkt = NULL;
137 itkExceptionMacro(<<
"Spatial reference of input image and samples don't match: \n" << projectionRefWkt <<
"\nvs\n" << layerSrsWkt);
143 template <
class TInputImage>
147 RegionType requested = this->GetOutput()->GetRequestedRegion();
148 input->SetRequestedRegion(requested);
152 template <
class TInputImage>
156 TInputImage* inputImage =
const_cast<TInputImage*
>(this->GetInput());
157 unsigned int nbBand = inputImage->GetNumberOfComponentsPerPixel();
159 ogr::Layer outputLayer = this->GetInMemoryOutput(threadid);
161 itk::ProgressReporter progress(
this, threadid, layerForThread.
GetFeatureCount(
true));
171 for (; featIt != layerForThread.
end(); ++featIt)
173 geom = featIt->ogr().GetGeometryRef();
174 switch (geom->getGeometryType())
179 OGRPoint* castPoint =
dynamic_cast<OGRPoint*
>(geom);
180 if (castPoint == NULL)
185 imgPoint[0] = castPoint->getX();
186 imgPoint[1] = castPoint->getY();
187 inputImage->TransformPhysicalPointToIndex(imgPoint, imgIndex);
188 imgPixel = inputImage->GetPixel(imgIndex);
191 dstFeature.
SetFrom(*featIt, TRUE);
192 dstFeature.
SetFID(featIt->GetFID());
193 for (
unsigned int i = 0; i < nbBand; ++i)
195 imgComp =
static_cast<double>(itk::DefaultConvertPixelTraits<PixelType>::GetNthComponent(i, imgPixel));
197 dstFeature[m_SampleFieldNames[i]].SetValue(imgComp);
208 progress.CompletedPixel();
213 template <
class TInputImage>
216 this->ClearAdditionalFields();
217 for (
unsigned int i = 0; i < m_SampleFieldNames.size(); ++i)
219 this->CreateAdditionalField(m_SampleFieldNames[i], OFTReal, 24, 15);
225 template <
class TInputImage>
228 this->GetFilter()->SetInput(image);
231 template <
class TInputImage>
234 return this->GetFilter()->GetInput();
237 template <
class TInputImage>
240 this->GetFilter()->SetOGRData(data);
243 template <
class TInputImage>
246 return this->GetFilter()->GetOGRData();
249 template <
class TInputImage>
252 this->GetFilter()->SetOutputSamples(data);
255 template <
class TInputImage>
258 return this->GetFilter()->GetOutputSamples();
261 template <
class TInputImage>
264 this->GetFilter()->SetSampleFieldPrefix(key);
267 template <
class TInputImage>
270 return this->GetFilter()->GetSampleFieldPrefix();
273 template <
class TInputImage>
276 this->GetFilter()->SetSampleFieldNames(names);
279 template <
class TInputImage>
282 return this->GetFilter()->GetSampleFieldNames();
286 template <
class TInputImage>
289 this->GetFilter()->SetLayerIndex(index);
292 template <
class TInputImage>
295 return this->GetFilter()->GetLayerIndex();
298 template <
class TInputImage>
301 this->GetFilter()->SetFieldName(name);
304 template <
class TInputImage>
307 return this->GetFilter()->GetFieldName();