github.com/stackb/rules_proto@v0.0.0-20240221195024-5428336c51f1/example/routeguide/scala/RouteGuideUtil.scala (about)

     1  package example.routeguide.scala
     2  
     3  import java.net.URL
     4  import java.util.logging.Logger
     5  import scalapb.json4s.JsonFormat
     6  import scala.io.Source
     7  
     8  import example.routeguide.{Feature, FeatureDatabase, Point}
     9  
    10  object RouteGuideUtil {
    11    val logger: Logger = Logger.getLogger(getClass.getName)
    12    val defaultFeatureFile: URL = getClass.getClassLoader.getResource("example/routeguide/feature_db.json")
    13  
    14    /**
    15      * Get a canned sequence of features so we don't have to parse the json file.
    16      */
    17    def getFeatures(): Seq[Feature] = {
    18      val features: Seq[Feature] = Seq(
    19        Feature(
    20          name = "Patriots Path, Mendham, NJ 07945, USA",
    21          location = Some(Point(407838351, -746143763))),
    22        Feature(
    23          name = "101 New Jersey 10, Whippany, NJ 07981, USA",
    24          location = Some(Point(408122808, -743999179)))
    25      )
    26      features
    27    }
    28  
    29    /**
    30      * Parses the JSON input file containing the list of features.
    31      */
    32    def parseFeatures(file: URL): Seq[Feature] = {
    33      logger.info(s"Loading features from ${file}")
    34      var features: Seq[Feature] = Seq.empty
    35      val input = file.openStream
    36      try {
    37        val source = Source.fromInputStream(input)
    38        try {
    39          val db = JsonFormat.fromJsonString[FeatureDatabase](source.getLines().mkString("\n"))
    40          features = db.feature
    41          logger.info(s"Parsed features from ${file.getPath}")
    42        } finally source.close()
    43      } finally input.close
    44      logger.info(s"Loaded ${features.size} features")
    45      features
    46    }
    47  
    48  }
    49  
    50  object RouteGuideServiceUtil {
    51    def isValid(feature: Feature): Boolean = feature.name.nonEmpty
    52  
    53    val COORD_FACTOR: Double = 1e7
    54    def getLatitude(point: Point): Double = point.latitude.toDouble / COORD_FACTOR
    55    def getLongitude(point: Point): Double = point.longitude.toDouble / COORD_FACTOR
    56    /**
    57      * Calculate the distance between two points using the "haversine" formula.
    58      * This code was taken from http://www.movable-type.co.uk/scripts/latlong.html.
    59      *
    60      * @param start The starting point
    61      * @param end   The end point
    62      * @return The distance between the points in meters
    63      */
    64    def calcDistance(start: Point, end: Point) = {
    65      val lat1 = getLatitude(start)
    66      val lat2 = getLatitude(end)
    67      val lon1 = getLongitude(start)
    68      val lon2 = getLongitude(end)
    69      val r = 6371000
    70      // meters
    71      import Math._
    72      val phi1 = toRadians(lat1)
    73      val phi2 = toRadians(lat2)
    74      val deltaPhi = toRadians(lat2 - lat1)
    75      val deltaLambda = toRadians(lon2 - lon1)
    76      val a = Math.sin(deltaPhi / 2) * Math.sin(deltaPhi / 2) + cos(phi1) * cos(phi2) * sin(deltaLambda / 2) * sin(deltaLambda / 2)
    77      val c = 2 * atan2(sqrt(a), sqrt(1 - a))
    78      (r * c).toInt
    79    }
    80  }