Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# 

2# LSST Data Management System 

3# See COPYRIGHT file at the top of the source tree. 

4# 

5# This product includes software developed by the 

6# LSST Project (http://www.lsst.org/). 

7# 

8# This program is free software: you can redistribute it and/or modify 

9# it under the terms of the GNU General Public License as published by 

10# the Free Software Foundation, either version 3 of the License, or 

11# (at your option) any later version. 

12# 

13# This program is distributed in the hope that it will be useful, 

14# but WITHOUT ANY WARRANTY; without even the implied warranty of 

15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

16# GNU General Public License for more details. 

17# 

18# You should have received a copy of the LSST License Statement and 

19# the GNU General Public License along with this program. If not, 

20# see <https://www.lsstcorp.org/LegalNotices/>. 

21# 

22 

23import pickle 

24try: 

25 import yaml 

26except ImportError: 

27 yaml = None 

28 

29import math 

30import unittest 

31 

32from lsst.sphgeom import (Angle, AngleInterval, Box, CONTAINS, DISJOINT, 

33 LonLat, NormalizedAngle, NormalizedAngleInterval, 

34 Region, UnitVector3d) 

35 

36 

37class BoxTestCase(unittest.TestCase): 

38 

39 def test_construction(self): 

40 b = Box(Box.allLongitudes(), Box.allLatitudes()) 

41 self.assertTrue(b.isFull()) 

42 b = Box.fromDegrees(-90, -45, 90, 45) 

43 self.assertEqual(b, Box(b.getLon(), b.getLat())) 

44 a = Box.fromRadians(-0.5 * math.pi, -0.25 * math.pi, 

45 0.5 * math.pi, 0.25 * math.pi) 

46 b = Box(LonLat.fromRadians(-0.5 * math.pi, -0.25 * math.pi), 

47 LonLat.fromRadians(0.5 * math.pi, 0.25 * math.pi)) 

48 c = Box(LonLat.fromRadians(0, 0), 

49 Angle(0.5 * math.pi), Angle(0.25 * math.pi)) 

50 d = c.clone() 

51 self.assertEqual(a, b) 

52 self.assertEqual(b, c) 

53 self.assertEqual(c, d) 

54 self.assertNotEqual(id(c), id(d)) 

55 b = Box() 

56 self.assertTrue(b.isEmpty()) 

57 self.assertTrue(Box.empty().isEmpty()) 

58 self.assertTrue(Box.full().isFull()) 

59 

60 def test_comparison_operators(self): 

61 self.assertEqual(Box(LonLat.fromDegrees(45, 45)), 

62 LonLat.fromDegrees(45, 45)) 

63 self.assertEqual(Box.fromDegrees(90, -45, 180, 45), 

64 Box(NormalizedAngleInterval.fromDegrees(90, 180), 

65 AngleInterval.fromDegrees(-45, 45))) 

66 self.assertNotEqual(Box(LonLat.fromDegrees(45, 45)), 

67 LonLat.fromDegrees(45, 90)) 

68 self.assertNotEqual(Box.fromDegrees(90, -45, 180, 45), 

69 Box.fromDegrees(90, -45, 180, 90)) 

70 

71 def test_center_and_dimensions(self): 

72 b = Box.fromDegrees(-90, -45, 90, 45) 

73 self.assertEqual(b.getCenter(), LonLat.fromDegrees(0, 0)) 

74 self.assertEqual(b.getWidth(), Angle.fromDegrees(180)) 

75 self.assertEqual(b.getHeight(), Angle.fromDegrees(90)) 

76 self.assertEqual(b.getLon().getA(), NormalizedAngle.fromDegrees(-90)) 

77 self.assertEqual(b.getLat().getB(), Angle.fromDegrees(45)) 

78 

79 def test_relationships(self): 

80 b1 = Box.fromDegrees(90, 0, 180, 45) 

81 p = LonLat.fromDegrees(135, 10) 

82 self.assertTrue(p in b1) 

