package com.se.simu.helper; import com.se.simu.domain.dto.*; 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.Geometry; import org.gdal.ogr.ogr; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; @Slf4j @SuppressWarnings("ALL") public class ComHelper { public static 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; } public static void savePng(BufferedImage image, String png) { try { ImageIO.write(image, "png", new File(png)); } catch (Exception ex) { log.error(ex.getMessage(), ex); } } public 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); } public static 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); } public static double getMinVal(double val, double radix) { return ((long) Math.floor(val * radix)) / radix; } public static double getMaxVal(double val, double radix) { return ((long) Math.ceil(val * radix)) / radix; } public static float getFloatValue(float val) { return (Float.isNaN(val) || val < -999) ? Float.NaN : val; } public static int getSafeValue(int val) { if (val < 0) return 0; if (val > 255) return 255; return val; } public static boolean isValid(Double val) { return !Double.isNaN(val) && val > Integer.MIN_VALUE; } public static void getFiles(List 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()); } } } public static String getNameWithExt(String file) { return file.substring(file.lastIndexOf(File.separator) + 1, file.lastIndexOf(".")); } public static 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(); } private List getValues(Dataset ds, Geometry g, double[] transform, int xSize, int ySize) { double[] env = new double[4]; g.GetEnvelope(env); int startX = (int) Math.floor((env[0] - transform[0]) / transform[1]); int endX = (int) Math.floor((env[1] - transform[0]) / transform[1]); int startY = (int) Math.floor((transform[3] - env[3]) / Math.abs(transform[5])); int endY = (int) Math.floor((transform[3] - env[2]) / Math.abs(transform[5])); 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; float[] values = new float[1]; List 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])); } } return null; } 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(); } } }