From 68372fd1905515c5c8dbb9e3b158241994439e06 Mon Sep 17 00:00:00 2001 From: dcb <xgybdcb@163.com> Date: 星期四, 19 六月 2025 14:18:53 +0800 Subject: [PATCH] 增加生成结果的水深过滤参数 --- src/main/java/com/se/nsl/service/TestService.java | 448 ++++++++++++++++++++++---------------------------------- 1 files changed, 176 insertions(+), 272 deletions(-) diff --git a/src/main/java/com/se/nsl/service/TestService.java b/src/main/java/com/se/nsl/service/TestService.java index 83c562e..bb90b25 100644 --- a/src/main/java/com/se/nsl/service/TestService.java +++ b/src/main/java/com/se/nsl/service/TestService.java @@ -4,36 +4,25 @@ import com.alibaba.fastjson.JSON; import com.se.nsl.config.PropertiesConfig; import com.se.nsl.domain.dto.*; -import com.se.nsl.domain.po.DataPo; -import com.se.nsl.domain.po.PondingPo; +import com.se.nsl.domain.po.SimuData; import com.se.nsl.domain.vo.BuildingDepthVo; import com.se.nsl.helper.ComHelper; -import com.se.nsl.helper.GdalHelper; -import com.se.nsl.helper.ShpHelper; +import com.se.nsl.utils.TimeFormatUtil; import lombok.extern.slf4j.Slf4j; import org.gdal.gdal.Band; import org.gdal.gdal.Dataset; import org.gdal.gdal.gdal; -//import org.gdal.gdalconstConstants.gdalconst; import org.gdal.gdalconst.gdalconstConstants; -import org.gdal.ogr.*; import org.springframework.stereotype.Service; -import org.springframework.util.CollectionUtils; import javax.annotation.Resource; import java.awt.*; import java.awt.image.BufferedImage; -import java.io.BufferedReader; import java.io.File; -import java.io.FileReader; import java.io.IOException; -import java.math.BigDecimal; -import java.math.RoundingMode; -import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import java.util.List; -import java.util.concurrent.CopyOnWriteArrayList; @Slf4j @Service @@ -42,16 +31,13 @@ @Resource PropertiesConfig config; - @Resource - ResultService resultService; + public static final double MIN_VAL = 0.00001; - public final static double MIN_VAL = 0.00001; + public static final double MAX_X_OFFSET = 0; - public final static double MAX_X_OFFSET = 0; + public static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm"); - public final static SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm"); - - public void test(DataPo data) throws Exception { + public void test(SimuData data) throws IOException { String basePath = config.getInPath() + File.separator + data.getInPath() + File.separator; ResultDto dto = new ResultDto( data.getInPath(), @@ -66,15 +52,26 @@ process(dto, layer); } - private void process(ResultDto dto, LayerDto layer) throws Exception { + private void process(ResultDto dto, LayerDto layer) throws IOException { try { copeTerrain(dto, layer); - //copeBuilding(dto, layer); - List<BuildingDepthVo> buildings = copeWater(dto, layer); - //copeFlow(dto, layer); + copeWater(dto, layer); + copeFlow(dto, layer); copeLayerJson(dto, layer); - //copeRainFallJson(dto, layer); - //copeBuildingDepthJson(dto, buildings); + } finally { + File dir = new File(dto.getTemp()); + if (dir.exists()) { + FileUtil.del(dir); + } + } + } + + public void processRealTime(ResultDto dto, LayerDto layer) throws IOException { + try { + copeTerrainRealTime(dto, layer); + copeWater(dto, layer); + copeFlow(dto, layer); + copeLayerJson(dto, layer); } finally { File dir = new File(dto.getTemp()); if (dir.exists()) { @@ -86,10 +83,10 @@ public void copeTerrain(ResultDto dto, LayerDto layer) { Dataset ds = null; try { - // gdalconst锛実dalconstConstants - ds = gdal.Open(dto.getTerrainFile(), gdalconstConstants.GA_ReadOnly); + ds = gdal.Open(dto.getTerrainFile(), gdalconstConstants.GA_ReadOnly); // gdalconst if (null == ds || 0 == ds.getRasterCount()) return; + setSizes(ds, layer); setTerrainInfo(ds, layer); setWaterInfo(dto, layer); createTerrainPng(dto, ds, layer); @@ -98,19 +95,45 @@ } } + private void copeTerrainRealTime(ResultDto dto, LayerDto layer) { + Dataset ds = null; + try { + ds = gdal.Open(dto.getTerrainFile(), gdalconstConstants.GA_ReadOnly); // gdalconst + if (null == ds || 0 == ds.getRasterCount()) return; + + setSizes(ds, layer); + setTerrainInfo(ds, layer); + setWaterInfo(dto, layer); + createTerrainPng(dto, ds, layer); + } finally { + if (null != ds) ds.delete(); + } + } + + private void setSizes(Dataset ds, LayerDto layer) { + int xSize = ds.getRasterXSize(); + int ySize = ds.getRasterYSize(); + double rate = xSize < ySize ? xSize * 1.0 / ySize : ySize * 1.0 / xSize; + + List<int[]> list = layer.getTerrain().getSize(); + for (int i = 0, c = list.size(); i < c; i++) { + if (xSize < ySize) { + list.get(i)[0] = (int) Math.floor(list.get(i)[0] * rate); + } else { + list.get(i)[1] = (int) Math.floor(list.get(i)[1] * rate); + } + } + } + public void setTerrainInfo(Dataset ds, LayerDto layer) { - Geometry minPoint = GdalHelper.getMinPoint(ds); - Geometry maxPoint = GdalHelper.getMaxPoint(ds); - 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)); + double minx = Double.MAX_VALUE; + double miny = Double.MAX_VALUE; + double maxx = Double.MIN_VALUE; + double maxy = 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)); layer.setExtension(new ExtensionDto(minx, miny, maxx, maxy, mm[0], mm[1])); } @@ -121,15 +144,15 @@ for (int[] sizes : layer.getTerrain().getSize()) { String tif = dto.getTemp() + File.separator + "terrain_" + sizes[0] + "_" + sizes[1] + ".tif"; - ComHelper.Resample(ds, tif, sizes[0], sizes[1], layer); + ComHelper.cutAndResample(ds, tif, sizes[0], sizes[1], layer, true); if (!new File(tif).exists()) continue; String png = terrainPath + File.separator + sizes[0] + "_" + sizes[1] + ".png"; - Terrain2Png(layer, tif, png, sizes[0], sizes[1]); + terrain2Png(layer, tif, png, sizes[0], sizes[1]); } } - public void Terrain2Png(LayerDto layer, String tif, String png, int width, int height) { + public void terrain2Png(LayerDto layer, String tif, String png, int width, int height) { Dataset ds = null; try { ds = gdal.Open(tif, gdalconstConstants.GA_ReadOnly); @@ -142,13 +165,16 @@ layer.getTerrain().getVals().put(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(); + double differ = layer.getExtension().getMaxHeight() - layer.getExtension().getMinHeight(); + double 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; + int r = 0; + int g; + int b; if (buffer[offset] - layer.getExtension().getMaxHeight() > 0) { g = b = 255; } else { @@ -171,7 +197,7 @@ public void setWaterInfo(ResultDto dto, LayerDto layer) { List<String> files = getFiles(dto.getWaterPath(), ".tif"); layer.getWaters().setFiles(files); - if (null == files || files.size() == 0) return; + if (null == files || files.isEmpty()) return; setWaterData(layer, files); setWaterHeight(layer, files); @@ -179,15 +205,11 @@ public List<BuildingDepthVo> copeWater(ResultDto dto, LayerDto layer) { List<String> files = layer.getWaters().getFiles(); - if (files.size() == 0 || files.size() != layer.getWaters().getData().size()) return null; + if (files.isEmpty() || files.size() != layer.getWaters().getData().size()) return Collections.emptyList(); processWaters(dto, files, layer); - if (new File(dto.getBuildingFile()).exists()) { - return processBuilding(dto, files, layer); - } - - return null; + return Collections.emptyList(); } public List<String> getFiles(String path, String suffix) { @@ -199,48 +221,18 @@ } public 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 = 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)); - 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); - - layer.getWaters().getData().add(calendar.getTime().getTime()); + long timestamp = TimeFormatUtil.toMillis(fileName, "yyyyMMddHHmmss"); + layer.getWaters().getData().add(timestamp); } layer.getDuration().setStart(layer.getWaters().getData().get(0)); layer.getDuration().setEnd(layer.getWaters().getData().get(layer.getWaters().getData().size() - 1)); } public void setWaterHeight(LayerDto layer, List<String> files) { - /*files.parallelStream().forEach(file -> { - Dataset ds = null; - try { - ds = gdal.Open(file, gdalconstConstants.GA_ReadOnly); - if (null == ds || 0 == ds.getRasterCount()) return; - - double[] mm = new double[2]; - ds.GetRasterBand(1).ComputeRasterMinMax(mm, 0); - layer.getWaters().setHeight(mm[0], mm[1]); - } finally { - if (null != ds) ds.delete(); - } - });*/ - - int c = files.size(), step = files.size() / 10; + int c = files.size(); + int step = files.size() / 10; if (step < 1) step = 1; for (int i = 0; i < c; i += step) { Dataset ds = null; @@ -263,13 +255,15 @@ } public void processWaters(ResultDto dto, List<String> files, LayerDto layer) { + List<Double> waterDepths = new ArrayList<>(); for (int i = 0, c = files.size(); i < c; i++) { Dataset ds = null; try { ds = gdal.Open(files.get(i), gdalconstConstants.GA_ReadOnly); if (null == ds || 0 == ds.getRasterCount()) return; if (null == ds.GetSpatialRef()) ds.SetSpatialRef(dto.getSpatialReference()); - + waterDepths.add(layer.getWaters().getMinHeight()); + waterDepths.add(layer.getWaters().getMaxHeight()); createWaterPng(dto, ds, layer, layer.getWaters().getData().get(i)); //if (config.getCopyTif()) copyWaterTif(dto, ds, layer.getWaters().getData().get(i)); ///createVectors(dto, ds, layer, layer.getWaters().getData().get(i)); @@ -277,6 +271,9 @@ if (null != ds) ds.delete(); } } + waterDepths.sort(null); + layer.getWaters().setMinHeight(waterDepths.get(0)); + layer.getWaters().setMaxHeight(waterDepths.get(waterDepths.size() - 1)); } public void createWaterPng(ResultDto dto, Dataset ds, LayerDto layer, long ticks) { @@ -287,7 +284,7 @@ for (int[] sizes : layer.getTerrain().getSize()) { String fileName = ComHelper.getNameWithExt(ds.GetDescription()) + "_" + sizes[0] + "_" + sizes[1]; String tif = dto.getTemp() + File.separator + fileName + ".tif"; - ComHelper.Resample2(ds, tif, sizes[0], sizes[1], layer); + ComHelper.coordinateTransformAndResample(ds, tif, sizes[0], sizes[1], layer, true); if (!new File(tif).exists()) continue; String png = waterPath + File.separator + sizes[0] + "_" + sizes[1] + ".png"; @@ -305,37 +302,58 @@ float[] buffer = new float[width * height]; band.ReadRaster(0, 0, width, height, buffer); double[] transform = ds.GetGeoTransform(); - buffer = interpolateGrid(buffer, width); + buffer = interpolateGrid(buffer, width, height); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); //double differ = layer.getWaters().getMaxHeight() - layer.getWaters().getMinHeight(), minHeight = layer.getWaters().getMinHeight(); - double differ = layer.getExtension().getDiffer(), maxHeight = layer.getExtension().getMaxHeight(), minHeight = layer.getExtension().getMinHeight(); + double differ = layer.getExtension().getDiffer(); + double maxHeight = layer.getExtension().getMaxHeight(); + double minHeight = layer.getExtension().getMinHeight(); float[] ts = layer.getTerrain().getVals().get(width + "_" + height); +// double minDepth = Double.MAX_VALUE; +// double maxDepth = Double.MIN_VALUE; 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(buffer[offset]) || buffer[offset] < MIN_VAL || Float.isNaN(ts[offset])) continue; + float depth = buffer[offset]; + if (Float.isNaN(depth) || depth < MIN_VAL || Float.isNaN(ts[offset])) 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] + ts[offset] > maxHeight) { + int r = 0; + int g; + int b; + if (depth + ts[offset] > maxHeight) { g = b = 255; } else { - int val = (int) ((buffer[offset] + ts[offset] - minHeight) / differ * 65535); + int val = (int) ((depth + ts[offset] - minHeight) / differ * 65535); //int val = (int) (buffer[offset] / differ * 65535); g = val / 256; b = val % 256; } - + g = ComHelper.getSafeValue(g); + b = ComHelper.getSafeValue(b); Color color = new Color(r, g, b, 127); image.setRGB(x, y, color.getRGB()); + +// maxDepth = Math.max(maxDepth, depth); +// minDepth = Math.min(minDepth, depth); } } +// Double srcMinHeight = layer.getWaters().getMinHeight(); +// Double srcMaxHeight = layer.getWaters().getMaxHeight(); +// if (srcMinHeight == null) { +// srcMinHeight = minDepth; +// layer.getWaters().setMinHeight(minDepth); +// } +// if (srcMaxHeight == null) { +// srcMaxHeight = maxDepth; +// layer.getWaters().setMaxHeight(maxDepth); +// } +// if (minDepth < srcMinHeight) { +// layer.getWaters().setMinHeight(minDepth); +// } +// if (maxDepth < srcMaxHeight) { +// layer.getWaters().setMaxHeight(maxDepth); +// } ComHelper.savePng(image, png); } finally { if (null != ds) ds.delete(); @@ -345,42 +363,42 @@ /** * 鎻掑�煎鐞�-鍥涢偦鍩� */ - public float[] interpolateGrid(float[] buffer, int size) { - float[] tempBuffer = new float[size * size]; - for (int i = 0; i < size; i++) { - for (int j = 0; j < size; j++) { - int index = i * size + j; + public float[] interpolateGrid(float[] buffer, int width, int height) { + float[] tempBuffer = new float[width * height]; + for (int x = 0; x < width; x++) { + for (int y = 0; y < height; y++) { + int index = x + y * width; float current = buffer[index]; - if (current == 0) { + if (current <= MIN_VAL) { float sum = 0; int count = 0; // 妫�鏌ヤ笂閭� - if (i > 0) { - float neighbor = buffer[(i - 1) * size + j]; + if (y > 0) { + float neighbor = buffer[x + (y - 1) * width]; if (neighbor > MIN_VAL) { sum += neighbor; count++; } } // 妫�鏌ヤ笅閭� - if (i < size - 1) { - float neighbor = buffer[(i + 1) * size + j]; + if (y < height - 1) { + float neighbor = buffer[x + (y + 1) * width]; if (neighbor > MIN_VAL) { sum += neighbor; count++; } } // 妫�鏌ュ乏閭� - if (j > 0) { - float neighbor = buffer[i * size + (j - 1)]; + if (x > 0) { + float neighbor = buffer[x - 1 + y * width]; if (neighbor > MIN_VAL) { sum += neighbor; count++; } } // 妫�鏌ュ彸閭� - if (j < size - 1) { - float neighbor = buffer[i * size + (j + 1)]; + if (x < width - 1) { + float neighbor = buffer[x + 1 + y * width]; if (neighbor > MIN_VAL) { sum += neighbor; count++; @@ -397,160 +415,13 @@ } } } - // 灏嗕复鏃舵暟缁勫鍒跺洖鍘熸暟缁� - //System.arraycopy(tempBuffer, 0, buffer, 0, tempBuffer.length); + //System.arraycopy(tempBuffer, 0, buffer, 0, tempBuffer.length); // 澶嶅埗鍥炲師鏁扮粍 return tempBuffer; } - public List<BuildingDepthVo> processBuilding(ResultDto dto, List<String> files, LayerDto layer) { - List<BuildingDepthVo> list = new CopyOnWriteArrayList<>(); - for (int i = 0, c = files.size(); i < c; i++) { - Dataset ds = null; - try { - ds = gdal.Open(files.get(i), gdalconstConstants.GA_ReadOnly); - if (null == ds || 0 == ds.getRasterCount()) continue; - if (null == ds.GetSpatialRef()) ds.SetSpatialRef(dto.getSpatialReference()); - - copeBuildingDepth(dto, ds, layer.getWaters().getData().get(i), list); - } finally { - if (null != ds) ds.delete(); - } - } - - return list; - } - - public void copeBuildingDepth(ResultDto dto, Dataset ds, long ticks, List<BuildingDepthVo> list) { - double[] transform = ds.GetGeoTransform(); - int xSize = ds.getRasterXSize(), ySize = ds.getRasterYSize(); - double minX = transform[0], pixelWidth = transform[1], maxY = transform[3], pixelHeight = Math.abs(transform[5]); - - for (BuildingDto building : dto.getBuildingList()) { - Double val = getValue(ds, building, xSize, ySize, minX, maxY, pixelWidth, pixelHeight); - list.add(new BuildingDepthVo(building.getId(), ticks, val)); - } - } - - public Double getValue(Dataset ds, BuildingDto building, int xSize, int ySize, double minX, double maxY, double pixelWidth, double pixelHeight) { - double[] env = new double[4]; - building.getGeom().GetEnvelope(env); - - int startX = (int) Math.floor((env[0] - minX) / pixelWidth); - int endX = (int) Math.floor((env[1] - minX) / pixelWidth); - int startY = (int) Math.floor((maxY - env[3]) / Math.abs(pixelHeight)); - int endY = (int) Math.floor((maxY - env[2]) / Math.abs(pixelHeight)); - 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; - - int width = endX - startX; - int height = endY - startY; - double[] pixelValues = new double[width * height]; - ds.GetRasterBand(1).ReadRaster(startX, startY, width, height, pixelValues); - - Double val = Arrays.stream(pixelValues).max().getAsDouble(); - - return ComHelper.isValid(val) ? val : null; - } - - public void createVectors(ResultDto dto, Dataset ds, LayerDto layer, Long ticks) { - String filePath = dto.getOutPath() + File.separator + "waters" + File.separator + ticks + File.separator + "polygonize.geojson"; - ShpHelper.polygonize2Geojson(ds, filePath); - - /*List<Geometry> geometries = getGeometries(filePath); - if (CollectionUtils.isEmpty(geometries)) return; - - List<PondingPo> list = copePonding(dto, ds, layer, geometries); - if (CollectionUtils.isEmpty(list)) return; - - try { - filePath = dto.getOutPath() + File.separator + "waters" + File.separator + ticks + File.separator + "water.json"; - ComHelper.writeJson(filePath, JSON.toJSONString(list)); - } catch (Exception ex) { - log.error(ex.getMessage(), ex); - }*/ - } - - public List<Geometry> getGeometries(String filePath) { - if (!FileUtil.exist(filePath)) return null; - - Driver driver = null; - DataSource dataSource = null; - org.gdal.ogr.Layer layer = null; - try { - driver = ogr.GetDriverByName("GeoJSON"); - if (null == driver) return null; - - DataSource ds = driver.Open(filePath); - if (null == ds) return null; - - layer = ds.GetLayer(0); - List<Geometry> list = new ArrayList<>(); - for (long i = 0, d = layer.GetFeatureCount(); i < d; i++) { - Feature f = layer.GetFeature(i); - if (f.GetFieldAsDouble("val") < -999) continue; - - list.add(f.GetGeometryRef()); - } - - return list; - } catch (Exception ex) { - log.error(ex.getMessage(), ex); - return null; - } finally { - GdalHelper.delete(layer, dataSource, driver); - } - } - - public List<PondingPo> copePonding(ResultDto dto, Dataset ds, LayerDto layer, List<Geometry> geometries) { - double[] transform = ds.GetGeoTransform(); - int xSize = ds.getRasterXSize(), ySize = ds.getRasterYSize(); - - List<PondingPo> list = new ArrayList<>(); - for (Geometry geometry : geometries) { - //List<PointDto> points = getValues(ds, geometry, transform, xSize, ySize); - //if (CollectionUtils.isEmpty(points)) continue; - - //PointDto point = Collections.max(points); - PointDto point = getValues(ds, geometry, transform, xSize, ySize); - if (null == point) continue; - - list.add(new PondingPo(geometry, point)); - } - - return list; - } - - public PointDto 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; - - int width = endX - startX; - int height = endY - startY; - double[] pixelValues = new double[width * height]; - ds.GetRasterBand(1).ReadRaster(startX, startY, width, height, pixelValues); - - Double val = Arrays.stream(pixelValues).max().getAsDouble(); - - return new PointDto(g.Centroid().GetX(), g.Centroid().GetY(), ComHelper.isValid(val) ? val : 0.0); - } - public void copeFlow(ResultDto dto, LayerDto layer) { - List<String> files = getFiles(dto.getFlowPath(), ".tif"); - if (null == files || files.size() != layer.getWaters().getData().size()) return; - + List<String> files = layer.getWaters().getFiles(); + if (null == files || files.isEmpty()) return; for (int i = 0, c = files.size(); i < c; i++) { Dataset ds = null; try { @@ -573,31 +444,64 @@ for (int[] sizes : layer.getTerrain().getSize()) { String name = ComHelper.getNameWithExt(ds.GetDescription()) + "_" + sizes[0] + "_" + sizes[1]; String tif = dto.getTemp() + File.separator + name + ".tif"; - ComHelper.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, tif, png, sizes[0], sizes[1]); + vxyTif2Png(tif, png, sizes[0], sizes[1]); } } - public void vxyTif2Png(LayerDto layer, String tif, String png, int width, int height) { + public void vxyTif2Png(String tif, String png, int width, int height) { Dataset ds = null; try { ds = gdal.Open(tif, gdalconstConstants.GA_ReadOnly); if (null == ds || 0 == ds.getRasterCount()) return; - float[] vxBuffer = new float[width * height], vyBuffer = new float[width * height]; - ds.GetRasterBand(1).ReadRaster(0, 0, width, height, vxBuffer); - ds.GetRasterBand(2).ReadRaster(0, 0, width, height, vyBuffer); + float[] depthBuffer = new float[width * height]; + float[] vxBuffer = new float[width * height]; + float[] vyBuffer = new float[width * height]; - createFlowPng(vxBuffer, vyBuffer, png, width, height); + ds.GetRasterBand(1).ReadRaster(0, 0, width, height, depthBuffer); + ds.GetRasterBand(2).ReadRaster(0, 0, width, height, vxBuffer); + ds.GetRasterBand(3).ReadRaster(0, 0, width, height, vyBuffer); + + createFlowPng(depthBuffer, vxBuffer, vyBuffer, png, width, height); } finally { if (null != ds) ds.delete(); } } - public void createFlowPng(float[] vxBuffer, float[] vyBuffer, String png, int width, int height) { + public void createFlowPng(float[] depthBuffer, float[] vxBuffer, float[] vyBuffer, String png, int width, int height) { + BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); + for (int x = 0; x < width; x++) { + for (int y = 0; y < height; y++) { + int offset = x + y * width; + float depth = ComHelper.getFloatValue(depthBuffer[offset]); + float fx = ComHelper.getFloatValue(vxBuffer[offset]); + float fy = ComHelper.getFloatValue(vyBuffer[offset]); + if (Float.isNaN(fx) && Float.isNaN(fy) || (fx < MIN_VAL && fy < MIN_VAL)) continue; + + if (depth == 0) { + fx = 0f; + fy = 0f; + } else { + fx = Float.isNaN(fx) ? 0 : fx / depth; + fy = Float.isNaN(fy) ? 0 : fy / depth; + } + 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(ComHelper.getSafeValue(r), ComHelper.getSafeValue(g), ComHelper.getSafeValue(b), 127); + image.setRGB(x, y, color.getRGB()); + } + } + ComHelper.savePng(image, png); + } + + /*public void createFlowPng(float[] vxBuffer, float[] vyBuffer, String png, int width, int height) { BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { @@ -619,7 +523,7 @@ } } ComHelper.savePng(image, png); - } + }*/ public void copeLayerJson(ResultDto dto, LayerDto layer) throws IOException { layer.getWaters().setFiles(null); @@ -631,8 +535,8 @@ layer.setFlowUrl("/hls/f" + path + ".m3u8"); String json = JSON.toJSONString(layer); - // String json = JSONUtil.toJsonPrettyStr(layer); - String filePath = dto.getOutPath() + File.separator + "layer.json"; +// String filePath = dto.getOutPath() + File.separator + "layer.json"; + String filePath = dto.getOutPath() + File.separator + layer.getName(); ComHelper.writeJson(filePath, json); } -- Gitblit v1.9.3