1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
/*
* Copyright (C) 2005-2022 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.
*/
#ifndef otbOGRDataToSamplePositionFilter_h
#define otbOGRDataToSamplePositionFilter_h
#include "otbPersistentSamplingFilterBase.h"
#include "otbPersistentFilterStreamingDecorator.h"
#include "otbSamplingRateCalculator.h"
#include "otbPeriodicSampler.h"
#include "otbImage.h"
#include <string>
namespace otb
{
/**
* \class PersistentOGRDataToSamplePositionFilter
*
* \brief Persistent filter to extract sample position from an image
*
* This filter uses an input image (to define the sampling grid), an optional
* input mask, and a set of input vectors that define the sampling regions for
* each class.
*
* The filter has a set of samplers (one for each class), they define what
* sampling rate and strategy should be performed.
*
* Several levels of output are supported. For instance, with two outputs :
* when the sampler from level 1 discards a sample, the sampler from level 2 is
* called.
*
* \ingroup OTBSampling
*/
template <class TInputImage, class TMaskImage, class TSampler>
class ITK_EXPORT PersistentOGRDataToSamplePositionFilter : public PersistentSamplingFilterBase<TInputImage, TMaskImage>
{
public:
/** Standard Self typedef */
typedef PersistentOGRDataToSamplePositionFilter Self;
typedef PersistentSamplingFilterBase<TInputImage, TMaskImage> Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
typedef TInputImage InputImageType;
typedef typename InputImageType::Pointer InputImagePointer;
typedef typename InputImageType::RegionType RegionType;
typedef typename InputImageType::PointType PointType;
typedef TMaskImage MaskImageType;
typedef typename MaskImageType::Pointer MaskImagePointer;
typedef ogr::DataSource OGRDataType;
typedef ogr::DataSource::Pointer OGRDataPointer;
typedef TSampler SamplerType;
typedef typename SamplerType::Pointer SamplerPointerType;
typedef typename SamplerType::ParameterType SamplerParameterType;
typedef typename std::map<std::string, SamplerPointerType> SamplerMapType;
typedef std::map<std::string, unsigned int> ClassPartitionType;
typedef itk::DataObject::DataObjectPointerArraySizeType DataObjectPointerArraySizeType;
/** Method for creation through the object factory. */
itkNewMacro(Self);
/** Runtime information support. */
itkTypeMacro(PersistentOGRDataToSamplePositionFilter, PersistentSamplingFilterBase);
void Synthetize(void) override
{
}
/** Reset method called before starting the streaming*/
void Reset(void) override;
/** Get a reference to the internal samplers at a given level */
SamplerMapType& GetSamplers(unsigned int level);
/** Set an output container for sample position associated
* with corresponding rates, for a given level.*/
void SetOutputPositionContainerAndRates(otb::ogr::DataSource* data, const SamplingRateCalculator::MapRateType& map, unsigned int level);
/** Get the output position container of a given level */
const otb::ogr::DataSource* GetOutputPositionContainer(unsigned int level) const;
otb::ogr::DataSource* GetOutputPositionContainer(unsigned int level);
/** Get the number of sampling levels used in this filter.*/
unsigned int GetNumberOfLevels();
/** Clear all output position containers */
void ClearOutputs();
/** Make a DataObject of the correct type to be used as the specified
* output. */
itk::DataObject::Pointer MakeOutput(DataObjectPointerArraySizeType idx) override;
using Superclass::MakeOutput;
/** Get/Set of the field name storing the original FID of each sample */
itkSetMacro(OriginFieldName, std::string);
itkGetMacro(OriginFieldName, std::string);
protected:
/** Constructor */
PersistentOGRDataToSamplePositionFilter();
/** Destructor */
~PersistentOGRDataToSamplePositionFilter() override
{
}
/** Call samplers on a current position, for a given class */
void ProcessSample(const ogr::Feature& feature, typename TInputImage::IndexType& imgIndex, typename TInputImage::PointType& imgPoint,
itk::ThreadIdType& threadid) override;
/** Method to split the input OGRDataSource
* according to the class partition
*/
void DispatchInputVectors(void) override;
/** Fill the output vectors with a special ordering (class partition) */
void FillOneOutput(unsigned int outIdx, ogr::DataSource* outDS, bool update) override;
private:
PersistentOGRDataToSamplePositionFilter(const Self&) = delete;
void operator=(const Self&) = delete;
void ComputeClassPartition(void);
/** (internal) map associating a class name with a thread number */
ClassPartitionType m_ClassPartition;
/** Internal samplers*/
std::vector<SamplerMapType> m_Samplers;
/** Field name to store the FID of the geometry each sample comes from.
* When this name is empty, no FID is stored. */
std::string m_OriginFieldName;
/** Flag to enable/disable origin FID in outputs */
bool m_UseOriginField;
};
/**
* \class OGRDataToSamplePositionFilter
*
* \brief Extracts sample position from an image using a persistent filter
*
* \sa PersistentOGRDataToSamplePositionFilter
*
* \ingroup OTBSampling
*/
template <class TInputImage, class TMaskImage = otb::Image<unsigned char>, class TSampler = otb::PeriodicSampler>
class ITK_EXPORT OGRDataToSamplePositionFilter
: public PersistentFilterStreamingDecorator<PersistentOGRDataToSamplePositionFilter<TInputImage, TMaskImage, TSampler>>
{
public:
/** Standard Self typedef */
typedef OGRDataToSamplePositionFilter Self;
typedef PersistentFilterStreamingDecorator<PersistentOGRDataToSamplePositionFilter<TInputImage, TMaskImage, TSampler>> Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
typedef TInputImage InputImageType;
typedef TMaskImage MaskImageType;
typedef otb::ogr::DataSource OGRDataType;
typedef typename Superclass::FilterType FilterType;
typedef TSampler SamplerType;
typedef typename SamplerType::Pointer SamplerPointerType;
typedef typename SamplerType::ParameterType SamplerParameterType;
typedef typename std::map<std::string, SamplerPointerType> SamplerMapType;
/** Type macro */
itkNewMacro(Self);
/** Creation through object factory macro */
itkTypeMacro(OGRDataToSamplePositionFilter, PersistentFilterStreamingDecorator);
using Superclass::SetInput;
/** Set the input image */
virtual void SetInput(const TInputImage* image);
/** Get the input image*/
const TInputImage* GetInput();
/** Set the input OGRDataSource containing sampling areas */
void SetOGRData(const otb::ogr::DataSource* data);
/** Get the input OGRDataSource containing sampling areas */
const otb::ogr::DataSource* GetOGRData();
/** Set the input mask (optional) */
void SetMask(const TMaskImage* mask);
/** Get input mask (may be null)*/
const TMaskImage* GetMask();
/** Set the field name containing class names*/
void SetFieldName(std::string key);
/** Get the field name containing class names*/
std::string GetFieldName();
/** Set the layer index containing sampling areas*/
void SetLayerIndex(int index);
/** Get the layer index containing sampling areas*/
int GetLayerIndex();
/** Set the sampling parameters for all classes at a given level.*/
void SetSamplerParameters(SamplerParameterType param, unsigned int level = 0);
/** Get a reference to the internal sampler map at a given level.*/
SamplerMapType& GetSamplers(unsigned int level = 0);
/** Set the output container with the associated rates at a given level.*/
void SetOutputPositionContainerAndRates(otb::ogr::DataSource* data, const SamplingRateCalculator::MapRateType& map, unsigned int level = 0);
/** Get the output position container at a given level.*/
otb::ogr::DataSource* GetOutputPositionContainer(unsigned int level = 0);
/** Set the field name storing the original FID of each sample*/
void SetOriginFieldName(std::string key);
/** Get the field name storing the original FID of each sample*/
std::string GetOriginFieldName();
protected:
/** Constructor */
OGRDataToSamplePositionFilter()
{
}
/** Destructor */
~OGRDataToSamplePositionFilter() override
{
}
private:
OGRDataToSamplePositionFilter(const Self&) = delete;
void operator=(const Self&) = delete;
};
} // end of namespace otb
#ifndef OTB_MANUAL_INSTANTIATION
#include "otbOGRDataToSamplePositionFilter.hxx"
#endif
#endif
|