Newer
Older
# -*- coding: utf-8 -*-
"""
/***************************************************************************
EO Time Series Viewer
-------------------
begin : 2015-08-20
git sha : $Format:%H$
copyright : (C) 2017 by HU-Berlin
email : benjamin.jakimow@geo.hu-berlin.de
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
# noinspection PyPep8Naming
import numpy as np
from qgis.gui import *
from qgis.PyQt.QtCore import *
from qgis.PyQt.QtGui import *
from qgis.PyQt.QtWidgets import *
import eotimeseriesviewer.externals.qps.testing
import eotimeseriesviewer.externals.qps
from eotimeseriesviewer.utils import file_search
from eotimeseriesviewer import DIR_EXAMPLES, DIR_QGIS_RESOURCES, DIR_UI, DIR_REPO
from eotimeseriesviewer.timeseries import TimeSeries
from eotimeseriesviewer.externals.qps.testing import TestObjects, TestCase, start_app
from eotimeseriesviewer.externals.qps.resources import initQtResources
@classmethod
def setUpClass(cls):
if DIR_QGIS_RESOURCES.is_dir():
initQtResources(DIR_QGIS_RESOURCES)
initQtResources(DIR_REPO / 'eotimeseriesviewer' / 'externals')
eotsv_resources = DIR_UI / 'eotsv_resources_rc.py'
assert eotsv_resources.is_file(), \
'eotsv_resources_rc.py not compiled. run python scripts/compile_resourcefiles.py first.'
initQtResources(DIR_UI)
import os
os.environ['EOTSV_DEBUG'] = 'True'
def closeBlockingWidget(self):
"""
Closes the active blocking (modal) widget
"""
w = QApplication.instance().activeModalWidget()
if isinstance(w, QWidget):
print('Close blocking {} "{}"'.format(w.__class__.__name__, w.windowTitle()))
w.close()
return list(file_search(os.path.dirname(example.__file__), '*.tif', recursive=True))
def createTimeSeries(self) -> TimeSeries:
files = testRasterFiles()
TS = TimeSeries()
self.assertIsInstance(TS, TimeSeries)
TS.addSources(files)
self.assertTrue(len(TS) > 0)
return TS
class TestObjects(eotimeseriesviewer.externals.qps.testing.TestObjects):
"""
Creates objects to be used for testing. It is preferred to generate objects in-memory.
"""
@staticmethod

Benjamin Jakimow
committed
files = file_search(DIR_EXAMPLES, '*.tif', recursive=True)

Benjamin Jakimow
committed
assert len(TS) > 0
@staticmethod
vsiDir = '/vsimem/tmp'
d1 = np.datetime64('2000-01-01')
print('Create in-memory test timeseries of length {}...'.format(n))
files = testRasterFiles()
paths = []
i = 0
import itertools
drv = gdal.GetDriverByName('GTiff')
assert isinstance(drv, gdal.Driver)
for file in itertools.cycle(files):
if i >= n:
break
date = d1 + i
path = os.path.join(vsiDir, 'file.{}.{}.tif'.format(i, date))
dsDst = drv.CreateCopy(path, gdal.Open(file))
assert isinstance(dsDst, gdal.Dataset)
paths.append(path)
i += 1
print('Done!')
return paths
@staticmethod
def createTimeSeriesStacks():
vsiDir = '/vsimem/tmp'
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
ns = 50
nl = 100
r1 = np.arange('2000-01-01', '2005-06-14', step=np.timedelta64(16, 'D'), dtype=np.datetime64)
r2 = np.arange('2000-01-01', '2005-06-14', step=np.timedelta64(8, 'D'), dtype=np.datetime64)
drv = gdal.GetDriverByName('ENVI')
crs = osr.SpatialReference()
crs.ImportFromEPSG(32633)
assert isinstance(drv, gdal.Driver)
datasets = []
for i, r in enumerate([r1, r2]):
p = '{}tmpstack{}.bsq'.format(vsiDir, i + 1)
ds = drv.Create(p, ns, nl, len(r), eType=gdal.GDT_Float32)
assert isinstance(ds, gdal.Dataset)
ds.SetProjection(crs.ExportToWkt())
dateString = ','.join([str(d) for d in r])
dateString = '{{{}}}'.format(dateString)
ds.SetMetadataItem('wavelength', dateString, 'ENVI')
for b, date in enumerate(r):
decimalYear = date2num(date)
band = ds.GetRasterBand(b + 1)
assert isinstance(band, gdal.Band)
band.Fill(decimalYear)
ds.FlushCache()
datasets.append(p)
return datasets
@staticmethod
def testImagePaths() -> list:
import example
path = pathlib.Path(example.__file__).parent / 'Images'
files = list(file_search(path, '*.tif', recursive=True))
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
assert len(files) > 0
return files
@staticmethod
def createTestImageSeries(n=1) -> list:
assert n > 0
datasets = []
for i in range(n):
ds = TestObjects.inMemoryImage()
datasets.append(ds)
return datasets
@staticmethod
def createMultiSourceTimeSeries() -> list:
import example
# real files
files = TestObjects.testImagePaths()
movedFiles = []
d = r'/vsimem/'
for pathSrc in files:
bn = os.path.basename(pathSrc)
pathDst = d + 'shifted_' + bn + '.bsq'
dsSrc = gdal.Open(pathSrc)
tops = gdal.TranslateOptions(format='ENVI')
gdal.Translate(pathDst, dsSrc, options=tops)
dsDst = gdal.Open(pathDst, gdal.GA_Update)
assert isinstance(dsDst, gdal.Dataset)
gt = list(dsSrc.GetGeoTransform())
ns, nl = dsDst.RasterXSize, dsDst.RasterYSize
gt[0] = gt[0] + 0.5 * ns * gt[1]
gt[3] = gt[3] + abs(0.5 * nl * gt[5])
dsDst.SetGeoTransform(gt)
dsDst.SetMetadata(dsSrc.GetMetadata(''), '')
dsDst.FlushCache()
dsDst = None
dsDst = gdal.Open(pathDst)
assert list(dsDst.GetGeoTransform()) == gt
movedFiles.append(pathDst)
final = []
for f1, f2 in zip(files, movedFiles):
final.append(f1)
final.append(f2)
return final