Example #1
0
    def test_file_parse_sample_two(self):
        with self.assertLogs("openSpectra.OpenSpectraHeader",
                             level='INFO') as log:
            test_file = "test/unit_tests/resources/sample_header_2.hdr"
            os_header = OpenSpectraHeader(test_file)
            self.assertIsNotNone(os_header)
            os_header.load()
            # print(os_header.dump())
            self.assertEqual(os_header.samples(), 400)
            self.assertEqual(os_header.lines(), 350)
            self.assertEqual(os_header.band_count(), 50)
            self.assertEqual(os_header.data_type(), np.int16)

            # TODO the rest?  just what is different?

            default_stretch = os_header.default_stretch()
            self.assertTrue(isinstance(default_stretch, PercentageStretch))
            self.assertEqual(default_stretch.percentage(), 5.0)

            map_info: OpenSpectraHeader.MapInfo = os_header.map_info()
            self.assertIsNotNone(map_info)
            self.assertEqual(map_info.projection_name(), "UTM")
            self.assertEqual(map_info.x_reference_pixel(), 1.0)
            self.assertEqual(map_info.y_reference_pixel(), 1.0)
            self.assertEqual(map_info.x_zero_coordinate(), 50000.0)
            self.assertEqual(map_info.y_zero_coordinate(), 4000000.0)
            self.assertEqual(map_info.x_pixel_size(), 2.0000000000e+001)
            self.assertEqual(map_info.y_pixel_size(), 2.0000000000e+001)
            self.assertEqual(map_info.projection_zone(), 12)
            self.assertEqual(map_info.projection_area(), "North")
            self.assertEqual(map_info.datum(), "WGS-84")
            self.assertEqual(map_info.units(), "Meters")
            self.assertEqual(map_info.rotation(), math.radians(30.0))
            self.assertEqual(map_info.rotation_deg(), 30.0)
Example #2
0
    def setUp(self) -> None:
        test_file1 = "test/unit_tests/resources/sample_header_1.hdr"
        self.__header1 = OpenSpectraHeader(test_file1)
        self.__header1.load()

        test_file2 = "test/unit_tests/resources/sample_header_2.hdr"
        self.__header2 = OpenSpectraHeader(test_file2)
        self.__header2.load()
Example #3
0
 def test_save2(self):
     mutable_header = MutableOpenSpectraHeader(
         os_header=self.__source_header4)
     test_file_name = "test/unit_tests/resources/ang20160928t135411_rfl_v1nx_nonortho_copy"
     mutable_header.save(test_file_name)
     header_copy = OpenSpectraHeader(test_file_name + ".hdr")
     header_copy.load()
     self.assertHeadersMatch(self.__source_header4, header_copy)
     self.__clean_up_list.append(test_file_name + ".hdr")
Example #4
0
 def test_save1(self):
     mutable_header = MutableOpenSpectraHeader(
         os_header=self.__source_header1)
     test_file_name = "test/unit_tests/resources/sample_header_1_copy"
     mutable_header.save(test_file_name)
     header_copy = OpenSpectraHeader(test_file_name + ".hdr")
     header_copy.load()
     self.assertHeadersMatch(self.__source_header1, header_copy)
     self.__clean_up_list.append(test_file_name + ".hdr")
Example #5
0
    def test_file_parse_sample_three(self):
        with self.assertLogs("openSpectra.OpenSpectraHeader",
                             level='INFO') as log:
            test_file = "test/unit_tests/resources/ang20160928t135411_rfl_v1nx_nonortho.hdr"
            os_header = OpenSpectraHeader(test_file)
            self.assertIsNotNone(os_header)
            os_header.load()
            self.assertEqual(os_header.samples(), 598)

            # TODO, much more to test

        for message in log.output:
            self.assertFalse(message.startswith("WARNING"))
Example #6
0
class MapInfoTest(unittest.TestCase):
    def setUp(self) -> None:
        test_file1 = "test/unit_tests/resources/sample_header_1.hdr"
        self.__header1 = OpenSpectraHeader(test_file1)
        self.__header1.load()

        test_file2 = "test/unit_tests/resources/sample_header_2.hdr"
        self.__header2 = OpenSpectraHeader(test_file2)
        self.__header2.load()

    def test_calculate_single_pixels(self):
        # TODO test needs work doesn't actaully verify the calculation is correct!
        coords_int = self.__header1.map_info().calculate_coordinates(5, 5)
        # print("calculated coords: {}, {}".format(coords_int[0], coords_int[1]))

        coords_float = self.__header1.map_info().calculate_coordinates(
            5.0, 5.0)
        # print("calculated coords: {}, {}".format(coords_float[0], coords_float[1]))
        self.assertEqual(coords_int[0], coords_float[0])
        self.assertEqual(coords_int[1], coords_float[1])

        coords_int = self.__header2.map_info().calculate_coordinates(5, 5)
        # print("calculated coords: {}, {}".format(coords_int[0], coords_int[1]))

        coords_float = self.__header2.map_info().calculate_coordinates(
            5.0, 5.0)
        # print("calculated coords: {}, {}".format(coords_float[0], coords_float[1]))
        self.assertEqual(coords_int[0], coords_float[0])
        self.assertEqual(coords_int[1], coords_float[1])
