Skip to content
Snippets Groups Projects
Commit fa215b38 authored by Guillaume Pasero's avatar Guillaume Pasero
Browse files

ENH: adapt Rasterization application to new framework

parent 38668122
No related branches found
No related tags found
No related merge requests found
......@@ -15,197 +15,208 @@
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#include "otbRasterization.h"
#include "otbWrapperApplication.h"
#include "otbWrapperApplicationFactory.h"
#include <iostream>
#include "otbCommandLineArgumentParser.h"
//Image
#include "otbImage.h"
#include "otbImageFileReader.h"
#include "otbStreamingImageFileWriter.h"
#include "itkRGBAPixel.h"
//VectorData
#include "otbVectorData.h"
#include "otbVectorDataExtractROI.h"
#include "otbVectorDataProjectionFilter.h"
#include "otbVectorDataFileReader.h"
#include "otbVectorDataFileWriter.h"
#include "otbVectorDataProperties.h"
//Rasterization
#include "otbVectorDataToImageFilter.h"
#include "otbGeoInformationConversion.h"
//Misc
#include "otbRemoteSensingRegion.h"
#include "otbStandardWriterWatcher.h"
#include "otbPipelineMemoryPrintCalculator.h"
namespace otb
{
int Rasterization::Describe(ApplicationDescriptor* descriptor)
namespace Wrapper
{
descriptor->SetName("Rasterization");
descriptor->SetDescription("Reproject and Rasterize a Vector Data.");
descriptor->AddOption("InputVData", "The input vector data to be rasterized",
"in", 1, true, ApplicationDescriptor::FileName);
descriptor->AddOption("OutputImage", "An output image containing the rasterized vector data",
"out", 1, true, ApplicationDescriptor::OutputImage);
descriptor->AddOption("InputImage", "A reference image for extraction region and projection information (optional)",
"im", 1, false, ApplicationDescriptor::InputImage);
descriptor->AddOption("SizeX", "OutputSize[0] (useless if support image is given)",
"szx", 1, false, ApplicationDescriptor::Real);
descriptor->AddOption("SizeY", "OutputSize[1] (useless if support image is given)",
"szy", 1, false, ApplicationDescriptor::Real);
descriptor->AddOption("RSID", "Projection System RSID number (RSID 4326 for WGS84 32631 for UTM31N) (useless if support image is given)",
"epsg", 1, false, ApplicationDescriptor::Integer);
descriptor->AddOption("OriginX", "OutputOrigin[0] (optional if support image is given )",
"orx", 1, false, ApplicationDescriptor::Real);
descriptor->AddOption("OriginY", "OutputOrigin[1] (optional, useless if support image is given)",
"ory", 1, false, ApplicationDescriptor::Real);
descriptor->AddOption("SpacingX", "OutputSpacing[0] (optional, useless if support image is given )",
"spx", 1, false, ApplicationDescriptor::Real);
descriptor->AddOption("SpacingY", "OutputSpacing[1] (optional, useless if support image is given)",
"spy", 1, false, ApplicationDescriptor::Real);
descriptor->AddOption("AvailableMemory", "Set the maximum of available memory for the pipeline execution in mega bytes (optional, 256 by default)",
"ram", 1, false, otb::ApplicationDescriptor::Integer);
return EXIT_SUCCESS;
}
int Rasterization::Execute(otb::ApplicationOptionsResult* parseResult)
class Rasterization : public Application
{
// Images
//typedef itk::RGBAPixel<unsigned char> PixelType;
typedef unsigned char PixelType;
typedef otb::Image<PixelType, 2> ImageType;
typedef ImageType::PointType PointType;
typedef ImageType::SizeType SizeType;
typedef ImageType::SpacingType SpacingType;
typedef ImageType::IndexType IndexType;
typedef otb::StreamingImageFileWriter<ImageType> WriterType;
// VectorData
typedef otb::VectorData<> VectorDataType;
typedef VectorDataType::DataNodeType DataNodeType;
typedef VectorDataType::DataTreeType DataTreeType;
typedef DataNodeType::PointType PointType;
typedef otb::VectorDataFileReader<VectorDataType> VectorDataReaderType;
typedef otb::VectorDataFileWriter<VectorDataType> VectorDataWriterType;
public:
/** Standard class typedefs. */
typedef Rasterization Self;
typedef Application Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
/** Standard macro */
itkNewMacro(Self);
itkTypeMacro(Rasterization, otb::Application);
/** Filters typedef */
// the application produces a binary mask : no need to use a FloatVectorImageType
typedef UInt8ImageType::PointType PointType;
typedef UInt8ImageType::SizeType SizeType;
typedef UInt8ImageType::SpacingType SpacingType;
typedef UInt8ImageType::IndexType IndexType;
typedef VectorDataProjectionFilter<VectorDataType, VectorDataType> VectorDataProjectionFilterType;
typedef VectorDataExtractROI<VectorDataType> VectorDataExtractROIType;
typedef VectorDataProperties<VectorDataType> VectorDataPropertiesType;
// Image
typedef otb::ImageFileReader<ImageType> ImageReaderType;
// Rasterization
typedef otb::VectorDataToImageFilter<VectorDataType, ImageType> VectorDataToImageFilterType;
typedef otb::VectorDataToImageFilter<VectorDataType, UInt8ImageType> VectorDataToImageFilterType;
// Misc
typedef otb::RemoteSensingRegion<double> RemoteSensingRegionType;
typedef RemoteSensingRegionType::SizeType SizePhyType;
typedef otb::PipelineMemoryPrintCalculator MemoryCalculatorType;
ImageReaderType::Pointer imReader;
ImageType::Pointer referenceImage;
// Reading the VectorData
std::string vdFilename = parseResult->GetParameterString("InputVData");
std::cout << "Processing vector data : " << vdFilename << std::endl;
VectorDataReaderType::Pointer vdReader = VectorDataReaderType::New();
vdReader->SetFileName(vdFilename);
vdReader->Update();
private:
Rasterization()
{
SetName("Rasterization");
SetDescription("Reproject and Rasterize a Vector Data.");
SetDocName("Rasterization Application");
SetDocLongDescription("Reproject and Rasterize a Vector Data.");
SetDocLimitations("None");
SetDocAuthors("OTB-Team");
SetDocSeeAlso(" ");
AddDocTag(Tags::Vector);
}
// region information
SizeType size;
PointType origin;
SpacingType spacing;
SizePhyType sizePhy;
virtual ~Rasterization()
{
}
// reading projection information
// two choice :
std::string outputProjectionRef;
// a reference image is given as input
if (parseResult->IsOptionPresent("InputImage"))
void DoCreateParameters()
{
AddParameter(ParameterType_InputVectorData, "in", "InputVectorData");
SetParameterDescription( "in", "The input vector data to be rasterized" );
AddParameter(ParameterType_OutputImage, "out", "OutputImage");
SetParameterDescription( "out", "An output image containing the rasterized vector data" );
AddParameter(ParameterType_InputImage, "im", "InputImage");
SetParameterDescription( "im", "A reference image for extraction region and projection information (optional)" );
MandatoryOff("im");
AddParameter(ParameterType_Float, "szx", "SizeX");
SetParameterDescription( "szx", "OutputSize[0] (useless if support image is given)" );
MandatoryOff("szx");
AddParameter(ParameterType_Float, "szy", "SizeY");
SetParameterDescription( "szy", "OutputSize[1] (useless if support image is given)" );
MandatoryOff("szy");
AddParameter(ParameterType_Int, "epsg", "RSID");
SetParameterDescription( "epsg", "Projection System RSID number (RSID 4326 for WGS84 32631 for UTM31N) (useless if support image is given)" );
MandatoryOff("epsg");
AddParameter(ParameterType_Float, "orx", "OriginX");
SetParameterDescription( "orx", "OutputOrigin[0] (useless if support image is given)" );
MandatoryOff("orx");
AddParameter(ParameterType_Float, "ory", "OriginY");
SetParameterDescription( "ory", "OutputOrigin[1] (useless if support image is given)" );
MandatoryOff("ory");
AddParameter(ParameterType_Float, "spx", "SpacingX");
SetParameterDescription( "spx", "OutputSpacing[0] (useless if support image is given)" );
MandatoryOff("spx");
AddParameter(ParameterType_Float, "spy", "SpacingY");
SetParameterDescription( "spy", "OutputSpacing[1] (useless if support image is given)" );
MandatoryOff("spy");
AddParameter(ParameterType_RAM, "ram", "Available RAM");
SetDefaultParameterInt("ram", 256);
MandatoryOff("ram");
SetDocExampleParameterValue("in","Rasterization/polgons.shp");
SetDocExampleParameterValue("out", "rasterImage.tif");
SetDocExampleParameterValue("szx","1100");
SetDocExampleParameterValue("szy","1100");
}
void DoUpdateParameters()
{
if (parseResult->IsOptionPresent("SizeX") || parseResult->IsOptionPresent("SizeY")
|| parseResult->IsOptionPresent("OriginX") || parseResult->IsOptionPresent("OriginY")
|| parseResult->IsOptionPresent("SpacingX") || parseResult->IsOptionPresent("SpacingY")
|| parseResult->IsOptionPresent("RSID"))
// Nothing to do
}
void DoExecute()
{
VectorDataType::Pointer inputVData = GetParameterVectorData("in");
inputVData->Update();
UInt8ImageType::Pointer referenceImage;
// region information
SizeType size;
PointType origin;
SpacingType spacing;
SizePhyType sizePhy;
// reading projection information
// two choice :
std::string outputProjectionRef;
// a reference image is given as input
if (HasValue("im"))
{
std::cerr << "Input entry problem " << std::endl
<< " reference image can't be used with other projection parameters. " << std::endl;
return EXIT_FAILURE;
if (HasValue("szx") || HasValue("szy") || HasValue("orx") || HasValue("ory")
|| HasValue("spx") || HasValue("spy") || HasValue("epsg"))
{
otbAppLogWARNING("A reference image has been given, other parameters "
"regarding the output image will be ignored");
}
referenceImage = GetParameterUInt8Image("im");
outputProjectionRef = referenceImage->GetProjectionRef();
size[0] = referenceImage->GetLargestPossibleRegion().GetSize(0);
size[1] = referenceImage->GetLargestPossibleRegion().GetSize(1);
origin = referenceImage->GetOrigin();
spacing = referenceImage->GetSpacing();
sizePhy[0] = size[0] * spacing[0];
sizePhy[1] = size[1] * spacing[1];
}
std::string imFilename = parseResult->GetParameterString("InputImage");
imReader = ImageReaderType::New();
imReader->SetFileName(imFilename);
imReader->Update();
referenceImage = imReader->GetOutput();
outputProjectionRef = referenceImage->GetProjectionRef();
size[0] = referenceImage->GetLargestPossibleRegion().GetSize(0);
size[1] = referenceImage->GetLargestPossibleRegion().GetSize(1);
origin = referenceImage->GetOrigin();
spacing = referenceImage->GetSpacing();
sizePhy[0] = size[0] * spacing[0];
sizePhy[1] = size[1] * spacing[1];
}
else
if (parseResult->IsOptionPresent("SizeX") && parseResult->IsOptionPresent("SizeY"))
else if (HasValue("szx") && HasValue("szy"))
{
if (parseResult->IsOptionPresent("RSID"))
if (HasValue("epsg"))
{
unsigned int RSID = parseResult->GetParameterInt("RSID");
unsigned int RSID = GetParameterInt("epsg");
outputProjectionRef = otb::GeoInformationConversion::ToWKT(RSID);
}
else
{
outputProjectionRef = vdReader->GetOutput()->GetProjectionRef();
outputProjectionRef = inputVData->GetProjectionRef();
}
VectorDataPropertiesType::Pointer vdProperties = VectorDataPropertiesType::New();
vdProperties->SetVectorDataObject(vdReader->GetOutput());
vdProperties->ComputeBoundingRegion();
m_VdProperties = VectorDataPropertiesType::New();
m_VdProperties->SetVectorDataObject(inputVData);
m_VdProperties->ComputeBoundingRegion();
size[0] = parseResult->GetParameterDouble("SizeX");
size[1] = parseResult->GetParameterDouble("SizeY");
size[0] = GetParameterFloat("szx");
size[1] = GetParameterFloat("szy");
if (parseResult->IsOptionPresent("OriginX") && parseResult->IsOptionPresent("OriginY"))
if ( HasValue("orx") && HasValue("ory"))
{
origin[0] = parseResult->GetParameterDouble("OriginX");
origin[1] = parseResult->GetParameterDouble("OriginY");
origin[0] = GetParameterFloat("orx");
origin[1] = GetParameterFloat("ory");
}
else
{
origin = vdProperties->GetBoundingRegion().GetIndex();
origin = m_VdProperties->GetBoundingRegion().GetIndex();
}
if (parseResult->IsOptionPresent("SpacingX") && parseResult->IsOptionPresent("SpacingY"))
if (HasValue("spx") && HasValue("spy"))
{
spacing[0] = parseResult->GetParameterDouble("SpacingX");
spacing[1] = parseResult->GetParameterDouble("SpacingY");
spacing[0] = GetParameterFloat("spx");
spacing[1] = GetParameterFloat("spy");
}
else
{
spacing[0] = vdProperties->GetBoundingRegion().GetSize()[0] / size[0];
spacing[1] = vdProperties->GetBoundingRegion().GetSize()[1] / size[1];
spacing[0] = m_VdProperties->GetBoundingRegion().GetSize()[0] / size[0];
spacing[1] = m_VdProperties->GetBoundingRegion().GetSize()[1] / size[1];
}
sizePhy[0] = size[0] * spacing[0];
......@@ -214,68 +225,60 @@ int Rasterization::Execute(otb::ApplicationOptionsResult* parseResult)
}
else
{
std::cerr << "Input entry problem " << std::endl << " if no reference image given, region size is needed "
<< std::endl;
return EXIT_FAILURE;
otbAppLogFATAL("Input entry problem, if no reference image given, region size is needed ");
}
// Reprojecting the VectorData
VectorDataProjectionFilterType::Pointer vproj = VectorDataProjectionFilterType::New();
vproj->SetInput(vdReader->GetOutput());
//
vproj->SetInputProjectionRef(vdReader->GetOutput()->GetProjectionRef());
vproj->SetOutputProjectionRef(outputProjectionRef);
if (parseResult->IsOptionPresent("InputImage"))
{
vproj->SetOutputKeywordList(referenceImage->GetImageKeywordlist());
}
RemoteSensingRegionType region;
region.SetSize(sizePhy);
region.SetOrigin(origin);
region.SetRegionProjection(outputProjectionRef);
if (parseResult->IsOptionPresent("InputImage"))
{
region.SetKeywordList(referenceImage->GetImageKeywordlist());
}
VectorDataExtractROIType::Pointer vdextract = VectorDataExtractROIType::New();
vdextract->SetRegion(region);
vdextract->SetInput(vproj->GetOutput());
VectorDataToImageFilterType::Pointer vectorDataRendering = VectorDataToImageFilterType::New();
vectorDataRendering->SetInput(vdextract->GetOutput());
vectorDataRendering->SetSize(size);
vectorDataRendering->SetOrigin(origin);
vectorDataRendering->SetSpacing(spacing);
vectorDataRendering->SetVectorDataProjectionWKT(outputProjectionRef);
vectorDataRendering->SetRenderingStyleType(VectorDataToImageFilterType::Binary);
ImageType::Pointer outputImage = vectorDataRendering->GetOutput();
if (parseResult->IsOptionPresent("InputImage"))
{
outputImage->SetMetaDataDictionary(referenceImage->GetMetaDataDictionary());
}
// Instantiate the writer
std::string oFilename = parseResult->GetParameterString("OutputImage");
WriterType::Pointer oWriter = WriterType::New();
oWriter->SetFileName(oFilename);
oWriter->SetInput(outputImage);
unsigned int ram = 256;
if (parseResult->IsOptionPresent("AvailableMemory"))
{
ram = parseResult->GetParameterUInt("AvailableMemory");
// Reprojecting the VectorData
m_Vproj = VectorDataProjectionFilterType::New();
m_Vproj->SetInput(inputVData);
m_Vproj->SetInputProjectionRef(inputVData->GetProjectionRef());
m_Vproj->SetOutputProjectionRef(outputProjectionRef);
if (HasValue("im"))
{
m_Vproj->SetOutputKeywordList(referenceImage->GetImageKeywordlist());
}
RemoteSensingRegionType region;
region.SetSize(sizePhy);
region.SetOrigin(origin);
region.SetRegionProjection(outputProjectionRef);
if (HasValue("im"))
{
region.SetKeywordList(referenceImage->GetImageKeywordlist());
}
m_Vdextract = VectorDataExtractROIType::New();
m_Vdextract->SetRegion(region);
m_Vdextract->SetInput(m_Vproj->GetOutput());
m_VectorDataRendering = VectorDataToImageFilterType::New();
m_VectorDataRendering->SetInput(m_Vdextract->GetOutput());
m_VectorDataRendering->SetSize(size);
m_VectorDataRendering->SetOrigin(origin);
m_VectorDataRendering->SetSpacing(spacing);
m_VectorDataRendering->SetVectorDataProjectionWKT(outputProjectionRef);
m_VectorDataRendering->SetRenderingStyleType(VectorDataToImageFilterType::Binary);
UInt8ImageType::Pointer outputImage = m_VectorDataRendering->GetOutput();
if (HasValue("im"))
{
outputImage->SetMetaDataDictionary(referenceImage->GetMetaDataDictionary());
}
AddProcess(m_VectorDataRendering,"Rasterization");
SetParameterOutputImage<UInt8ImageType>("out",outputImage);
}
oWriter->SetAutomaticTiledStreaming(ram);
otb::StandardWriterWatcher watcher(oWriter, "Rasterization");
oWriter->Update();
VectorDataPropertiesType::Pointer m_VdProperties;
VectorDataProjectionFilterType::Pointer m_Vproj;
VectorDataExtractROIType::Pointer m_Vdextract;
VectorDataToImageFilterType::Pointer m_VectorDataRendering;
};
return EXIT_SUCCESS;
}
}
OTB_APPLICATION_EXPORT(otb::Wrapper::Rasterization)
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