From 3a0ce10870ea944b180a2c82c63dca298776a0ae Mon Sep 17 00:00:00 2001 From: Laurentiu Nicola <lnicola@dend.ro> Date: Sun, 16 Sep 2018 20:05:02 +0300 Subject: [PATCH] ENH: add support for GDAL's ALL_TOUCHED mode --- .../include/otbOGRDataSourceToLabelImageFilter.h | 6 ++++++ .../otbOGRDataSourceToLabelImageFilter.hxx | 9 +++++++-- .../include/otbRasterizeVectorDataFilter.h | 6 ++++++ .../include/otbRasterizeVectorDataFilter.hxx | 13 +++++++++++-- .../include/otbVectorDataToLabelImageFilter.h | 8 ++++++++ .../include/otbVectorDataToLabelImageFilter.hxx | 15 ++++++++++++--- 6 files changed, 50 insertions(+), 7 deletions(-) diff --git a/Modules/Segmentation/Conversion/include/otbOGRDataSourceToLabelImageFilter.h b/Modules/Segmentation/Conversion/include/otbOGRDataSourceToLabelImageFilter.h index 6f086648d7..63717bf0a5 100644 --- a/Modules/Segmentation/Conversion/include/otbOGRDataSourceToLabelImageFilter.h +++ b/Modules/Segmentation/Conversion/include/otbOGRDataSourceToLabelImageFilter.h @@ -137,6 +137,11 @@ public: itkGetConstReferenceMacro(BurnAttributeMode,bool); itkBooleanMacro(BurnAttributeMode); + /** Set/Get the AllTouchedMode flag */ + itkSetMacro(AllTouchedMode,bool); + itkGetConstReferenceMacro(AllTouchedMode,bool); + itkBooleanMacro(AllTouchedMode); + /** Useful to set the output parameters from an existing image*/ void SetOutputParametersFromImage(const ImageBaseType * image); @@ -169,6 +174,7 @@ private: OutputImageInternalPixelType m_BackgroundValue; OutputImageInternalPixelType m_ForegroundValue; bool m_BurnAttributeMode; + bool m_AllTouchedMode; }; // end of class VectorDataToLabelImageFilter } // end of namespace otb diff --git a/Modules/Segmentation/Conversion/include/otbOGRDataSourceToLabelImageFilter.hxx b/Modules/Segmentation/Conversion/include/otbOGRDataSourceToLabelImageFilter.hxx index 30c3e9fba8..4e33a951a5 100644 --- a/Modules/Segmentation/Conversion/include/otbOGRDataSourceToLabelImageFilter.hxx +++ b/Modules/Segmentation/Conversion/include/otbOGRDataSourceToLabelImageFilter.hxx @@ -39,7 +39,8 @@ OGRDataSourceToLabelImageFilter<TOutputImage> ::OGRDataSourceToLabelImageFilter() : m_BurnAttribute("DN"), m_BackgroundValue(0), m_ForegroundValue(255), - m_BurnAttributeMode(true) + m_BurnAttributeMode(true), + m_AllTouchedMode(false) { this->SetNumberOfRequiredInputs(1); @@ -129,7 +130,7 @@ OGRDataSourceToLabelImageFilter<TOutputImage> this->SetOutputOrigin ( image->GetOrigin() ); this->SetOutputSpacing ( internal::GetSignedSpacing( image ) ); this->SetOutputSize ( image->GetLargestPossibleRegion().GetSize() ); - + ImageMetadataInterfaceBase::Pointer imi = ImageMetadataInterfaceFactory::CreateIMI(image->GetMetaDataDictionary()); this->SetOutputProjectionRef(imi->GetProjectionRef()); @@ -251,6 +252,10 @@ OGRDataSourceToLabelImageFilter<TOutputImage>::GenerateData() { options.push_back("ATTRIBUTE="+m_BurnAttribute); } + if(m_AllTouchedMode) + { + options.push_back("ALL_TOUCHED=TRUE"); + } GDALRasterizeLayers( dataset, nbBands, &m_BandsToBurn[0], diff --git a/Modules/Segmentation/Conversion/include/otbRasterizeVectorDataFilter.h b/Modules/Segmentation/Conversion/include/otbRasterizeVectorDataFilter.h index d708f1d995..d719606ae3 100644 --- a/Modules/Segmentation/Conversion/include/otbRasterizeVectorDataFilter.h +++ b/Modules/Segmentation/Conversion/include/otbRasterizeVectorDataFilter.h @@ -99,6 +99,11 @@ public: typedef itk::DataObject DataObjectType; + /** Set/Get the AllTouchedMode flag */ + itkSetMacro(AllTouchedMode,bool); + itkGetConstReferenceMacro(AllTouchedMode,bool); + itkBooleanMacro(AllTouchedMode); + const InputImageType * GetInput(); const DataObjectType* GetInput(unsigned int idx); @@ -163,6 +168,7 @@ private: std::vector<double> m_BurnValues; std::vector<double> m_FullBurnValues; std::vector<int> m_BandsToBurn; + bool m_AllTouchedMode; }; // end of class RasterizeVectorDataFilter diff --git a/Modules/Segmentation/Conversion/include/otbRasterizeVectorDataFilter.hxx b/Modules/Segmentation/Conversion/include/otbRasterizeVectorDataFilter.hxx index c140348c04..6b45a3fcdf 100644 --- a/Modules/Segmentation/Conversion/include/otbRasterizeVectorDataFilter.hxx +++ b/Modules/Segmentation/Conversion/include/otbRasterizeVectorDataFilter.hxx @@ -30,7 +30,8 @@ namespace otb template<class TVectorData, class TInputImage, class TOutputImage> RasterizeVectorDataFilter<TVectorData, TInputImage, TOutputImage> ::RasterizeVectorDataFilter() - : m_OGRDataSourcePointer(nullptr) + : m_OGRDataSourcePointer(nullptr), + m_AllTouchedMode(false) { this->SetNumberOfRequiredInputs(1); } @@ -188,6 +189,12 @@ RasterizeVectorDataFilter<TVectorData, TInputImage, TOutputImage>::GenerateData( geoTransform[4] = 0.; GDALSetGeoTransform(dataset,const_cast<double*>(geoTransform.GetDataPointer())); + char **options = nullptr; + if (m_AllTouchedMode) + { + options = CSLSetNameValue(options, "ALL_TOUCHED", "TRUE"); + } + // Burn the geometries into the dataset if (dataset != nullptr) { @@ -196,9 +203,11 @@ RasterizeVectorDataFilter<TVectorData, TInputImage, TOutputImage>::GenerateData( m_SrcDataSetLayers.size(), &(m_SrcDataSetLayers[0]), nullptr, nullptr, &(m_FullBurnValues[0]), - nullptr, + options, GDALDummyProgress, nullptr ); + CSLDestroy(options); + // release the dataset GDALClose( dataset ); } diff --git a/Modules/Segmentation/Conversion/include/otbVectorDataToLabelImageFilter.h b/Modules/Segmentation/Conversion/include/otbVectorDataToLabelImageFilter.h index 63c7d85cc8..6cdbabc397 100644 --- a/Modules/Segmentation/Conversion/include/otbVectorDataToLabelImageFilter.h +++ b/Modules/Segmentation/Conversion/include/otbVectorDataToLabelImageFilter.h @@ -129,6 +129,11 @@ public: itkSetMacro(DefaultBurnValue, OutputImageInternalPixelType); itkGetMacro(DefaultBurnValue, OutputImageInternalPixelType); + /** Set/Get the AllTouchedMode flag */ + itkSetMacro(AllTouchedMode,bool); + itkGetConstReferenceMacro(AllTouchedMode,bool); + itkBooleanMacro(AllTouchedMode); + /** Useful to set the output parameters from an existing image*/ void SetOutputParametersFromImage(const ImageBaseType * image); @@ -176,6 +181,9 @@ private: // Background value OutputImageInternalPixelType m_BackgroundValue; + // All touched mode + bool m_AllTouchedMode; + // Output params std::string m_OutputProjectionRef; OutputSpacingType m_OutputSpacing; diff --git a/Modules/Segmentation/Conversion/include/otbVectorDataToLabelImageFilter.hxx b/Modules/Segmentation/Conversion/include/otbVectorDataToLabelImageFilter.hxx index 49afc91079..ab370f7545 100644 --- a/Modules/Segmentation/Conversion/include/otbVectorDataToLabelImageFilter.hxx +++ b/Modules/Segmentation/Conversion/include/otbVectorDataToLabelImageFilter.hxx @@ -40,7 +40,8 @@ VectorDataToLabelImageFilter<TVectorData, TOutputImage> m_BandsToBurn(1, 1), m_BurnAttribute("FID"), m_DefaultBurnValue(1.), - m_BackgroundValue(0.) + m_BackgroundValue(0.), + m_AllTouchedMode(false) { this->SetNumberOfRequiredInputs(1); @@ -262,7 +263,7 @@ VectorDataToLabelImageFilter<TVectorData, TOutputImage>::GenerateData() // Fill the buffer with the background value this->GetOutput()->FillBuffer(m_BackgroundValue); - + // nb bands unsigned int nbBands = this->GetOutput()->GetNumberOfComponentsPerPixel(); @@ -303,6 +304,12 @@ VectorDataToLabelImageFilter<TVectorData, TOutputImage>::GenerateData() geoTransform[4] = 0.; GDALSetGeoTransform(dataset,const_cast<double*>(geoTransform.GetDataPointer())); + char **options = nullptr; + if (m_AllTouchedMode) + { + options = CSLSetNameValue(options, "ALL_TOUCHED", "TRUE"); + } + // Burn the geometries into the dataset if (dataset != nullptr) { @@ -311,9 +318,11 @@ VectorDataToLabelImageFilter<TVectorData, TOutputImage>::GenerateData() m_SrcDataSetGeometries.size(), &(m_SrcDataSetGeometries[0]), nullptr, nullptr, &(m_FullBurnValues[0]), - nullptr, + options, GDALDummyProgress, nullptr ); + CSLDestroy(options); + // release the dataset GDALClose( dataset ); } -- GitLab