From c526551c27cb0b685b84e3ce6416e5ac08fd0663 Mon Sep 17 00:00:00 2001
From: Julien Malik <julien.malik@c-s.fr>
Date: Tue, 1 Mar 2011 13:26:03 +0100
Subject: [PATCH] ENH: add test for instanciation of GenericRSTransform from
 image projref and kwl

---
 Code/Projections/otbGenericRSTransform.txx    | 31 ++++++---
 Testing/Code/Projections/CMakeLists.txt       |  5 ++
 .../otbGenericRSTransformFromImage.cxx        | 66 +++++++++++++++++++
 .../otbImageToGenericRSOutputParameters.cxx   |  8 +--
 .../Code/Projections/otbProjectionsTests3.cxx |  1 +
 5 files changed, 99 insertions(+), 12 deletions(-)
 create mode 100644 Testing/Code/Projections/otbGenericRSTransformFromImage.cxx

diff --git a/Code/Projections/otbGenericRSTransform.txx b/Code/Projections/otbGenericRSTransform.txx
index 0d8965ca22..3e07a9fde1 100644
--- a/Code/Projections/otbGenericRSTransform.txx
+++ b/Code/Projections/otbGenericRSTransform.txx
@@ -208,18 +208,33 @@ GenericRSTransform<TScalarType, NInputDimensions, NOutputDimensions>
     {
     typedef otb::InverseSensorModel<double, InputSpaceDimension, OutputSpaceDimension> InverseSensorModelType;
     typename InverseSensorModelType::Pointer sensorModel = InverseSensorModelType::New();
-    sensorModel->SetImageGeometry(m_OutputKeywordList);
-    if (!m_DEMDirectory.empty())
+    
+    bool imageGeometrySet = false;
+    try
+      {
+      sensorModel->SetImageGeometry(m_OutputKeywordList);
+      imageGeometrySet = true;
+      }
+    catch( itk::ExceptionObject& e)
       {
-      sensorModel->SetDEMDirectory(m_DEMDirectory);
+      otbMsgDevMacro(<< "Unable to instanciate a sensor model with the provided output keyword list. Exception caught : " << e)
       }
-    else if (m_AverageElevation != -32768.0)
+
+    
+    if (imageGeometrySet)
       {
-      sensorModel->SetAverageElevation(m_AverageElevation);
+      if (!m_DEMDirectory.empty())
+        {
+        sensorModel->SetDEMDirectory(m_DEMDirectory);
+        }
+      else if (m_AverageElevation != -32768.0)
+        {
+        sensorModel->SetAverageElevation(m_AverageElevation);
+        }
+      m_OutputTransform = sensorModel.GetPointer();
+      outputTransformIsSensor = true;
+      otbMsgDevMacro(<< "Output projection set to sensor model");
       }
-    m_OutputTransform = sensorModel.GetPointer();
-    outputTransformIsSensor = true;
-    otbMsgDevMacro(<< "Output projection set to sensor model");
     }
 
 
diff --git a/Testing/Code/Projections/CMakeLists.txt b/Testing/Code/Projections/CMakeLists.txt
index 764f56b11b..7483bd67db 100644
--- a/Testing/Code/Projections/CMakeLists.txt
+++ b/Testing/Code/Projections/CMakeLists.txt
@@ -819,6 +819,10 @@ ADD_TEST(prTvGenericRSTransformWithSRID ${PROJECTIONS_TESTS3}
 )
 
 
+ADD_TEST(prTvGenericRSTransformFromImage ${PROJECTIONS_TESTS3} 
+	       otbGenericRSTransformFromImage
+	       ${INPUTDATA}/WithoutProjRefWithKeywordlist.tif)
+
 ADD_TEST(prTuVectorDataProjectionFilterNew ${PROJECTIONS_TESTS3}  otbVectorDataProjectionFilterNew )
 
 #test points
@@ -1062,6 +1066,7 @@ otbGenericMapProjection.cxx
 otbGenericRSTransformNew.cxx
 otbGenericRSTransform.cxx
 otbGenericRSTransformWithSRID.cxx
+otbGenericRSTransformFromImage.cxx
 otbVectorDataProjectionFilterNew.cxx
 otbVectorDataProjectionFilter.cxx
 otbVectorDataProjectionFilterFromMapToSensor.cxx
