From 90d2ba0bc9d6b862bfe4eed8095522f67a19c9ae Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期日, 17 十一月 2024 10:03:59 +0800 Subject: [PATCH] 1 --- src/main/java/com/se/simu/service/ResultService.java | 190 +++++----------------------------------------- 1 files changed, 23 insertions(+), 167 deletions(-) diff --git a/src/main/java/com/se/simu/service/ResultService.java b/src/main/java/com/se/simu/service/ResultService.java index abb97ea..9c92150 100644 --- a/src/main/java/com/se/simu/service/ResultService.java +++ b/src/main/java/com/se/simu/service/ResultService.java @@ -7,6 +7,7 @@ import com.se.simu.domain.po.DataPo; import com.se.simu.domain.po.PondingPo; import com.se.simu.domain.vo.BuildingDepthVo; +import com.se.simu.helper.ComHelper; import com.se.simu.helper.GdalHelper; import com.se.simu.helper.ShpHelper; import lombok.extern.slf4j.Slf4j; @@ -20,7 +21,6 @@ import org.springframework.util.CollectionUtils; import javax.annotation.Resource; -import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; @@ -91,10 +91,10 @@ private void setTerrainInfo(Dataset ds, LayerDto layer) { Geometry minPoint = GdalHelper.getMinPoint(ds); Geometry maxPoint = GdalHelper.getMaxPoint(ds); - double minx = getMinVal(minPoint.GetX(0), 10000000); - double miny = getMinVal(minPoint.GetY(0), 10000000); - double maxx = getMaxVal(maxPoint.GetX(0) + MAX_X_OFFSET, 10000000); - double maxy = getMaxVal(maxPoint.GetY(0), 10000000); + double minx = ComHelper.getMinVal(minPoint.GetX(0), 10000000); + double miny = ComHelper.getMinVal(minPoint.GetY(0), 10000000); + double maxx = ComHelper.getMaxVal(maxPoint.GetX(0) + MAX_X_OFFSET, 10000000); + double maxy = ComHelper.getMaxVal(maxPoint.GetY(0), 10000000); //layer.setExtension(new ExtensionDto(minx, miny, maxx, maxy, Double.MAX_VALUE, Double.MIN_VALUE)); Band band = ds.GetRasterBand(1); @@ -102,14 +102,6 @@ band.ComputeRasterMinMax(mm, 0); //layer.getTerrain().setHeight(getMinVal(mm[0], 1000), getMaxVal(mm[1], 1000)); layer.setExtension(new ExtensionDto(minx, miny, maxx, maxy, mm[0], mm[1])); - } - - private double getMinVal(double val, double radix) { - return ((long) Math.floor(val * radix)) / radix; - } - - private double getMaxVal(double val, double radix) { - return ((long) Math.ceil(val * radix)) / radix; } private void createTerrainPng(ResultDto dto, Dataset ds, LayerDto layer) { @@ -186,28 +178,9 @@ image.setRGB(x, y, color.getRGB()); } } - savePng(image, png); + ComHelper.savePng(image, png); } finally { if (null != ds) ds.delete(); - } - } - - private BufferedImage createImage(int width, int height) { - BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); - Graphics2D graphic = image.createGraphics(); - Color transparent = new Color(0, 0, 0, 0); - graphic.setColor(transparent); - graphic.clearRect(0, 0, width, height); - graphic.dispose(); - - return image; - } - - private void savePng(BufferedImage image, String png) { - try { - ImageIO.write(image, "png", new File(png)); - } catch (Exception ex) { - log.error(ex.getMessage(), ex); } } @@ -260,31 +233,10 @@ private List<String> getFiles(String path, String suffix) { List<String> files = new ArrayList<>(); - getFiles(files, new File(path), suffix); + ComHelper.getFiles(files, new File(path), suffix); files.sort((a, b) -> a.compareToIgnoreCase(b)); return files; - } - - private void getFiles(List<String> files, File file, String suffix) { - if (!file.exists()) return; - - if (file.isDirectory()) { - File[] fileList = file.listFiles(); - for (File f : fileList) { - if (f.isDirectory()) { - getFiles(files, f, suffix); - } else { - if (f.getName().toLowerCase().endsWith(suffix)) { - files.add(f.getPath()); - } - } - } - } else { - if (file.getName().toLowerCase().endsWith(suffix)) { - files.add(file.getPath()); - } - } } private void setWaterData(LayerDto layer, List<String> files) { @@ -293,7 +245,7 @@ calendar.set(Calendar.MILLISECOND, 0); for (String file : files) { - String fileName = getNameWithExt(file); + String fileName = ComHelper.getNameWithExt(file); int year = Integer.parseInt(fileName.substring(0, 4)); int month = Integer.parseInt(fileName.substring(4, 6)); int day = Integer.parseInt(fileName.substring(6, 8)); @@ -334,8 +286,8 @@ //layer.getWaters().setMinHeight(getMinVal(layer.getWaters().getMinHeight() - 1, 1000)); //layer.getWaters().setMaxHeight(getMaxVal(layer.getWaters().getMaxHeight() + 1, 1000)); layer.getExtension().setMaxHeight(layer.getExtension().getMaxHeight() + layer.getWaters().getMaxHeight()); - layer.getExtension().setMaxHeight(getMaxVal(layer.getExtension().getMaxHeight(), 1000000)); - layer.getExtension().setMinHeight(getMaxVal(layer.getExtension().getMinHeight(), 1000000)); + layer.getExtension().setMaxHeight(ComHelper.getMaxVal(layer.getExtension().getMaxHeight(), 1000000)); + layer.getExtension().setMinHeight(ComHelper.getMaxVal(layer.getExtension().getMinHeight(), 1000000)); layer.getExtension().setDiffer(); } @@ -362,57 +314,13 @@ if (!dir.exists() || !dir.isDirectory()) dir.mkdirs(); for (int[] sizes : layer.getTerrain().getSize()) { - String fileName = getNameWithExt(ds.GetDescription()) + "_" + sizes[0] + "_" + sizes[1]; + String fileName = ComHelper.getNameWithExt(ds.GetDescription()) + "_" + sizes[0] + "_" + sizes[1]; String tif = dto.getTemp() + File.separator + fileName + ".tif"; Resample(ds, tif, sizes[0], sizes[1], layer); if (!new File(tif).exists()) continue; String png = waterPath + File.separator + sizes[0] + "_" + sizes[1] + ".png"; water2Png(dto, layer, tif, png, sizes[0], sizes[1]); - } - } - - private String getNameWithExt(String file) { - return file.substring(file.lastIndexOf(File.separator) + 1, file.lastIndexOf(".")); - } - - private void water2Png2(ResultDto dto, LayerDto layer, String tif, String png, int width, int height) { - Dataset ds = null; - try { - ds = gdal.Open(tif, gdalconst.GA_ReadOnly); - if (null == ds || 0 == ds.getRasterCount()) return; - - Band band = ds.GetRasterBand(1); - float[] buffer = new float[width * height]; - band.ReadRaster(0, 0, width, height, buffer); - - BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); - double differ = layer.getExtension().getDiffer(), maxHeight = layer.getExtension().getMaxHeight(), minHeight = layer.getExtension().getMinHeight(); - float[] ts = layer.getTerrain().getVals().get(width + "_" + height); - - /*layer.getTerrain().getXyo().get(width + "_" + height).stream().forEach(xyo -> { - - });*/ - for (XYO xyo : layer.getTerrain().getXyo().get(width + "_" + height)) { - float depth = buffer[xyo.getOffset()] + ts[xyo.getOffset()]; - if (Float.isNaN(depth) || depth < minHeight) continue; - - int r = 0, g, b; - if (depth > maxHeight) { - g = b = 255; - } else { - int val = (int) ((depth - minHeight) / differ * 65535); - g = val / 256; - b = val % 256; - } - - Color color = new Color(r, g, b, 127); - image.setRGB(xyo.getX(), xyo.getY(), color.getRGB()); - } - - savePng(image, png); - } finally { - if (null != ds) ds.delete(); } } @@ -456,18 +364,10 @@ image.setRGB(x, y, color.getRGB()); } } - savePng(image, png); + ComHelper.savePng(image, png); } finally { if (null != ds) ds.delete(); } - } - - private 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 List<BuildingDepthVo> processBuilding(ResultDto dto, List<String> files, LayerDto layer) { @@ -520,11 +420,7 @@ Double val = Arrays.stream(pixelValues).max().getAsDouble(); - return isValid(val) ? val : null; - } - - public boolean isValid(Double val) { - return !Double.isNaN(val) && val > Integer.MIN_VALUE; + return ComHelper.isValid(val) ? val : null; } private void copyWaterTif(ResultDto dto, Dataset ds, long ticks) { @@ -545,7 +441,7 @@ try { filePath = dto.getOutPath() + File.separator + "waters" + File.separator + ticks + File.separator + "water.json"; - writeJson(filePath, JSON.toJSONString(list)); + ComHelper.writeJson(filePath, JSON.toJSONString(list)); } catch (Exception ex) { log.error(ex.getMessage(), ex); } @@ -612,19 +508,6 @@ if (endY > ds.getRasterYSize()) endY = ds.getRasterYSize(); if (endX - startX < 1 || endY - startY < 1) return null; - /*float[] values = new float[1]; - List<PointDto> points = new ArrayList<>(); - for (int x = startX; x <= endX; x++) { - for (int y = startY; y <= endY; y++) { - double X = transform[0] + x * transform[1] + y * transform[2]; - double Y = transform[3] + x * transform[4] + y * transform[5]; - ds.GetRasterBand(1).ReadRaster(x, y, 1, 1, values); - - if (Float.isNaN(values[0]) || values[0] < -999 || !isContains(g, X, Y)) continue; - points.add(new PointDto(X, Y, values[0])); - } - }*/ - List<XYDto> xyList = new ArrayList<>(); for (int x = startX; x <= endX; x++) { for (int y = startY; y <= endY; y++) { @@ -639,20 +522,12 @@ float[] values = new float[1]; ds.GetRasterBand(1).ReadRaster(xy.getX(), xy.getY(), 1, 1, values); - if (Float.isNaN(values[0]) || values[0] < -999 || !isContains(g, X, Y)) return; + if (Float.isNaN(values[0]) || values[0] < -999 || !ComHelper.isContains(g, X, Y)) return; points.add(new PointDto(X, Y, values[0])); }); return points; - } - - private boolean isContains(Geometry g, double x, double y) { - Geometry p = new Geometry(ogr.wkbPoint); - p.AddPoint_2D(x, y); - p.AssignSpatialReference(g.GetSpatialReference()); - - return g.Contains(p); } private void copeFlow(ResultDto dto, LayerDto layer) { @@ -679,7 +554,7 @@ if (!dir.exists() || !dir.isDirectory()) dir.mkdirs(); for (int[] sizes : layer.getTerrain().getSize()) { - String name = getNameWithExt(ds.GetDescription()) + "_" + sizes[0] + "_" + sizes[1]; + String name = ComHelper.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; @@ -710,8 +585,8 @@ for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { int offset = x + y * width; - float fx = getFloatValue(vxBuffer[offset]); - float fy = getFloatValue(vyBuffer[offset]); + float fx = ComHelper.getFloatValue(vxBuffer[offset]); + float fy = ComHelper.getFloatValue(vyBuffer[offset]); if (Float.isNaN(fx) && Float.isNaN(fy) || (fx == 0 && fy == 0)) continue; fx = Float.isNaN(fx) ? 0 : fx; @@ -722,22 +597,11 @@ int g = (int) ((fx / dr * 0.5 + 0.5) * 255); int b = (int) ((fy / dr * 0.5 + 0.5) * 255); - Color color = new Color(getSafeValue(r), getSafeValue(g), getSafeValue(b), 127); + Color color = new Color(ComHelper.getSafeValue(r), ComHelper.getSafeValue(g), ComHelper.getSafeValue(b), 127); image.setRGB(x, y, color.getRGB()); } } - savePng(image, png); - } - - private float getFloatValue(float val) { - return (Float.isNaN(val) || val < -999) ? Float.NaN : val; - } - - private int getSafeValue(int val) { - if (val < 0) return 0; - if (val > 255) return 255; - - return val; + ComHelper.savePng(image, png); } private void copeLayerJson(ResultDto dto, LayerDto layer) throws IOException { @@ -749,7 +613,7 @@ // String json = JSONUtil.toJsonPrettyStr(layer); String filePath = dto.getOutPath() + File.separator + "layer.json"; - writeJson(filePath, json); + ComHelper.writeJson(filePath, json); } public void copeRainFallJson(ResultDto dto, LayerDto layer) throws IOException, ParseException { @@ -773,7 +637,7 @@ fr.close(); String json = JSON.toJSONString(map); - writeJson(filePath, json); + ComHelper.writeJson(filePath, json); } private void copeBuildingDepthJson(ResultDto dto, List<BuildingDepthVo> list) throws IOException { @@ -782,14 +646,6 @@ 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); - bw.close(); - fw.close(); + ComHelper.writeJson(filePath, json); } } -- Gitblit v1.9.3