1
13693261870
2024-09-22 e5d40172f9bd9b0b967cb0e74b551f1a135dae6f
src/main/java/com/se/simu/service/GedbService.java
@@ -13,6 +13,7 @@
import com.se.simu.helper.StringHelper;
import lombok.extern.slf4j.Slf4j;
import org.gdal.gdal.Dataset;
import org.gdal.gdal.WarpOptions;
import org.gdal.gdal.gdal;
import org.gdal.gdalconst.gdalconst;
import org.springframework.beans.factory.annotation.Value;
@@ -99,7 +100,7 @@
        String filePath = inPath + File.separator + fileDb.getDbid();
        downloadFiles(token, filePath, files, fileDb.getDbid(), fileId);
        clipDemFile(filePath, files, basePath, bbox);
        clipDemFile(filePath, files, basePath, bbox, 4528);
        return true;
    }
@@ -332,24 +333,50 @@
        is.close();
    }
    private void clipDemFile(String filePath, List<GeFile> files, String basePath, String bbox) throws Exception {
    private void clipDemFile(String filePath, List<GeFile> files, String basePath, String bbox, int epsg) throws Exception {
        String target = basePath + File.separator + demName;
        for (GeFile file : files) {
            if (file.getName().toLowerCase().endsWith(demType)) {
                String source = filePath + File.separator + file.getName();
                clipDem(source, target, bbox);
                clipDem(source, target, bbox, epsg);
                break;
            }
        }
    }
    private void clipDem(String source, String target, String bbox) throws Exception {
    private void clipDem(String source, String target, String bbox, int epsg) throws Exception {
        Dataset ds = null;
        try {
            ds = gdal.Open(source, gdalconst.GA_ReadOnly);
            if (null == ds || ds.getRasterCount() < 1 || null == ds.GetSpatialRef()) throw new Exception("DEM数据无效");
            // String bbox = "116.64388473935195,39.884315914604464,116.64754729082588,39.887069143903496";
            String[] bb = bbox.split(",");
            // gdalwarp -ot UInt16 -s_srs EPSG:4326 -t_srs EPSG:2382 -r bilinear -of GTiff -te 116.526854182 40.0481829856 116.532848182 40.0541769856
            // -te_srs EPSG:4326 -co COMPRESS=DEFLATE -co PREDICTOR=1 -co ZLEVEL=6 -co TILED=YES -wo OPTIMIZE_SIZE=TRUE E:\GDALhomework\000002.tif E:/CSDN/warped1.tif
            Vector<String> vector = new Vector<>();
            //vector.add("-s_srs");
            //vector.add("EPSG:" + 4326);
            vector.add("-t_srs");
            vector.add("EPSG:" + epsg);
            vector.add("-r");
            vector.add("bilinear");
            vector.add("-of");
            vector.add("GTiff");
            vector.add("-te");
            vector.add(bb[0]);
            vector.add(bb[1]);
            vector.add(bb[2]);
            vector.add(bb[3]);
            vector.add("-te_srs");
            vector.add("EPSG:" + 4326);
            WarpOptions warpOptions = new WarpOptions(vector);
            Dataset destDs = gdal.Warp(target, new Dataset[]{ds}, warpOptions);
            destDs.delete();
        } finally {
            if (null != ds) ds.delete();
        }