13693261870
2024-11-07 c63f5c032bee42eb339cbbd95c8cee4f7132cf7e
src/main/java/com/se/simu/helper/GdalHelper.java
@@ -5,10 +5,8 @@
import org.gdal.gdal.Dataset;
import org.gdal.gdal.gdal;
import org.gdal.gdalconst.gdalconst;
import org.gdal.ogr.DataSource;
import org.gdal.ogr.Driver;
import org.gdal.ogr.Layer;
import org.gdal.ogr.ogr;
import org.gdal.ogr.*;
import org.gdal.osr.CoordinateTransformation;
import org.gdal.osr.SpatialReference;
import org.gdal.osr.osr;
@@ -87,6 +85,14 @@
        }
    }
    public static SpatialReference createSpatialReference(int epsg) {
        SpatialReference sr = new SpatialReference();
        sr.ImportFromEPSG(epsg);
        sr.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER);
        return sr;
    }
    /**
     * 创建金字塔
     */
@@ -143,4 +149,120 @@
            log.error(ex.getMessage(), ex);
        }
    }
    public static Geometry getMinPoint(Dataset ds)
    {
        double[] transform = new double[6];
        ds.GetGeoTransform(transform);
        double xMin = transform[0];
        double yMin = transform[3] - ds.getRasterYSize() * transform[1];
        Geometry point = new Geometry(ogr.wkbPoint);
        point.AddPoint(xMin, yMin, 0);
        return Transform(ds, point);
    }
    public static Geometry getMaxPoint(Dataset ds)
    {
        /*
         * transform[0] 左上角x坐标
         * transform[1] 东西方向分辨率
         * transform[2] 旋转角度, 0表示图像 "北方朝上"
         *
         * transform[3] 左上角y坐标
         * transform[4] 旋转角度, 0表示图像 "北方朝上"
         * transform[5] 南北方向分辨率
         */
        double[] transform = new double[6];
        ds.GetGeoTransform(transform);
        double xMax = transform[0] + (ds.getRasterYSize() * transform[1]);
        double yMax = transform[3];
        Geometry point = new Geometry(ogr.wkbPoint);
        point.AddPoint(xMax, yMax, 0);
        return Transform(ds, point);
    }
    public static Geometry Transform(Dataset ds, Geometry point)
    {
        point.AssignSpatialReference(ds.GetSpatialRef());
        if (ds.GetSpatialRef().IsGeographic() > 0)
        {
            return point;
        }
        String srsName = ds.GetSpatialRef().GetName();
        //if (srsName.Contains(CGCS2000))
        //{
        //    point.TransformTo(sr4490);
        //}
        //else
        //{
        point.TransformTo(SR4326);
        //}
        //point.SwapXY();
        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 g) {
        // 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 g.TransformTo(sr);
    }
    public static Geometry createPolygon(SpatialReference sr, Double minx, Double miny, Double maxx, Double maxy) {
        Geometry ring = new Geometry(ogr.wkbLinearRing);
        ring.AddPoint_2D(minx, maxy);
        ring.AddPoint_2D(maxx, maxy);
        ring.AddPoint_2D(maxx, miny);
        ring.AddPoint_2D(minx, miny);
        ring.AddPoint_2D(minx, maxy);
        Geometry poly = new Geometry(ogr.wkbPolygon);
        poly.AddGeometry(ring);
        if (null != sr) {
            poly.AssignSpatialReference(sr);
        }
        return poly;
    }
}