1
13693261870
2024-11-06 2164262d9b15d2dcf0dbdc30ef9d829ba99e37af
1
已修改3个文件
61 ■■■■■ 文件已修改
src/main/java/com/se/simu/controller/TestController.java 13 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/simu/helper/ShpHelper.java 44 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/simu/service/ResultService.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/simu/controller/TestController.java
@@ -2,6 +2,7 @@
import com.se.simu.domain.po.DataPo;
import com.se.simu.domain.vo.R;
import com.se.simu.helper.ShpHelper;
import com.se.simu.helper.StringHelper;
import com.se.simu.service.GedbService;
import com.se.simu.service.ResultService;
@@ -125,4 +126,16 @@
            return fail(ex, null);
        }
    }
    @ApiOperation(value = "testPolygonize *")
    @GetMapping("/testPolygonize")
    public R<Object> testPolygonize() {
        try {
            ShpHelper.test();
            return success("ok");
        } catch (Exception ex) {
            return fail(ex, null);
        }
    }
}
src/main/java/com/se/simu/helper/ShpHelper.java
@@ -6,8 +6,10 @@
import com.se.simu.domain.dto.GeField;
import com.se.simu.domain.dto.GeLayer;
import lombok.extern.slf4j.Slf4j;
import org.gdal.gdal.Band;
import org.gdal.gdal.Dataset;
import org.gdal.gdal.gdal;
import org.gdal.gdalconst.gdalconst;
import org.gdal.ogr.*;
import org.gdal.osr.SpatialReference;
@@ -302,7 +304,7 @@
        return sr.GetAuthorityCode(null);
    }
    public static boolean polygonize(Dataset ds, String filePath) {
    public static boolean polygonize2Shp(Dataset ds, String filePath) {
        Driver driver = null;
        DataSource dataSource = null;
        Layer layer = null;
@@ -316,10 +318,9 @@
            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);
            layer.CreateField(new FieldDefn("val", ogr.OFTReal));
            gdal.Polygonize(ds.GetRasterBand(1), null, layer, 0, null);
            gdal.Polygonize(ds.GetRasterBand(1), ds.GetRasterBand(1).GetMaskBand(), layer, 0);
            return true;
        } catch (Exception ex) {
@@ -329,4 +330,39 @@
            GdalHelper.delete(layer, dataSource, driver);
        }
    }
    public static boolean polygonize2Geojson(Dataset ds, String filePath) {
        Driver driver = null;
        DataSource dataSource = null;
        Layer layer = null;
        try {
            driver = ogr.GetDriverByName("GeoJSON");
            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;
            layer.CreateField(new FieldDefn("val", ogr.OFTReal));
            gdal.Polygonize(ds.GetRasterBand(1), ds.GetRasterBand(1).GetMaskBand(), layer, 0);
            return true;
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return false;
        } finally {
            GdalHelper.delete(layer, dataSource, driver);
        }
    }
    public static void test() {
        String path = "D:\\simu\\out\\20241010095328\\waters\\1730217635000\\";
        Dataset ds = gdal.Open(path + "water.tif", gdalconst.GA_ReadOnly);
        ds.SetSpatialRef(GdalHelper.createSpatialReference(4548));
        polygonize2Geojson(ds, path + "water.geojson");
    }
}
src/main/java/com/se/simu/service/ResultService.java
@@ -505,8 +505,8 @@
    }
    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);
        String filePath = dto.getOutPath() + File.separator + "waters" + File.separator + ticks + File.separator + "water.geojson";
        ShpHelper.polygonize2Geojson(ds, filePath);
    }
    private void copeFlow(ResultDto dto, LayerDto layer) {