Example #7
0
 def assertUnsupportedPropsMatch(self, first: OpenSpectraHeader,
                                 second: OpenSpectraHeader):
     self.assertEqual(len(first.unsupported_props()),
                      len(second.unsupported_props()))
     self.assertSetEqual(set(first.unsupported_props().keys()),
                         set(second.unsupported_props().keys()))
     for key, value in first.unsupported_props().items():
         if isinstance(value, list):
             if len(value) == first.band_count():
                 self.assertEqual(second.band_count(), len(value))
             else:
                 self.assertListEqual(
                     list(first.unsupported_props().values()),
                     list(second.unsupported_props().values()))
         else:
             self.assertEqual(first.unsupported_props().get(key), value)
Example #8
0
    def setUp(self) -> None:
        test_file1 = "test/unit_tests/resources/sample_header_1.hdr"
        self.__source_header1 = OpenSpectraHeader(test_file1)
        self.__source_header1.load()

        test_file2 = "test/unit_tests/resources/sample_header_2.hdr"
        self.__source_header2 = OpenSpectraHeader(test_file2)
        self.__source_header2.load()

        # this one has no map_info
        test_file3 = "test/unit_tests/resources/cup95_eff_fixed.hdr"
        self.__source_header3 = OpenSpectraHeader(test_file3)
        self.__source_header3.load()

        test_file4 = "test/unit_tests/resources/ang20160928t135411_rfl_v1nx_nonortho.hdr"
        self.__source_header4 = OpenSpectraHeader(test_file4)
        self.__source_header4.load()

        self.__clean_up_list = list()
    def test_iterate_with_rotation(self):
        point_checked = False
        map_info = OpenSpectraHeader.MapInfo([
            "UTM", "1.000", "1.000", "50000.000", "4000000.000",
            "2.0000000000e+001", "2.0000000000e+001", "4", "North", "WGS-84",
            "units=Meters", "rotation=30.00000000"
        ])

        roi = RegionOfInterest(
            self.__points, 1.0, 1.0, 1000, 1000,
            BandDescriptor("file_name", "band_label", "wavelength_label"),
            "test", map_info)
        for r in roi:
            # print("x: {0}, y: {1}, x_coord: {2}, y_coord: {3}".format(
            #     r.x_point(), r.y_point(), r.x_coordinate(), r.y_coordinate()))
            if r.x_point() == 348 and r.y_point() == 206:
                self.assertEqual(r.x_coordinate(), 58087.53681033969)
                self.assertEqual(r.y_coordinate(), 3999911.9753364082)
                point_checked = True

        self.assertTrue(point_checked)
    def test_save_single_band(self):
        map_info_list = [
            "UTM", "1.000", "1.000", "50000.000", "4000000.000",
            "2.0000000000e+001", "2.0000000000e+001", "4", "North", "WGS-84",
            "units=Meters"
        ]
        map_info = OpenSpectraHeader.MapInfo(map_info_list)

        test_file = "test/unit_tests/resources/cup95_eff_fixed"
        os_file = OpenSpectraFileFactory.create_open_spectra_file(test_file)
        os_header = os_file.header()
        band_tools = OpenSpectraBandTools(os_file)

        roi = RegionOfInterest(
            self.__points, 1.0, 1.0, os_header.lines(), os_header.samples(),
            BandDescriptor("file_name", "band_label", "wavelength_label"),
            "region_name", map_info)

        region_tools = OpenSpectraRegionTools(roi, band_tools)
        output = io.StringIO()
        region_tools.save_region(text_stream=output, include_bands=True)

        lines = output.getvalue().split("\n")
        self.assertEqual(lines[0], "# name:region_name")
        self.assertEqual(lines[1], "# file name:file_name")
        self.assertEqual(lines[2], "# band name:band_label")
        self.assertEqual(lines[3], "# wavelength:wavelength_label")
        self.assertEqual(lines[4], "# image width:400")
        self.assertEqual(lines[5], "# image height:350")
        self.assertEqual(lines[6], "# projection:UTM 4 North WGS-84")
        self.assertEqual(
            lines[7],
            "# description:file_name - band_label - wavelength_label")
        self.assertEqual(lines[8], "# data:")
        self.assertEqual(
            lines[9],
            "sample,line,x_coordinate,y_coordinate,Band 172-1.990800,Band 173-2.000900,"
            "Band 174-2.010900,Band 175-2.020900,Band 176-2.030900,Band 177-2.040900,"
            "Band 178-2.050900,Band 179-2.060900,Band 180-2.071000,Band 181-2.081000,"
            "Band 182-2.091000,Band 183-2.101000,Band 184-2.111000,Band 185-2.121000,"
            "Band 186-2.130900,Band 187-2.140900,Band 188-2.150900,Band 189-2.160900,"
            "Band 190-2.170900,Band 191-2.180900,Band 192-2.190800,Band 193-2.200800,"
            "Band 194-2.210800,Band 195-2.220800,Band 196-2.230700,Band 197-2.240700,"
            "Band 198-2.250600,Band 199-2.260600,Band 200-2.270600,Band 201-2.280500,"
            "Band 202-2.290400,Band 203-2.300400,Band 204-2.310400,Band 205-2.320300,"
            "Band 206-2.330200,Band 207-2.340200,Band 208-2.350100,Band 209-2.360000,"
            "Band 210-2.370000,Band 211-2.379900,Band 212-2.389800,Band 213-2.399700,"
            "Band 214-2.409600,Band 215-2.419600,Band 216-2.429500,Band 217-2.439400,"
            "Band 218-2.449300,Band 219-2.459200,Band 220-2.469100,Band 221-2.479000"
        )

        x_expected = 348
        y_expected = 206
        x_coord_expected = 56940.0
        y_coord_expected = 3995900.0
        for index in range(10, len(lines) - 1):
            line = lines[index].split(",")
            x_val = int(line[0])
            self.assertEqual(x_val, x_expected)
            y_val = int(line[1])
            self.assertEqual(y_val, y_expected)
            self.assertEqual(float(line[2]), x_coord_expected)
            self.assertEqual(float(line[3]), y_coord_expected)
            band_values = np.array([int(item) for item in line[4:len(line)]])
            expected_band_values = os_file.bands(y_val, x_val)
            np.array_equal(expected_band_values, band_values)

            if y_expected == 208:
                y_expected = 206
                x_expected += 1
            else:
                y_expected += 1

            if y_coord_expected == 3995860.0:
                y_coord_expected = 3995900.0
                x_coord_expected += 20.0
            else:
                y_coord_expected -= 20.0

        output.close()
    def test_rotation(self):
        map_info_list = [
            "UTM", "1.000", "1.000", "50000.000", "4000000.000",
            "2.0000000000e+001", "2.0000000000e+001", "4", "North", "WGS-84",
            "units=Meters", "rotation=30.00000000"
        ]
        map_info = OpenSpectraHeader.MapInfo(map_info_list)

        test_file = "test/unit_tests/resources/cup95_eff_fixed"
        os_file = OpenSpectraFileFactory.create_open_spectra_file(test_file)
        os_header = os_file.header()
        band_tools = OpenSpectraBandTools(os_file)

        roi = RegionOfInterest(
            self.__points, 1.0, 1.0, os_header.lines(), os_header.samples(),
            BandDescriptor("file_name", "band_label", "wavelength_label"),
            "region_name", map_info)

        region_tools = OpenSpectraRegionTools(roi, band_tools)
        output = io.StringIO()
        region_tools.save_region(text_stream=output, include_bands=True)

        lines = output.getvalue().split("\n")
        self.assertEqual(lines[0], "# name:region_name")
        self.assertEqual(lines[1], "# file name:file_name")
        self.assertEqual(lines[2], "# band name:band_label")
        self.assertEqual(lines[3], "# wavelength:wavelength_label")
        self.assertEqual(lines[4], "# image width:400")
        self.assertEqual(lines[5], "# image height:350")
        self.assertEqual(lines[6], "# projection:UTM 4 North WGS-84")
        self.assertEqual(
            lines[7],
            "# description:file_name - band_label - wavelength_label")
        self.assertEqual(lines[8], "# data:")
        self.assertEqual(
            lines[9],
            "sample,line,x_coordinate,y_coordinate,Band 172-1.990800,Band 173-2.000900,"
            "Band 174-2.010900,Band 175-2.020900,Band 176-2.030900,Band 177-2.040900,"
            "Band 178-2.050900,Band 179-2.060900,Band 180-2.071000,Band 181-2.081000,"
            "Band 182-2.091000,Band 183-2.101000,Band 184-2.111000,Band 185-2.121000,"
            "Band 186-2.130900,Band 187-2.140900,Band 188-2.150900,Band 189-2.160900,"
            "Band 190-2.170900,Band 191-2.180900,Band 192-2.190800,Band 193-2.200800,"
            "Band 194-2.210800,Band 195-2.220800,Band 196-2.230700,Band 197-2.240700,"
            "Band 198-2.250600,Band 199-2.260600,Band 200-2.270600,Band 201-2.280500,"
            "Band 202-2.290400,Band 203-2.300400,Band 204-2.310400,Band 205-2.320300,"
            "Band 206-2.330200,Band 207-2.340200,Band 208-2.350100,Band 209-2.360000,"
            "Band 210-2.370000,Band 211-2.379900,Band 212-2.389800,Band 213-2.399700,"
            "Band 214-2.409600,Band 215-2.419600,Band 216-2.429500,Band 217-2.439400,"
            "Band 218-2.449300,Band 219-2.459200,Band 220-2.469100,Band 221-2.479000"
        )

        self.assertEqual(
            lines[10],
            '348,206,58060.216302264,3999919.2958444837,305,307,318,323,324,324,322,321,318,316,319,315,314,308,305,302,301,295,292,291,293,294,291,290,290,287,289,285,280,279,281,274,269,272,272,274,270,273,267,266,250,251,241,236,228,221,203,195,170,161'
        )
        self.assertEqual(
            lines[11],
            '348,207,58070.216302264,3999901.9753364082,313,318,318,325,332,332,334,332,333,334,331,333,328,328,322,317,314,310,308,305,306,298,300,300,298,299,296,291,293,291,291,288,283,286,287,286,281,276,274,274,265,250,246,242,233,228,203,204,172,148'
        )
        self.assertEqual(
            lines[12],
            '348,208,58080.216302264,3999884.6548283324,310,311,306,321,325,330,325,329,327,329,328,328,320,318,315,312,309,308,303,302,302,300,300,294,294,295,294,289,290,286,286,284,281,279,281,285,276,270,274,273,261,253,248,241,229,218,205,194,182,162'
        )
        self.assertEqual(
            lines[13],
            '349,206,58077.53681033969,3999929.2958444837,332,336,343,346,352,358,360,358,354,359,357,352,353,348,346,336,334,330,331,326,328,324,320,319,319,320,317,317,315,313,311,310,309,304,305,302,303,296,299,290,278,281,265,254,250,240,229,204,198,163'
        )
        self.assertEqual(
            lines[14],
            '349,207,58087.53681033969,3999911.9753364082,336,338,342,354,360,364,360,363,362,361,364,359,358,350,350,345,338,336,335,331,330,333,325,324,322,323,323,324,321,321,320,316,317,311,310,311,309,312,305,298,290,289,279,265,259,240,231,225,205,182'
        )
        self.assertEqual(
            lines[15],
            '349,208,58097.53681033969,3999894.6548283324,327,330,345,348,349,353,355,355,357,355,354,353,350,345,344,336,333,330,329,326,325,325,320,319,318,317,315,316,315,314,311,310,308,303,305,307,302,305,297,290,285,278,264,264,253,242,229,219,198,169'
        )
        self.assertEqual(
            lines[16],
            '350,206,58094.85731841538,3999939.2958444837,333,339,342,342,353,356,354,352,354,356,353,356,347,344,341,336,331,327,323,320,320,321,316,319,317,314,316,311,313,308,305,306,299,297,297,302,306,303,292,287,278,271,270,257,252,232,221,218,193,172'
        )
        self.assertEqual(
            lines[17],
            '350,207,58104.85731841538,3999921.9753364082,325,325,331,337,343,351,351,347,349,350,351,350,347,343,337,331,331,326,319,321,317,319,314,313,312,312,312,310,308,305,302,303,299,295,301,299,299,291,287,282,274,265,263,253,244,243,215,207,197,172'
        )
        self.assertEqual(
            lines[18],
            '350,208,58114.85731841538,3999904.6548283324,306,303,295,312,323,332,328,328,329,333,332,332,331,328,321,319,316,311,307,305,305,305,300,298,297,298,297,294,293,288,289,287,288,282,286,283,280,278,276,269,266,259,250,234,233,229,203,200,177,136'
        )

        output.close()
