From 51fb573d82bae0f158a149134c67c1cc1ef3498c Mon Sep 17 00:00:00 2001
From: "benjamin.jakimow@geo.hu-berlin.de" <q8DTkxUg-BB>
Date: Fri, 2 Dec 2016 16:22:54 +0100
Subject: [PATCH] create test image of ~20 MB in total

---
 make/make.py               | 123 +++++++++++++++++++++++++++----------
 make/testdata_sources2.txt |   5 ++
 2 files changed, 94 insertions(+), 34 deletions(-)
 create mode 100644 make/testdata_sources2.txt

diff --git a/make/make.py b/make/make.py
index 85753970..f4b36948 100644
--- a/make/make.py
+++ b/make/make.py
@@ -1,7 +1,11 @@
 import os, sys, fnmatch, six, subprocess, re
-from PyQt4.QtSvg import *
-from PyQt4.QtCore import *
+from qgis import *
+from qgis.core import *
+from qgis.gui import *
 from PyQt4.QtGui import *
+from PyQt4.QtCore import *
+
+from PyQt4.QtSvg import *
 from PyQt4.QtXml import *
 from PyQt4.QtXmlPatterns import *
 
@@ -23,7 +27,7 @@ def getDOMAttributes(elem):
     return values
 
 
-def createTestData(pathTS, subsetRectangle, crs, dirTestData):
+def createTestData(dirTestData, pathTS, subsetRectangle, crs, drv=None):
     lines = open(pathTS).readlines()
     import tempfile, random
     from timeseriesviewer.main import TimeSeries, TimeSeriesDatum
@@ -44,53 +48,71 @@ def createTestData(pathTS, subsetRectangle, crs, dirTestData):
     max_offset_y = sw / 100 * max_offset
     center = subsetRectangle.center()
 
-    os.mkdir(dirTestData)
-    pathVRT = tempfile.mkstemp(suffix='.vrt', prefix='tempVRT')
+    if not os.path.exists(dirTestData):
+        os.mkdir(dirTestData)
+    dirImages = os.path.join(dirTestData, 'Images')
+    if not os.path.exists(dirImages):
+        os.mkdir(dirImages)
 
-    def random_xy():
-        offset_x = center.x() + random.randrange(-max_offset_x, max_offset_x)
-        offset_y = center.y() + random.randrange(-max_offset_y, max_offset_y)
+    def random_offset():
+        offset_x = random.randrange(-max_offset_x, max_offset_x) if max_offset_x > 0 else 0
+        offset_y = random.randrange(-max_offset_y, max_offset_y) if max_offset_y > 0 else 0
         return offset_x, offset_y
 
-    for TSD in TS:
+    drvMEM = gdal.GetDriverByName('MEM')
+
+    from timeseriesviewer.main import transformGeometry
+    for TSD in TS.data:
         assert isinstance(TSD, TimeSeriesDatum)
 
-        ox, oy = random_xy()
+        ox, oy = random_offset()
+
         UL = QgsPoint(subsetRectangle.xMinimum() + ox,
                       subsetRectangle.yMaximum() + oy)
         LR = QgsPoint(subsetRectangle.xMaximum() + ox,
                       subsetRectangle.yMinimum() + oy)
-
-        lyr = QgsRasterLayer(TSD.pathImg)
-        crsDS = lyr.dataProvider().crs()
-        assert isinstance(crsDS, QgsCoordinateReferenceSystem)
-        transform = QgsCoordinateTransform(crs, crsDS)
-        #transform UL and LR into CRS of source data set
-        UL = transform.transform(UL)
-        LR = transform.transform(LR)
+        UL = transformGeometry(UL, crs, TSD.crs)
+        LR = transformGeometry(LR, crs, TSD.crs)
         BBOX = QgsRectangle(UL, LR)
-
-        #crop src dataset to UL-LR box
+        if not BBOX.intersects(TSD.getBoundingBox()):
+            print('Please note: no intersection with BBOX: '+TSD.pathImg)
+        #crop src dataset to BBOX
         #for this we use GDAL
+        LUT_EXT = {'ENVI':'.bsq'}
+
 
-        dsSrc = gdal.Open(TSD.pathImg)
-        assert isinstance(dsSrc, gdal.Dataset)
-        proj = dsSrc.GetProjection()
-        trans = dsSrc.GetGeoTransform()
-        trans[0] = UL.x()
-        trans[3] = UL.y()
+        filesToCopy = [f for f in [TSD.pathImg, TSD.pathMsk] if f is not None and os.path.exists(f)]
+        for pathSrc in filesToCopy:
+            dsSrc = gdal.Open(pathSrc)
 
-        ns = BBOX.width() / lyr.rasterUnitsPerPixelX()
-        nl = BBOX.heigth() / lyr.rasterUnitsPerPixelY()
+            assert isinstance(dsSrc, gdal.Dataset)
+            proj = dsSrc.GetProjection()
+            trans = list(dsSrc.GetGeoTransform())
+            trans[0] = UL.x()
+            trans[3] = UL.y()
 
