package com.cr.common

import com.esri.arcgisruntime.geometry.*
import org.locationtech.jts.geom.Coordinate
import org.locationtech.jts.geom.GeometryFactory

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/8/16 13:37
 * 描述：JTS地理计算工具
 */
class CrJTMManager {
    companion object {
        /**
         * 根据经纬度计算距离
         * @param lon1 Double 经度1
         * @param lat1 Double 纬度1
         * @param lon2 Double 经度2
         * @param lat2 Double 纬度2
         * @return Double 计算距离
         */
        fun calculateDistance(lon1:Double,lat1:Double,lon2:Double,lat2:Double):Double{
            var pt1 = Point(lon1,lat1, SpatialReference.create(4326))
            var pt2 = Point(lon2,lat2, SpatialReference.create(4326))
            var polylineBuilder = PolylineBuilder(SpatialReference.create(4326))
            polylineBuilder.addPoint(pt1)
            polylineBuilder.addPoint(pt2)
            var linearUnit = LinearUnit(LinearUnitId.METERS)
            return GeometryEngine.lengthGeodetic(polylineBuilder.toGeometry(),linearUnit,GeodeticCurveType.GREAT_ELLIPTIC)
        }

        /**
         * 通过函数计算经纬度距离
         * @param longitude1 Double 经度1
         * @param latitude1 Double 纬度1
         * @param longitude2 Double 经度2
         * @param latitude2 Double 纬度2
         * @return Double
         */
        fun getDistance(longitude1:Double, latitude1:Double, longitude2:Double, latitude2:Double):Double {
            var lat1 = (Math.PI / 180) * latitude1;
            var lat2 = (Math.PI / 180) * latitude2;

            var lon1 = (Math.PI / 180) * longitude1;
            var lon2 = (Math.PI / 180) * longitude2;

            //两点间距离 km，如果想要米的话，结果*1000
            var d = Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1)) * 6378.137
            return if (d < 1)
                (d * 1000)
            else
                String.format("%.2f", d * 1000).toDouble()
        }
    }
}