From b6899cd6d1bafb1c349f1fbd190e6632183719f5 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期五, 01 十一月 2024 20:44:00 +0800 Subject: [PATCH] 11 --- src/main/java/com/se/simu/service/ResultService.java | 231 +++++++++++++++++++++++++++++++++++---------------------- 1 files changed, 142 insertions(+), 89 deletions(-) diff --git a/src/main/java/com/se/simu/service/ResultService.java b/src/main/java/com/se/simu/service/ResultService.java index 772ec93..b7bd2dd 100644 --- a/src/main/java/com/se/simu/service/ResultService.java +++ b/src/main/java/com/se/simu/service/ResultService.java @@ -3,10 +3,12 @@ import cn.hutool.core.io.FileUtil; import com.alibaba.fastjson.JSON; import com.se.simu.config.PropertiesConfig; +import com.se.simu.domain.dto.BuildingDto; import com.se.simu.domain.dto.ExtensionDto; import com.se.simu.domain.dto.LayerDto; import com.se.simu.domain.dto.ResultDto; import com.se.simu.domain.po.DataPo; +import com.se.simu.domain.vo.BuildingDepthVo; import com.se.simu.helper.GdalHelper; import lombok.extern.slf4j.Slf4j; import org.gdal.gdal.Band; @@ -14,7 +16,7 @@ import org.gdal.gdal.WarpOptions; import org.gdal.gdal.gdal; import org.gdal.gdalconst.gdalconst; -import org.gdal.ogr.Geometry; +import org.gdal.ogr.*; import org.springframework.stereotype.Service; import javax.annotation.Resource; @@ -22,6 +24,10 @@ import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.*; import java.util.List; @@ -38,22 +44,9 @@ @Resource PropertiesConfig config; - public final static double MAX_X_OFFSET = 0.002; + public final static double MAX_X_OFFSET = 0; - public String testResuslt() throws Exception { - ResultDto dto = new ResultDto( - "202409", - "D:\\simu\\input\\tongzhou-local-mesh2-terrain.tif", - "D:\\simu\\input\\tongzhou-local-mesh2-buildings.tif", - "D:\\simu\\input\\waters", - "D:\\simu\\input\\flows", - "D:\\simu\\out", - ""); - LayerDto layer = new LayerDto(config.getVer(), 4528, config.getSizes()); - process(dto, layer); - - return "OK"; - } + private final static SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm"); public void process(DataPo data) throws Exception { String basePath = config.getInPath() + File.separator + data.getInPath() + File.separator; @@ -63,8 +56,7 @@ basePath + config.getBuildingFile(), basePath + config.getWaterPath(), basePath + config.getFlowPath(), - config.getOutPath(), - ""); + config.getOutPath()); LayerDto layer = new LayerDto(config.getVer(), data.getEpsg(), config.getSizes()); process(dto, layer); } @@ -76,6 +68,7 @@ copeWater(dto, layer); copeFlow(dto, layer); copeLayerJson(dto, layer); + copeRainFallJson(dto, layer); } finally { File dir = new File(dto.getTemp()); if (dir.exists()) { @@ -91,7 +84,7 @@ Dataset ds = null; try { ds = gdal.Open(dto.getTerrainFile(), gdalconst.GA_ReadOnly); - if (null == ds || 0 == ds.getRasterCount() || null == ds.GetSpatialRef()) return; + if (null == ds || 0 == ds.getRasterCount()) return; setTerrainInfo(ds, layer); setWaterInfo(dto, layer); @@ -139,20 +132,25 @@ } } + /** + * 閲嶉噰鏍� + */ private static void Resample(Dataset ds, String dest, int width, int height, LayerDto layer) { Vector<String> vector = new Vector<>(); - vector.add("-t_srs"); - vector.add("EPSG:" + 4326); + //vector.add("-s_srs"); + //vector.add("EPSG:" + 4548); + //vector.add("-t_srs"); + //vector.add("EPSG:" + 4326); vector.add("-ts"); vector.add("" + width); vector.add("" + height); - vector.add("-te"); - vector.add("" + layer.getExtension().getMinx()); - vector.add("" + layer.getExtension().getMiny()); - vector.add("" + layer.getExtension().getMaxx()); - vector.add("" + layer.getExtension().getMaxy()); - vector.add("-te_srs"); - vector.add("EPSG:" + 4326); + //vector.add("-te"); + //vector.add("" + layer.getExtension().getMinx()); + //vector.add("" + layer.getExtension().getMiny()); + //vector.add("" + layer.getExtension().getMaxx()); + //vector.add("" + layer.getExtension().getMaxy()); + //vector.add("-te_srs"); + //vector.add("EPSG:" + 4326); vector.add("-r"); vector.add("bilinear"); vector.add("-of"); @@ -224,28 +222,31 @@ * 寤虹瓚 */ private void copeBuilding(ResultDto dto, LayerDto layer) { - Dataset ds = null; + Driver driver = null; + DataSource dataSource = null; + Layer shpLayer = null; try { - ds = gdal.Open(dto.getBuildingFile(), gdalconst.GA_ReadOnly); - if (null == ds || 0 == ds.getRasterCount()) return; + driver = ogr.GetDriverByName("ESRI shapefile"); + if (null == driver) return; - for (int[] sizes : layer.getTerrain().getSize()) { - String tif = dto.getTemp() + File.separator + "building_" + sizes[0] + "_" + sizes[1] + ".tif"; - Resample(ds, tif, sizes[0], sizes[1], layer); - if (!new File(tif).exists()) continue; + dataSource = driver.Open(dto.getBuildingFile()); + if (null == dataSource) return; - Dataset dataset = gdal.Open(tif, gdalconst.GA_ReadOnly); - if (null == dataset || 0 == dataset.getRasterCount()) return; + shpLayer = dataSource.GetLayer(0); + if (shpLayer.GetFeatureCount() < 1) return; - float[] buffer = new float[sizes[0] * sizes[1]]; - //dataset.GetRasterBand(1).ReadRaster(0, 0, sizes[0], sizes[1], buffer, sizes[0], sizes[1], 0, 0); - dataset.GetRasterBand(1).ReadRaster(0, 0, sizes[0], sizes[1], buffer); - dto.getBuildings().put(sizes[0] + "_" + sizes[1], buffer); + do { + Feature f = shpLayer.GetNextFeature(); + if (null == f) break; - dataset.delete(); - } + String id = f.GetFieldAsString(config.getBuildingKey()); + BuildingDto building = new BuildingDto(id, f.GetGeometryRef()); + dto.getBuildingList().add(building); + } while (true); } finally { - if (null != ds) ds.delete(); + if (null != shpLayer) shpLayer.delete(); + if (null != dataSource) dataSource.delete(); + if (null != driver) driver.delete(); } } @@ -304,10 +305,16 @@ for (String file : files) { String fileName = getNameWithExt(file); - int hour = Integer.parseInt(fileName.substring(0, 2)); - int minute = Integer.parseInt(fileName.substring(2, 4)); - int second = Integer.parseInt(fileName.substring(4, 6)); + int year = Integer.parseInt(fileName.substring(0, 4)); + int month = Integer.parseInt(fileName.substring(4, 6)); + int day = Integer.parseInt(fileName.substring(6, 8)); + int hour = Integer.parseInt(fileName.substring(8, 10)); + int minute = Integer.parseInt(fileName.substring(10, 12)); + int second = Integer.parseInt(fileName.substring(12, 14)); + calendar.set(Calendar.YEAR, year); + calendar.set(Calendar.MONTH, month); + calendar.set(Calendar.DAY_OF_MONTH, day); calendar.set(Calendar.HOUR_OF_DAY, hour); calendar.set(Calendar.MINUTE, minute); calendar.set(Calendar.SECOND, second); @@ -323,7 +330,7 @@ Dataset ds = null; try { ds = gdal.Open(file, gdalconst.GA_ReadOnly); - if (null == ds || 0 == ds.getRasterCount() || null == ds.GetSpatialRef()) return; + if (null == ds || 0 == ds.getRasterCount()) return; double[] mm = new double[2]; ds.GetRasterBand(1).ComputeRasterMinMax(mm, 0); @@ -341,9 +348,10 @@ Dataset ds = null; try { ds = gdal.Open(files.get(i), gdalconst.GA_ReadOnly); - if (null == ds || 0 == ds.getRasterCount() || null == ds.GetSpatialRef()) return; + if (null == ds || 0 == ds.getRasterCount()) return; createWaterPng(dto, ds, layer, layer.getWaters().getData().get(i)); + copyWaterTif(dto, ds, layer.getWaters().getData().get(i)); } finally { if (null != ds) ds.delete(); } @@ -366,6 +374,12 @@ } } + 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) { return file.substring(file.lastIndexOf(File.separator) + 1, file.lastIndexOf(".")); } @@ -378,9 +392,8 @@ Band band = ds.GetRasterBand(1); float[] buffer = new float[width * height]; - //band.ReadRaster(0, 0, width, height, buffer, width, height, 0, 0); band.ReadRaster(0, 0, width, height, buffer); - float[] building = dto.getBuildings().get(width + "_" + height); + double[] transform = ds.GetGeoTransform(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); double differ = layer.getExtension().getMaxHeight() - layer.getExtension().getMinHeight(), minHeight = layer.getExtension().getMinHeight(); @@ -388,7 +401,11 @@ 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(building[offset]) && building[offset] > -999) continue; + + double X = transform[0] + x * transform[1] + y * transform[2]; + double Y = transform[3] + x * transform[4] + y * transform[5]; + //BuildingDto building = intersects(dto, X, Y); + //if (null != building) continue; int r = 0, g, b; if (buffer[offset] - layer.getExtension().getMaxHeight() > 0) { @@ -409,68 +426,64 @@ } } + /** + * 鏄惁鐩镐氦 + * https://blog.csdn.net/flyingshineangel/article/details/135423025 + */ + private static BuildingDto intersects(ResultDto dto, double x, double y) { + Geometry p = new Geometry(ogr.wkbPoint); + p.AddPoint_2D(x, y); + p.AssignSpatialReference(dto.getBuildingList().get(0).getGeom().GetSpatialReference()); + + return dto.getBuildingList().parallelStream().filter(b -> b.getGeom().Intersects(p)).findFirst().orElse(null); + } + private void copeFlow(ResultDto dto, LayerDto layer) { - List<String> vxFiles = new ArrayList<>(); - List<String> vyFiles = new ArrayList<>(); List<String> files = getFiles(dto.getFlowPath(), ".tif"); - for (String file : files) { - if (file.contains(File.separator + "vx")) vxFiles.add(file); - if (file.contains(File.separator + "vy")) vyFiles.add(file); - } - if (null == vxFiles || null == vyFiles || vxFiles.size() != vyFiles.size() || vxFiles.size() != layer.getWaters().getData().size()) - return; + if (null == files || files.size() != layer.getWaters().getData().size()) return; - for (int i = 0, c = vxFiles.size(); i < c; i++) { - Dataset vxDs = null, vyDs = null; + for (int i = 0, c = files.size(); i < c; i++) { + Dataset ds = null; try { - vxDs = gdal.Open(vxFiles.get(i), gdalconst.GA_ReadOnly); - vyDs = gdal.Open(vyFiles.get(i), gdalconst.GA_ReadOnly); - if (null == vxDs || 0 == vxDs.getRasterCount() || null == vxDs.GetSpatialRef() || null == vyDs || 0 == vyDs.getRasterCount() || null == vyDs.GetSpatialRef()) - return; + ds = gdal.Open(files.get(i), gdalconst.GA_ReadOnly); + if (null == ds || 0 == ds.getRasterCount()) return; - createFlowPng(dto, vxDs, vyDs, layer, layer.getWaters().getData().get(i)); + createFlowPng(dto, ds, layer, layer.getWaters().getData().get(i)); } finally { - if (null != vxDs) vxDs.delete(); - if (null != vyDs) vyDs.delete(); + if (null != ds) ds.delete(); } } } - private static void createFlowPng(ResultDto dto, Dataset vxDs, Dataset vyDs, LayerDto layer, long ticks) { + private static void createFlowPng(ResultDto dto, Dataset ds, LayerDto layer, long ticks) { String flowPath = dto.getOutPath() + File.separator + "flows" + File.separator + ticks; File dir = new File(flowPath); if (!dir.exists() || !dir.isDirectory()) dir.mkdirs(); for (int[] sizes : layer.getTerrain().getSize()) { - String vxName = getNameWithExt(vxDs.GetDescription()) + "_" + sizes[0] + "_" + sizes[1]; - String vxTif = dto.getTemp() + File.separator + vxName + ".tif"; - Resample(vxDs, vxTif, sizes[0], sizes[1], layer); - - String vyName = getNameWithExt(vyDs.GetDescription()) + "_" + sizes[0] + "_" + sizes[1]; - String vyTif = dto.getTemp() + File.separator + vyName + ".tif"; - Resample(vyDs, vyTif, sizes[0], sizes[1], layer); - if (!new File(vxTif).exists() || !new File(vyTif).exists()) continue; + String name = getNameWithExt(ds.GetDescription()) + "_" + sizes[0] + "_" + sizes[1]; + String tif = dto.getTemp() + File.separator + name + ".tif"; + Resample(ds, tif, sizes[0], sizes[1], layer); + if (!new File(tif).exists()) continue; String png = flowPath + File.separator + sizes[0] + "_" + sizes[1] + ".png"; - vxyTif2Png(layer, vxTif, vyTif, png, sizes[0], sizes[1]); + vxyTif2Png(layer, tif, png, sizes[0], sizes[1]); } } - private static void vxyTif2Png(LayerDto layer, String vxTif, String vyTif, String png, int width, int height) { - Dataset vxDs = null, vyDs = null; + private static void vxyTif2Png(LayerDto layer, String tif, String png, int width, int height) { + Dataset ds = null; try { - vxDs = gdal.Open(vxTif, gdalconst.GA_ReadOnly); - vyDs = gdal.Open(vyTif, gdalconst.GA_ReadOnly); - if (null == vxDs || 0 == vxDs.getRasterCount() || null == vyDs || 0 == vyDs.getRasterCount()) return; + ds = gdal.Open(tif, gdalconst.GA_ReadOnly); + if (null == ds || 0 == ds.getRasterCount()) return; float[] vxBuffer = new float[width * height], vyBuffer = new float[width * height]; - vxDs.GetRasterBand(1).ReadRaster(0, 0, width, height, vxBuffer); - vyDs.GetRasterBand(1).ReadRaster(0, 0, width, height, vyBuffer); + ds.GetRasterBand(1).ReadRaster(0, 0, width, height, vxBuffer); + ds.GetRasterBand(2).ReadRaster(0, 0, width, height, vyBuffer); createFlowPng(vxBuffer, vyBuffer, png, width, height); } finally { - if (null != vxDs) vxDs.delete(); - if (null != vyDs) vyDs.delete(); + if (null != ds) ds.delete(); } } @@ -515,10 +528,50 @@ */ private void copeLayerJson(ResultDto dto, LayerDto layer) throws IOException { layer.getWaters().setFiles(null); - //String json = JSONUtil.toJsonPrettyStr(layer); + layer.getTerrain().setEpsg(null); + String json = JSON.toJSONString(layer); + // String json = JSONUtil.toJsonPrettyStr(layer); String filePath = dto.getOutPath() + File.separator + "layer.json"; + writeJson(filePath, json); + } + + /** + * 澶勭悊闄嶆按鏇茬嚎鏂囦欢鏇茬嚎鍥� + */ + public void copeRainFallJson(ResultDto dto, LayerDto layer) throws IOException, ParseException { + String rainGageFilePath = config.getInPath() + File.separator + dto.getServiceName() + File.separator + "RainGage.dat"; + String filePath = dto.getOutPath() + File.separator + "rainfall.json"; + + Map<String, Double> map = new LinkedHashMap<>(); + FileReader fr = new FileReader(rainGageFilePath); + BufferedReader br = new BufferedReader(fr); + + String line = br.readLine(); + while ((line = br.readLine()) != null) { + String[] rainFall = line.split(" "); + if (rainFall.length < 7) continue; + + String sdt = rainFall[1] + "-" + rainFall[2] + "-" + rainFall[3] + " " + rainFall[4] + ":" + rainFall[5]; + BigDecimal num = new BigDecimal(rainFall[6]); + map.put("" + SDF.parse(sdt).getTime(), num.setScale(2, RoundingMode.HALF_UP).doubleValue()); + } + br.close(); + fr.close(); + + String json = JSON.toJSONString(map); + writeJson(filePath, json); + } + + private void copeBuildingDepthJson(ResultDto dto, List<BuildingDepthVo> list) throws IOException { + String json = JSON.toJSONString(list); + String filePath = dto.getOutPath() + File.separator + "building.json"; + + writeJson(filePath, json); + } + + private void writeJson(String filePath, String json) throws IOException { FileWriter fw = new FileWriter(filePath); BufferedWriter bw = new BufferedWriter(fw); bw.write(json); -- Gitblit v1.9.3