xref: /aosp_15_r20/external/s2-geometry-library-java/tests/com/google/common/geometry/S2LatLngTest.java (revision 608e00cb6fb903b02fd0af903766fe7954affbe4)
1 /*
2  * Copyright 2005 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.google.common.geometry;
17 
18 public strictfp class S2LatLngTest extends GeometryTestCase {
19 
testBasic()20   public void testBasic() {
21     S2LatLng llRad = S2LatLng.fromRadians(S2.M_PI_4, S2.M_PI_2);
22     assertTrue(llRad.lat().radians() == S2.M_PI_4);
23     assertTrue(llRad.lng().radians() == S2.M_PI_2);
24     assertTrue(llRad.isValid());
25     S2LatLng llDeg = S2LatLng.fromDegrees(45, 90);
26     assertEquals(llDeg, llRad);
27     assertTrue(llDeg.isValid());
28     assertTrue(!S2LatLng.fromDegrees(-91, 0).isValid());
29     assertTrue(!S2LatLng.fromDegrees(0, 181).isValid());
30 
31     S2LatLng bad = S2LatLng.fromDegrees(120, 200);
32     assertTrue(!bad.isValid());
33     S2LatLng better = bad.normalized();
34     assertTrue(better.isValid());
35     assertEquals(better.lat(), S1Angle.degrees(90));
36     assertDoubleNear(better.lng().radians(), S1Angle.degrees(-160).radians());
37 
38     bad = S2LatLng.fromDegrees(-100, -360);
39     assertTrue(!bad.isValid());
40     better = bad.normalized();
41     assertTrue(better.isValid());
42     assertEquals(better.lat(), S1Angle.degrees(-90));
43     assertDoubleNear(better.lng().radians(), 0);
44 
45     assertTrue((S2LatLng.fromDegrees(10, 20).add(S2LatLng.fromDegrees(20, 30))).approxEquals(
46         S2LatLng.fromDegrees(30, 50)));
47     assertTrue((S2LatLng.fromDegrees(10, 20).sub(S2LatLng.fromDegrees(20, 30))).approxEquals(
48         S2LatLng.fromDegrees(-10, -10)));
49     assertTrue((S2LatLng.fromDegrees(10, 20).mul(0.5)).approxEquals(S2LatLng.fromDegrees(5, 10)));
50   }
51 
testConversion()52   public void testConversion() {
53     // Test special cases: poles, "date line"
54     assertDoubleNear(
55         new S2LatLng(S2LatLng.fromDegrees(90.0, 65.0).toPoint()).lat().degrees(), 90.0);
56     assertEquals(
57         new S2LatLng(S2LatLng.fromRadians(-S2.M_PI_2, 1).toPoint()).lat().radians(), -S2.M_PI_2);
58     assertDoubleNear(
59         Math.abs(new S2LatLng(S2LatLng.fromDegrees(12.2, 180.0).toPoint()).lng().degrees()), 180.0);
60     assertEquals(
61         Math.abs(new S2LatLng(S2LatLng.fromRadians(0.1, -S2.M_PI).toPoint()).lng().radians()),
62         S2.M_PI);
63 
64     // Test a bunch of random points.
65     for (int i = 0; i < 100000; ++i) {
66       S2Point p = randomPoint();
67       assertTrue(S2.approxEquals(p, new S2LatLng(p).toPoint()));
68     }
69 
70     // Test generation from E5
71     S2LatLng test = S2LatLng.fromE5(123456, 98765);
72     assertDoubleNear(test.lat().degrees(), 1.23456);
73     assertDoubleNear(test.lng().degrees(), 0.98765);
74   }
75 
testDistance()76   public void testDistance() {
77     assertEquals(
78         S2LatLng.fromDegrees(90, 0).getDistance(S2LatLng.fromDegrees(90, 0)).radians(), 0.0);
79     assertDoubleNear(
80         S2LatLng.fromDegrees(-37, 25).getDistance(S2LatLng.fromDegrees(-66, -155)).degrees(), 77,
81         1e-13);
82     assertDoubleNear(
83         S2LatLng.fromDegrees(0, 165).getDistance(S2LatLng.fromDegrees(0, -80)).degrees(), 115,
84         1e-13);
85     assertDoubleNear(
86         S2LatLng.fromDegrees(47, -127).getDistance(S2LatLng.fromDegrees(-47, 53)).degrees(), 180,
87         2e-6);
88   }
89 }
90