wuww
2025-04-15 2975fe8b88c0dd70a2dd5e06ad2fcd7d375c5043
src/main/java/com/se/nsl/service/TestService.java
@@ -3,10 +3,7 @@
import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSON;
import com.se.nsl.config.PropertiesConfig;
import com.se.nsl.domain.dto.ExtensionDto;
import com.se.nsl.domain.dto.LayerDto;
import com.se.nsl.domain.dto.PointDto;
import com.se.nsl.domain.dto.ResultDto;
import com.se.nsl.domain.dto.*;
import com.se.nsl.domain.po.DataPo;
import com.se.nsl.domain.po.PondingPo;
import com.se.nsl.domain.vo.BuildingDepthVo;
@@ -17,7 +14,8 @@
import org.gdal.gdal.Band;
import org.gdal.gdal.Dataset;
import org.gdal.gdal.gdal;
import org.gdal.gdalconst.gdalconst;
//import org.gdal.gdalconstConstants.gdalconst;
import org.gdal.gdalconst.gdalconstConstants;
import org.gdal.ogr.*;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
@@ -35,6 +33,7 @@
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
@Slf4j
@Service
@@ -69,7 +68,7 @@
        try {
            copeTerrain(dto, layer);
            //copeBuilding(dto, layer);
            //List<BuildingDepthVo> buildings = copeWater(dto, layer);
            List<BuildingDepthVo> buildings = copeWater(dto, layer);
            //copeFlow(dto, layer);
            copeLayerJson(dto, layer);
            //copeRainFallJson(dto, layer);
@@ -85,7 +84,8 @@
    public void copeTerrain(ResultDto dto, LayerDto layer) {
        Dataset ds = null;
        try {
            ds = gdal.Open(dto.getTerrainFile(), gdalconst.GA_ReadOnly);
            // gdalconst,gdalconstConstants
            ds = gdal.Open(dto.getTerrainFile(), gdalconstConstants.GA_ReadOnly);
            if (null == ds || 0 == ds.getRasterCount()) return;
            setTerrainInfo(ds, layer);
@@ -130,7 +130,7 @@
    public void Terrain2Png(LayerDto layer, String tif, String png, int width, int height) {
        Dataset ds = null;
        try {
            ds = gdal.Open(tif, gdalconst.GA_ReadOnly);
            ds = gdal.Open(tif, gdalconstConstants.GA_ReadOnly);
            if (null == ds || 0 == ds.getRasterCount()) return;
            Band band = ds.GetRasterBand(1);
@@ -175,6 +175,19 @@
        setWaterHeight(layer, files);
    }
    public List<BuildingDepthVo> copeWater(ResultDto dto, LayerDto layer) {
        List<String> files = layer.getWaters().getFiles();
        if (files.size() == 0 || files.size() != layer.getWaters().getData().size()) return null;
        processWaters(dto, files, layer);
        if (new File(dto.getBuildingFile()).exists()) {
            return processBuilding(dto, files, layer);
        }
        return null;
    }
    public List<String> getFiles(String path, String suffix) {
        List<String> files = new ArrayList<>();
        ComHelper.getFiles(files, new File(path), suffix);
@@ -214,7 +227,7 @@
        files.parallelStream().forEach(file -> {
            Dataset ds = null;
            try {
                ds = gdal.Open(file, gdalconst.GA_ReadOnly);
                ds = gdal.Open(file, gdalconstConstants.GA_ReadOnly);
                if (null == ds || 0 == ds.getRasterCount()) return;
                double[] mm = new double[2];
@@ -234,7 +247,7 @@
        for (int i = 0, c = files.size(); i < c; i++) {
            Dataset ds = null;
            try {
                ds = gdal.Open(files.get(i), gdalconst.GA_ReadOnly);
                ds = gdal.Open(files.get(i), gdalconstConstants.GA_ReadOnly);
                if (null == ds || 0 == ds.getRasterCount()) return;
                if (null == ds.GetSpatialRef()) ds.SetSpatialRef(dto.getSpatialReference());
@@ -266,7 +279,7 @@
    public void water2Png(ResultDto dto, LayerDto layer, String tif, String png, int width, int height) {
        Dataset ds = null;
        try {
            ds = gdal.Open(tif, gdalconst.GA_ReadOnly);
            ds = gdal.Open(tif, gdalconstConstants.GA_ReadOnly);
            if (null == ds || 0 == ds.getRasterCount()) return;
            Band band = ds.GetRasterBand(1);
@@ -282,7 +295,7 @@
                for (int y = 0; y < height; y++) {
                    int offset = x + y * width;
                    //if (Float.isNaN(buffer[offset]) || buffer[offset] < -999 || buffer[offset] < minHeight) continue;
                    if (Float.isNaN(buffer[offset]) || buffer[offset] <= 0 || Float.isNaN(ts[offset])) continue;
                    if (Float.isNaN(buffer[offset]) || buffer[offset] <= 0.001 || Float.isNaN(ts[offset])) continue;
                    //double X = transform[0] + x * transform[1] + y * transform[2];
                    //double Y = transform[3] + x * transform[4] + y * transform[5];
@@ -308,11 +321,64 @@
        }
    }
    public List<BuildingDepthVo> processBuilding(ResultDto dto, List<String> files, LayerDto layer) {
        List<BuildingDepthVo> list = new CopyOnWriteArrayList<>();
        for (int i = 0, c = files.size(); i < c; i++) {
            Dataset ds = null;
            try {
                ds = gdal.Open(files.get(i), gdalconstConstants.GA_ReadOnly);
                if (null == ds || 0 == ds.getRasterCount()) continue;
                if (null == ds.GetSpatialRef()) ds.SetSpatialRef(dto.getSpatialReference());
                copeBuildingDepth(dto, ds, layer.getWaters().getData().get(i), list);
            } finally {
                if (null != ds) ds.delete();
            }
        }
        return list;
    }
    public void copeBuildingDepth(ResultDto dto, Dataset ds, long ticks, List<BuildingDepthVo> list) {
        double[] transform = ds.GetGeoTransform();
        int xSize = ds.getRasterXSize(), ySize = ds.getRasterYSize();
        double minX = transform[0], pixelWidth = transform[1], maxY = transform[3], pixelHeight = Math.abs(transform[5]);
        for (BuildingDto building : dto.getBuildingList()) {
            Double val = getValue(ds, building, xSize, ySize, minX, maxY, pixelWidth, pixelHeight);
            list.add(new BuildingDepthVo(building.getId(), ticks, val));
        }
    }
    public Double getValue(Dataset ds, BuildingDto building, int xSize, int ySize, double minX, double maxY, double pixelWidth, double pixelHeight) {
        double[] env = new double[4];
        building.getGeom().GetEnvelope(env);
        int startX = (int) Math.floor((env[0] - minX) / pixelWidth);
        int endX = (int) Math.floor((env[1] - minX) / pixelWidth);
        int startY = (int) Math.floor((maxY - env[3]) / Math.abs(pixelHeight));
        int endY = (int) Math.floor((maxY - env[2]) / Math.abs(pixelHeight));
        if (startX < 0) startX = 0;
        if (startY < 0) startY = 0;
        if (endX > ds.getRasterXSize()) endX = ds.getRasterXSize();
        if (endY > ds.getRasterYSize()) endY = ds.getRasterYSize();
        if (endX - startX < 1 || endY - startY < 1) return null;
        int width = endX - startX;
        int height = endY - startY;
        double[] pixelValues = new double[width * height];
        ds.GetRasterBand(1).ReadRaster(startX, startY, width, height, pixelValues);
        Double val = Arrays.stream(pixelValues).max().getAsDouble();
        return ComHelper.isValid(val) ? val : null;
    }
    public void createVectors(ResultDto dto, Dataset ds, LayerDto layer, Long ticks) {
        String filePath = dto.getOutPath() + File.separator + "waters" + File.separator + ticks + File.separator + "polygonize.geojson";
        ShpHelper.polygonize2Geojson(ds, filePath);
        List<Geometry> geometries = getGeometries(filePath);
        /*List<Geometry> geometries = getGeometries(filePath);
        if (CollectionUtils.isEmpty(geometries)) return;
        List<PondingPo> list = copePonding(dto, ds, layer, geometries);
@@ -323,7 +389,7 @@
            ComHelper.writeJson(filePath, JSON.toJSONString(list));
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
        }*/
    }
    public List<Geometry> getGeometries(String filePath) {
@@ -407,7 +473,7 @@
        for (int i = 0, c = files.size(); i < c; i++) {
            Dataset ds = null;
            try {
                ds = gdal.Open(files.get(i), gdalconst.GA_ReadOnly);
                ds = gdal.Open(files.get(i), gdalconstConstants.GA_ReadOnly);
                if (null == ds || 0 == ds.getRasterCount()) return;
                if (null == ds.GetSpatialRef()) ds.SetSpatialRef(dto.getSpatialReference());
@@ -437,7 +503,7 @@
    public void vxyTif2Png(LayerDto layer, String tif, String png, int width, int height) {
        Dataset ds = null;
        try {
            ds = gdal.Open(tif, gdalconst.GA_ReadOnly);
            ds = gdal.Open(tif, gdalconstConstants.GA_ReadOnly);
            if (null == ds || 0 == ds.getRasterCount()) return;
            float[] vxBuffer = new float[width * height], vyBuffer = new float[width * height];