From 6d72319a6824712cc97c32bba0be2e3da291dbef Mon Sep 17 00:00:00 2001
From: Christian Schmitt <chris@ilovelinux.de>
Date: Fri, 21 Dec 2012 17:28:49 +0100
Subject: [PATCH] gdalchop: improve speed, especially when using a high number
 of datasets. We only pass images to gdal now, if they intersect in any way
 with the current bucket.

---
 src/Prep/GDALChop/CMakeLists.txt |  6 +++---
 src/Prep/GDALChop/gdalchop.cxx   | 21 +++++++++++++++++----
 2 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/src/Prep/GDALChop/CMakeLists.txt b/src/Prep/GDALChop/CMakeLists.txt
index c18d78b1..7c350d16 100644
--- a/src/Prep/GDALChop/CMakeLists.txt
+++ b/src/Prep/GDALChop/CMakeLists.txt
@@ -2,8 +2,8 @@ include_directories(${GDAL_INCLUDE_DIR})
 add_executable(gdalchop gdalchop.cxx)
 
 target_link_libraries(gdalchop
-    ${GDAL_LIBRARY}
-	${SIMGEAR_CORE_LIBRARIES}
-	${SIMGEAR_CORE_LIBRARY_DEPENDENCIES}
+        terragear ${GDAL_LIBRARY}
+        ${SIMGEAR_CORE_LIBRARIES}
+        ${SIMGEAR_CORE_LIBRARY_DEPENDENCIES}
 )
 install(TARGETS gdalchop RUNTIME DESTINATION bin)
diff --git a/src/Prep/GDALChop/gdalchop.cxx b/src/Prep/GDALChop/gdalchop.cxx
index 7fe38073..773ab421 100644
--- a/src/Prep/GDALChop/gdalchop.cxx
+++ b/src/Prep/GDALChop/gdalchop.cxx
@@ -33,6 +33,8 @@
 #include <simgear/misc/sg_path.hxx>
 #include <simgear/misc/sg_dir.hxx>
 
+#include <Lib/terragear/tg_rectangle.hxx>
+
 #include <gdal.h>
 #include <gdal_alg.h>
 #include <gdal_priv.h>
@@ -114,6 +116,12 @@ public:
         w = west;
     }
 
+    tgRectangle GetBoundingBox() const {
+        tgRectangle box( SGGeod::fromDeg(east, south), SGGeod::fromDeg(west, north) );
+        box.sanify();
+        return box;
+    }
+
     const char* GetDescription() const {
         return dataset->GetDescription();
     }
@@ -358,6 +366,9 @@ void process_bucket(const SGPath& work_dir, SGBucket bucket,
 {
     double bnorth, bsouth, beast, bwest;
 
+    tgRectangle BucketBounds(bucket.get_corner(0), bucket.get_corner(2));
+    BucketBounds.sanify();
+
     bnorth = bucket.get_center_lat() + bucket.get_height() * 0.5;
     bsouth = bucket.get_center_lat() - bucket.get_height() * 0.5;
     beast = bucket.get_center_lon() + bucket.get_width() * 0.5;
@@ -391,10 +402,12 @@ void process_bucket(const SGPath& work_dir, SGBucket bucket,
     ::memset(buffer.get(), 0, cellcount * sizeof(int));
 
     for (int i = 0; i < imagecount; i++) {
-        images[i]->GetDataChunk(work_dir, buffer.get(),
-                                bwest, bsouth,
-                                col_step / 3600.0, row_step / 3600.0,
-                                span_x, span_y );
+        if ( images[i]->GetBoundingBox().intersects(BucketBounds) ) {
+            images[i]->GetDataChunk(work_dir, buffer.get(),
+                                    bwest, bsouth,
+                                    col_step / 3600.0, row_step / 3600.0,
+                                    span_x, span_y );
+        }
     }
 
     /* ...check the amount of undefined cells... */