package com.se.simu.service;
|
|
import cn.hutool.core.io.FileUtil;
|
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;
|
import org.gdal.gdal.Dataset;
|
import org.gdal.gdal.WarpOptions;
|
import org.gdal.gdal.gdal;
|
import org.gdal.gdalconst.gdalconst;
|
import org.gdal.ogr.Geometry;
|
import org.springframework.stereotype.Service;
|
|
import javax.annotation.Resource;
|
import javax.imageio.ImageIO;
|
import java.awt.*;
|
import java.awt.image.BufferedImage;
|
import java.io.*;
|
import java.util.*;
|
import java.util.List;
|
|
/**
|
* 处理结果服务类
|
*
|
* @author WWW
|
* @date 2024-09-29
|
*/
|
@Slf4j
|
@Service
|
@SuppressWarnings("ALL")
|
public class ResultService {
|
@Resource
|
PropertiesConfig config;
|
|
public final static double MAX_X_OFFSET = 0.002;
|
|
public void process(DataPo data) throws Exception {
|
String basePath = config.getInPath() + File.separator + data.getInPath() + File.separator;
|
ResultDto dto = new ResultDto(
|
data.getInPath(),
|
basePath + config.getTerrainFile(),
|
basePath + config.getBuildingFile(),
|
basePath + config.getWaterPath(),
|
basePath + config.getFlowPath(),
|
config.getOutPath(),
|
"");
|
LayerDto layer = new LayerDto(config.getVer(), data.getEpsg(), config.getSizes());
|
process(dto, layer);
|
}
|
|
private void process(ResultDto dto, LayerDto layer) throws Exception {
|
try {
|
copeTerrain(dto, layer);
|
copeBuilding(dto, layer);
|
copeWater(dto, layer);
|
copeFlow(dto, layer);
|
copeLayerJson(dto, layer);
|
} finally {
|
File dir = new File(dto.getTemp());
|
if (dir.exists()) {
|
FileUtil.del(dir);
|
}
|
}
|
}
|
|
/**
|
* 地形
|
*/
|
private void copeTerrain(ResultDto dto, LayerDto layer) {
|
Dataset ds = null;
|
try {
|
ds = gdal.Open(dto.getTerrainFile(), gdalconst.GA_ReadOnly);
|
if (null == ds || 0 == ds.getRasterCount() || null == ds.GetSpatialRef()) return;
|
|
setTerrainInfo(ds, layer);
|
setWaterInfo(dto, layer);
|
createTerrainPng(dto, ds, layer);
|
} finally {
|
if (null != ds) ds.delete();
|
}
|
}
|
|
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);
|
layer.setExtension(new ExtensionDto(minx, miny, maxx, maxy, Double.MAX_VALUE, Double.MIN_VALUE));
|
|
Band band = ds.GetRasterBand(1);
|
double[] mm = new double[2];
|
band.ComputeRasterMinMax(mm, 0);
|
layer.getTerrain().setHeight(getMinVal(mm[0], 1000), getMaxVal(mm[1], 1000));
|
}
|
|
private static double getMinVal(double val, double radix) {
|
return ((long) Math.floor(val * radix)) / radix;
|
}
|
|
private static double getMaxVal(double val, double radix) {
|
return ((long) Math.ceil(val * radix)) / radix;
|
}
|
|
private void createTerrainPng(ResultDto dto, Dataset ds, LayerDto layer) {
|
String terrainPath = dto.getOutPath() + File.separator + "terrain";
|
File f = new File(terrainPath);
|
if (!f.exists() || !f.isDirectory()) f.mkdirs();
|
|
for (int[] sizes : layer.getTerrain().getSize()) {
|
String tif = dto.getTemp() + File.separator + "terrain_" + sizes[0] + "_" + sizes[1] + ".tif";
|
Resample(ds, tif, sizes[0], sizes[1], layer);
|
if (!new File(tif).exists()) continue;
|
|
String png = terrainPath + File.separator + sizes[0] + "_" + sizes[1] + ".png";
|
Terrain2Png(layer, tif, png, sizes[0], sizes[1]);
|
}
|
}
|
|
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("-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("-r");
|
vector.add("bilinear");
|
vector.add("-of");
|
vector.add("GTiff");
|
WarpOptions warpOptions = new WarpOptions(vector);
|
|
Dataset destDs = gdal.Warp(dest, new Dataset[]{ds}, warpOptions);
|
destDs.delete();
|
}
|
|
private static void Terrain2Png(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, width, height, 0, 0);
|
band.ReadRaster(0, 0, width, height, buffer);
|
|
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
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++) {
|
int offset = x + y * width;
|
if (Float.isNaN(buffer[offset]) || buffer[offset] < -999 || buffer[offset] < minHeight) continue;
|
|
int r = 0, g, b;
|
if (buffer[offset] - layer.getExtension().getMaxHeight() > 0) {
|
g = b = 255;
|
} else {
|
int val = (int) ((buffer[offset] - minHeight) / differ * 65535);
|
g = val / 256;
|
b = val % 256;
|
}
|
|
Color color = new Color(r, g, b, 127);
|
//graphic.drawImage(image, x, y, 1, 1, color, null);
|
image.setRGB(x, y, color.getRGB());
|
}
|
}
|
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) {
|
try {
|
ImageIO.write(image, "png", new File(png));
|
} catch (Exception ex) {
|
log.error(ex.getMessage(), ex);
|
}
|
}
|
|
/**
|
* 建筑
|
*/
|
private void copeBuilding(ResultDto dto, LayerDto layer) {
|
Dataset ds = null;
|
try {
|
ds = gdal.Open(dto.getBuildingFile(), gdalconst.GA_ReadOnly);
|
if (null == ds || 0 == ds.getRasterCount()) 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;
|
|
Dataset dataset = gdal.Open(tif, gdalconst.GA_ReadOnly);
|
if (null == dataset || 0 == dataset.getRasterCount()) 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);
|
|
dataset.delete();
|
}
|
} finally {
|
if (null != ds) ds.delete();
|
}
|
}
|
|
/**
|
* 水面
|
*/
|
private void setWaterInfo(ResultDto dto, LayerDto layer) {
|
List<String> files = getFiles(dto.getWaterPath(), ".tif");
|
layer.getWaters().setFiles(files);
|
if (null == files || files.size() == 0) return;
|
|
setWaterData(layer, files);
|
setWaterHeight(layer, files);
|
}
|
|
private void copeWater(ResultDto dto, LayerDto layer) {
|
List<String> files = layer.getWaters().getFiles();
|
if (files.size() == 0 || files.size() != layer.getWaters().getData().size()) return;
|
|
processWaters(dto, files, layer);
|
}
|
|
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 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 static void setWaterData(LayerDto layer, List<String> files) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.setTime(new Date());
|
calendar.set(Calendar.MILLISECOND, 0);
|
|
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));
|
|
calendar.set(Calendar.HOUR_OF_DAY, hour);
|
calendar.set(Calendar.MINUTE, minute);
|
calendar.set(Calendar.SECOND, second);
|
|
layer.getWaters().getData().add(calendar.getTime().getTime());
|
}
|
layer.getDuration().setStart(layer.getWaters().getData().get(0));
|
layer.getDuration().setEnd(layer.getWaters().getData().get(layer.getWaters().getData().size() - 1));
|
}
|
|
private static void setWaterHeight(LayerDto layer, List<String> files) {
|
files.parallelStream().forEach(file -> {
|
Dataset ds = null;
|
try {
|
ds = gdal.Open(file, gdalconst.GA_ReadOnly);
|
if (null == ds || 0 == ds.getRasterCount() || null == ds.GetSpatialRef()) return;
|
|
double[] mm = new double[2];
|
ds.GetRasterBand(1).ComputeRasterMinMax(mm, 0);
|
layer.getExtension().setHeight(mm[0], mm[1]);
|
} finally {
|
if (null != ds) ds.delete();
|
}
|
});
|
layer.getExtension().setMinHeight(getMinVal(layer.getExtension().getMinHeight() - 1, 1000));
|
layer.getExtension().setMaxHeight(getMaxVal(layer.getExtension().getMaxHeight() + 1, 1000));
|
}
|
|
private static void processWaters(ResultDto dto, List<String> files, LayerDto layer) {
|
for (int i = 0, c = files.size(); i < c; i++) {
|
Dataset ds = null;
|
try {
|
ds = gdal.Open(files.get(i), gdalconst.GA_ReadOnly);
|
if (null == ds || 0 == ds.getRasterCount() || null == ds.GetSpatialRef()) return;
|
|
createWaterPng(dto, ds, layer, layer.getWaters().getData().get(i));
|
} finally {
|
if (null != ds) ds.delete();
|
}
|
}
|
}
|
|
private static void createWaterPng(ResultDto dto, Dataset ds, LayerDto layer, long ticks) {
|
String waterPath = dto.getOutPath() + File.separator + "waters" + File.separator + ticks;
|
File dir = new File(waterPath);
|
if (!dir.exists() || !dir.isDirectory()) dir.mkdirs();
|
|
for (int[] sizes : layer.getTerrain().getSize()) {
|
String fileName = 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 static String getNameWithExt(String file) {
|
return file.substring(file.lastIndexOf(File.separator) + 1, file.lastIndexOf("."));
|
}
|
|
private static void water2Png(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, width, height, 0, 0);
|
band.ReadRaster(0, 0, width, height, buffer);
|
float[] building = dto.getBuildings().get(width + "_" + height);
|
|
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
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++) {
|
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;
|
|
int r = 0, g, b;
|
if (buffer[offset] - layer.getExtension().getMaxHeight() > 0) {
|
g = b = 255;
|
} else {
|
int val = (int) ((buffer[offset] - minHeight) / differ * 65535);
|
g = val / 256;
|
b = val % 256;
|
}
|
|
Color color = new Color(r, g, b, 127);
|
image.setRGB(x, y, color.getRGB());
|
}
|
}
|
savePng(image, png);
|
} finally {
|
if (null != ds) ds.delete();
|
}
|
}
|
|
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;
|
|
for (int i = 0, c = vxFiles.size(); i < c; i++) {
|
Dataset vxDs = null, vyDs = 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;
|
|
createFlowPng(dto, vxDs, vyDs, layer, layer.getWaters().getData().get(i));
|
} finally {
|
if (null != vxDs) vxDs.delete();
|
if (null != vyDs) vyDs.delete();
|
}
|
}
|
}
|
|
private static void createFlowPng(ResultDto dto, Dataset vxDs, Dataset vyDs, 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 png = flowPath + File.separator + sizes[0] + "_" + sizes[1] + ".png";
|
vxyTif2Png(layer, vxTif, vyTif, 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;
|
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;
|
|
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);
|
|
createFlowPng(vxBuffer, vyBuffer, png, width, height);
|
} finally {
|
if (null != vxDs) vxDs.delete();
|
if (null != vyDs) vyDs.delete();
|
}
|
}
|
|
private static void createFlowPng(float[] vxBuffer, float[] vyBuffer, String png, int width, int height) {
|
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
// 用 R通道表示,流向为归一化的二维向量(x,y),G通道表示为 x *255 , B通道表示为 y * 255
|
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]);
|
if (Float.isNaN(fx) && Float.isNaN(fy) || (fx == 0 && fy == 0)) continue;
|
|
fx = Float.isNaN(fx) ? 0 : fx;
|
fy = Float.isNaN(fy) ? 0 : fy;
|
double dr = Math.sqrt(Math.pow(fx, 2) + Math.pow(fy, 2));
|
|
int r = (int) (dr / 4 * 255);
|
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);
|
image.setRGB(x, y, color.getRGB());
|
}
|
}
|
savePng(image, png);
|
}
|
|
private static float getFloatValue(float val) {
|
return (Float.isNaN(val) || val < -999) ? Float.NaN : val;
|
}
|
|
private static int getSafeValue(int val) {
|
if (val < 0) return 0;
|
if (val > 255) return 255;
|
|
return val;
|
}
|
|
/**
|
* 元数据
|
*/
|
private void copeLayerJson(ResultDto dto, LayerDto layer) throws IOException {
|
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);
|
BufferedWriter bw = new BufferedWriter(fw);
|
bw.write(json);
|
bw.close();
|
fw.close();
|
}
|
}
|