def test_roundtrip_sigset_v1_one_label(self): """Test writing and reading back sigset file (v1) with a single label and fully qualified sigfile name""" # Create signature struct So = SigSet() I_InitSigSet(ctypes.byref(So), 1) self.assertEqual(So.nbands, 1) I_NewClassSig(ctypes.byref(So)) self.assertEqual(So.nclasses, 1) I_NewSubSig(ctypes.byref(So), ctypes.byref(So.ClassSig[0])) self.assertEqual(So.ClassSig[0].nsubclasses, 1) # Fill sigset struct with data So.title = ReturnString("Signature title") So.semantic_labels[0] = ctypes.create_string_buffer(b"The_Doors") So.ClassSig[0].used = 1 So.ClassSig[0].classnum = 2 So.ClassSig[0].title = ReturnString("1st class") So.ClassSig[0].type = 1 So.ClassSig[0].SubSig[0].pi = 3.14 So.ClassSig[0].SubSig[0].means[0] = 42.42 So.ClassSig[0].SubSig[0].R[0][0] = 69.69 # Write signatures to file p_new_sigfile = I_fopen_sigset_file_new(self.sig_name) sig_stat = os.stat(f"{self.sig_dir}/sig") self.assertTrue(stat.S_ISREG(sig_stat.st_mode)) I_WriteSigSet(p_new_sigfile, ctypes.byref(So)) self.libc.fclose(p_new_sigfile) # Read back from signatures file Sn = SigSet() fq_name = f"{self.sig_name}@{self.mapset_name}" p_old_sigfile = I_fopen_sigset_file_old(fq_name) ret = I_ReadSigSet(p_old_sigfile, ctypes.byref(Sn)) self.assertEqual(ret, 1) self.assertEqual(utils.decode(Sn.title), "Signature title") self.assertEqual(Sn.nbands, 1) semantic_label = utils.decode( ctypes.cast(Sn.semantic_labels[0], ctypes.c_char_p).value ) self.assertEqual(semantic_label, "The_Doors") self.assertEqual(Sn.nclasses, 1) self.assertEqual(Sn.ClassSig[0].nsubclasses, 1) self.assertEqual(Sn.ClassSig[0].used, 1) self.assertEqual(Sn.ClassSig[0].nsubclasses, 1) self.assertEqual(Sn.ClassSig[0].classnum, 2) self.assertEqual(utils.decode(Sn.ClassSig[0].title), "1st class") self.assertEqual(Sn.ClassSig[0].type, 1) self.assertEqual(Sn.ClassSig[0].SubSig[0].pi, 3.14) self.assertEqual(Sn.ClassSig[0].SubSig[0].means[0], 42.42) self.assertEqual(Sn.ClassSig[0].SubSig[0].R[0][0], 69.69)
def test_missing_label(self): # Prepare imagery group reference struct R = Ref() I_init_group_ref(ctypes.byref(R)) ret = I_add_file_to_group_ref(self.map1, self.mapset, ctypes.byref(R)) self.assertEqual(ret, 0) ret = I_add_file_to_group_ref(self.map2, self.mapset, ctypes.byref(R)) self.assertEqual(ret, 1) ret = I_add_file_to_group_ref(self.map3, self.mapset, ctypes.byref(R)) self.assertEqual(ret, 2) # Prepare signature struct S = SigSet() I_InitSigSet(ctypes.byref(S), 10) self.assertEqual(S.nbands, 10) I_NewClassSig(ctypes.byref(S)) self.assertEqual(S.nclasses, 1) I_NewSubSig(ctypes.byref(S), ctypes.byref(S.ClassSig[0])) self.assertEqual(S.ClassSig[0].nsubclasses, 1) S.title = ReturnString("Signature title") S.semantic_labels[0] = ctypes.create_string_buffer(b"The_Who") S.ClassSig[0].used = 1 S.ClassSig[0].classnum = 2 S.ClassSig[0].title = ReturnString("1st class") S.ClassSig[0].type = 1 S.ClassSig[0].SubSig[0].pi = 3.14 S.ClassSig[0].SubSig[0].means[0] = 42.42 S.ClassSig[0].SubSig[0].R[0][0] = 69.69 # This should result in two error strings in ret ret = I_SortSigSetBySemanticLabel(ctypes.byref(S), ctypes.byref(R)) self.assertTrue(bool(ret)) sig_err = utils.decode(ctypes.cast(ret[0], ctypes.c_char_p).value) ref_err = utils.decode(ctypes.cast(ret[1], ctypes.c_char_p).value) self.assertEqual( sig_err, "<semantic label missing>,<semantic label missing>," + "<semantic label missing>,<semantic label missing>," + "<semantic label missing>,<semantic label missing>," + "<semantic label missing>,<semantic label missing>," + "<semantic label missing>", ) self.assertEqual(ref_err, f"The_Doors,{self.map3}") # Clean up memory to help track memory leaks when run by valgrind I_free_group_ref(ctypes.byref(R)) if ret: if ret[0]: self.libc.free(ret[0]) if ret[1]: self.libc.free(ret[1]) self.libc.free(ret)
def test_single_complete_match(self): # Prepare imagery group reference struct R = Ref() I_init_group_ref(ctypes.byref(R)) ret = I_add_file_to_group_ref(self.map1, self.mapset, ctypes.byref(R)) self.assertEqual(ret, 0) # Prepare signature struct S = SigSet() I_InitSigSet(ctypes.byref(S), 1) self.assertEqual(S.nbands, 1) I_NewClassSig(ctypes.byref(S)) self.assertEqual(S.nclasses, 1) I_NewSubSig(ctypes.byref(S), ctypes.byref(S.ClassSig[0])) self.assertEqual(S.ClassSig[0].nsubclasses, 1) S.title = ReturnString("Signature title") S.semantic_labels[0] = ctypes.create_string_buffer(b"The_Doors") S.ClassSig[0].used = 1 S.ClassSig[0].classnum = 2 S.ClassSig[0].title = ReturnString("1st class") S.ClassSig[0].type = 1 S.ClassSig[0].SubSig[0].pi = 3.14 S.ClassSig[0].SubSig[0].means[0] = 42.42 S.ClassSig[0].SubSig[0].R[0][0] = 69.69 # This should result in returning NULL ret = I_SortSigSetBySemanticLabel(ctypes.byref(S), ctypes.byref(R)) self.assertFalse(bool(ret)) semantic_label = utils.decode( ctypes.cast(S.semantic_labels[0], ctypes.c_char_p).value ) self.assertEqual(semantic_label, "The_Doors") self.assertEqual(S.ClassSig[0].SubSig[0].pi, 3.14) self.assertEqual(S.ClassSig[0].SubSig[0].means[0], 42.42) self.assertEqual(S.ClassSig[0].SubSig[0].R[0][0], 69.69) # Clean up memory to help track memory leaks when run by valgrind I_free_group_ref(ctypes.byref(R)) if ret: if ret[0]: self.libc.free(ret[0]) if ret[1]: self.libc.free(ret[1]) self.libc.free(ret)
def test_read_fail_sigset_v1_one_label(self): """Reading back should fail as semantic label length exceeds limit""" # Create signature struct So = SigSet() I_InitSigSet(ctypes.byref(So), 1) self.assertEqual(So.nbands, 1) I_NewClassSig(ctypes.byref(So)) self.assertEqual(So.nclasses, 1) I_NewSubSig(ctypes.byref(So), ctypes.byref(So.ClassSig[0])) self.assertEqual(So.ClassSig[0].nsubclasses, 1) # Fill sigset struct with data So.title = ReturnString("Signature title") So.semantic_labels[0] = ctypes.create_string_buffer(tempname(252).encode()) So.ClassSig[0].used = 1 So.ClassSig[0].classnum = 2 So.ClassSig[0].title = ReturnString("1st class") So.ClassSig[0].type = 1 So.ClassSig[0].SubSig[0].pi = 3.14 So.ClassSig[0].SubSig[0].means[0] = 42.42 So.ClassSig[0].SubSig[0].R[0][0] = 69.69 # Write signatures to file p_new_sigfile = I_fopen_sigset_file_new(self.sig_name) sig_stat = os.stat(f"{self.sig_dir}/sig") self.assertTrue(stat.S_ISREG(sig_stat.st_mode)) I_WriteSigSet(p_new_sigfile, ctypes.byref(So)) self.libc.fclose(p_new_sigfile) # Read back from signatures file Sn = SigSet() I_InitSigSet(ctypes.byref(So), 0) p_old_sigfile = I_fopen_sigset_file_old(self.sig_name) ret = I_ReadSigSet(p_old_sigfile, ctypes.byref(Sn)) self.assertEqual(ret, -1)
def test_double_complete_match_same_order(self): # Prepare imagery group reference struct R = Ref() I_init_group_ref(ctypes.byref(R)) ret = I_add_file_to_group_ref(self.map2, self.mapset, ctypes.byref(R)) self.assertEqual(ret, 0) ret = I_add_file_to_group_ref(self.map1, self.mapset, ctypes.byref(R)) self.assertEqual(ret, 1) # Prepare signature struct S = SigSet() I_InitSigSet(ctypes.byref(S), 2) self.assertEqual(S.nbands, 2) I_NewClassSig(ctypes.byref(S)) self.assertEqual(S.nclasses, 1) I_NewSubSig(ctypes.byref(S), ctypes.byref(S.ClassSig[0])) self.assertEqual(S.ClassSig[0].nsubclasses, 1) S.title = ReturnString("Signature title") S.semantic_labels[0] = ctypes.create_string_buffer(b"The_Who") S.semantic_labels[1] = ctypes.create_string_buffer(b"The_Doors") S.ClassSig[0].used = 1 S.ClassSig[0].classnum = 2 S.ClassSig[0].title = ReturnString("1st class") S.ClassSig[0].type = 1 S.ClassSig[0].SubSig[0].pi = 3.14 S.ClassSig[0].SubSig[0].means[0] = 42.42 S.ClassSig[0].SubSig[0].means[1] = 24.24 S.ClassSig[0].SubSig[0].R[0][0] = 69.69 S.ClassSig[0].SubSig[0].R[0][1] = 96.96 S.ClassSig[0].SubSig[0].R[1][0] = -69.69 S.ClassSig[0].SubSig[0].R[1][1] = -96.96 # This should result in returning NULL ret = I_SortSigSetBySemanticLabel(ctypes.byref(S), ctypes.byref(R)) self.assertFalse(bool(ret)) # Semantic labels and sig items should not be swapped # Static items self.assertEqual(S.ClassSig[0].SubSig[0].pi, 3.14) # Reordered items semantic_label1 = utils.decode( ctypes.cast(S.semantic_labels[0], ctypes.c_char_p).value ) self.assertEqual(semantic_label1, "The_Who") semantic_label2 = utils.decode( ctypes.cast(S.semantic_labels[1], ctypes.c_char_p).value ) self.assertEqual(semantic_label2, "The_Doors") self.assertEqual(S.ClassSig[0].SubSig[0].means[0], 42.42) self.assertEqual(S.ClassSig[0].SubSig[0].means[1], 24.24) self.assertEqual(S.ClassSig[0].SubSig[0].R[0][0], 69.69) self.assertEqual(S.ClassSig[0].SubSig[0].R[0][1], 96.96) self.assertEqual(S.ClassSig[0].SubSig[0].R[1][0], -69.69) self.assertEqual(S.ClassSig[0].SubSig[0].R[1][1], -96.96) # Clean up memory to help track memory leaks when run by valgrind I_free_group_ref(ctypes.byref(R)) if ret: if ret[0]: self.libc.free(ret[0]) if ret[1]: self.libc.free(ret[1]) self.libc.free(ret)