Example #12
0
    def assertHeadersMatch(self, first: OpenSpectraHeader,
                           second: OpenSpectraHeader):
        self.assertEqual(first.description(), second.description())
        self.assertEqual(first.samples(), second.samples())
        self.assertEqual(first.lines(), second.lines())
        self.assertEqual(first.band_count(), second.band_count())
        self.assertEqual(first.header_offset(), second.header_offset())
        self.assertEqual(first.file_type(), second.file_type())
        self.assertEqual(first.data_type(), second.data_type())
        self.assertEqual(first.interleave(), second.interleave())
        self.assertEqual(first.sensor_type(), second.sensor_type())
        self.assertEqual(first.byte_order(), second.byte_order())
        self.assertEqual(first.wavelength_units(), second.wavelength_units())
        self.assertEqual(first.reflectance_scale_factor(),
                         second.reflectance_scale_factor())
        self.assertMapInfoEqual(first.map_info(), second.map_info())
        self.assertEqual(first.coordinate_system_string(),
                         second.coordinate_system_string())
        self.assertStretchEqual(first.default_stretch(),
                                second.default_stretch())

        if first.band_names() is None:
            self.assertIsNone(second.band_names())
        else:
            self.assertListEqual(first.band_names(), second.band_names())
        self.assertTrue(
            np.array_equal(first.wavelengths(), second.wavelengths()))

        if first.bad_band_list() is None:
            self.assertIsNone(second.bad_band_list())
        else:
            self.assertListEqual(first.bad_band_list(), second.bad_band_list())

        self.assertUnsupportedPropsMatch(first, second)
