Commit 47ed8348 authored by Daniel Scheffler's avatar Daniel Scheffler
Browse files

Merge branch 'Tests'

parents 76f15f8b a58b5bc3
#!/usr/bin/env python
# -*- coding: utf-8 -*-
###################################################################################
"""
test_geoarray
----------------------------------
Tests for `geoarray` module.
Tests for the basic functions of the 'GeoArray' class in the 'geoarray' module.
For this purpose the TIFF-file "L8_2bands_extract10x11.tif" in the directory
'../tests/data' is used. The outputs of the 'GeoArray'-class functions are tested
against the well known properties of the test-image.
Note that the tests in the test case "Test_GeoarrayAppliedOnTiff" follow the same
order as in the 'GeoArray'-class (but they are executed in alphanumeric order).
Functions that depend on each other are tested together.
"""
###################################################################################
__author__ = 'Jessica Palka'
import sys
# Imports from the python standard library
import unittest
from collections import OrderedDict
import numpy as np
import os.path
import osgeo.osr
# Imports regarding the created python module
from geoarray import GeoArray
from geoarray import __file__ as path_geoarrayLib
###################################################################################
# Test case: Test_GeoarrayAppliedOnTiffPath
class Test_GeoarrayAppliedOnTiffPath(unittest.TestCase):
@classmethod
def setUpClass(cls):
global L8_2bands_extract10x11
L8_2bands_extract10x11 = \
os.path.join(os.path.dirname(path_geoarrayLib), '../tests/data/L8_2bands_extract10x11.tif')
cls.testtiff = GeoArray(L8_2bands_extract10x11)
cls.TiffIsInstanceOfGeoarray(cls)
cls.ArrOfTiffIsInMemory(cls)
def TiffIsInstanceOfGeoarray(self):
"""
Indirect test.
Testing, if the object "testtiff" is an instance of the class "GeoArray". Is an exception raised for an error,
all tests of the test case "Test_GeoarrayAppliedOnTiffPath" will be skipped.
"""
try:
assert isinstance(self.testtiff, GeoArray)
except AssertionError:
self.skipTest(Test_GeoarrayAppliedOnTiffPath,
reason="The created object 'testtiff' is not an instance of class 'GeoArray'. "
"All tests of the Test Case 'Test_GeoarrayAppliedOnTiffPath' will be skipped!")
def ArrOfTiffIsInMemory(self):
"""
Testing the functions: arr - is_inmem, indirect test with 2 stages.
Stage 1: Checking, if the argument passed to the "GeoArray" class is a path or a numpy.ndarray.
Stage 2: It is tested, if the arr- and is_inmem-function give the expected output.
If, for any possibility that was tested, an exception is raised for an error, all tests of the test case
"Test_GeoarrayAppliedOnTiffPath" will be skipped.
"""
testtiff_basicfunctions = (self.testtiff.arr, self.testtiff.is_inmem)
expected_conditions = ((None, False), (True, True))
#PATH
if os.path.isfile(L8_2bands_extract10x11):
try:
assert (testtiff_basicfunctions[0] == expected_conditions[0][0]) and \
(testtiff_basicfunctions[1] == expected_conditions[0][1])
except AssertionError:
self.skipTest(Test_GeoarrayAppliedOnTiffPath,
reason="A path is passed to 'GeoArray'. But the output of the functions "
"arr and is_inmem %s do not match as expected %s!"
%(testtiff_basicfunctions, expected_conditions[0]))
#NUMPY.NDARRAY
elif isinstance(L8_2bands_extract10x11, np.ndarray):
try:
assert (isinstance(testtiff_basicfunctions[0], np.ndarray) == expected_conditions[1][0]) and \
(testtiff_basicfunctions[1] == expected_conditions[1][1])
except AssertionError:
self.skipTest(Test_GeoarrayAppliedOnTiffPath,
reason="A numpy.ndarray is passed to 'GeoArray'. But the output of the functions "
"arr and is_inmem %s do not match as expected %s!"
%(testtiff_basicfunctions, expected_conditions[1]))
else:
self.skipTest(Test_GeoarrayAppliedOnTiffPath,
reason="The variable committed to the 'GeoArray'-class is neither a path nor a numpy.ndarray."
"All tests of the Test Case 'Test_GeoarrayAppliedOnTiffPath' will be skipped!")
def test_Bandnames(self):
"""
Test, if the default band names were correctly assigned.
"""
expected_bandnames = OrderedDict([('B1', 0), ('B2', 1)])
self.assertEqual(self.testtiff.bandnames, expected_bandnames,
msg="The bandnames of the Tiff-file are different than ['B1', 'B2'] (format: OrderedDict)")
def test_ShapeOfTiffArray(self):
"""
Testing the functions: shape - ndim - rows - columns - bands, test with 2 stages.
Stage 1: Comparing the shape of the testtiff-image with the output of the shape-function.
When identical, induction of stage 2...
Stage 2: Comparing the output of the ndim-, rows-, columns- and bands-function with the expected results.
If the the shape of the image is not as expected (Stage 1), the whole test will be skipped.
"""
expected_shape = (10, 11, 2)
expected_result = (3, *expected_shape) #dimensions, rows, columns, bands
testtiff_shapefunctions = (self.testtiff.ndim, self.testtiff.rows, self.testtiff.columns, self.testtiff.bands)
shape_property = ('DIMENSIONS', 'ROWS', 'COLUMNS', 'BANDS')
if self.testtiff.shape == expected_shape:
for i in range(0, 4, 1):
with self.subTest(i=i):
self.assertEqual(testtiff_shapefunctions[i], expected_result[i],
msg='The number of {i} is different from the expected result!'.format(i=shape_property[i]))
else:
print("The shape of the array behind the 'Geoarray'-object is not as expected! "
"The test 'test_ShapeOfTiffArray' will be skipped.")
self.skipTest(Test_GeoarrayAppliedOnTiffPath)
def test_DtypeOfTiffArray(self):
"""
Test, if the data type of the .ndarray behind the "GeoArray"-class was correctly assigned.
"""
expected_dtype = np.dtype('float32')
self.assertEqual(self.testtiff.dtype, expected_dtype,
msg='The dtype of the corresponding array is not as expected!')
def test_GeotransformTiff(self):
"""
Testing the functions: geotransform - xgsd - ygsd - xygrid_specs, test with 3 stages.
Stage 1: Comparing the geotransform-tupel of the geotransform-function with the expected result.
When identical, induction of stage 2...
Stage 2: Comparing the resolution from the geotransform-tupel with the expected resolution.
When identical, induction of stage 3...
Stage 3: Comparing the x/y coordinate grid given by the xygrid_specs-function with the expected result.
If an exception is raised as an error in stage 1 or 2, the test will be skipped.
"""
expected_geotransform = (365985.0, 30.0, 0.0, 5916615.0, 0.0, -30.0)
expected_resolution = (30, 30) #(expected_geotransform[1], expected_geotransform[-1])
expected_grid = [[365985.0, 366015.0], [5916615.0, 5916585.0]]
testtiff_resolutionfunctions = (self.testtiff.xgsd, self.testtiff.ygsd)
if self.testtiff.geotransform == expected_geotransform:
if testtiff_resolutionfunctions == expected_resolution:
self.assertEqual(self.testtiff.xygrid_specs, expected_grid,
msg='The [[xOrigin, xGSD], [yOrigin, yGSD]]-grid is not as expected!')
else:
print("The x/y-resolution %s of the grid of the tested Tiff-file is not as expected %s! "
"The function 'XYGRID_SPECS' will not be tested."
%(testtiff_resolutionfunctions, expected_resolution))
self.skipTest(Test_GeoarrayAppliedOnTiffPath)
else:
print("The geotransform-tuple of the array behind the 'GeoArray'-object is not as expected! "
"The test 'test_GeotransformTiff' will be skipped.")
self.skipTest(Test_GeoarrayAppliedOnTiffPath)
def test_ProjectionTiff(self):
"""
Testing the functions: projection - epsg, test with 2 stages.
Stage 1: After translating the projection-string provided by the projection-function to a pszProj4-string,
it is compared to the expected pszProj4-string. When identical, induction of stage 2...
Stage 2: Comparing the EPSG-code provided by the epsg-function with the expected EPSG-code.
If the pszProj5-string is not as expected (Stage 1), the whole test will be skipped.
"""
expected_pszProj4_string = '+proj=utm +zone=33 +datum=WGS84 +units=m +no_defs'
expected_epsg = 32633
# Convert WKT-string of the projection to a pszProj4_string
# Code adapted from source:
# mgleahy, 2010-11-21 http://spatialnotes.blogspot.de/2010/11/converting-wkt-projection-info-to-proj4.html
srs = osgeo.osr.SpatialReference()
srs.ImportFromWkt(self.testtiff.projection)
testtiff_pszProj4_string = srs.ExportToProj4()
if testtiff_pszProj4_string.strip(' /t/n/r') == expected_pszProj4_string:
self.assertEqual(self.testtiff.epsg, expected_epsg,
msg="The EPSG-code returned by the 'GeoArray' epsg-function (%s) is not "
"equal to the expected code (%s)." %(self.testtiff.epsg, expected_epsg))
else:
print("The projections of the 'GeoArray'-object is not as expected! "
"The test 'test_ProjectionTiff' will be skipped.")
self.skipTest(Test_GeoarrayAppliedOnTiffPath)
def test_NoDataValueOfTiff(self):
"""
Test, if the nodata value of the image was correctly assigned.
"""
expected_nodata = -9999.0
class TestGeoarray(unittest.TestCase):
self.assertEqual(self.testtiff.nodata, expected_nodata,
msg="The nodata-value of the tested Tiff-file (%s) is not as expected (%s)!"
%(self.testtiff.nodata, expected_nodata))
def setUp(self):
pass
def tearDown(self):
pass
def test_000_something(self):
pass
if __name__ == '__main__':
unittest.main(argv=['first-arg-is-ignored'],exit=False, verbosity=2)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment