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 os, re, io, importlib, uuid
from qgis.core import *
import numpy as np
from qgis.gui import *
from qgis.PyQt.QtCore import *
from qgis.PyQt.QtGui import *
from qgis.PyQt.QtWidgets import *
from osgeo import ogr, osr, gdal, gdal_array
import example
from eotimeseriesviewer import DIR_EXAMPLES
from eotimeseriesviewer.timeseries import TimeSeries
def initQgisApplication(*args, **kwds)->QgsApplication:
"""
Initializes a QGIS Environment
:return: QgsApplication instance of local QGIS installation
"""
if isinstance(QgsApplication.instance(), QgsApplication):
return QgsApplication.instance()
else:
app = qps.testing.initQgisApplication(*args, **kwds)
import eotimeseriesviewer
eotimeseriesviewer.initEditorWidgets()
def testRasterFiles()->list:
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(qps.testing.TestObjects):
"""
Creates objects to be used for testing. It is preferred to generate objects in-memory.
"""
@staticmethod
def createTimeSeries():
TS = TimeSeries()

Benjamin Jakimow
committed
files = file_search(DIR_EXAMPLES, '*.tif', recursive=True)
TS.addSources(list(files))
assert len(TS) > 0
return TS
@staticmethod
def createTimeSeriesStacks():
vsiDir = '/vsimem/tmp'
from eotimeseriesviewer.temporalprofiles2d import date2num
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
117
118
119
120
121
122
123
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
158
159
160
161
162
163
@staticmethod
def testImagePaths() -> list:
import example
files = list(file_search(os.path.dirname(example.__file__), '*.tif', recursive=True))
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)
return files + movedFiles