Coverage for tests/test_Box.py : 18%

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#
23import pickle
25import math
26import unittest
28from lsst.sphgeom import (Angle, AngleInterval, Box, CONTAINS, DISJOINT,
29 LonLat, NormalizedAngle, NormalizedAngleInterval,
30 Region, UnitVector3d)
33class BoxTestCase(unittest.TestCase):
35 def test_construction(self):
36 b = Box(Box.allLongitudes(), Box.allLatitudes())
37 self.assertTrue(b.isFull())
38 b = Box.fromDegrees(-90, -45, 90, 45)
39 self.assertEqual(b, Box(b.getLon(), b.getLat()))
40 a = Box.fromRadians(-0.5 * math.pi, -0.25 * math.pi,
41 0.5 * math.pi, 0.25 * math.pi)
42 b = Box(LonLat.fromRadians(-0.5 * math.pi, -0.25 * math.pi),
43 LonLat.fromRadians(0.5 * math.pi, 0.25 * math.pi))
44 c = Box(LonLat.fromRadians(0, 0),
45 Angle(0.5 * math.pi), Angle(0.25 * math.pi))
46 d = c.clone()
47 self.assertEqual(a, b)
48 self.assertEqual(b, c)
49 self.assertEqual(c, d)
50 self.assertNotEqual(id(c), id(d))
51 b = Box()
52 self.assertTrue(b.isEmpty())
53 self.assertTrue(Box.empty().isEmpty())
54 self.assertTrue(Box.full().isFull())
56 def test_comparison_operators(self):
57 self.assertEqual(Box(LonLat.fromDegrees(45, 45)),
58 LonLat.fromDegrees(45, 45))
59 self.assertEqual(Box.fromDegrees(90, -45, 180, 45),
60 Box(NormalizedAngleInterval.fromDegrees(90, 180),
61 AngleInterval.fromDegrees(-45, 45)))
62 self.assertNotEqual(Box(LonLat.fromDegrees(45, 45)),
63 LonLat.fromDegrees(45, 90))
64 self.assertNotEqual(Box.fromDegrees(90, -45, 180, 45),
65 Box.fromDegrees(90, -45, 180, 90))
67 def test_center_and_dimensions(self):
68 b = Box.fromDegrees(-90, -45, 90, 45)
69 self.assertEqual(b.getCenter(), LonLat.fromDegrees(0, 0))
70 self.assertEqual(b.getWidth(), Angle.fromDegrees(180))
71 self.assertEqual(b.getHeight(), Angle.fromDegrees(90))
72 self.assertEqual(b.getLon().getA(), NormalizedAngle.fromDegrees(-90))
73 self.assertEqual(b.getLat().getB(), Angle.fromDegrees(45))
75 def test_relationships(self):
76 b1 = Box.fromDegrees(90, 0, 180, 45)
77 p = LonLat.fromDegrees(135, 10)
78 self.assertTrue(p in b1)
79 self.assertTrue(b1.contains(p))
80 b2 = Box.fromDegrees(135, 15, 135, 30)
81 self.assertTrue(b1.contains(b2))
82 self.assertTrue(b2.isWithin(b1))
83 b3 = Box.fromDegrees(0, -45, 90, 0)
84 u = UnitVector3d(1, 1, -1)
85 self.assertTrue(b1.intersects(b3))
86 self.assertTrue(u in b3)
87 self.assertTrue(b3.contains(u))
88 b4 = Box.fromDegrees(200, 10, 300, 20)
89 self.assertTrue(b1.isDisjointFrom(b4))
90 r = b1.relate(LonLat.fromDegrees(135, 10))
91 self.assertEqual(r, CONTAINS)
92 r = b4.relate(b1)
93 self.assertEqual(r, DISJOINT)
95 def test_expanding_and_clipping(self):
96 a = Box.fromDegrees(0, 0, 10, 10)
97 b = (a.expandedTo(LonLat.fromDegrees(20, 20))
98 .expandedTo(Box.fromDegrees(0, 0, 30, 10))
99 .clippedTo(Box.fromDegrees(10, 10, 15, 15))
100 .clippedTo(LonLat.fromDegrees(11, 11)))
101 a.expandTo(LonLat.fromDegrees(20, 20))
102 a.expandTo(Box.fromDegrees(0, 0, 30, 10))
103 a.clipTo(Box.fromDegrees(10, 10, 15, 15))
104 a.clipTo(LonLat.fromDegrees(11, 11))
105 self.assertEqual(a, b)
106 self.assertEqual(a, LonLat.fromDegrees(11, 11))
107 a.clipTo(LonLat.fromDegrees(0, 0))
108 self.assertTrue(a.isEmpty())
110 def test_dilation_and_erosion(self):
111 a = Box.fromRadians(0.5, -0.5, 1.5, 0.5)
112 b = a.dilatedBy(Angle(0.5), Angle(0.5)).erodedBy(Angle(1), Angle(1))
113 a.dilateBy(Angle(0.5), Angle(0.5)).erodeBy(Angle(1), Angle(1))
114 self.assertEqual(a, b)
115 self.assertEqual(a, LonLat.fromRadians(1, 0))
117 def test_codec(self):
118 b = Box.fromRadians(0, 0, 1, 1)
119 s = b.encode()
120 self.assertEqual(Box.decode(s), b)
121 self.assertEqual(Region.decode(s), b)
123 def test_string(self):
124 b = Box.fromRadians(0, 0, 1, 1)
125 self.assertEqual(str(b), 'Box([0.0, 1.0], [0.0, 1.0])')
126 self.assertEqual(
127 repr(b),
128 'Box(NormalizedAngleInterval.fromRadians(0.0, 1.0), '
129 'AngleInterval.fromRadians(0.0, 1.0))'
130 )
131 self.assertEqual(b, eval(repr(b), dict(
132 AngleInterval=AngleInterval, Box=Box,
133 NormalizedAngleInterval=NormalizedAngleInterval
134 )))
136 def test_pickle(self):
137 a = Box.fromDegrees(0, 0, 10, 10)
138 b = pickle.loads(pickle.dumps(a, pickle.HIGHEST_PROTOCOL))
139 self.assertEqual(a, b)
142if __name__ == '__main__': 142 ↛ 143line 142 didn't jump to line 143, because the condition on line 142 was never true
143 unittest.main()