11
13693261870
2024-11-11 138b959cc11dc9a73b0c766030b99ba1180d8650
src/main/java/com/se/simu/helper/GdalHelper.java
@@ -5,18 +5,13 @@
import org.gdal.gdal.Dataset;
import org.gdal.gdal.gdal;
import org.gdal.gdalconst.gdalconst;
import org.gdal.ogr.ogr;
import org.gdal.ogr.*;
import org.gdal.osr.CoordinateTransformation;
import org.gdal.osr.SpatialReference;
import org.gdal.osr.osr;
import java.io.File;
/**
 * GDAL帮助类
 *
 * @author WWW
 * @date 2024-09-12
 */
@Slf4j
@SuppressWarnings("ALL")
public class GdalHelper {
@@ -51,29 +46,20 @@
            }
        }
        // 支持中文路径
        gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES");
        // 属性表支持中文:CP936
        gdal.SetConfigOption("SHAPE_ENCODING", "");
        gdal.SetConfigOption("PGEO_DRIVER_TEMPLATE", "DRIVER=Microsoft Access Driver (*.mdb, *.accdb);DBQ=%s");
        gdal.SetConfigOption("MDB_DRIVER_TEMPLATE", "DRIVER=Microsoft Access Driver (*.mdb, *.accdb);DBQ=%s");
        // 注册所有的驱动
        gdal.AllRegister();
        ogr.RegisterAll();
        initSr();
    }
    /**
     * 初始化坐标系
     *
     * https://blog.csdn.net/CallmeAdo/article/details/127558139
     */
    public static void initSr() {
        try {
            SR4326 = new SpatialReference();
            SR4326.ImportFromEPSG(I4326);
            // 对于lat/long顺序的地理CRS,数据仍然是long/lat顺序的
            SR4326.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER);
            SR4490 = new SpatialReference();
@@ -84,9 +70,14 @@
        }
    }
    /**
     * 创建金字塔
     */
    public static SpatialReference createSpatialReference(int epsg) {
        SpatialReference sr = new SpatialReference();
        sr.ImportFromEPSG(epsg);
        sr.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER);
        return sr;
    }
    public static void createPyramid(String file) {
        Dataset ds = null;
        try {
@@ -100,7 +91,6 @@
                return;
            }
            // 创建金字塔
            Band band = ds.GetRasterBand(1);
            if (0 == band.GetOverviewCount()) {
                ds.BuildOverviews("nearest", new int[]{2, 4, 6, 8, 16}, null);
@@ -113,4 +103,124 @@
            }
        }
    }
    public static void delete(Layer layer, DataSource dataSource, Driver driver) {
        try {
            if (null != layer) {
                layer.delete();
            }
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
        try {
            if (null != dataSource) {
                dataSource.delete();
            }
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
        try {
            if (null != driver) {
                driver.delete();
            }
        } catch (Exception ex) {
            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)
    {
        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;
    }
    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;
    }
    public static double[] fromWgs84(SpatialReference sr, double x, double y) {
        CoordinateTransformation ct = new CoordinateTransformation(GdalHelper.SR4326, sr);
        if (sr.IsProjected() != 1) {
            sr.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER);
        }
        return ct.TransformPoint(x, y);
    }
    public static int fromWgs84(SpatialReference sr, Geometry g) {
        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;
    }
}