From 3ca68ddf619126fc6cf462da1ef7c48397082b4a Mon Sep 17 00:00:00 2001
From: Manuel Grizonnet <manuel.grizonnet@cnes.fr>
Date: Thu, 24 May 2012 15:03:31 +0200
Subject: [PATCH] ENH:reimplement get_lenght method from gdal/ogr It would
 allow to use ogr fusion functionalities even with gdal version < 1.8.0 The
 implementation of the get_lenght method is based on the method available in
 OGRGeometryCollection class The behaviour is not modify for otb compiled with
 gdal > 1.8.0. The related compiles for now but there are remainig issues with
 layer deletation in the related test

---
 Code/OBIA/otbFusionOGRTileFilter.h   | 14 ++++++++----
 Code/OBIA/otbFusionOGRTileFilter.txx | 34 +++++++++++++++++++++++++---
 Testing/Code/OBIA/CMakeLists.txt     | 10 ++++----
 Testing/Code/OBIA/otbOBIATests1.cxx  |  4 ++--
 4 files changed, 48 insertions(+), 14 deletions(-)

diff --git a/Code/OBIA/otbFusionOGRTileFilter.h b/Code/OBIA/otbFusionOGRTileFilter.h
index ed18d633a0..aa6676c624 100644
--- a/Code/OBIA/otbFusionOGRTileFilter.h
+++ b/Code/OBIA/otbFusionOGRTileFilter.h
@@ -20,9 +20,9 @@
 
 #include "otbOGRDataSourceWrapper.h"
 
-#if(GDAL_VERSION_NUM < 1800)
-#error FusionOGRTileFilter requires GDAL version >= 1.8.0
-#endif
+//#if(GDAL_VERSION_NUM < 1800)
+//#error FusionOGRTileFilter requires GDAL version >= 1.8.0
+//#endif
 
 #include "itkProcessObject.h"
 
@@ -121,7 +121,13 @@ protected:
   } SortFeature;
   
   void ProcessStreamingLine(bool line);
-  
+  /** get length in case of  OGRGeometryCollection.
+   * This function recodes the get_lenght method available since gdal 1.8.0 
+   * in the case of OGRGeometryCollection. The aim is allow to access polygon stiching  
+   * functionalities with gdal 1.6.
+   */
+  double GetLengthOGRGeometryCollection(OGRGeometryCollection * intersection);
+
 private:
   FusionOGRTileFilter(const Self &);  //purposely not implemented
   void operator =(const Self&);      //purposely not implemented
diff --git a/Code/OBIA/otbFusionOGRTileFilter.txx b/Code/OBIA/otbFusionOGRTileFilter.txx
index 4d33c185f8..27cdf15454 100644
--- a/Code/OBIA/otbFusionOGRTileFilter.txx
+++ b/Code/OBIA/otbFusionOGRTileFilter.txx
@@ -73,7 +73,30 @@ FusionOGRTileFilter<TInputImage>
    return static_cast<OGRDataSourceType *> (this->itk::ProcessObject::GetInput(1));
 }
 
-
+template<class TInputImage>
+double
+FusionOGRTileFilter<TInputImage>
+::GetLengthOGRGeometryCollection( OGRGeometryCollection * intersection )
+{
+  double dfLength = 0.0;
+  for( int iGeom = 0; iGeom < intersection->getNumGeometries(); iGeom++ )
+    {
+      OGRGeometry* geom = intersection->getGeometryRef(iGeom); 
+      switch( wkbFlatten(geom->getGeometryType()) )
+	{
+	case wkbLinearRing:
+	case wkbLineString:
+	  dfLength += ((OGRCurve *) geom)->get_Length();
+	  break;
+	case wkbGeometryCollection:
+	  dfLength += GetLengthOGRGeometryCollection(dynamic_cast<OGRGeometryCollection *> (geom));
+	  break;
+	default:
+	  break;
+	}
+    }
+  return dfLength;
+}
 template<class TInputImage>
 void
 FusionOGRTileFilter<TInputImage>
