1 /*
2  * Copyright (C) 2023 The Android Open Source Project
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 
17 package com.android.telephony.tools.sats2;
18 
19 import com.android.telephony.sats2range.read.SatS2RangeFileReader;
20 
21 import com.beust.jcommander.JCommander;
22 import com.beust.jcommander.Parameter;
23 import com.google.common.geometry.S2CellId;
24 import com.google.common.geometry.S2LatLng;
25 
26 import java.io.File;
27 
28 /** A util class for checking if a location is in the input satellite S2 file. */
29 public final class SatS2LocationLookup {
30     /**
31      *  A util method for checking if a location is in the input satellite S2 file.
32      */
main(String[] args)33     public static void main(String[] args) throws Exception {
34         Arguments arguments = new Arguments();
35         JCommander.newBuilder()
36                 .addObject(arguments)
37                 .build()
38                 .parse(args);
39 
40         try (SatS2RangeFileReader satS2RangeFileReader =
41                      SatS2RangeFileReader.open(new File(arguments.inputFile))) {
42             S2CellId s2CellId = getS2CellId(arguments.latDegrees, arguments.lngDegrees,
43                     satS2RangeFileReader.getS2Level());
44             System.out.println("s2CellId=" + Long.toUnsignedString(s2CellId.id()));
45             if (satS2RangeFileReader.findEntryByCellId(s2CellId.id()) == null) {
46                 System.out.println("The input file does not contain the input location");
47             } else {
48                 System.out.println("The input file contains the input location");
49             }
50         }
51     }
52 
getS2CellId(double latDegrees, double lngDegrees, int s2Level)53     private static S2CellId getS2CellId(double latDegrees, double lngDegrees, int s2Level) {
54         // Create the leaf S2 cell containing the given S2LatLng
55         S2CellId cellId = S2CellId.fromLatLng(S2LatLng.fromDegrees(latDegrees, lngDegrees));
56 
57         // Return the S2 cell at the expected S2 level
58         return cellId.parent(s2Level);
59     }
60 
61     private static class Arguments {
62         @Parameter(names = "--input-file",
63                 description = "sat s2 file",
64                 required = true)
65         public String inputFile;
66 
67         @Parameter(names = "--lat-degrees",
68                 description = "lat degress of the location",
69                 required = true)
70         public double latDegrees;
71 
72         @Parameter(names = "--lng-degrees",
73                 description = "lng degress of the location",
74                 required = true)
75         public double lngDegrees;
76     }
77 }
78