From 59b432c883011119649c283cc9d4c1d357599802 Mon Sep 17 00:00:00 2001 From: dcb <xgybdcb@163.com> Date: 星期一, 30 六月 2025 15:13:56 +0800 Subject: [PATCH] 对于没有不能获取到数据的雨量计,使用其他雨量计数据填充 --- src/main/java/com/se/nsl/utils/SolverTifUtil.java | 104 ++++++++++ src/main/java/com/se/nsl/service/ResolveService.java | 216 ++++++++++----------- src/main/java/com/se/nsl/service/RealTimeSimulationService.java | 176 ++++++++++++++++ src/main/resources/application-dev.yml | 2 src/main/java/com/se/nsl/service/SimuService.java | 102 +--------- 5 files changed, 390 insertions(+), 210 deletions(-) diff --git a/src/main/java/com/se/nsl/service/RealTimeSimulationService.java b/src/main/java/com/se/nsl/service/RealTimeSimulationService.java index 404d4c3..9c1622b 100644 --- a/src/main/java/com/se/nsl/service/RealTimeSimulationService.java +++ b/src/main/java/com/se/nsl/service/RealTimeSimulationService.java @@ -237,11 +237,16 @@ List<String> newLines = new ArrayList<>(); newLines.add(title); Map<String, Integer> deviceInfoMap = deviceInfos.stream().collect(Collectors.toMap(DeviceInfo::getId, DeviceInfo::getMappingId)); + List<RainRecord> rainRecords = new ArrayList<>(); for (RainGauge gauge : gauges) { String id = gauge.getId(); Integer mappingId = deviceInfoMap.getOrDefault(id, 0); - String l = getRainGaugeResult(gauge, mappingId, currentTime); - newLines.add(l); + RainRecord rr = getRainGaugeResult(gauge, mappingId, currentTime); + rainRecords.add(rr); + } + fillEmptyValue(rainRecords); //濉厖娌℃湁鍊肩殑闆ㄩ噺璁� + for (RainRecord rr : rainRecords) { + newLines.add(rr.toString()); } File newDatFile = new File(serviceNameDir, "rainfall_" + currentTime + ".dat"); if (!newDatFile.exists()) newDatFile.createNewFile(); @@ -249,7 +254,52 @@ return newDatFile; } - private String getRainGaugeResult(RainGauge gauge,int stationId, long currentTime) throws JsonProcessingException { + //灏嗛洦閲忚涓┖鍊硷紙intensity涓�-1锛夌殑閮ㄥ垎濉厖鍊� + private void fillEmptyValue(List<RainRecord> rainRecords) { + double[] rainValues = rainRecords.stream().mapToDouble(r -> r.getIntensity()).toArray(); + double[] forwardValues = forwardFill(rainValues); + double[] backwordValues = backwordFill(rainValues); + for (int i = 0; i < rainRecords.size(); i++) { + RainRecord rr = rainRecords.get(i); + double filledValue = forwardValues[i] != -1 ? forwardValues[i] : backwordValues[i]; + rr.setIntensity(filledValue); + } + //濡傛灉閮芥槸-1锛屽垯璁剧疆涓�0 + for (RainRecord rr : rainRecords) { + double intensity = rr.getIntensity(); + if (intensity == -1) { + rr.setIntensity(0); + } + } + } + + private double[] forwardFill(double[] rainValues) { + double[] result = Arrays.copyOf(rainValues, rainValues.length); + double lastValid = -1D; + for (int i = 0; i < result.length; i++) { + if (result[i] != -1) { + lastValid = result[i]; + } else if (lastValid != -1) { + result[i] = lastValid; + } + } + return result; + } + + private double[] backwordFill(double[] rainValues) { + double[] result = Arrays.copyOf(rainValues, rainValues.length); + double lastValid = -1D; + for (int i = result.length - 1; i >= 0; i--) { + if (result[i] != -1) { + lastValid = result[i]; + } else if (lastValid != -1) { + result[i] = lastValid; + } + } + return result; + } + + private RainRecord getRainGaugeResult(RainGauge gauge,int stationId, long currentTime) throws JsonProcessingException { double lon = gauge.getX(); double lat = gauge.getY(); LocalDateTime dateTime = TimeFormatUtil.toDate(currentTime); @@ -263,8 +313,9 @@ String startTime = TimeFormatUtil.formatTime(someMinutesAgo, YYYY_MM_DD_HH_MM_SS); String endTime = TimeFormatUtil.formatTime(currentTime, YYYY_MM_DD_HH_MM_SS); double intensity = getIntensityByDeviceId(id, startTime, endTime); //淇濈暀鎸囧畾浣嶆暟灏忔暟 - return String.format("%s %s %s %s %s %s %s %s %s", - stationId, lon, lat, year, month, day, hour, minute, String.format("%.6f", intensity)); +// return String.format("%s %s %s %s %s %s %s %s %s", +// stationId, lon, lat, year, month, day, hour, minute, String.format("%.6f", intensity)); + return new RainRecord(stationId, lon, lat, year, month, day, hour, minute, intensity); } //鏍规嵁闆ㄩ噺璁℃爣璇嗘煡璇㈤洦閲忔暟鎹� @@ -285,9 +336,8 @@ if (!pageData.isEmpty()) { return pageData.get(0).get("value").asDouble(); } - return 0; + return -1D; } - private String callBat(String cmd) { try { @@ -309,4 +359,116 @@ } } + static class RainRecord { + private int stationId; + private double lon; + private double lat; + private int year; + private int month; + private int day; + private int hour; + private int minute; + private double intensity; //-1浠h〃娌℃湁鏁板�� + + public RainRecord() {} + + public RainRecord(int stationId, double lon, double lat, int year, int month, + int day, int hour, int minute, double intensity) { + this.stationId = stationId; + this.lon = lon; + this.lat = lat; + this.year = year; + this.month = month; + this.day = day; + this.hour = hour; + this.minute = minute; + this.intensity = intensity; + } + + public int getStationId() { + return stationId; + } + + public void setStationId(int stationId) { + this.stationId = stationId; + } + + public double getLon() { + return lon; + } + + public void setLon(double lon) { + this.lon = lon; + } + + public double getLat() { + return lat; + } + + public void setLat(double lat) { + this.lat = lat; + } + + public int getYear() { + return year; + } + + public void setYear(int year) { + this.year = year; + } + + public int getMonth() { + return month; + } + + public void setMonth(int month) { + this.month = month; + } + + public int getDay() { + return day; + } + + public void setDay(int day) { + this.day = day; + } + + public int getHour() { + return hour; + } + + public void setHour(int hour) { + this.hour = hour; + } + + public int getMinute() { + return minute; + } + + public void setMinute(int minute) { + this.minute = minute; + } + + public double getIntensity() { + return intensity; + } + + public void setIntensity(double intensity) { + this.intensity = intensity; + } + + @Override + public String toString() { + return "" + stationId + + " " + lon + + " " + lat + + " " + year + + " " + month + + " " + day + + " " + hour + + " " + minute + + " " + String.format("%.6f", intensity); + } + } + } diff --git a/src/main/java/com/se/nsl/service/ResolveService.java b/src/main/java/com/se/nsl/service/ResolveService.java index e041ab5..a78e688 100644 --- a/src/main/java/com/se/nsl/service/ResolveService.java +++ b/src/main/java/com/se/nsl/service/ResolveService.java @@ -11,8 +11,6 @@ import com.se.nsl.domain.po.Simu; import com.se.nsl.domain.po.SimuData; import com.se.nsl.domain.vo.ConfigVo; -import com.se.nsl.domain.vo.RealTimeInput; -import com.se.nsl.domain.vo.ResultVo; import com.se.nsl.helper.ComHelper; import com.se.nsl.helper.GdalHelper; import com.se.nsl.helper.StringHelper; @@ -37,7 +35,6 @@ import javax.annotation.Resource; import java.io.BufferedReader; import java.io.File; -import java.io.IOException; import java.io.InputStreamReader; import java.math.BigDecimal; import java.math.RoundingMode; @@ -46,7 +43,6 @@ import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.time.Instant; -import java.time.LocalDateTime; import java.time.temporal.ChronoUnit; import java.util.*; import java.util.concurrent.ExecutorService; @@ -156,7 +152,7 @@ /** * 鍒濆鍖栧弬鏁� */ - public void initArgs(Simu simu, SimuData data) throws IOException { + public void initArgs(Simu simu, SimuData data) { String inPath = config.getInPath() + File.separator + data.getInPath(); createDir(inPath); createDir(inPath + File.separator + "depth"); @@ -522,114 +518,114 @@ } //瀹炴椂妯℃嫙 - public String realTimeSimulate(RealTimeInput input) throws IOException { - long currentTime = System.currentTimeMillis(); - //鏍规嵁鏈嶅姟鎵惧埌鎸囧畾鐨勬枃浠跺す - String serviceName = input.getServiceName(); - File serviceNameDir = new File(config.getInPath(), serviceName); - //鐢熸垚涓�涓柊鐨勯洦閲忔枃浠�,闇�瑕佸師鍏堥洦閲忔枃浠剁殑涓�浜涗俊鎭紝鎵�浠ラ渶瑕佸厛璇诲彇鏃х殑 - String[] values = readTheOldFirstLineRainfallValue(serviceNameDir); - File newDatFile = generateNewRainfallFile(input, values, serviceNameDir, currentTime); +// public String realTimeSimulate(RealTimeInput input) throws IOException { +// long currentTime = System.currentTimeMillis(); +// //鏍规嵁鏈嶅姟鎵惧埌鎸囧畾鐨勬枃浠跺す +// String serviceName = input.getServiceName(); +// File serviceNameDir = new File(config.getInPath(), serviceName); +// //鐢熸垚涓�涓柊鐨勯洦閲忔枃浠�,闇�瑕佸師鍏堥洦閲忔枃浠剁殑涓�浜涗俊鎭紝鎵�浠ラ渶瑕佸厛璇诲彇鏃х殑 +// String[] values = readTheOldFirstLineRainfallValue(serviceNameDir); +// File newDatFile = generateNewRainfallFile(input, values, serviceNameDir, currentTime); +// +// //鐢熸垚涓�涓柊鐨勭敓鎴恴arr鐨勯厤缃枃浠� +// File newConfigFile = generateNewZarrConfigFile(serviceNameDir, serviceName, currentTime, newDatFile); +// //鎵ц姹傝В鍣ㄨ繍绠� +// String cmd = String.format("%s \"%s\"", config.getUwSolverBat(), newConfigFile); +// callBat2(cmd); +// +// //鐢熸垚涓�涓柊鐨剒arr杞瑃if鐨刯son鏂囦欢 +// File newZarr2TifJson = generateNewZarr2TifJson(serviceNameDir, currentTime); +// //鎵цzarr杞瑃if +// String zarr2TifCmd = String.format("%s \"%s\"", config.getZarr2tifBat(), newZarr2TifJson); +// callBat2(zarr2TifCmd); +// //杩斿洖鏂扮殑layer.json鍚嶇О +// return generateLayerJsonAndPng(serviceName, serviceNameDir, currentTime); +// +// } - //鐢熸垚涓�涓柊鐨勭敓鎴恴arr鐨勯厤缃枃浠� - File newConfigFile = generateNewZarrConfigFile(serviceNameDir, serviceName, currentTime, newDatFile); - //鎵ц姹傝В鍣ㄨ繍绠� - String cmd = String.format("%s \"%s\"", config.getUwSolverBat(), newConfigFile); - callBat2(cmd); +// private String generateLayerJsonAndPng(String serviceName, File serviceNameDir, long currentTime) throws IOException { +// ResultDto resultDto = new ResultDto(); +// resultDto.setServiceName(serviceName); +// File temp = Paths.get(config.getOutPath(), serviceName, "temp").toFile(); +// if (!temp.exists()) temp.mkdir(); +// resultDto.setTemp(temp.getAbsolutePath()); +// resultDto.setOutPath(Paths.get(config.getOutPath(), serviceName).toString()); +// File dem = new File(serviceNameDir, "DEM.tif"); +// resultDto.setTerrainFile(dem.getAbsolutePath()); +// File newDepthDir = new File(serviceNameDir + File.separator + "depth_" + currentTime); +// resultDto.setWaterPath(newDepthDir.getAbsolutePath()); +// LayerDto layerDto = new LayerDto(config.getVer(), 4548, config.getSizes()); +// String newLayerJsonName = "layer_" + currentTime + ".json"; +// layerDto.setName(newLayerJsonName); +// testService.processRealTime(resultDto, layerDto); +// return newLayerJsonName; +// } - //鐢熸垚涓�涓柊鐨剒arr杞瑃if鐨刯son鏂囦欢 - File newZarr2TifJson = generateNewZarr2TifJson(serviceNameDir, currentTime); - //鎵цzarr杞瑃if - String zarr2TifCmd = String.format("%s \"%s\"", config.getZarr2tifBat(), newZarr2TifJson); - callBat2(zarr2TifCmd); - //杩斿洖鏂扮殑layer.json鍚嶇О - return generateLayerJsonAndPng(serviceName, serviceNameDir, currentTime); +// private File generateNewZarr2TifJson(File serviceNameDir, long currentTime) throws IOException { +// File srcZarr2TifJson = new File(serviceNameDir, "zarr2tif.json"); +// Zarr2Tif zarr2Tif = mapper.readValue(srcZarr2TifJson, Zarr2Tif.class); +// //淇敼zarr2tif瀵硅薄涓殑瀛楁 +//// String stamp = TimeFormatUtil.formatTime(currentTime, "yyyy-MM-dd HH:mm:ss"); +//// zarr2Tif.setStart_timestamp(stamp); +//// String newZarrPath = serviceNameDir + File.separator + "result_" + currentTime + ".zarr"; +// String newZarrPath = serviceNameDir + File.separator + "result.zarr"; +// zarr2Tif.setZarr_file(newZarrPath); +// zarr2Tif.setGeotiff_dir(serviceNameDir + File.separator + "depth_" + currentTime); +// File newZarr2TifJson = new File(serviceNameDir, "zarr2tif_" + currentTime + ".json"); +// mapper.writeValue(newZarr2TifJson, zarr2Tif); +// return newZarr2TifJson; +// } - } +// private File generateNewZarrConfigFile(File serviceNameDir, String serviceName, long currentTime, File newDatFile) throws IOException { +// File configFile = new File(serviceNameDir, serviceName + ".json"); +// ConfigVo configVo = mapper.readValue(configFile, ConfigVo.class); +// int simulateTime = 300; //妯℃嫙鏃堕棿锛岄粯璁や负5min锛屽嵆300s +// int intervalTime = 60; //姣忓抚鐨勯棿闅旀椂闂达紝榛樿涓�60s,60s鐢熸垚涓�甯� +// configVo.getRaingage().set(0, newDatFile.getAbsolutePath()); //raingage file +// ResultVo result = configVo.getResult(); +// Integer oldDuration = configVo.getDuration(); +//// result.setSave_start(oldDuration); //璧峰鏃堕棿瑕佸湪涓婃鏃堕棿鐨勫熀纭�涓婂紑濮� +// configVo.setDuration(oldDuration + simulateTime); //鍥哄畾涓�5min +// result.setSave_interval(intervalTime); +// result.setSave_frames(result.getSave_frames() + (simulateTime / intervalTime)); //淇濈暀5甯э紝鍦ㄥ師鏉ョ殑鍩虹涓婂鍔�5甯� +//// String newZarrPath = serviceNameDir + File.separator + "result_" + currentTime + ".zarr"; +// String newZarrPath = serviceNameDir + File.separator + "result.zarr"; +// result.setSave_name(newZarrPath); +// File newConfigFile = new File(serviceNameDir, currentTime + ".json"); +// mapper.writeValue(newConfigFile, configVo); +// return newConfigFile; +// } - private String generateLayerJsonAndPng(String serviceName, File serviceNameDir, long currentTime) throws IOException { - ResultDto resultDto = new ResultDto(); - resultDto.setServiceName(serviceName); - File temp = Paths.get(config.getOutPath(), serviceName, "temp").toFile(); - if (!temp.exists()) temp.mkdir(); - resultDto.setTemp(temp.getAbsolutePath()); - resultDto.setOutPath(Paths.get(config.getOutPath(), serviceName).toString()); - File dem = new File(serviceNameDir, "DEM.tif"); - resultDto.setTerrainFile(dem.getAbsolutePath()); - File newDepthDir = new File(serviceNameDir + File.separator + "depth_" + currentTime); - resultDto.setWaterPath(newDepthDir.getAbsolutePath()); - LayerDto layerDto = new LayerDto(config.getVer(), 4548, config.getSizes()); - String newLayerJsonName = "layer_" + currentTime + ".json"; - layerDto.setName(newLayerJsonName); - testService.processRealTime(resultDto, layerDto); - return newLayerJsonName; - } +// private File generateNewRainfallFile(RealTimeInput input, String[] values, File serviceNameDir, long currentTime) throws IOException { +// String station = values[0]; +// double lon = Double.parseDouble(values[1]); +// double lat = Double.parseDouble(values[2]); +// String title = config.getRainfallTitle(); +// List<String> newLines = new ArrayList<>(); +// newLines.add(title); +// List<RealTimeInput.RealTimeData> data = input.getData(); +// for (RealTimeInput.RealTimeData rd : data) { +// LocalDateTime dateTime = rd.getDateTime(); +// int year = dateTime.getYear(); +// int month = dateTime.getMonthValue(); +// int day = dateTime.getDayOfMonth(); +// int hour = dateTime.getHour(); +// int minute = dateTime.getMinute(); +// double intensity = rd.getIntensity(); //淇濈暀鎸囧畾浣嶆暟灏忔暟 +// String l = String.format("%s %s %s %s %s %s %s %s %s", +// station, lon, lat, year, month, day, hour, minute, String.format("%.6f", intensity)); +// newLines.add(l); +// } +// File newDatFile = new File(serviceNameDir, "rainfall_" + currentTime + ".dat"); +// if (!newDatFile.exists()) newDatFile.createNewFile(); +// Files.write(newDatFile.toPath(), newLines, StandardOpenOption.TRUNCATE_EXISTING); +// return newDatFile; +// } - private File generateNewZarr2TifJson(File serviceNameDir, long currentTime) throws IOException { - File srcZarr2TifJson = new File(serviceNameDir, "zarr2tif.json"); - Zarr2Tif zarr2Tif = mapper.readValue(srcZarr2TifJson, Zarr2Tif.class); - //淇敼zarr2tif瀵硅薄涓殑瀛楁 -// String stamp = TimeFormatUtil.formatTime(currentTime, "yyyy-MM-dd HH:mm:ss"); -// zarr2Tif.setStart_timestamp(stamp); -// String newZarrPath = serviceNameDir + File.separator + "result_" + currentTime + ".zarr"; - String newZarrPath = serviceNameDir + File.separator + "result.zarr"; - zarr2Tif.setZarr_file(newZarrPath); - zarr2Tif.setGeotiff_dir(serviceNameDir + File.separator + "depth_" + currentTime); - File newZarr2TifJson = new File(serviceNameDir, "zarr2tif_" + currentTime + ".json"); - mapper.writeValue(newZarr2TifJson, zarr2Tif); - return newZarr2TifJson; - } - - private File generateNewZarrConfigFile(File serviceNameDir, String serviceName, long currentTime, File newDatFile) throws IOException { - File configFile = new File(serviceNameDir, serviceName + ".json"); - ConfigVo configVo = mapper.readValue(configFile, ConfigVo.class); - int simulateTime = 300; //妯℃嫙鏃堕棿锛岄粯璁や负5min锛屽嵆300s - int intervalTime = 60; //姣忓抚鐨勯棿闅旀椂闂达紝榛樿涓�60s,60s鐢熸垚涓�甯� - configVo.getRaingage().set(0, newDatFile.getAbsolutePath()); //raingage file - ResultVo result = configVo.getResult(); - Integer oldDuration = configVo.getDuration(); -// result.setSave_start(oldDuration); //璧峰鏃堕棿瑕佸湪涓婃鏃堕棿鐨勫熀纭�涓婂紑濮� - configVo.setDuration(oldDuration + simulateTime); //鍥哄畾涓�5min - result.setSave_interval(intervalTime); - result.setSave_frames(result.getSave_frames() + (simulateTime / intervalTime)); //淇濈暀5甯э紝鍦ㄥ師鏉ョ殑鍩虹涓婂鍔�5甯� -// String newZarrPath = serviceNameDir + File.separator + "result_" + currentTime + ".zarr"; - String newZarrPath = serviceNameDir + File.separator + "result.zarr"; - result.setSave_name(newZarrPath); - File newConfigFile = new File(serviceNameDir, currentTime + ".json"); - mapper.writeValue(newConfigFile, configVo); - return newConfigFile; - } - - private File generateNewRainfallFile(RealTimeInput input, String[] values, File serviceNameDir, long currentTime) throws IOException { - String station = values[0]; - double lon = Double.parseDouble(values[1]); - double lat = Double.parseDouble(values[2]); - String title = config.getRainfallTitle(); - List<String> newLines = new ArrayList<>(); - newLines.add(title); - List<RealTimeInput.RealTimeData> data = input.getData(); - for (RealTimeInput.RealTimeData rd : data) { - LocalDateTime dateTime = rd.getDateTime(); - int year = dateTime.getYear(); - int month = dateTime.getMonthValue(); - int day = dateTime.getDayOfMonth(); - int hour = dateTime.getHour(); - int minute = dateTime.getMinute(); - double intensity = rd.getIntensity(); //淇濈暀鎸囧畾浣嶆暟灏忔暟 - String l = String.format("%s %s %s %s %s %s %s %s %s", - station, lon, lat, year, month, day, hour, minute, String.format("%.6f", intensity)); - newLines.add(l); - } - File newDatFile = new File(serviceNameDir, "rainfall_" + currentTime + ".dat"); - if (!newDatFile.exists()) newDatFile.createNewFile(); - Files.write(newDatFile.toPath(), newLines, StandardOpenOption.TRUNCATE_EXISTING); - return newDatFile; - } - - private static String[] readTheOldFirstLineRainfallValue(File serviceNameDir) throws IOException { - File srcRailfallFile = new File(serviceNameDir, "rainfall.dat"); - List<String> lines = Files.readAllLines(srcRailfallFile.toPath()); - String secondLine = lines.get(1); - return secondLine.split(" "); - } +// private static String[] readTheOldFirstLineRainfallValue(File serviceNameDir) throws IOException { +// File srcRailfallFile = new File(serviceNameDir, "rainfall.dat"); +// List<String> lines = Files.readAllLines(srcRailfallFile.toPath()); +// String secondLine = lines.get(1); +// return secondLine.split(" "); +// } } diff --git a/src/main/java/com/se/nsl/service/SimuService.java b/src/main/java/com/se/nsl/service/SimuService.java index ccc2b3d..b98d3ee 100644 --- a/src/main/java/com/se/nsl/service/SimuService.java +++ b/src/main/java/com/se/nsl/service/SimuService.java @@ -13,12 +13,9 @@ import com.se.nsl.helper.StringHelper; import com.se.nsl.mapper.SimuMapper; import com.se.nsl.utils.CoordinateTransformer; +import com.se.nsl.utils.SolverTifUtil; 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.gdalconst.gdalconstConstants; import org.springframework.stereotype.Service; import ucar.ma2.InvalidRangeException; @@ -36,7 +33,7 @@ @SuppressWarnings("ALL") public class SimuService { public static final String TIF_EXTSION = ".tif"; - public static final String YYYY_MM_DD_HHMMSS = "yyyyMMddHHmmss"; + public static final String YYYY_MM_DD_HH_MM_SS = "yyyyMMddHHmmss"; @Resource SimuMapper simuMapper; @@ -56,9 +53,7 @@ Page<Simu> page = new Page<>(pageNum, pageSize); page.addOrder(OrderItem.desc("id")); - IPage<Simu> paged = simuMapper.selectPage(page, wrapper); - - return paged; + return simuMapper.selectPage(page, wrapper); } private QueryWrapper<Simu> getPageWrapper(SimuVo vo, int pageNum, int pageSize) { @@ -144,7 +139,7 @@ public List<SimuResult> queryByPosition(double lon, double lat, String serviceName) { //transform coordiante from 4326 to 4548 - double[] xy = CoordinateTransformer.transform(4326, 4548, lon, lat); + double[] xy = CoordinateTransformer.transform(4326, config.getEpsg(), lon, lat); // System.out.println(String.format("杞崲鍓嶇殑鍧愭爣锛歺:%s,y:%s", lon, lat)); // System.out.println(String.format("杞崲鍚庣殑鍧愭爣锛歺:%s,y:%s", xy[0], xy[1])); //read from zarr @@ -172,8 +167,7 @@ // index++; // } File dem = new File(inPath, serviceName + File.separator + "DEM.tif"); - ColumnRow cr = getColumnRow(dem.getAbsoluteFile(), x, y); - if (cr == null) return null; + //TODO File zarr = new File(inPath, serviceName + File.separator + "result.zarr"); try { @@ -186,14 +180,10 @@ } catch (IOException | InvalidRangeException e) { throw new RuntimeException(e); } - - return null; } private SimuResult queryByTif(double[] xy, long time, String serviceName) { - double x = xy[0]; - double y = xy[1]; File inPath = new File(config.getInPath()); String prefix = formatTime(time); String child = serviceName + File.separator + "depth" + File.separator + prefix + TIF_EXTSION; @@ -201,22 +191,10 @@ if (!tifFile.exists()) { return null; } - - ColumnRow cr = getColumnRow(tifFile, x, y); - if (cr == null) return null; -// System.out.println("col:" + cr.col + " ,row:" + cr.row); - float depth = readPixelValue(cr.dataset, cr.col, cr.row, 1); - float velocity = calcVelocity(cr.dataset, cr.col, cr.row); - SimuResult result = new SimuResult(); - result.setDepth(depth); - result.setVelocity(velocity); - result.setTime(time); - return result; + return SolverTifUtil.getSimuResult(tifFile, xy); } private List<SimuResult> queryByTif(double[] xy, String serviceName) { - double x = xy[0]; - double y = xy[1]; List<SimuResult> res = new ArrayList<>(); File inPath = new File(config.getInPath()); Path depthPath = Paths.get(inPath.getAbsolutePath(), serviceName, "depth"); @@ -225,75 +203,15 @@ for (File tifFile : files) { String name = tifFile.getName(); if (!name.endsWith(TIF_EXTSION)) continue; - ColumnRow cr = getColumnRow(tifFile, x, y); - if (cr == null) continue; - float depth = readPixelValue(cr.dataset, cr.col, cr.row, 1); - float velocity = calcVelocity(cr.dataset, cr.col, cr.row); - SimuResult result = new SimuResult(); - result.setDepth(depth); - result.setVelocity(velocity); - String time = tifFile.getName().replace(".tif", ""); - result.setTime(TimeFormatUtil.toMillis(time, YYYY_MM_DD_HHMMSS)); + SimuResult result = SolverTifUtil.getSimuResult(tifFile, xy); + if (result == null) continue; res.add(result); } - return res; - } - - private static ColumnRow getColumnRow(File tifFile, double x, double y) { - Dataset dataset = gdal.Open(tifFile.getAbsolutePath(), gdalconstConstants.GA_ReadOnly); - // 鑾峰彇鍦扮悊鍙樻崲鍙傛暟锛�6鍏冪礌鏁扮粍锛� - // [0]: 宸︿笂瑙扻鍧愭爣, [1]: 鍍忓厓瀹藉害, [2]: X鏂瑰悜鏃嬭浆, - // [3]: 宸︿笂瑙扽鍧愭爣, [4]: Y鏂瑰悜鏃嬭浆, [5]: 鍍忓厓楂樺害锛堣礋鍊艰〃绀篩杞村悜涓嬶級 - double[] geoTransform = dataset.GetGeoTransform(); - //璁$畻鏍呮牸琛屽垪鍙� - int col = (int) ((x - geoTransform[0]) / geoTransform[1]); - int row = (int) ((geoTransform[3] - y) / Math.abs(geoTransform[5])); - int width = dataset.getRasterXSize(); - int height = dataset.getRasterYSize(); - if (col < 0 || col > width || row < 0 || row > height) { - log.warn("琛屽垪鍙蜂笉鍦╰if鑼冨洿鍐�"); - return null; - } - return new ColumnRow(dataset, col, row); - } - - private static class ColumnRow { - public final Dataset dataset; - public final int col; - public final int row; - - public ColumnRow(Dataset dataset, int col, int row) { - this.dataset = dataset; - this.col = col; - this.row = row; - } - } - - private float calcVelocity(Dataset dataset, int col, int row) { - float x = readPixelValue(dataset, col, row, 2); - float y = readPixelValue(dataset, col, row, 3); - float velocity = 0f; - if (Float.isNaN(x) && Float.isNaN(y)) { - velocity = 0f; - } else if (Float.isNaN(x)) { - velocity = y; - } else if (Float.isNaN(y)) { - velocity = x; - } else { - velocity = (float) Math.sqrt(x * x + y * y); - } - return velocity; - } - - private float readPixelValue(Dataset dataset, int col, int row, int bandNum) { - Band band = dataset.GetRasterBand(bandNum); - float[] values = new float[1]; - band.ReadRaster(col, row, 1, 1, values); - return values[0]; + return res; } private String formatTime(long time) { - return TimeFormatUtil.formatTime(time, YYYY_MM_DD_HHMMSS); + return TimeFormatUtil.formatTime(time, YYYY_MM_DD_HH_MM_SS); } } diff --git a/src/main/java/com/se/nsl/utils/SolverTifUtil.java b/src/main/java/com/se/nsl/utils/SolverTifUtil.java new file mode 100644 index 0000000..f9f4adc --- /dev/null +++ b/src/main/java/com/se/nsl/utils/SolverTifUtil.java @@ -0,0 +1,104 @@ +package com.se.nsl.utils; + +import com.se.nsl.domain.vo.SimuResult; +import lombok.extern.slf4j.Slf4j; +import org.gdal.gdal.Band; +import org.gdal.gdal.Dataset; +import org.gdal.gdal.gdal; +import org.gdal.gdalconst.gdalconstConstants; + +import java.io.File; + +//姹傝В鍣ㄤ骇鐢熺殑tif鏂囦欢澶勭悊宸ュ叿 +@Slf4j +public class SolverTifUtil { + + private SolverTifUtil() {} + + private static final String YYYY_MM_DD_HH_MM_SS = "yyyyMMddHHmmss"; + + private static class ColumnRow { + public final Dataset dataset; + public final int col; + public final int row; + + public ColumnRow(Dataset dataset, int col, int row) { + this.dataset = dataset; + this.col = col; + this.row = row; + } + } + + public static SimuResult getSimuResult(File tifFile, double[] position) { + double x = position[0]; + double y = position[1]; + ColumnRow cr = getColumnRow(tifFile, x, y); + if (cr == null) return null; + float depth = readPixelValue(cr.dataset, cr.col, cr.row, 1); + float velocity = calcVelocity(cr.dataset, cr.col, cr.row); + SimuResult result = new SimuResult(); + result.setDepth(depth); + result.setVelocity(velocity); + String time = tifFile.getName().replace(".tif", ""); + result.setTime(TimeFormatUtil.toMillis(time, YYYY_MM_DD_HH_MM_SS)); + return result; + } + + public static float getDepth(File tifFile, double[] position) { + double x = position[0]; + double y = position[1]; + ColumnRow cr = getColumnRow(tifFile, x, y); + if (cr == null) return 0; + return readPixelValue(cr.dataset, cr.col, cr.row, 1); + } + + public static float getValue(File tifFile, double[] position, int band) { + double x = position[0]; + double y = position[1]; + ColumnRow cr = getColumnRow(tifFile, x, y); + if (cr == null) return 0; + return readPixelValue(cr.dataset, cr.col, cr.row, band); + } + + private static ColumnRow getColumnRow(File tifFile, double x, double y) { + Dataset dataset = gdal.Open(tifFile.getAbsolutePath(), gdalconstConstants.GA_ReadOnly); + // 鑾峰彇鍦扮悊鍙樻崲鍙傛暟锛�6鍏冪礌鏁扮粍锛� + // [0]: 宸︿笂瑙扻鍧愭爣, [1]: 鍍忓厓瀹藉害, [2]: X鏂瑰悜鏃嬭浆, + // [3]: 宸︿笂瑙扽鍧愭爣, [4]: Y鏂瑰悜鏃嬭浆, [5]: 鍍忓厓楂樺害锛堣礋鍊艰〃绀篩杞村悜涓嬶級 + double[] geoTransform = dataset.GetGeoTransform(); + //璁$畻鏍呮牸琛屽垪鍙� + int col = (int) ((x - geoTransform[0]) / geoTransform[1]); + int row = (int) ((geoTransform[3] - y) / Math.abs(geoTransform[5])); + int width = dataset.getRasterXSize(); + int height = dataset.getRasterYSize(); + if (col < 0 || col > width || row < 0 || row > height) { + log.warn("琛屽垪鍙蜂笉鍦╰if鑼冨洿鍐�"); + return null; + } + return new ColumnRow(dataset, col, row); + } + + private static float calcVelocity(Dataset dataset, int col, int row) { + float x = readPixelValue(dataset, col, row, 2); + float y = readPixelValue(dataset, col, row, 3); + float velocity; + if (Float.isNaN(x) && Float.isNaN(y)) { + velocity = 0f; + } else if (Float.isNaN(x)) { + velocity = y; + } else if (Float.isNaN(y)) { + velocity = x; + } else { + velocity = (float) Math.sqrt(x * x + y * y); + } + return velocity; + } + + private static float readPixelValue(Dataset dataset, int col, int row, int bandNum) { + Band band = dataset.GetRasterBand(bandNum); + float[] values = new float[1]; + band.ReadRaster(col, row, 1, 1, values); + return values[0]; + } + +} diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 818ca20..5e119e7 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -175,7 +175,7 @@ token: YjhhYjAwOWFhMjk1MTM1ZDA0NGU3ZWZlMDQzMzUzZDE1MGJmY2Q4ZWEyYjliNjQzZjcwMjhlNDY0ZjAxNWZjOTZmNzMwYmNmZDA2YmVmNTIzNjU0ZDgzODRjYTUxYTM1 realTimeInterval: 5 #璇锋眰闆ㄩ噺璁℃暟鎹椂锛屾椂闂磋寖鍥寸浉宸灏戝垎閽� - requestOffsetMinutes: 10 + requestOffsetMinutes: 15 #寰�鍓嶅亸绉诲灏戝ぉ锛屼负浜嗘柟渚垮疄鏃舵ā鎷熶互鍓嶇殑闄嶉洦鎯呭喌,璁剧疆涓�0琛ㄧず锛屼粠褰撳墠鏃堕棿寮�濮嬭绠� offsetDays: 0 -- Gitblit v1.9.3