13693261870
2024-10-31 c738483367653c6485ddc9a6dcdea019ad08cc63
src/main/java/com/se/simu/helper/GdalHelper.java
@@ -6,6 +6,7 @@
import org.gdal.gdal.gdal;
import org.gdal.gdalconst.gdalconst;
import org.gdal.ogr.*;
import org.gdal.osr.CoordinateTransformation;
import org.gdal.osr.SpatialReference;
import org.gdal.osr.osr;
@@ -199,4 +200,44 @@
        return point;
    }
    /**
     * 转换为WGS84坐标
     */
    public static Geometry toWgs84(SpatialReference sr, double x, double y) {
        Geometry point = new Geometry(ogr.wkbPoint);
        point.AssignSpatialReference(sr);
        point.AddPoint(x, y);
        point.TransformTo(GdalHelper.SR4326);
        //point.SwapXY();
        return point;
    }
    /**
     * WGS84转换为目标坐标
     */
    public static double[] fromWgs84(SpatialReference sr, double x, double y) {
        // https://blog.csdn.net/weixin_34910922/article/details/129208661
        CoordinateTransformation ct = new CoordinateTransformation(GdalHelper.SR4326, sr);
        if (sr.IsProjected() != 1) {
            sr.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER);
        }
        return ct.TransformPoint(x, y);
    }
    /**
     * WGS84转换为目标坐标
     */
    public static int fromWgs84(SpatialReference sr, Geometry point) {
        // https://blog.csdn.net/weixin_34910922/article/details/129208661
        CoordinateTransformation ct = new CoordinateTransformation(GdalHelper.SR4326, sr);
        if (sr.IsProjected() != 1) {
            sr.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER);
        }
        return point.TransformTo(sr);
    }
}