Example #13
0
class MutableOpenSpectraHeaderTest(unittest.TestCase):
    def setUp(self) -> None:
        test_file1 = "test/unit_tests/resources/sample_header_1.hdr"
        self.__source_header1 = OpenSpectraHeader(test_file1)
        self.__source_header1.load()

        test_file2 = "test/unit_tests/resources/sample_header_2.hdr"
        self.__source_header2 = OpenSpectraHeader(test_file2)
        self.__source_header2.load()

        # this one has no map_info
        test_file3 = "test/unit_tests/resources/cup95_eff_fixed.hdr"
        self.__source_header3 = OpenSpectraHeader(test_file3)
        self.__source_header3.load()

        test_file4 = "test/unit_tests/resources/ang20160928t135411_rfl_v1nx_nonortho.hdr"
        self.__source_header4 = OpenSpectraHeader(test_file4)
        self.__source_header4.load()

        self.__clean_up_list = list()

    def tearDown(self) -> None:
        for file_name in self.__clean_up_list:
            if os.path.isfile(file_name):
                os.remove(file_name)

    def test_deep_copy(self):
        orig_byte_order = self.__source_header1.byte_order()
        orig_lines = self.__source_header1.lines()
        orig_samples = self.__source_header1.samples()
        orig_interleave = self.__source_header1.interleave()
        orig_band_count = self.__source_header1.band_count()
        orig_band_names = self.__source_header1.band_names()
        orig_wavelengths = self.__source_header1.wavelengths()
        orig_bad_bands = self.__source_header1.bad_band_list()
        orig_map_info = self.__source_header1.map_info()

        mutable_header = MutableOpenSpectraHeader(
            os_header=self.__source_header1)
        self.assertEqual(orig_byte_order, mutable_header.byte_order())
        self.assertEqual(orig_lines, mutable_header.lines())
        self.assertEqual(orig_samples, mutable_header.samples())
        self.assertEqual(orig_interleave, mutable_header.interleave())
        self.assertEqual(orig_band_count, mutable_header.band_count())
        self.assertListEqual(orig_band_names, mutable_header.band_names())
        self.assertTrue(
            np.array_equal(orig_wavelengths, mutable_header.wavelengths()))
        self.assertListEqual(orig_bad_bands, mutable_header.bad_band_list())
        self.assertMapInfoEqual(orig_map_info, mutable_header.map_info())

        new_lines = 100
        self.assertNotEqual(orig_lines, new_lines)
        mutable_header.set_lines(new_lines)
        self.assertEqual(new_lines, mutable_header.lines())
        self.assertEqual(orig_lines, self.__source_header1.lines())

        new_samples = 100
        self.assertNotEqual(orig_samples, new_samples)
        mutable_header.set_samples(new_samples)
        self.assertEqual(new_samples, mutable_header.samples())
        self.assertEqual(orig_samples, self.__source_header1.samples())

        new_interleave = OpenSpectraHeader.BSQ_INTERLEAVE
        self.assertNotEqual(orig_interleave, new_interleave)
        mutable_header.set_interleave(new_interleave)
        self.assertEqual(new_interleave, mutable_header.interleave())
        self.assertEqual(orig_interleave, self.__source_header1.interleave())

        self.assertEqual(50, orig_band_count)
        band_slice = slice(5, 32)
        new_band_names = orig_band_names[band_slice]
        new_band_count = len(new_band_names)
        new_wavelengths = orig_wavelengths[band_slice]
        new_bad_bands = orig_bad_bands[band_slice]

        self.assertEqual(27, new_band_count)
        self.assertEqual(new_band_count, len(new_band_names))
        self.assertEqual(new_band_count, len(new_wavelengths))
        self.assertEqual(new_band_count, len(new_bad_bands))

        mutable_header.set_bands(new_band_count, new_band_names,
                                 new_wavelengths, new_bad_bands)
        self.assertListEqual(new_band_names, mutable_header.band_names())
        self.assertListEqual(orig_band_names,
                             self.__source_header1.band_names())

        self.assertTrue(
            np.array_equal(new_wavelengths, mutable_header.wavelengths()))
        self.assertTrue(
            np.array_equal(orig_wavelengths,
                           self.__source_header1.wavelengths()))

        self.assertListEqual(new_bad_bands, mutable_header.bad_band_list())
        self.assertListEqual(orig_bad_bands,
                             self.__source_header1.bad_band_list())

        self.assertMapInfoEqual(orig_map_info, mutable_header.map_info())

    def test_no_map_info(self):
        self.assertIsNone(self.__source_header3.map_info())
        mutable_header = MutableOpenSpectraHeader(
            os_header=self.__source_header3)
        self.assertIsNone(mutable_header.map_info())

    def test_map_info(self):
        orig_map_info = self.__source_header2.map_info()
        self.assertEqual(1.0, orig_map_info.x_reference_pixel())
        self.assertEqual(50000.0, orig_map_info.x_zero_coordinate())
        self.assertEqual(1.0, orig_map_info.y_reference_pixel())
        self.assertEqual(4000000.0, orig_map_info.y_zero_coordinate())

        mutable_header = MutableOpenSpectraHeader(
            os_header=self.__source_header2)
        self.assertMapInfoEqual(orig_map_info, mutable_header.map_info())

        self.assertNotEqual(3.0, orig_map_info.x_reference_pixel())
        self.assertNotEqual(55000.0, orig_map_info.x_zero_coordinate())
        mutable_header.set_x_reference(3.0, 55000.0)
        self.assertMapInfoOtherEqual(orig_map_info, mutable_header.map_info())
        self.assertEqual(3.0, mutable_header.map_info().x_reference_pixel())
        self.assertEqual(55000.0,
                         mutable_header.map_info().x_zero_coordinate())
        self.assertEqual(orig_map_info.y_reference_pixel(),
                         mutable_header.map_info().y_reference_pixel())
        self.assertEqual(orig_map_info.y_zero_coordinate(),
                         mutable_header.map_info().y_zero_coordinate())

        self.assertNotEqual(4.0, orig_map_info.y_reference_pixel())
        self.assertNotEqual(4007000.0, orig_map_info.y_zero_coordinate())
        mutable_header.set_y_reference(4.0, 4007000.0)
        self.assertMapInfoOtherEqual(orig_map_info, mutable_header.map_info())
        self.assertEqual(3.0, mutable_header.map_info().x_reference_pixel())
        self.assertEqual(55000.0,
                         mutable_header.map_info().x_zero_coordinate())
        self.assertEqual(4.0, mutable_header.map_info().y_reference_pixel())
        self.assertEqual(4007000.0,
                         mutable_header.map_info().y_zero_coordinate())

        self.assertEqual(1.0, orig_map_info.x_reference_pixel())
        self.assertEqual(50000.0, orig_map_info.x_zero_coordinate())
        self.assertEqual(1.0, orig_map_info.y_reference_pixel())
        self.assertEqual(4000000.0, orig_map_info.y_zero_coordinate())

    def test_load(self):
        # Just making sure MutableOpenSpectraHeader.load() doesn't blow up
        mutable_header = MutableOpenSpectraHeader(
            os_header=self.__source_header1)
        mutable_header.load()

    def test_save1(self):
        mutable_header = MutableOpenSpectraHeader(
            os_header=self.__source_header1)
        test_file_name = "test/unit_tests/resources/sample_header_1_copy"
        mutable_header.save(test_file_name)
        header_copy = OpenSpectraHeader(test_file_name + ".hdr")
        header_copy.load()
        self.assertHeadersMatch(self.__source_header1, header_copy)
        self.__clean_up_list.append(test_file_name + ".hdr")

    def test_save2(self):
        mutable_header = MutableOpenSpectraHeader(
            os_header=self.__source_header4)
        test_file_name = "test/unit_tests/resources/ang20160928t135411_rfl_v1nx_nonortho_copy"
        mutable_header.save(test_file_name)
        header_copy = OpenSpectraHeader(test_file_name + ".hdr")
        header_copy.load()
        self.assertHeadersMatch(self.__source_header4, header_copy)
        self.__clean_up_list.append(test_file_name + ".hdr")

    def assertHeadersMatch(self, first: OpenSpectraHeader,
                           second: OpenSpectraHeader):
        self.assertEqual(first.description(), second.description())
        self.assertEqual(first.samples(), second.samples())
        self.assertEqual(first.lines(), second.lines())
        self.assertEqual(first.band_count(), second.band_count())
        self.assertEqual(first.header_offset(), second.header_offset())
        self.assertEqual(first.file_type(), second.file_type())
        self.assertEqual(first.data_type(), second.data_type())
        self.assertEqual(first.interleave(), second.interleave())
        self.assertEqual(first.sensor_type(), second.sensor_type())
        self.assertEqual(first.byte_order(), second.byte_order())
        self.assertEqual(first.wavelength_units(), second.wavelength_units())
        self.assertEqual(first.reflectance_scale_factor(),
                         second.reflectance_scale_factor())
        self.assertMapInfoEqual(first.map_info(), second.map_info())
        self.assertEqual(first.coordinate_system_string(),
                         second.coordinate_system_string())
        self.assertStretchEqual(first.default_stretch(),
                                second.default_stretch())

        if first.band_names() is None:
            self.assertIsNone(second.band_names())
        else:
            self.assertListEqual(first.band_names(), second.band_names())
        self.assertTrue(
            np.array_equal(first.wavelengths(), second.wavelengths()))

        if first.bad_band_list() is None:
            self.assertIsNone(second.bad_band_list())
        else:
            self.assertListEqual(first.bad_band_list(), second.bad_band_list())

        self.assertUnsupportedPropsMatch(first, second)

    def assertUnsupportedPropsMatch(self, first: OpenSpectraHeader,
                                    second: OpenSpectraHeader):
        self.assertEqual(len(first.unsupported_props()),
                         len(second.unsupported_props()))
        self.assertSetEqual(set(first.unsupported_props().keys()),
                            set(second.unsupported_props().keys()))
        for key, value in first.unsupported_props().items():
            if isinstance(value, list):
                if len(value) == first.band_count():
                    self.assertEqual(second.band_count(), len(value))
                else:
                    self.assertListEqual(
                        list(first.unsupported_props().values()),
                        list(second.unsupported_props().values()))
            else:
                self.assertEqual(first.unsupported_props().get(key), value)

    def assertStretchEqual(self, first: LinearImageStretch,
                           second: LinearImageStretch):
        if first is None:
            self.assertIsNone(second)
        elif isinstance(first, PercentageStretch) and isinstance(
                second, PercentageStretch):
            self.assertEqual(first.percentage(), second.percentage())
            with self.assertRaises(NotImplementedError):
                first.low()
            with self.assertRaises(NotImplementedError):
                first.high()
        elif isinstance(first, ValueStretch) and isinstance(
                second, ValueStretch):
            self.assertEqual(first.low(), second.low())
            self.assertEqual(first.high(), second.high())
            with self.assertRaises(NotImplementedError):
                first.percentage()
        else:
            self.fail(
                "assertStretchEqual expects both values to have the same type")

    def assertMapInfoEqual(self, first: OpenSpectraHeader.MapInfo,
                           second: OpenSpectraHeader.MapInfo):
        if first is None:
            self.assertIsNone(second)
        else:
            self.assertEqual(first.projection_name(), second.projection_name())
            self.assertEqual(first.x_reference_pixel(),
                             second.x_reference_pixel())
            self.assertEqual(first.y_reference_pixel(),
                             second.y_reference_pixel())
            self.assertEqual(first.x_zero_coordinate(),
                             second.x_zero_coordinate())
            self.assertEqual(first.y_zero_coordinate(),
                             second.y_zero_coordinate())
            self.assertEqual(first.x_pixel_size(), second.x_pixel_size())
            self.assertEqual(first.projection_zone(), second.projection_zone())
            self.assertEqual(first.projection_area(), second.projection_area())
            self.assertEqual(first.datum(), second.datum())
            self.assertEqual(first.units(), second.units())
            self.assertEqual(first.rotation(), second.rotation())
            self.assertEqual(first.rotation_deg(), second.rotation_deg())

    def assertMapInfoOtherEqual(self, first: OpenSpectraHeader.MapInfo,
                                second: OpenSpectraHeader.MapInfo):
        if first is None:
            self.assertIsNone(second)
        else:
            self.assertEqual(first.projection_name(), second.projection_name())
            self.assertEqual(first.y_pixel_size(), second.y_pixel_size())
            self.assertEqual(first.projection_zone(), second.projection_zone())
            self.assertEqual(first.projection_area(), second.projection_area())
            self.assertEqual(first.datum(), second.datum())
            self.assertEqual(first.units(), second.units())
            self.assertEqual(first.rotation(), second.rotation())
            self.assertEqual(first.rotation_deg(), second.rotation_deg())
