Skip to content
Snippets Groups Projects
Commit 37084140 authored by Otmane Lahlou's avatar Otmane Lahlou
Browse files

ENH: port application to new framework api

parent 4c90ff23
No related branches found
No related tags found
No related merge requests found
......@@ -15,156 +15,175 @@
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#include "otbVectorDataExtractROIApplication.h"
#include "otbWrapperApplication.h"
#include "otbWrapperApplicationFactory.h"
#include <iostream>
#include "otbCommandLineArgumentParser.h"
//Image
#include "otbImage.h"
#include "otbImageFileReader.h"
//VectorData
#include "otbVectorData.h"
#include "otbVectorDataExtractROI.h"
#include "otbVectorDataProjectionFilter.h"
#include "otbVectorDataFileReader.h"
#include "otbVectorDataFileWriter.h"
#include "otbVectorDataProperties.h"
//Misc
#include "otbRemoteSensingRegion.h"
#include "otbStandardWriterWatcher.h"
#include <iostream>
namespace otb
{
int VectorDataExtractROIApplication::Describe(ApplicationDescriptor* descriptor)
namespace Wrapper
{
descriptor->SetName("VectorDataExtractROIApplication");
descriptor->SetDescription("Perform an extract ROI on the input vector data according to the input image extent");
descriptor->AddOption("InputVData", "The input vector data",
"in", 1, true, ApplicationDescriptor::FileName);
descriptor->AddOption("InputImage", "The support image",
"img", 1, true, ApplicationDescriptor::InputImage);
descriptor->AddOption("DEMPath", "The DEM directory",
"dem", 1, false, ApplicationDescriptor::DirectoryName);
descriptor->AddOption("OutputVData", "The output vector data",
"out", 1, true, ApplicationDescriptor::FileName);
return EXIT_SUCCESS;
}
int VectorDataExtractROIApplication::Execute(otb::ApplicationOptionsResult* parseResult)
class VectorDataExtractROIApplication : public Application
{
public:
/** Standard class typedefs. */
typedef VectorDataExtractROIApplication Self;
typedef Application Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
/** Standard macro */
itkNewMacro(Self);
itkTypeMacro(VectorDataExtractROIApplication, otb::Application);
/** Filters typedef */
// Images
typedef unsigned char PixelType;
typedef otb::Image<PixelType, 2> ImageType;
typedef ImageType::PointType PointType;
typedef ImageType::IndexType IndexType;
typedef ImageType::SizeType SizeType;
typedef ImageType::SpacingType SpacingType;
typedef otb::ImageFileReader<ImageType> ImageReaderType;
// VectorData
typedef otb::VectorData<> VectorDataType;
typedef FloatVectorImageType::PointType PointType;
typedef FloatVectorImageType::IndexType IndexType;
typedef FloatVectorImageType::SizeType SizeType;
typedef FloatVectorImageType::SpacingType SpacingType;
typedef otb::VectorDataFileReader<VectorDataType> VectorDataReaderType;
typedef otb::VectorDataFileWriter<VectorDataType> VectorDataWriterType;
typedef VectorDataProjectionFilter<
VectorDataType, VectorDataType> VectorDataProjectionFilterType;
VectorDataType, VectorDataType> VectorDataProjectionFilterType;
typedef VectorDataExtractROI<VectorDataType> VectorDataExtractROIType;
// Misc
typedef otb::RemoteSensingRegion<double> RemoteSensingRegionType;
// Reading the VectorData
std::string vdFilename = parseResult->GetParameterString("InputVData");
VectorDataReaderType::Pointer vdReader = VectorDataReaderType::New();
vdReader->SetFileName(vdFilename);
vdReader->Update();
// Reading the Image
std::string imgFilename = parseResult->GetParameterString("InputImage");
ImageReaderType::Pointer imgReader = ImageReaderType::New();
imgReader->SetFileName(imgFilename);
imgReader->UpdateOutputInformation();
private:
VectorDataExtractROIApplication()
{
SetName("VectorDataExtractROIApplication");
SetDescription("Perform an extract ROI on the input vector data according to the input image extent");
SetDocName("VectorData Extract ROI Application");
SetDocLongDescription("This application extract the VectorData features belonging to a region specified by the support image envelope");
SetDocLimitations("None");
SetDocAuthors("OTB-Team");
SetDocSeeAlso(" ");
SetDocCLExample("otbApplicationLauncherCommandLine VectorDataExtractROI ${OTB-BIN}/bin"
" --vd ${OTB-DATA}/Input/franceCoastline.shp --in ${OTB-DATA}/Input/ --out franceCoastline_extract.shp");
AddDocTag("VectorData Manipulation");
}
virtual ~VectorDataExtractROIApplication()
{
}
void DoCreateParameters()
{
AddParameter(ParameterType_Group,"io","Input and output data");
AddParameter(ParameterType_InputVectorData, "io.vd", "Input Vector data");
AddParameter(ParameterType_InputImage, "io.in", "Support image");
AddParameter(ParameterType_OutputVectorData,"io.out","Output Vector data");
AddParameter(ParameterType_Group,"elev","Elevation management");
SetParameterDescription("elev","This group of parameters allows to manage elevation values in the VectorData projection process");
AddParameter(ParameterType_Directory, "elev.dem", "DEM directory");
std::ostringstream oss;
oss << "This parameter allows to select a directory containing ";
oss << "Digital Elevation Model tiles. Supported formats are SRTM, DTED ";
oss << "or any geotiff processed by the DEM import application";
SetParameterDescription("elev.dem", oss.str());
MandatoryOff("elev.dem");
}
void DoUpdateParameters()
{
// Nothing to do here for the parameters : all are independent
}
void DoExecute()
{
// Get the inputs
VectorDataType* vd = GetParameterVectorData("io.vd");
FloatVectorImageType* inImage = GetParameterImage("io.in");
// Extracting the VectorData
VectorDataExtractROIType::Pointer vdExtract = VectorDataExtractROIType::New();
vdExtract->SetInput(vdReader->GetOutput());
// Extracting the VectorData
m_VdExtract = VectorDataExtractROIType::New();
m_VdExtract->SetInput(vd);
// Find the geographic region of interest
// Get the index of the corner of the image
IndexType ul, ur, ll, lr;
PointType pul, pur, pll, plr;
ul = imgReader->GetOutput()->GetLargestPossibleRegion().GetIndex();
ur = ul;
ll = ul;
lr = ul;
ur[0] += imgReader->GetOutput()->GetLargestPossibleRegion().GetSize()[0];
lr[0] += imgReader->GetOutput()->GetLargestPossibleRegion().GetSize()[0];
lr[1] += imgReader->GetOutput()->GetLargestPossibleRegion().GetSize()[1];
ll[1] += imgReader->GetOutput()->GetLargestPossibleRegion().GetSize()[1];
// Transform to physical point
imgReader->GetOutput()->TransformIndexToPhysicalPoint(ul, pul);
imgReader->GetOutput()->TransformIndexToPhysicalPoint(ur, pur);
imgReader->GetOutput()->TransformIndexToPhysicalPoint(ll, pll);
imgReader->GetOutput()->TransformIndexToPhysicalPoint(lr, plr);
// Get DEM directory
std::string demPath;
if(parseResult->IsOptionPresent("DEMPath"))
{
demPath = parseResult->GetParameterString("DEMPath");
}
// Build the cartographic region
RemoteSensingRegionType rsRegion;
RemoteSensingRegionType::IndexType rsOrigin;
RemoteSensingRegionType::SizeType rsSize;
rsOrigin[0] = std::min(pul[0], plr[0]);
rsOrigin[1] = std::min(pul[1], plr[1]);
rsSize[0] = vcl_abs(pul[0] - plr[0]);
rsSize[1] = vcl_abs(pul[1] - plr[1]);
// Find the geographic region of interest
// Get the index of the corner of the image
IndexType ul, ur, ll, lr;
PointType pul, pur, pll, plr;
ul = inImage->GetLargestPossibleRegion().GetIndex();
ur = ul;
ll = ul;
lr = ul;
ur[0] += inImage->GetLargestPossibleRegion().GetSize()[0];
lr[0] += inImage->GetLargestPossibleRegion().GetSize()[0];
lr[1] += inImage->GetLargestPossibleRegion().GetSize()[1];
ll[1] += inImage->GetLargestPossibleRegion().GetSize()[1];
// Transform to physical point
inImage->TransformIndexToPhysicalPoint(ul, pul);
inImage->TransformIndexToPhysicalPoint(ur, pur);
inImage->TransformIndexToPhysicalPoint(ll, pll);
inImage->TransformIndexToPhysicalPoint(lr, plr);
// Build the cartographic region
RemoteSensingRegionType rsRegion;
RemoteSensingRegionType::IndexType rsOrigin;
RemoteSensingRegionType::SizeType rsSize;
rsOrigin[0] = std::min(pul[0], plr[0]);
rsOrigin[1] = std::min(pul[1], plr[1]);
rsSize[0] = vcl_abs(pul[0] - plr[0]);
rsSize[1] = vcl_abs(pul[1] - plr[1]);
rsRegion.SetOrigin(rsOrigin);
rsRegion.SetSize(rsSize);
rsRegion.SetRegionProjection(imgReader->GetOutput()->GetProjectionRef());
rsRegion.SetKeywordList(imgReader->GetOutput()->GetImageKeywordlist());
// Set the cartographic region to the extract roi filter
vdExtract->SetRegion(rsRegion);
if(parseResult->IsOptionPresent("DEMPath"))
{
vdExtract->SetDEMDirectory(demPath);
}
// Reprojecting the VectorData
vdReader->GetOutput()->GetProjectionRef();
VectorDataProjectionFilterType::Pointer vdProj = VectorDataProjectionFilterType::New();
vdProj->SetInput(vdExtract->GetOutput());
vdProj->SetInputProjectionRef(vdReader->GetOutput()->GetProjectionRef());
vdProj->SetOutputKeywordList(imgReader->GetOutput()->GetImageKeywordlist());
vdProj->SetOutputProjectionRef(imgReader->GetOutput()->GetProjectionRef());
vdProj->SetOutputOrigin(imgReader->GetOutput()->GetOrigin());
vdProj->SetOutputSpacing(imgReader->GetOutput()->GetSpacing());
if(parseResult->IsOptionPresent("DEMPath"))
{
vdProj->SetDEMDirectory(demPath);
}
// Instantiate the writer
std::string outFilename = parseResult->GetParameterString("OutputVData");
rsRegion.SetOrigin(rsOrigin);
rsRegion.SetSize(rsSize);
rsRegion.SetRegionProjection(inImage->GetProjectionRef());
rsRegion.SetKeywordList(inImage->GetImageKeywordlist());
// Set the cartographic region to the extract roi filter
m_VdExtract->SetRegion(rsRegion);
// Reprojecting the VectorData
m_VdProj = VectorDataProjectionFilterType::New();
m_VdProj->SetInput(m_VdExtract->GetOutput());
m_VdProj->SetInputProjectionRef(inImage->GetProjectionRef());
m_VdProj->SetOutputKeywordList(inImage->GetImageKeywordlist());
m_VdProj->SetOutputProjectionRef(inImage->GetProjectionRef());
m_VdProj->SetOutputOrigin(inImage->GetOrigin());
m_VdProj->SetOutputSpacing(inImage->GetSpacing());
// Set the DEM directory if used
if (IsParameterEnabled("elev.dem") && HasValue("elev.dem"))
{
m_VdExtract->SetDEMDirectory(GetParameterString("elev.dem"));
m_VdProj->SetDEMDirectory(GetParameterString("elev.dem"));
}
else
{
if ( otb::ConfigurationFile::GetInstance()->IsValid() )
{
m_VdExtract->SetDEMDirectory(otb::ConfigurationFile::GetInstance()->GetDEMDirectory());
m_VdProj->SetDEMDirectory(otb::ConfigurationFile::GetInstance()->GetDEMDirectory());
}
}
// Set the output vectorData
SetParameterOutputVectorData("io.out", m_VdProj->GetOutput());
}
VectorDataWriterType::Pointer vdWriter = VectorDataWriterType::New();
vdWriter->SetFileName(outFilename);
vdWriter->SetInput(vdExtract->GetOutput());
vdWriter->Update();
VectorDataExtractROIType::Pointer m_VdExtract;
VectorDataProjectionFilterType::Pointer m_VdProj;
};
return EXIT_SUCCESS;
}
}
OTB_APPLICATION_EXPORT(otb::Wrapper::VectorDataExtractROIApplication)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment