13693261870
2024-11-06 deeef732865c7dd901f07b7123e8f7a3c9f189c9
栅格矢量化
已修改2个文件
49 ■■■■ 文件已修改
src/main/java/com/se/simu/helper/ShpHelper.java 30 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/simu/service/ResultService.java 19 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/simu/helper/ShpHelper.java
@@ -6,6 +6,8 @@
import com.se.simu.domain.dto.GeField;
import com.se.simu.domain.dto.GeLayer;
import lombok.extern.slf4j.Slf4j;
import org.gdal.gdal.Dataset;
import org.gdal.gdal.gdal;
import org.gdal.ogr.*;
import org.gdal.osr.SpatialReference;
@@ -299,4 +301,32 @@
    private String getEpsg(SpatialReference sr) {
        return sr.GetAuthorityCode(null);
    }
    public static boolean polygonize(Dataset ds, String filePath) {
        Driver driver = null;
        DataSource dataSource = null;
        Layer layer = null;
        try {
            driver = ogr.GetDriverByName("ESRI shapefile");
            if (null == driver) return false;
            dataSource = driver.CreateDataSource(filePath, null);
            if (null == dataSource) return false;
            layer = dataSource.CreateLayer(FileUtil.getName(filePath), ds.GetSpatialRef(), ogr.wkbPolygon, getOptions());
            if (null == layer) return false;
            FieldDefn field = new FieldDefn("val", ogr.OFTReal);
            layer.CreateField(field);
            gdal.Polygonize(ds.GetRasterBand(1), null, layer, 0, null);
            return true;
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return false;
        } finally {
            GdalHelper.delete(layer, dataSource, driver);
        }
    }
}
src/main/java/com/se/simu/service/ResultService.java
@@ -10,6 +10,7 @@
import com.se.simu.domain.po.DataPo;
import com.se.simu.domain.vo.BuildingDepthVo;
import com.se.simu.helper.GdalHelper;
import com.se.simu.helper.ShpHelper;
import lombok.extern.slf4j.Slf4j;
import org.gdal.gdal.Band;
import org.gdal.gdal.Dataset;
@@ -359,6 +360,7 @@
                createWaterPng(dto, ds, layer, layer.getWaters().getData().get(i));
                copyWaterTif(dto, ds, layer.getWaters().getData().get(i));
                createVectorShp(dto, ds, layer, layer.getWaters().getData().get(i));
            } finally {
                if (null != ds) ds.delete();
            }
@@ -379,12 +381,6 @@
            String png = waterPath + File.separator + sizes[0] + "_" + sizes[1] + ".png";
            water2Png(dto, layer, tif, png, sizes[0], sizes[1]);
        }
    }
    private static void copyWaterTif(ResultDto dto, Dataset ds, long ticks) {
        String source = ds.GetDescription();
        String target = dto.getOutPath() + File.separator + "waters" + File.separator + ticks + File.separator + "water.tif";
        FileUtil.copyFile(source, target);
    }
    private static String getNameWithExt(String file) {
@@ -502,6 +498,17 @@
        return !Double.isNaN(val) && val > Integer.MIN_VALUE;
    }
    private static void copyWaterTif(ResultDto dto, Dataset ds, long ticks) {
        String source = ds.GetDescription();
        String target = dto.getOutPath() + File.separator + "waters" + File.separator + ticks + File.separator + "water.tif";
        FileUtil.copyFile(source, target);
    }
    private static void createVectorShp(ResultDto dto, Dataset ds, LayerDto layer, Long ticks) {
        String shpFile = dto.getOutPath() + File.separator + "waters" + File.separator + ticks + File.separator + "water.shp";
        ShpHelper.polygonize(ds, shpFile);
    }
    private void copeFlow(ResultDto dto, LayerDto layer) {
        List<String> files = getFiles(dto.getFlowPath(), ".tif");
        if (null == files || files.size() != layer.getWaters().getData().size()) return;