package cn.tsy.idsse.dsds.helper;

import java.util.List;

import cn.tsy.data.idsse.dsds.model.voyage.VoyageNavigation;

public class DSDSGeoCalculator {

	// 地球半径，单位为海里
	private static final double EARTH_RADIUS = 3440;

	/**
	 * 计算两点之间的距离，结果以海里为单位
	 * 
	 * @param lat1 第一个点的纬度
	 * @param lon1 第一个点的经度
	 * @param lat2 第二个点的纬度
	 * @param lon2 第二个点的经度
	 * @return 两点之间的距离，单位为海里
	 */
	public static double calculateDistanceInNauticalMiles(double lat1, double lon1, double lat2, double lon2) {
		double latDistance = Math.toRadians(lat2 - lat1);
		double lonDistance = Math.toRadians(lon2 - lon1);
		double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
		double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
		double distance = EARTH_RADIUS * c;
		return distance;
	}

	public static double calcuateVoyage(List<VoyageNavigation> list) {
		if (list.size() < 2)
			return 0d;
		double distance = 0;
		for (int i = 0; i < list.size()-1; i++) {
			distance += calculateDistanceInNauticalMiles(Double.parseDouble(list.get(i).getNav_lat()),
					Double.parseDouble(list.get(i).getNav_lon()),
					Double.parseDouble(list.get(i+1).getNav_lat()),
					Double.parseDouble(list.get(i+1).getNav_lon()));
		}
		return distance;
	}

//    public static void main(String[] args) {
//        // 示例经纬度
//        double lat1 = 40.7128;
//        double lon1 = -74.0060;
//        double lat2 = 40.7829;
//        double lon2 = -73.9654;
// 
//        double distance = calculateDistanceInNauticalMiles(lat1, lon1, lat2, lon2);
//        System.out.println("Distance in nautical miles: " + distance);
//    }
}
