| | |
| | | import cn.hutool.core.io.FileUtil; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.se.simu.config.PropertiesConfig; |
| | | import com.se.simu.domain.dto.BuildingDto; |
| | | 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.dto.*; |
| | | 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; |
| | | import org.gdal.gdal.Band; |
| | | import org.gdal.gdal.Dataset; |
| | |
| | | import org.gdal.gdalconst.gdalconst; |
| | | import org.gdal.ogr.*; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.CollectionUtils; |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.imageio.ImageIO; |
| | | import java.awt.*; |
| | | import java.awt.image.BufferedImage; |
| | | import java.io.*; |
| | |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | import java.util.List; |
| | | import java.util.concurrent.CopyOnWriteArrayList; |
| | | |
| | | /** |
| | | * 处理结果服务类 |
| | | * |
| | | * @author WWW |
| | | * @date 2024-09-29 |
| | | */ |
| | | @Slf4j |
| | | @Service |
| | | @SuppressWarnings("ALL") |
| | |
| | | basePath + config.getBuildingFile(), |
| | | basePath + config.getWaterPath(), |
| | | basePath + config.getFlowPath(), |
| | | config.getOutPath()); |
| | | config.getInPath(), |
| | | config.getOutPath(), |
| | | data.getEpsg()); |
| | | LayerDto layer = new LayerDto(config.getVer(), data.getEpsg(), config.getSizes()); |
| | | process(dto, layer); |
| | | } |
| | |
| | | try { |
| | | copeTerrain(dto, layer); |
| | | copeBuilding(dto, layer); |
| | | copeWater(dto, layer); |
| | | List<BuildingDepthVo> buildings = 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()) { |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 地形 |
| | | */ |
| | | private void copeTerrain(ResultDto dto, LayerDto layer) { |
| | | Dataset ds = null; |
| | | try { |
| | |
| | | if (null == ds || 0 == ds.getRasterCount()) return; |
| | | |
| | | setTerrainInfo(ds, layer); |
| | | setWaterInfo(dto, layer); |
| | | createTerrainPng(dto, ds, layer); |
| | | setWaterInfo(dto, 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)); |
| | | 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); |
| | | 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; |
| | | //layer.getTerrain().setHeight(getMinVal(mm[0], 1000), getMaxVal(mm[1], 1000)); |
| | | layer.setExtension(new ExtensionDto(minx, miny, maxx, maxy, mm[0], mm[1])); |
| | | } |
| | | |
| | | private void createTerrainPng(ResultDto dto, Dataset ds, LayerDto layer) { |
| | |
| | | |
| | | 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); |
| | | ComHelper.Resample(ds, tif, sizes[0], sizes[1], layer); |
| | | if (!new File(tif).exists()) continue; |
| | | |
| | | String png = terrainPath + File.separator + sizes[0] + "_" + sizes[1] + ".png"; |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 重采样 |
| | | */ |
| | | 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:" + 4548); |
| | | //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) { |
| | | private void Terrain2Png(LayerDto layer, String tif, String png, int width, int height) { |
| | | Dataset ds = null; |
| | | try { |
| | | ds = gdal.Open(tif, gdalconst.GA_ReadOnly); |
| | |
| | | float[] buffer = new float[width * height]; |
| | | //band.ReadRaster(0, 0, width, height, buffer, width, height, 0, 0); |
| | | band.ReadRaster(0, 0, width, height, buffer); |
| | | 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(); |
| | |
| | | image.setRGB(x, y, color.getRGB()); |
| | | } |
| | | } |
| | | savePng(image, png); |
| | | ComHelper.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) { |
| | | Driver driver = null; |
| | | DataSource dataSource = null; |
| | |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 水面 |
| | | */ |
| | | private void setWaterInfo(ResultDto dto, LayerDto layer) { |
| | | List<String> files = getFiles(dto.getWaterPath(), ".tif"); |
| | | layer.getWaters().setFiles(files); |
| | |
| | | setWaterHeight(layer, files); |
| | | } |
| | | |
| | | private void copeWater(ResultDto dto, LayerDto layer) { |
| | | private List<BuildingDepthVo> copeWater(ResultDto dto, LayerDto layer) { |
| | | List<String> files = layer.getWaters().getFiles(); |
| | | if (files.size() == 0 || files.size() != layer.getWaters().getData().size()) return; |
| | | if (files.size() == 0 || files.size() != layer.getWaters().getData().size()) return null; |
| | | |
| | | processWaters(dto, files, layer); |
| | | |
| | | return processBuilding(dto, files, layer); |
| | | } |
| | | |
| | | private static List<String> getFiles(String path, String suffix) { |
| | | 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 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) { |
| | | private 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); |
| | | 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)); |
| | |
| | | layer.getDuration().setEnd(layer.getWaters().getData().get(layer.getWaters().getData().size() - 1)); |
| | | } |
| | | |
| | | private static void setWaterHeight(LayerDto layer, List<String> files) { |
| | | private void setWaterHeight(LayerDto layer, List<String> files) { |
| | | files.parallelStream().forEach(file -> { |
| | | Dataset ds = null; |
| | | try { |
| | |
| | | |
| | | double[] mm = new double[2]; |
| | | ds.GetRasterBand(1).ComputeRasterMinMax(mm, 0); |
| | | layer.getExtension().setHeight(mm[0], mm[1]); |
| | | //layer.getExtension().setHeight(mm[0], mm[1]); |
| | | layer.getWaters().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)); |
| | | //layer.getExtension().setMinHeight(getMinVal(layer.getExtension().getMinHeight() - 1, 1000)); |
| | | //layer.getExtension().setMaxHeight(getMaxVal(layer.getExtension().getMaxHeight() + 1, 1000)); |
| | | //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(ComHelper.getMaxVal(layer.getExtension().getMaxHeight(), 1000000)); |
| | | layer.getExtension().setMinHeight(ComHelper.getMaxVal(layer.getExtension().getMinHeight(), 1000000)); |
| | | layer.getExtension().setDiffer(); |
| | | } |
| | | |
| | | private static void processWaters(ResultDto dto, List<String> files, LayerDto layer) { |
| | | private 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()) return; |
| | | if (null == ds.GetSpatialRef()) ds.SetSpatialRef(dto.getSpatialReference()); |
| | | |
| | | createWaterPng(dto, ds, layer, layer.getWaters().getData().get(i)); |
| | | copyWaterTif(dto, ds, 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)); |
| | | } finally { |
| | | if (null != ds) ds.delete(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | private static void createWaterPng(ResultDto dto, Dataset ds, LayerDto layer, long ticks) { |
| | | private 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 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); |
| | | ComHelper.Resample(ds, tif, sizes[0], sizes[1], layer); |
| | | if (!new File(tif).exists()) continue; |
| | | |
| | | String png = waterPath + File.separator + sizes[0] + "_" + sizes[1] + ".png"; |
| | |
| | | } |
| | | } |
| | | |
| | | private static void copyWaterTif(ResultDto dto, Dataset ds, long ticks) { |
| | | String source = ds.GetDescription(); |
| | | String target = dto.getOutPath() + File.separator + "waters" + File.separator + ticks + File.separator + "water.tif"; |
| | | FileUtil.copyFile(source, target); |
| | | } |
| | | |
| | | 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) { |
| | | private 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); |
| | |
| | | double[] transform = ds.GetGeoTransform(); |
| | | |
| | | 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.getWaters().getMaxHeight() - layer.getWaters().getMinHeight(), minHeight = layer.getWaters().getMinHeight(); |
| | | double differ = layer.getExtension().getDiffer(), maxHeight = layer.getExtension().getMaxHeight(), minHeight = layer.getExtension().getMinHeight(); |
| | | float[] ts = layer.getTerrain().getVals().get(width + "_" + height); |
| | | 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] < -999 || buffer[offset] < minHeight) continue; |
| | | if (Float.isNaN(buffer[offset]) || buffer[offset] <= 0 || 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]; |
| | | //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] - layer.getExtension().getMaxHeight() > 0) { |
| | | if (buffer[offset] + ts[offset] > maxHeight) { |
| | | g = b = 255; |
| | | } else { |
| | | int val = (int) ((buffer[offset] - minHeight) / differ * 65535); |
| | | int val = (int) ((buffer[offset] + ts[offset] - minHeight) / differ * 65535); |
| | | g = val / 256; |
| | | b = val % 256; |
| | | } |
| | |
| | | image.setRGB(x, y, color.getRGB()); |
| | | } |
| | | } |
| | | savePng(image, png); |
| | | ComHelper.savePng(image, png); |
| | | } finally { |
| | | if (null != ds) ds.delete(); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 是否相交 |
| | | * https://blog.csdn.net/flyingshineangel/article/details/135423025 |
| | | */ |
| | | private 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()); |
| | | private 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), gdalconst.GA_ReadOnly); |
| | | if (null == ds || 0 == ds.getRasterCount()) continue; |
| | | if (null == ds.GetSpatialRef()) ds.SetSpatialRef(dto.getSpatialReference()); |
| | | |
| | | return dto.getBuildingList().parallelStream().filter(b -> b.getGeom().Intersects(p)).findFirst().orElse(null); |
| | | copeBuildingDepth(dto, ds, layer.getWaters().getData().get(i), list); |
| | | } finally { |
| | | if (null != ds) ds.delete(); |
| | | } |
| | | } |
| | | |
| | | return list; |
| | | } |
| | | |
| | | private 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)); |
| | | } |
| | | } |
| | | |
| | | private 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; |
| | | } |
| | | |
| | | private void copyWaterTif(ResultDto dto, Dataset ds, long ticks) { |
| | | String source = ds.GetDescription(); |
| | | String target = dto.getOutPath() + File.separator + "waters" + File.separator + ticks + File.separator + "water.tif"; |
| | | FileUtil.copyFile(source, target); |
| | | } |
| | | |
| | | private 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); |
| | | } |
| | | } |
| | | |
| | | private 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); |
| | | } |
| | | } |
| | | |
| | | private 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; |
| | | } |
| | | |
| | | private 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); |
| | | } |
| | | |
| | | private void copeFlow(ResultDto dto, LayerDto layer) { |
| | |
| | | try { |
| | | ds = gdal.Open(files.get(i), gdalconst.GA_ReadOnly); |
| | | if (null == ds || 0 == ds.getRasterCount()) return; |
| | | if (null == ds.GetSpatialRef()) ds.SetSpatialRef(dto.getSpatialReference()); |
| | | |
| | | createFlowPng(dto, ds, layer, layer.getWaters().getData().get(i)); |
| | | } finally { |
| | |
| | | } |
| | | } |
| | | |
| | | private static void createFlowPng(ResultDto dto, Dataset ds, LayerDto layer, long ticks) { |
| | | private void createFlowPng(ResultDto dto, Dataset ds, 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 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); |
| | | 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"; |
| | |
| | | } |
| | | } |
| | | |
| | | private static void vxyTif2Png(LayerDto layer, String tif, String png, int width, int height) { |
| | | private void vxyTif2Png(LayerDto layer, String tif, String png, int width, int height) { |
| | | Dataset ds = null; |
| | | try { |
| | | ds = gdal.Open(tif, gdalconst.GA_ReadOnly); |
| | |
| | | } |
| | | } |
| | | |
| | | private static void createFlowPng(float[] vxBuffer, float[] vyBuffer, String png, int width, int height) { |
| | | private 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]); |
| | | 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; |
| | |
| | | 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); |
| | | ComHelper.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); |
| | | layer.getTerrain().setEpsg(null); |
| | | layer.getExtension().setDiffer(null); |
| | | |
| | | String json = JSON.toJSONString(layer); |
| | | // 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 { |
| | | String rainGageFilePath = config.getInPath() + File.separator + dto.getServiceName() + File.separator + "RainGage.dat"; |
| | | String filePath = dto.getOutPath() + File.separator + "rainfall.json"; |
| | |
| | | fr.close(); |
| | | |
| | | String json = JSON.toJSONString(map); |
| | | writeJson(filePath, json); |
| | | ComHelper.writeJson(filePath, json); |
| | | } |
| | | |
| | | private void copeBuildingDepthJson(ResultDto dto, List<BuildingDepthVo> list) throws IOException { |
| | | if (CollectionUtils.isEmpty(list)) return; |
| | | |
| | | 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); |
| | | } |
| | | } |