diff --git a/Testing/Code/Projections/otbGenericRSTransformFromImage.cxx b/Testing/Code/Projections/otbGenericRSTransformFromImage.cxx
new file mode 100644
index 0000000000..415d40aefd
--- /dev/null
+++ b/Testing/Code/Projections/otbGenericRSTransformFromImage.cxx
@@ -0,0 +1,66 @@
+/*=========================================================================
+
+  Program:   ORFEO Toolbox
+  Language:  C++
+  Date:      $Date$
+  Version:   $Revision$
+
+
+  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
+  See OTBCopyright.txt for details.
+
+
+     This software is distributed WITHOUT ANY WARRANTY; without even
+     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+     PURPOSE.  See the above copyright notices for more information.
+
+=========================================================================*/
+#if defined(_MSC_VER)
+#pragma warning ( disable : 4786 )
+#endif
+
+#include "otbVectorImage.h"
+#include "otbImageFileReader.h"
+#include "otbGenericRSTransform.h"
+#include <ogr_spatialref.h>
+#include <fstream>
+
+typedef otb::VectorImage<double, 2>       ImageType;
+typedef otb::ImageFileReader<ImageType>   ReaderType;
+typedef otb::GenericRSTransform<>         TransformType;
+typedef TransformType::InputPointType     PointType;
+
+int otbGenericRSTransformFromImage(int argc, char* argv[])
+{
+  /*
+   * This test checks if we can instanciate a GenericRSTransform from the image information
+   * without throwing an exception
+   */
+
+  // Reader
+  ReaderType::Pointer reader = ReaderType::New();
+  reader->SetFileName(argv[1]);
+  reader->UpdateOutputInformation();
+  
+  // Build wgs ref
+  OGRSpatialReference oSRS;
+  oSRS.SetWellKnownGeogCS("WGS84");
+  char * wgsRef = NULL;
+  oSRS.exportToWkt(&wgsRef);
+
+  // Instanciate WGS->Image transform
+  TransformType::Pointer wgs2img = TransformType::New();
+  wgs2img->SetInputProjectionRef(wgsRef);
+  wgs2img->SetOutputProjectionRef(reader->GetOutput()->GetProjectionRef());
+  wgs2img->SetOutputKeywordList(reader->GetOutput()->GetImageKeywordlist());
+  wgs2img->InstanciateTransform();
+
+  // Instanciate Image->WGS transform
+  TransformType::Pointer img2wgs = TransformType::New();
+  img2wgs->SetInputProjectionRef(reader->GetOutput()->GetProjectionRef());
+  img2wgs->SetInputKeywordList(reader->GetOutput()->GetImageKeywordlist());
+  img2wgs->SetOutputProjectionRef(wgsRef);
+  img2wgs->InstanciateTransform();
+
+  return EXIT_SUCCESS;
+}
diff --git a/Testing/Code/Projections/otbImageToGenericRSOutputParameters.cxx b/Testing/Code/Projections/otbImageToGenericRSOutputParameters.cxx
index 89b9a19d6c..19e39ba69f 100644
--- a/Testing/Code/Projections/otbImageToGenericRSOutputParameters.cxx
+++ b/Testing/Code/Projections/otbImageToGenericRSOutputParameters.cxx
@@ -59,13 +59,13 @@ int otbImageToGenericRSOutputParameters (int argc, char * argv[])
   spacing[0] = 0.000006;
   spacing[1] = -0.000006;
   
-  // Filter  : Taget SRS : WGS84
+  // Filter  : Target SRS : WGS84
   FilterType::Pointer filter = FilterType::New();
   filter->SetInput(reader->GetOutput());
   filter->SetOutputProjectionRef(otb::GeoInformationConversion::ToWKT(4326));  //WGS84
   filter->Compute();
     
-  // Otuput file
+  // Output file
   std::ofstream outfile(outfname);
 
   outfile<<"Output Parameters for SRID : 4326 (WGS84)"<<std::endl;
@@ -74,7 +74,7 @@ int otbImageToGenericRSOutputParameters (int argc, char * argv[])
   outfile<<"Output Size : "<<filter->GetOutputSize()<<std::endl;
   outfile<< std::endl;
 
-  // target SRS : 32631 UTM 31 N
+  // Target SRS : 32631 UTM 31 N
   filter->SetOutputProjectionRef(otb::GeoInformationConversion::ToWKT(32631));  //WGS84
   filter->Compute();
   
@@ -84,7 +84,7 @@ int otbImageToGenericRSOutputParameters (int argc, char * argv[])
   outfile<<"Output Size : "<<filter->GetOutputSize()<<std::endl;
   outfile<< std::endl;
 
-  // target SRS : lambertII
+  // Target SRS : lambertII
   typedef otb::Lambert2EtenduForwardProjection Lambert2Type;
   Lambert2Type::Pointer lambert2Projection = Lambert2Type::New();
   std::string  lambertRef = lambert2Projection->GetWkt();
diff --git a/Testing/Code/Projections/otbProjectionsTests3.cxx b/Testing/Code/Projections/otbProjectionsTests3.cxx
index 8a7823dc64..cfac112ba3 100644
--- a/Testing/Code/Projections/otbProjectionsTests3.cxx
+++ b/Testing/Code/Projections/otbProjectionsTests3.cxx
@@ -33,6 +33,7 @@ void RegisterTests()
   REGISTER_TEST(otbGenericRSTransformNew);
   REGISTER_TEST(otbGenericRSTransform);
   REGISTER_TEST(otbGenericRSTransformWithSRID);
+  REGISTER_TEST(otbGenericRSTransformFromImage);
   REGISTER_TEST(otbVectorDataProjectionFilterNew);
   REGISTER_TEST(otbVectorDataProjectionFilter);
   REGISTER_TEST(otbVectorDataProjectionFilterFromMapToSensor);
-- 
GitLab