0
点赞
收藏
分享

微信扫一扫

langchian进阶二:LCEL表达式,轻松进行chain的组装

代码敲到深夜 2024-05-25 阅读 35

文章目录

前言

Scala 语言


  • 通过多个经纬度坐标点, 计算出中心点, 这里使用的是 Scala 语言,其他的语言需要自行转换。
  • 求出来的并不是原有的点,而是原有点的中心位置的点。

scala 代码

package com.dw.process.mid

import java.lang.Double.parseDouble
import scala.annotation.tailrec
import scala.collection.mutable
import scala.math._

object GeoCalculatorTest {

  def getPointsCenter(points: Array[String]): (Double, Double) = {
    val pointNum = points.length
    var X = 0.0
    var Y = 0.0
    var Z = 0.0

    @tailrec
    def accumulate(i: Int): Unit = {
      if (i < pointNum) {
        val point = points(i).split(',')
        if (point.length == 2) {
          val lat = parseDouble(point(0)) * math.Pi / 180
          val lng = parseDouble(point(1)) * math.Pi / 180
          val x = cos(lat) * cos(lng)
          val y = cos(lat) * sin(lng)
          val z = sin(lat)

          X += x
          Y += y
          Z += z
        }
        accumulate(i + 1)
      }
    }

    accumulate(0)

    X /= pointNum
    Y /= pointNum
    Z /= pointNum

    val tmpLng = atan2(Y, X)
    val tmpLat = atan2(Z, sqrt(X * X + Y * Y))

    (tmpLat * 180 / math.Pi, tmpLng * 180 / math.Pi)
  }

  def main(args: Array[String]): Unit = {
    val str = "30.866603259732102,104.39074045163579;30.8619616865538,104.38696390134282;30.842287763333733,104.38807970029302;30.843761605258894,104.43202501279302;30.851572589486928,104.43545824033208;30.874265047548715,104.41202646237798"
    val arr = str.split(';')
    val tmpCenter = getPointsCenter(arr)
    println(s"Center latitude: ${tmpCenter._1}, Center longitude: ${tmpCenter._2}")


    // 如果使用腾讯地图或其他地图API,可以像下面这样创建坐标点
    // val defaultPoint = new qq.maps.LatLng(tmpCenter._1, tmpCenter._2)
  }
}
  • 运行得出的中心点的经纬度
Center latitude: 30.85674358236576, Center longitude: 104.40754961360216
  • 核心算法已经给出,请自行根据项目需求修改。

  • 多个经纬度组成的多边形
    多个经纬度组成的多边形
    得出的中心点
    在这里插入图片描述


总结

参考1

举报

相关推荐

0 条评论