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 

24import yaml 

25 

26import math 

27import unittest 

28 

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

30 LonLat, NormalizedAngle, NormalizedAngleInterval, 

31 Region, UnitVector3d) 

32 

33 

34class BoxTestCase(unittest.TestCase): 

35 

36 def test_construction(self): 

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

38 self.assertTrue(b.isFull()) 

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

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

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

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

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

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

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

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

47 d = c.clone() 

48 self.assertEqual(a, b) 

49 self.assertEqual(b, c) 

50 self.assertEqual(c, d) 

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

52 b = Box() 

53 self.assertTrue(b.isEmpty()) 

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

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

56 

57 def test_comparison_operators(self): 

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

59 LonLat.fromDegrees(45, 45)) 

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

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

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

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

64 LonLat.fromDegrees(45, 90)) 

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

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

67 

68 def test_center_and_dimensions(self): 

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

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

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

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

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

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

75 

76 def test_relationships(self): 

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

78 p = LonLat.fromDegrees(135, 10) 

79 self.assertTrue(p in b1) 

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

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

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

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

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

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

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

87 self.assertTrue(u in b3) 

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

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

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

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

92 self.assertEqual(r, CONTAINS) 

93 r = b4.relate(b1) 

94 self.assertEqual(r, DISJOINT) 

95 

96 def test_expanding_and_clipping(self): 

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

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

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

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

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

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

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

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

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

106 self.assertEqual(a, b) 

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

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

109 self.assertTrue(a.isEmpty()) 

110 

111 def test_dilation_and_erosion(self): 

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

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

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

115 self.assertEqual(a, b) 

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

117 

118 def test_codec(self): 

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

120 s = b.encode() 

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

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

123 

124 def test_string(self): 

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

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

127 self.assertEqual( 

128 repr(b), 

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

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

131 ) 

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

133 AngleInterval=AngleInterval, Box=Box, 

134 NormalizedAngleInterval=NormalizedAngleInterval 

135 ))) 

136 

137 def test_pickle(self): 

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

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

140 self.assertEqual(a, b) 

141 

142 def test_yaml(self): 

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

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

145 self.assertEqual(a, b) 

146 

147 

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

149 unittest.main()