test_metadata.py 2.77 KB
Newer Older
1 2 3
#!/usr/bin/env python
# -*- coding: utf-8 -*-

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# geoarray, A fast Python interface for image geodata - either on disk or in memory.
#
# Copyright (C) 2019  Daniel Scheffler (GFZ Potsdam, daniel.scheffler@gfz-potsdam.de)
#
# This software was developed within the context of the GeoMultiSens project funded
# by the German Federal Ministry of Education and Research
# (project grant code: 01 IS 14 010 A-C).
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.

25
from unittest import TestCase
26 27 28
import os
import numpy as np
import tempfile
29 30

from geoarray.metadata import GDAL_Metadata
31
from geoarray import GeoArray
32 33 34 35
import geoarray


tests_path = os.path.abspath(os.path.join(geoarray.__path__[0], "..", "tests"))
36 37 38


class Test_GDAL_Metadata(TestCase):
39 40 41 42 43 44 45 46
    @classmethod
    def setUp(cls):
        cls.test_filePath = os.path.join(tests_path, 'data', 'subset_metadata.bsq')
        cls.tmpOutdir = tempfile.TemporaryDirectory()

    @classmethod
    def tearDownClass(cls):
        cls.tmpOutdir.cleanup()
47 48

    def test_init(self):
49 50 51 52
        meta = GDAL_Metadata(self.test_filePath)
        self.assertIsInstance(meta, GDAL_Metadata)

    def test_save(self):
53 54
        outPath = os.path.join(self.tmpOutdir.name, 'save_bandnames_from_file.bsq')

55 56
        gA = GeoArray(self.test_filePath)
        gA.to_mem()
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
        gA.bandnames = ['test_%s' % i for i in range(1, gA.bands + 1)]
        gA.save(outPath)

        with open(os.path.splitext(outPath)[0] + '.hdr', 'r') as inF:
            content = inF.read()

        for bN in gA.bandnames.keys():
            self.assertTrue(bN in content, msg="The band name '%s' is not in the written header." % bN)

    def test_save_bandnames(self):
        outPath = os.path.join(self.tmpOutdir.name, 'save_bandnames_from_numpy.bsq')

        gA = GeoArray(np.random.randint(1, 10, (5, 5, 3)))
        gA.bandnames = ['test1', 'band_2', 'layer 3']
        gA.save(outPath)

        with open(os.path.splitext(outPath)[0] + '.hdr', 'r') as inF:
            content = inF.read()

        for bN in gA.bandnames.keys():
            self.assertTrue(bN in content, msg="The band name '%s' is not in the written header. "
                                               "Header contains:  \n\n%s" % (bN, content))