package com.fdkankan.geo;

import org.locationtech.proj4j.*;

/**
 * @author Xiewj
 * @date 2024/12/31
 */
public class Proj4Tool {
    /**
     * proj4的常用坐标转换方法
     *
     * @param coordinate 经度或者x轴 纬度或者y轴
     * @param targetCRS  坐标系代码   原始数据坐标系 "4326";  "EPSG:3857";
     *                   //     * @param targetCRS   坐标系代码  结果数据坐标系
     * @return
     */
    public static ProjCoordinate convertByTargetCRS(ProjCoordinate coordinate, String sourceCRS, String targetCRS) {
        CoordinateTransformFactory ctFactory = new CoordinateTransformFactory();
        CRSFactory csFactory = new CRSFactory();

        CoordinateReferenceSystem crs1 = csFactory.createFromName("EPSG:" + sourceCRS);
        CoordinateReferenceSystem crs2 = csFactory.createFromName("EPSG:" + targetCRS);

        CoordinateTransform trans = ctFactory.createTransform(crs1, crs2);

        ProjCoordinate p2 = new ProjCoordinate();

        return trans.transform(coordinate, p2);
    }
}