-        drv = dsSrc.GetDriver()
-        assert isinstance(drv, gdal.Driver)
+            nsDst = int(BBOX.width() / TSD.lyrImg.rasterUnitsPerPixelX())
+            nlDst = int(BBOX.height() / TSD.lyrImg.rasterUnitsPerPixelY())
 
-        dsDst = drv.Create(ns, nl, dsSrc.RasterCount, eType = dsSrc.GetRasterBand(1).)
-        assert isinstance(dsDst, gdal.Dataset)
-        dsDst.SetProjection(proj)
-        dsDst.SetGeoTransform(trans)
+            dsDst = drvMEM.Create('', nsDst, nlDst, dsSrc.RasterCount, eType = dsSrc.GetRasterBand(1).DataType)
+            assert isinstance(dsDst, gdal.Dataset)
+            dsDst.SetProjection(proj)
+            dsDst.SetGeoTransform(trans)
+            wo = gdal.WarpOptions()
+            r = gdal.Warp(dsDst, dsSrc)
 
+            assert r > 0
+
+            drvDst = gdal.GetDriverByName(drv) if drv is not None else dsSrc.GetDriver()
+            #try to retireve an extension
+            pathDst = os.path.join(dirImages, os.path.splitext(os.path.basename(pathSrc))[0])
+            ext = drvDst.GetMetadata_Dict().get('DMD_EXTENSION','')
+            if ext == '':
+                ext = LUT_EXT.get(drvDst.ShortName, '')
+            if not pathDst.endswith(ext):
+                pathDst += ext
+            print('Write {}'.format(pathDst))
+            drvDst.CreateCopy(pathDst, dsDst)
 
 
 
@@ -259,6 +281,39 @@ def png2qrc(icondir, pathQrc, pngprefix='timeseriesviewer/png'):
 if __name__ == '__main__':
     icondir = jp(DIR_UI, *['icons'])
     pathQrc = jp(DIR_UI,'resources.qrc')
+    if True:
+        from qgis import *
+        from qgis.core import *
+        from qgis.gui import *
+
+        if sys.platform == 'darwin':
+            PATH_QGS = r'/Applications/QGIS.app/Contents/MacOS'
+            os.environ['GDAL_DATA'] = r'/usr/local/Cellar/gdal/1.11.3_1/share'
+        else:
+            # assume OSGeo4W startup
+            PATH_QGS = os.environ['QGIS_PREFIX_PATH']
+        assert os.path.exists(PATH_QGS)
+
+        qgsApp = QgsApplication([], True)
+        QApplication.addLibraryPath(r'/Applications/QGIS.app/Contents/PlugIns')
+        QApplication.addLibraryPath(r'/Applications/QGIS.app/Contents/PlugIns/qgis')
+        qgsApp.setPrefixPath(PATH_QGS, True)
+        qgsApp.initQgis()
+
+        pathDirTestData = r'C:\Users\geo_beja\Repositories\QGIS_Plugins\SenseCarbonTSViewer\example'
+        #path Novo Progresso site L7/L8/RE time series
+        #pathTS = r'C:\Users\geo_beja\Repositories\QGIS_Plugins\SenseCarbonTSViewer\make\testdata_sources2.txt'
+        pathTS = r'C:\Users\geo_beja\Repositories\QGIS_Plugins\SenseCarbonTSViewer\make\testdata_sources.txt'
+        from qgis.core import QgsCoordinateReferenceSystem, QgsPoint, QgsRectangle
+        subset = QgsRectangle(QgsPoint(-55.36091,-6.79851), #UL
+                              QgsPoint(-55.34132,-6.80514)) #LR
+
+        crs = QgsCoordinateReferenceSystem('EPSG:4326') # lat lon coordinates
+
+
+        createTestData(pathDirTestData, pathTS,subset, crs, drv='ENVI')
+        exit(0)
+
     if True:
         #convert SVG to PNG and link them into the resource file
         #svg2png(icondir, overwrite=True)
diff --git a/make/testdata_sources2.txt b/make/testdata_sources2.txt
new file mode 100644
index 00000000..56f061a5
--- /dev/null
+++ b/make/testdata_sources2.txt
@@ -0,0 +1,5 @@
+#Time series definition file: 2016-02-19T14:13:05Z
+#<image path>[;<mask path>]
+\\141.20.140.91\SAN_Projects\SenseCarbon\BJ\COS_BACKUP\01_RasterData\02_CuttedVRT\2012-04-07_LE72270652012098EDC00_BOA.vrt
+\\141.20.140.91\SAN_RSDBrazil\RapidEye\3A_VRTs\re_2012-06-12.vrt
+\\141.20.140.91\SAN_Projects\SenseCarbon\BJ\COS_BACKUP\01_RasterData\02_CuttedVRT\2012-06-26_LE72270652012178EDC00_BOA.vrt
-- 
GitLab