83 self.assertTrue(b1.contains(p)) 

84 b2 = Box.fromDegrees(135, 15, 135, 30) 

85 self.assertTrue(b1.contains(b2)) 

86 self.assertTrue(b2.isWithin(b1)) 

87 b3 = Box.fromDegrees(0, -45, 90, 0) 

88 u = UnitVector3d(1, 1, -1) 

89 self.assertTrue(b1.intersects(b3)) 

90 self.assertTrue(u in b3) 

91 self.assertTrue(b3.contains(u)) 

92 b4 = Box.fromDegrees(200, 10, 300, 20) 

93 self.assertTrue(b1.isDisjointFrom(b4)) 

94 r = b1.relate(LonLat.fromDegrees(135, 10)) 

95 self.assertEqual(r, CONTAINS) 

96 r = b4.relate(b1) 

97 self.assertEqual(r, DISJOINT) 

98 

99 def test_expanding_and_clipping(self): 

100 a = Box.fromDegrees(0, 0, 10, 10) 

101 b = (a.expandedTo(LonLat.fromDegrees(20, 20)) 

102 .expandedTo(Box.fromDegrees(0, 0, 30, 10)) 

103 .clippedTo(Box.fromDegrees(10, 10, 15, 15)) 

104 .clippedTo(LonLat.fromDegrees(11, 11))) 

105 a.expandTo(LonLat.fromDegrees(20, 20)) 

106 a.expandTo(Box.fromDegrees(0, 0, 30, 10)) 

107 a.clipTo(Box.fromDegrees(10, 10, 15, 15)) 

108 a.clipTo(LonLat.fromDegrees(11, 11)) 

109 self.assertEqual(a, b) 

110 self.assertEqual(a, LonLat.fromDegrees(11, 11)) 

111 a.clipTo(LonLat.fromDegrees(0, 0)) 

112 self.assertTrue(a.isEmpty()) 

113 

114 def test_dilation_and_erosion(self): 

115 a = Box.fromRadians(0.5, -0.5, 1.5, 0.5) 

116 b = a.dilatedBy(Angle(0.5), Angle(0.5)).erodedBy(Angle(1), Angle(1)) 

117 a.dilateBy(Angle(0.5), Angle(0.5)).erodeBy(Angle(1), Angle(1)) 

118 self.assertEqual(a, b) 

119 self.assertEqual(a, LonLat.fromRadians(1, 0)) 

120 

121 def test_codec(self): 

122 b = Box.fromRadians(0, 0, 1, 1) 

123 s = b.encode() 

124 self.assertEqual(Box.decode(s), b) 

125 self.assertEqual(Region.decode(s), b) 

126 

127 def test_string(self): 

128 b = Box.fromRadians(0, 0, 1, 1) 

129 self.assertEqual(str(b), 'Box([0.0, 1.0], [0.0, 1.0])') 

130 self.assertEqual( 

131 repr(b), 

132 'Box(NormalizedAngleInterval.fromRadians(0.0, 1.0), ' 

133 'AngleInterval.fromRadians(0.0, 1.0))' 

134 ) 

135 self.assertEqual(b, eval(repr(b), dict( 

136 AngleInterval=AngleInterval, Box=Box, 

137 NormalizedAngleInterval=NormalizedAngleInterval 

138 ))) 

139 

140 def test_pickle(self): 

141 a = Box.fromDegrees(0, 0, 10, 10) 

142 b = pickle.loads(pickle.dumps(a, pickle.HIGHEST_PROTOCOL)) 

143 self.assertEqual(a, b) 

144 

145 @unittest.skipIf(not yaml, "YAML module can not be imported") 

146 def test_yaml(self): 

147 a = Box.fromDegrees(0, 0, 10, 10) 

148 b = yaml.safe_load(yaml.dump(a)) 

149 self.assertEqual(a, b) 

150 

151 

152if __name__ == '__main__': 152 ↛ 153line 152 didn't jump to line 153, because the condition on line 152 was never true

153 unittest.main()