
//	计算两点之间直线距离
export const algorithm = (point1, point2)=> {
	let [x1, y1] = point1;
	let [x2, y2] = point2;
    let Lat1 = rad(x1); // 纬度
    let Lat2 = rad(x2);
    let a = Lat1 - Lat2;//	两点纬度之差
    let b = rad(y1) - rad(y2); //	经度之差
    let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) 
        	+ Math.cos(Lat1) * Math.cos(Lat2) * Math.pow(Math.sin(b / 2), 2)));
        //	计算两点距离的公式
    s = s * 6378137.0;//	弧长等于弧度乘地球半径（半径为米）
    s = Math.round(s * 10000) / 10000;//	精确距离的数值
    return s;

}

//	角度转换成弧度
const rad =(d) =>{
    return d * Math.PI / 180.00;
};



/**
 *  判断经纬度是否超出中国境内
 */
export function isLocationOutOfChina(latitude, longitude) {
  if (longitude < 72.004 || longitude > 137.8347 || latitude < 0.8293 || latitude > 55.8271)
    return true;
  return false;
}


/**
 *  将WGS-84(国际标准)转为GCJ-02(火星坐标):
 */
export function transformFromWGSToGCJ(latitude, longitude) {
  let lat = "";
  let lon = "";
  let ee = 0.00669342162296594323;
  let a = 6378245.0;
  let pi = 3.14159265358979324;

  if (isLocationOutOfChina(latitude, longitude)) {
    lat = latitude;
    lon = longitude;
  }
  else {
    let adjustLat = transformLatWithXY(longitude - 105.0, latitude - 35.0);
    let adjustLon = transformLonWithXY(longitude - 105.0, latitude - 35.0);
    let radLat = latitude / 180.0 * pi;
    let magic = Math.sin(radLat);
    magic = 1 - ee * magic * magic;
    let sqrtMagic = Math.sqrt(magic);
    adjustLat = (adjustLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
    adjustLon = (adjustLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
    latitude = latitude + adjustLat;
    longitude = longitude + adjustLon;
  }
  return { latitude: latitude, longitude: longitude };

}

/**
 *  将GCJ-02(火星坐标)转为百度坐标:
 */
export function transformFromGCJToBaidu(latitude, longitude) {  
  let pi = 3.14159265358979324 * 3000.0 / 180.0;

  let z = Math.sqrt(longitude * longitude + latitude * latitude) + 0.00002 * Math.sin(latitude * pi);
  let theta = Math.atan2(latitude, longitude) + 0.000003 * Math.cos(longitude * pi);
  let a_latitude = (z * Math.sin(theta) + 0.006);
  let a_longitude = (z * Math.cos(theta) + 0.0065);

  return { latitude: a_latitude, longitude: a_longitude };
}

/**
 *  将百度坐标转为GCJ-02(火星坐标):
 */
export function transformFromBaiduToGCJ(latitude, longitude) {
  let xPi = 3.14159265358979323846264338327950288 * 3000.0 / 180.0;

  let x = longitude - 0.0065;
  let y = latitude - 0.006;
  let z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * xPi);
  let theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * xPi);
  let a_latitude = z * Math.sin(theta);
  let a_longitude = z * Math.cos(theta);

  return { latitude: a_latitude, longitude: a_longitude };
}

/**
 *  将GCJ-02(火星坐标)转为WGS-84:
 */
export function transformFromGCJToWGS({latitude, longitude}) {
  let threshold = 0.00001;

  // The boundary
  let minLat = latitude - 0.5;
  let maxLat = latitude + 0.5;
  let minLng = longitude - 0.5;
  let maxLng = longitude + 0.5;

  let delta = 1;
  let maxIteration = 30;
  let success = false
  while (success == false) {
    let leftBottom = transformFromWGSToGCJ(minLat, minLng);
    let rightBottom = transformFromWGSToGCJ(minLat, maxLng);
    let leftUp = transformFromWGSToGCJ(maxLat, minLng);
    let midPoint = transformFromWGSToGCJ((minLat + maxLat) / 2, (minLng + maxLng) / 2);
    delta = Math.abs(midPoint.latitude - latitude) + Math.abs(midPoint.longitude - longitude);

    if (maxIteration-- <= 0 || delta <= threshold) {
      return { latitude: (minLat + maxLat) / 2, longitude: (minLng + maxLng) / 2 };
    }

    if (isContains({ latitude: latitude, longitude: longitude }, leftBottom, midPoint)) {
      maxLat = (minLat + maxLat) / 2;
      maxLng = (minLng + maxLng) / 2;
    }
    else if (isContains({ latitude: latitude, longitude: longitude }, rightBottom, midPoint)) {
      maxLat = (minLat + maxLat) / 2;
      minLng = (minLng + maxLng) / 2;
    }
    else if (isContains({ latitude: latitude, longitude: longitude }, leftUp, midPoint)) {
      minLat = (minLat + maxLat) / 2;
      maxLng = (minLng + maxLng) / 2;
    }
    else {
      minLat = (minLat + maxLat) / 2;
      minLng = (minLng + maxLng) / 2;
    }
  }

}

export function toPointWkt(location){
  let {longitude,latitude} = location
  return "POINT(" + longitude + " " + latitude + ")"
}

export function pointWktToLocation(wkt){
  let start = wkt.indexOf("(")
  let end = wkt.indexOf(")")
  let arr = wkt.substring(start + 1,end).trim().split(" ")
  return {longitude:parseFloat(arr[0]),latitude:parseFloat(arr[1])}
}

export function isContains(point, p1, p2) {
  return (point.latitude >= Math.min(p1.latitude, p2.latitude) && point.latitude <= Math.max(p1.latitude, p2.latitude)) && (point.longitude >= Math.min(p1.longitude, p2.longitude) && point.longitude <= Math.max(p1.longitude, p2.longitude));
}

export function transformLatWithXY(x, y) {
  let pi = 3.14159265358979324;
  let lat = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x));
  lat += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
  lat += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
  lat += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
  return lat;
}

export function transformLonWithXY(x, y) {
  let pi = 3.14159265358979324;
  let lon = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x));
  lon += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
  lon += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
  lon += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0;
  return lon;
}


//检查视野范围是否超过了bbox
export function checkRegion(bbox,region){
  if(bbox.northeast.latitude < region.northeast.latitude ){
    return true;
  }
  if(bbox.northeast.longitude < region.northeast.longitude ){
    return true;
  }
  if( bbox.southwest.latitude > region.southwest.latitude  ){
    return true;
  }
  if(bbox.southwest.longitude > region.southwest.longitude  ){
    return true;
  }
  return false
}


