| | |
| | | package com.se.simu.service; |
| | | |
| | | import cn.hutool.core.io.FileUtil; |
| | | import cn.hutool.json.JSONUtil; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.se.simu.config.PropertiesConfig; |
| | | 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.helper.GdalHelper; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.gdal.gdal.Band; |
| | |
| | | |
| | | public final static double MAX_X_OFFSET = 0.002; |
| | | |
| | | public String test() throws Exception { |
| | | public void process(DataPo data) throws Exception { |
| | | String basePath = config.getInPath() + File.separator + data.getInPath() + File.separator; |
| | | 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", |
| | | data.getInPath(), |
| | | basePath + config.getTerrainFile(), |
| | | basePath + config.getBuildingFile(), |
| | | basePath + config.getWaterPath(), |
| | | basePath + config.getFlowPath(), |
| | | config.getOutPath(), |
| | | ""); |
| | | process(dto); |
| | | |
| | | return "OK"; |
| | | LayerDto layer = new LayerDto(config.getVer(), data.getEpsg(), config.getSizes()); |
| | | process(dto, layer); |
| | | } |
| | | |
| | | public void process(ResultDto dto) throws Exception { |
| | | private void process(ResultDto dto, LayerDto layer) throws Exception { |
| | | try { |
| | | LayerDto layer = new LayerDto(config.getVer()); |
| | | |
| | | copeTerrain(dto, layer); |
| | | copeBuilding(dto, layer); |
| | | copeWater(dto, layer); |
| | |
| | | |
| | | private static void Resample(Dataset ds, String dest, int width, int height, LayerDto layer) { |
| | | Vector<String> vector = new Vector<>(); |
| | | //vector.add("-s_srs"); |
| | | //vector.add("EPSG:" + 4326); |
| | | vector.add("-t_srs"); |
| | | vector.add("EPSG:" + 4326); |
| | | vector.add("-r"); |
| | | vector.add("bilinear"); |
| | | vector.add("-of"); |
| | | vector.add("GTiff"); |
| | | 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().getMaxy()); |
| | | vector.add("-te_srs"); |
| | | vector.add("EPSG:" + 4326); |
| | | vector.add("-r"); |
| | | vector.add("bilinear"); |
| | | vector.add("-of"); |
| | | vector.add("GTiff"); |
| | | WarpOptions warpOptions = new WarpOptions(vector); |
| | | |
| | | Dataset destDs = gdal.Warp(dest, new Dataset[]{ds}, warpOptions); |
| | |
| | | band.ReadRaster(0, 0, width, height, buffer); |
| | | |
| | | 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); |
| | | |
| | | double differ = layer.getExtension().getMaxHeight() - layer.getExtension().getMinHeight(), minHeight = layer.getExtension().getMinHeight(); |
| | | for (int x = 0; x < width; x++) { |
| | | for (int y = 0; y < height; y++) { |
| | |
| | | } |
| | | |
| | | Color color = new Color(r, g, b, 127); |
| | | //image.setRGB(x, y, color.getRGB()); |
| | | graphic.drawImage(image, x, y, 1, 1, color, null); |
| | | //graphic.drawImage(image, x, y, 1, 1, color, null); |
| | | image.setRGB(x, y, color.getRGB()); |
| | | } |
| | | } |
| | | graphic.dispose(); |
| | | savePng(image, png); |
| | | } finally { |
| | | if (null != ds) ds.delete(); |
| | | } |
| | | } |
| | | |
| | | private 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; |
| | | } |
| | | |
| | | private static void savePng(BufferedImage image, String png) { |
| | |
| | | private static List<String> getFiles(String path, String suffix) { |
| | | List<String> files = new ArrayList<>(); |
| | | getFiles(files, new File(path), suffix); |
| | | files.sort((a, b) -> a.compareToIgnoreCase(b)); |
| | | |
| | | return files; |
| | | } |
| | |
| | | } |
| | | |
| | | private static void setWaterHeight(LayerDto layer, List<String> files) { |
| | | files.parallelStream().forEach(file->{ |
| | | files.parallelStream().forEach(file -> { |
| | | Dataset ds = null; |
| | | try { |
| | | ds = gdal.Open(file, gdalconst.GA_ReadOnly); |
| | |
| | | float[] building = dto.getBuildings().get(width + "_" + 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); |
| | | |
| | | double differ = layer.getExtension().getMaxHeight() - layer.getExtension().getMinHeight(), minHeight = layer.getExtension().getMinHeight(); |
| | | for (int x = 0; x < width; x++) { |
| | | for (int y = 0; y < height; y++) { |
| | |
| | | } |
| | | |
| | | Color color = new Color(r, g, b, 127); |
| | | //image.setRGB(x, y, color.getRGB()); |
| | | graphic.drawImage(image, x, y, 1, 1, color, null); |
| | | image.setRGB(x, y, color.getRGB()); |
| | | } |
| | | } |
| | | graphic.dispose(); |
| | | savePng(image, png); |
| | | } finally { |
| | | if (null != ds) ds.delete(); |
| | |
| | | List<String> vyFiles = new ArrayList<>(); |
| | | List<String> files = getFiles(dto.getFlowPath(), ".tif"); |
| | | for (String file : files) { |
| | | if (file.startsWith("vx")) vxFiles.add(file); |
| | | if (file.startsWith("vy")) vyFiles.add(file); |
| | | 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; |
| | |
| | | |
| | | private static void createFlowPng(float[] vxBuffer, float[] vyBuffer, String png, 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); |
| | | |
| | | // 用 R通道表示,流向为归一化的二维向量(x,y),G通道表示为 x *255 , B通道表示为 y * 255 |
| | | for (int x = 0; x < width; x++) { |
| | | for (int y = 0; y < height; y++) { |
| | |
| | | int b = (int) ((fy / dr * 0.5 + 0.5) * 255); |
| | | |
| | | Color color = new Color(getSafeValue(r), getSafeValue(g), getSafeValue(b), 127); |
| | | //image.setRGB(x, y, color.getRGB()); |
| | | graphic.drawImage(image, x, y, 1, 1, color, null); |
| | | image.setRGB(x, y, color.getRGB()); |
| | | } |
| | | } |
| | | graphic.dispose(); |
| | | savePng(image, png); |
| | | } |
| | | |
| | |
| | | * 元数据 |
| | | */ |
| | | private void copeLayerJson(ResultDto dto, LayerDto layer) throws IOException { |
| | | String json = JSONUtil.toJsonPrettyStr(layer); |
| | | layer.getWaters().setFiles(null); |
| | | //String json = JSONUtil.toJsonPrettyStr(layer); |
| | | String json = JSON.toJSONString(layer); |
| | | String filePath = dto.getOutPath() + File.separator + "layer.json"; |
| | | |
| | | FileWriter fw = new FileWriter(filePath); |