Example #14
0
 def test_map_info_string(self):
     test_file = "test/unit_tests/resources/sample_header_2.hdr"
     header = OpenSpectraHeader(test_file)
     header.load()
     expected_str = "{UTM, 1.000, 1.000, 50000.000, 4000000.000, 2.0000000000e+001, 2.0000000000e+001, 12, North, WGS-84, units=Meters, rotation=30.00000000}"
     self.assertEqual(expected_str, str(header.map_info()))
Example #15
0
    def test_file_parse_sample_one(self):
        expected_wavelengths:List[float] = \
            [1.990800, 2.000900, 2.010900, 2.020900, 2.030900, 2.040900, 2.050900,
                2.060900, 2.071000, 2.081000, 2.091000, 2.101000, 2.111000, 2.121000,
                2.130900, 2.140900, 2.150900, 2.160900, 2.170900, 2.180900, 2.190800,
                2.200800, 2.210800, 2.220800, 2.230700, 2.240700, 2.250600, 2.260600,
                2.270600, 2.280500, 2.290400, 2.300400, 2.310400, 2.320300, 2.330200,
                2.340200, 2.350100, 2.360000, 2.370000, 2.379900, 2.389800, 2.399700,
                2.409600, 2.419600, 2.429500, 2.439400, 2.449300, 2.459200, 2.469100,
                2.479000]

        expected_bbl = [
            False, False, False, False, False, False, False, False, False,
            False, False, False, False, False, False, False, False, False,
            False, False, False, False, False, False, False, False, False,
            False, False, False, False, False, False, False, False, False,
            False, False, True, True, True, True, True, False, False, False,
            False, False, False, False
        ]

        with self.assertLogs("openSpectra.OpenSpectraHeader",
                             level='INFO') as log:
            test_file = "test/unit_tests/resources/sample_header_1.hdr"
            os_header = OpenSpectraHeader(test_file)
            self.assertIsNotNone(os_header)
            os_header.load()
            self.assertEqual(os_header.byte_order(), 0)
            self.assertEqual(os_header.samples(), 400)
            self.assertEqual(os_header.lines(), 350)
            band_count: int = os_header.band_count()
            self.assertEqual(band_count, 50)
            self.assertEqual(os_header.data_type(), np.int16)
            self.assertEqual(os_header.interleave(),
                             OpenSpectraHeader.BIL_INTERLEAVE)
            self.assertEqual(os_header.header_offset(), 0)
            self.assertEqual(os_header.wavelength_units(), "Micrometers")
            self.assertEqual(os_header.sensor_type(), "Unknown")
            self.assertEqual(os_header.file_type(), "ENVI Standard")
            self.assertEqual(
                os_header.description(),
                "\n  1995 AVIRIS \"Effort\" Corrected ATREM [Thu Apr 25 00:52:03 1996] [Thu Mar\n  2912:49:46 2012]"
            )
            self.assertEqual(os_header.data_ignore_value(), -9999)
            self.assertEqual(
                os_header.coordinate_system_string(),
                "PROJCS[\"UTM_Zone_12N\",GEOGCS[\"GCS_WGS_1984\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137.0,298.257223563]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Degree\",0.0174532925199433]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"False_Easting\",500000.0],PARAMETER[\"False_Northing\",0.0],PARAMETER[\"Central_Meridian\",-111.0],PARAMETER[\"Scale_Factor\",0.9996],PARAMETER[\"Latitude_Of_Origin\",0.0],UNIT[\"Meter\",1.0]]"
            )
            default_stretch: LinearImageStretch = os_header.default_stretch()
            self.assertTrue(isinstance(default_stretch, ValueStretch))
            self.assertEqual(default_stretch.low(), 0.0)
            self.assertEqual(default_stretch.high(), 1000.0)

            bbl = os_header.bad_band_list()
            self.assertEqual(len(bbl), band_count)
            self.assertEqual(bbl, expected_bbl)

            band_names: list = os_header.band_names()
            self.assertEqual(len(band_names), band_count)

            band_labels: List[Tuple[str, str]] = os_header.band_labels()

            index = 0
            band_num = 172
            for band_name in band_names:
                expected_name = "Band " + str(band_num)
                self.assertEqual(band_name, expected_name)
                self.assertEqual(os_header.band_name(index), expected_name)
                self.assertEqual(band_labels[index][0], expected_name)
                band_num += 1
                index += 1
            self.assertEqual(band_num, 222)

            wavelengths = os_header.wavelengths()
            self.assertEqual(wavelengths.size, band_count)
            for index in range(0, band_count):
                self.assertEqual(wavelengths[index],
                                 expected_wavelengths[index])
                self.assertEqual(
                    band_labels[index][1],
                    "{0:1.6f}".format(expected_wavelengths[index]))

            map_info: OpenSpectraHeader.MapInfo = os_header.map_info()
            self.assertIsNotNone(map_info)
            self.assertEqual(map_info.projection_name(), "UTM")
            self.assertEqual(map_info.x_reference_pixel(), 1.0)
            self.assertEqual(map_info.y_reference_pixel(), 1.0)
            self.assertEqual(map_info.x_zero_coordinate(), 50000.0)
            self.assertEqual(map_info.y_zero_coordinate(), 4000000.0)
            self.assertEqual(map_info.x_pixel_size(), 2.0000000000e+001)
            self.assertEqual(map_info.y_pixel_size(), 2.0000000000e+001)
            self.assertEqual(map_info.projection_zone(), 12)
            self.assertEqual(map_info.projection_area(), "North")
            self.assertEqual(map_info.datum(), "WGS-84")
            self.assertEqual(map_info.units(), "Meters")
            self.assertIsNone(map_info.rotation())
            self.assertIsNone(map_info.rotation_deg())