package com.fdkankan.geo;

import org.locationtech.proj4j.*;

/**
 * @author Xiewj
 * @date 2024/12/31
 */
public class GeoTransformUtil {

    static String wgs84CRS = "4326";

    /**
     * 获取wgs84坐标
     *
     * @param sourceCRS   原始坐标系代码 EPSG:3857; 只需要EPSG冒号后的编码
     * @param coordinate   new ProjCoordinate(lon, lat) 坐标系代码   原始数据坐标系 "4326";  "EPSG:3857";
     * @return
     */
    public static Double[] getWgs84(ProjCoordinate coordinate, String sourceCRS) {
        ProjCoordinate projCoordinate = Proj4Tool.convertByTargetCRS(coordinate, sourceCRS, wgs84CRS);
        return new Double[]{projCoordinate.x,projCoordinate.y,coordinate.z};
    }
    /**
     * 获取高德坐标系  wgs84ToGcj02
     * 即wgs84 转 高德
     * @param coordinate   new ProjCoordinate(lon, lat) 坐标系代码   wgs84坐标系
     * @return
     */
    public static Double[] wgs84ToGcj02(ProjCoordinate coordinate) {
        Double[] doubles = MapTool.WGS84ToGCJ02(coordinate.x, coordinate.y);
        return new Double[]{doubles[0],doubles[1],coordinate.z};
    }
    /**
     * 获取百度坐标系  gcj02ToBd09
     * 即高德 转 百度
     * @param coordinate   new ProjCoordinate(lon, lat) 坐标系代码   wgs84坐标系
     * @return
     */
    public static Double[] gcj02ToBd09(ProjCoordinate coordinate) {
        Double[] doubles = MapTool.GCJ02ToBD09(coordinate.x, coordinate.y);
        return new Double[]{doubles[0],doubles[1],coordinate.z};
    }

    public static void main(String[] args) {
        ProjCoordinate coordinate1 = new ProjCoordinate(454938.2,2476768.45,46.8);
        Double[] wgs84 = getWgs84(coordinate1, "4547");
        System.out.println("wgs84:"+wgs84[0]+","+wgs84[1]+","+wgs84[2]);
        ProjCoordinate coordinate2 = new ProjCoordinate(wgs84[0],wgs84[1],wgs84[2]);
        Double[] gcj02 = wgs84ToGcj02(coordinate2);
        System.out.println("gcj02:"+gcj02[0]+","+gcj02[1]+","+gcj02[2]);
    }
}
