坐标系之前的数据转换(比如3857转4326)
implementation("org.osgeo:proj4j:0.1.0")
import org.osgeo.proj4j.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


public class CoordinateSystemUtils {

    public static double[] translateExtent(Integer sourceEPSG, Integer targetEPSG, double xmin, double ymin) {
        CoordinateTransformFactory transformFactory = new CoordinateTransformFactory();
        CRSFactory crsFactory = new CRSFactory();
        CoordinateReferenceSystem source = crsFactory.createFromName("EPSG:" + sourceEPSG);
        CoordinateReferenceSystem target = crsFactory.createFromName("EPSG:" + targetEPSG);

        CoordinateTransform transform = transformFactory.createTransform(source, target);
        ProjCoordinate minCoordinate = new ProjCoordinate(xmin, ymin);
        transform.transform(minCoordinate, minCoordinate);
        double[] box = new double[]{minCoordinate.x, minCoordinate.y};
        return box;
    }

    public static void main(String[] args) {
//        List<Double> list = new ArrayList<>();
//        list.add(13369378.41490000D);
//        list.add(4072485.38040000D);
//        System.out.println(JSON.toJSONString(translateExtent(4490, 3857, list)));
        double[] a = translateExtent(3857, 4490, 13369378.41490000D, 4072485.38040000D);
        System.out.println(a);
    }

    public static List<Double> translateExtent(Integer sourceEPSG, Integer targetEPSG, List<BigDecimal> data){
        if(data == null || data.size() < 2){
            return null;
        }
        List<Double> list = new ArrayList<>();
        CoordinateTransformFactory transformFactory = new CoordinateTransformFactory();
        CRSFactory crsFactory = new CRSFactory();
        CoordinateReferenceSystem source = crsFactory.createFromName("EPSG:" + sourceEPSG);
        CoordinateReferenceSystem target = crsFactory.createFromName("EPSG:" + targetEPSG);

        CoordinateTransform transform = transformFactory.createTransform(source, target);
        if(data.size() == 2){
            ProjCoordinate minCoordinate = new ProjCoordinate(data.get(0).doubleValue(), data.get(1).doubleValue());
            transform.transform(minCoordinate, minCoordinate);
            list.add(minCoordinate.x);
            list.add(minCoordinate.y);
            return list;
        }else if(data.size() == 3){
            ProjCoordinate minCoordinate = new ProjCoordinate(data.get(0).doubleValue(), data.get(1).doubleValue(), data.get(2).doubleValue());
            transform.transform(minCoordinate, minCoordinate);
            list.add(minCoordinate.x);
            list.add(minCoordinate.y);
            list.add(minCoordinate.z);
            return list;
        }
        return list;
    }

    public static List<Double> translateExtentByDouble(Integer sourceEPSG, Integer targetEPSG, List<Double> data){
        if(data == null || data.size() < 2){
            return null;
        }
        List<Double> list = new ArrayList<>();
        CoordinateTransformFactory transformFactory = new CoordinateTransformFactory();
        CRSFactory crsFactory = new CRSFactory();
        CoordinateReferenceSystem source = crsFactory.createFromName("EPSG:" + sourceEPSG);
        CoordinateReferenceSystem target = crsFactory.createFromName("EPSG:" + targetEPSG);

        CoordinateTransform transform = transformFactory.createTransform(source, target);
        if(data.size() == 2){
            ProjCoordinate minCoordinate = new ProjCoordinate(data.get(0), data.get(1));
            transform.transform(minCoordinate, minCoordinate);
            list.add(minCoordinate.x);
            list.add(minCoordinate.y);
            return list;
        }else if(data.size() == 3){
            ProjCoordinate minCoordinate = new ProjCoordinate(data.get(0), data.get(1), data.get(2));
            transform.transform(minCoordinate, minCoordinate);
            list.add(minCoordinate.x);
            list.add(minCoordinate.y);
            list.add(minCoordinate.z);
            return list;
        }
        return list;
    }


}

  

posted on 2021-07-02 11:16  james-roger  阅读(1530)  评论(0编辑  收藏  举报