@@ -210,8 +233,13 @@ FusionOGRTileFilter<TInputImage>
                      }
                      else if (intersection->getGeometryType() == wkbMultiLineString)
                      {
-                        fusion.overlap = dynamic_cast<OGRMultiLineString *>(intersection.get())->get_Length();
-                     }
+                        #if(GDAL_VERSION_NUM < 1800)
+			fusion.overlap = GetLengthOGRGeometryCollection(dynamic_cast<OGRGeometryCollection *> (intersection.get()));
+                        #else
+			fusion.overlap = dynamic_cast<OGRMultiLineString *>(intersection.get())->get_Length();
+                        #endif
+		     }
+	     
                      
                      long upperFID = upper.feat.GetFID();
                      long lowerFID = lower.feat.GetFID();
diff --git a/Testing/Code/OBIA/CMakeLists.txt b/Testing/Code/OBIA/CMakeLists.txt
index 9a2492c9ac..1706d330cb 100644
--- a/Testing/Code/OBIA/CMakeLists.txt
+++ b/Testing/Code/OBIA/CMakeLists.txt
@@ -270,7 +270,7 @@ ADD_TEST(obTvStreamingVectorizedSegmentationOGR ${OBIA_TESTS1}
      )
 
 
-IF(${GDAL_NUM_VERSION} VERSION_GREATER "10800")
+#IF(${GDAL_NUM_VERSION} VERSION_GREATER "10800")
 # -------            otb::FusionOGRTileFilter   -------------
 ADD_TEST(obTuFusionOGRTileFilter ${OBIA_TESTS1}
 #     --compare-ogr  ${EPSILON_8}
@@ -283,7 +283,7 @@ ADD_TEST(obTuFusionOGRTileFilter ${OBIA_TESTS1}
     layer
     112
     )
-ENDIF(${GDAL_NUM_VERSION} VERSION_GREATER "10800")
+#ENDIF(${GDAL_NUM_VERSION} VERSION_GREATER "10800")
 
 # OBIATests2 (need PQXX)
 IF(OTB_USE_PQXX)
@@ -353,9 +353,9 @@ otbStreamingVectorizedSegmentationOGR.cxx
 otbFusionOGRTileFilter.cxx
 )
 
-IF(${GDAL_NUM_VERSION} VERSION_LESS "10800")
-LIST(REMOVE_ITEM BasicOBIA_SRCS1 otbFusionOGRTileFilter.cxx)
-ENDIF(${GDAL_NUM_VERSION} VERSION_LESS "10800")
+#IF(${GDAL_NUM_VERSION} VERSION_LESS "10800")
+#LIST(REMOVE_ITEM BasicOBIA_SRCS1 otbFusionOGRTileFilter.cxx)
+#ENDIF(${GDAL_NUM_VERSION} VERSION_LESS "10800")
 
 
 IF(OTB_USE_PQXX)
diff --git a/Testing/Code/OBIA/otbOBIATests1.cxx b/Testing/Code/OBIA/otbOBIATests1.cxx
index d3884c23c5..f8a2a3c60c 100644
--- a/Testing/Code/OBIA/otbOBIATests1.cxx
+++ b/Testing/Code/OBIA/otbOBIATests1.cxx
@@ -68,7 +68,7 @@ REGISTER_TEST(otbLabelImageToOGRDataSourceFilterNew);
 REGISTER_TEST(otbLabelImageToOGRDataSourceFilter);
 REGISTER_TEST(otbStreamingVectorizedSegmentationOGRNew);
 REGISTER_TEST(otbStreamingVectorizedSegmentationOGR);
-#if GDAL_VERSION_NUM >= 1800
+//#if GDAL_VERSION_NUM >= 1800
 REGISTER_TEST(otbFusionOGRTileFilter);
-#endif
+//#endif
 }
-- 
GitLab