From 0890b7861feae74bdcfd1851e577db6b9f31d484 Mon Sep 17 00:00:00 2001 From: xingjinshuang <xingjs@qq.com> Date: 星期四, 20 二月 2025 14:40:39 +0800 Subject: [PATCH] @xingjs@20250220@添加处理sww相关接口类 --- src/main/java/com/se/simu/service/WaterService.java | 269 +++++++++++++++++++++++++++++++---------------------- 1 files changed, 159 insertions(+), 110 deletions(-) diff --git a/src/main/java/com/se/simu/service/WaterService.java b/src/main/java/com/se/simu/service/WaterService.java index 1c05a40..46c7c96 100644 --- a/src/main/java/com/se/simu/service/WaterService.java +++ b/src/main/java/com/se/simu/service/WaterService.java @@ -1,40 +1,39 @@ package com.se.simu.service; +import cn.hutool.core.io.FileUtil; +import cn.hutool.json.JSONUtil; import com.se.simu.config.PropertiesConfig; +import com.se.simu.domain.po.DataPo; +import com.se.simu.domain.po.PondingPo; +import com.se.simu.domain.po.SimuPo; import com.se.simu.domain.vo.*; +import com.se.simu.helper.GdalHelper; +import com.se.simu.helper.StringHelper; 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.gdal.gdalconst.gdalconst; +import org.gdal.ogr.*; import org.gdal.osr.SpatialReference; import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; import javax.annotation.Resource; import java.io.File; import java.io.FileInputStream; -import java.util.ArrayList; -import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; -/** - * 鍐呮稘鏈嶅姟绫� - * - * @author WWW - * @date 2024-07-16 - */ @Slf4j @Service +@SuppressWarnings("ALL") public class WaterService { @Resource PropertiesConfig config; - /** - * 鑾峰彇鍏冩暟鎹俊鎭� - */ - public byte[] getLayerJson(String serviceName) { + public byte[] getson(String serviceName, String json) { try { - String filePath = config.getOutPath() + File.separator + serviceName + File.separator + "layer.json"; + String filePath = config.getOutPath() + File.separator + serviceName + File.separator + json; File dat = new File(filePath); if (!dat.exists()) { @@ -49,138 +48,188 @@ return bytes; } catch (Exception ex) { + log.error(ex.getMessage(), ex); return null; } } - /** - * 鑾峰彇鍦板舰楂樺害鍥� - */ public String getTerraMap(String serviceName, Integer width, Integer height) { return config.getOutPath() + File.separator + serviceName + File.separator + "terrain" + File.separator + width + "_" + height + ".png"; } - /** - * 鑾峰彇姘撮潰楂樺害鍥� - */ public String getWaterMap(String serviceName, Integer width, Integer height, Long timestamp) { return config.getOutPath() + File.separator + serviceName + File.separator + "waters" + File.separator + timestamp + File.separator + width + "_" + height + ".png"; } - /** - * 鑾峰彇姘存祦鍚戞祦閫熷浘 - */ public String getFlowMap(String serviceName, Integer width, Integer height, Long timestamp) { return config.getOutPath() + File.separator + serviceName + File.separator + "flows" + File.separator + timestamp + File.separator + width + "_" + height + ".png"; } - /** - * 鑾峰彇鍥惧眰 * - */ - public Layer getLayer(String serviceName) { - Layer layer = new Layer(); - layer.setVersion(config.getVer()); - layer.setDuration(new Duration(1719812810225L, 1719812810225L)); - layer.setExtension(new Extension(2.11062743358, 0.53812160220, 2.11070827834, 0.53819799453, 1.151, 38.83)); + public Double getWaterHeight(SimuPo simu, double x, double y, Long timestamp) { + String filePath = config.getOutPath() + File.separator + simu.getServiceName() + File.separator + "waters" + + File.separator + timestamp + File.separator + "water.tif"; + if (!FileUtil.exist(filePath)) return null; - List<Integer[]> sizes = new ArrayList<>(); - sizes.add(new Integer[]{64, 64}); - sizes.add(new Integer[]{128, 128}); - sizes.add(new Integer[]{256, 256}); - sizes.add(new Integer[]{512, 512}); - sizes.add(new Integer[]{1024, 1024}); - sizes.add(new Integer[]{2048, 2048}); - layer.setTerrain(new Terrain(sizes)); - - List<Long> data = new ArrayList<>(Arrays.asList(1719812812225L, 1719812812225L, 1719812812225L, 1719812812225L, 1719812812225L, 1719812812225L)); - layer.setWaters(new Water(data)); - - return layer; - } - - /** - * 鑾峰彇闄嶆按鏇茬嚎鏂囦欢鏇茬嚎鍥� - */ - public byte[] getRainfall(String serviceName) { + Dataset ds = null; try { - String filePath = config.getOutPath() + File.separator + serviceName + File.separator + "rainfall.json"; + ds = gdal.Open(filePath, gdalconst.GA_ReadOnly); + if (null == ds || ds.getRasterCount() < 1) { + return null; + } + if (null == ds.GetSpatialRef()) { + ds.SetSpatialRef(getSpatialRef(simu)); + } - File rainfall = new File(filePath); - if (!rainfall.exists()) { + double[] gt = ds.GetGeoTransform(); + double[] xy = GdalHelper.fromWgs84(ds.GetSpatialRef(), x, y); + int[] XY = coordinates2ColRow(gt, xy[0], xy[1]); + + if (XY[0] < 0 || XY[1] < 0 || XY[0] > ds.getRasterXSize() || XY[1] > ds.getRasterYSize()) { return null; } - byte[] bytes = new byte[(int) rainfall.length()]; + double[] vals = new double[1]; + ds.GetRasterBand(1).ReadRaster(XY[0], XY[1], 1, 1, vals); - FileInputStream fs = new FileInputStream(filePath); - fs.read(bytes); - fs.close(); - - return bytes; + return isValid(vals[0]) ? vals[0] : null; } catch (Exception ex) { + log.error(ex.getMessage(), ex); return null; + } finally { + if (null != ds) ds.delete(); } } - /** - * 鏍规嵁鍧愭爣鏌ヨ绉按娣卞害 - */ - public Double getWaterHeight(String serviceName, Double x, Double y, Long timestamp) { - String filePath = config.getOutPath() + File.separator + serviceName + File.separator + "waters" - + File.separator + timestamp + File.separator + "water.tif"; + private SpatialReference getSpatialRef(SimuPo simu) { + DataPo data = JSONUtil.toBean(simu.getData(), DataPo.class); - // 棣栧厛鏄墍鏈塯dal绋嬪簭閮介渶瑕佺殑娉ㄥ唽璇彞 - gdal.AllRegister(); - // 璇诲彇褰卞儚 - Dataset hDataset = gdal.Open(filePath, gdalconstConstants.GA_Update); + return data.getSpatialReference(); + } - // 璁剧疆鏁版嵁闆嗙殑鎶曞奖 - SpatialReference srs = new SpatialReference(); - srs.ImportFromEPSG(4548); - hDataset.SetProjection(srs.ExportToWkt()); + public int[] coordinates2ColRow(double[] gt, double x, double y) { + //Double col = Math.floor(((y - gt[3]) * gt[1] - (x - gt[0]) * gt[4]) / (gt[5] * gt[1] - gt[2] * gt[4])); + Double col = Math.floor((y * gt[1] - x * gt[4] + gt[0] * gt[4] - gt[3] * gt[1]) / (gt[5] * gt[1] - gt[2] * gt[4])); + Double row = Math.floor((x - gt[0] - col * gt[2]) / gt[1]); - //鑾峰彇鏍呮牸鏁伴噺 - int numBands=hDataset.GetRasterCount(); - System.out.println("RasterCount: " + numBands); - //鏋勯�犱豢灏勫彉鎹㈠弬鏁版暟缁勶紝骞惰幏鍙栨暟鎹� - double[] gt = new double[6]; - hDataset.GetGeoTransform(gt); - System.out.println("浠垮皠鍙樻崲鍙傛暟"+ Arrays.toString(gt)); + return new int[]{row.intValue(), col.intValue()}; + } - //缁忕含搴﹁浆鎹负鏍呮牸鍍忕礌鍧愭爣 - int[] ColRow=Coordinates2ColRow(gt,x,y); + public static boolean isValid(double val) { + return !Double.isNaN(val) && val > Integer.MIN_VALUE; + } - //鍒ゆ柇鏄惁鍧愭爣瓒呭嚭鑼冨洿 - if(ColRow[0]<0||ColRow[1]<0||ColRow[0]>hDataset.getRasterXSize()||ColRow[1]>hDataset.getRasterYSize()){ - System.out.println(Arrays.toString(ColRow)+"鍧愭爣鍊艰秴鍑烘爡鏍艰寖鍥达紒"); + public Double getWaterArea(SimuPo simu, double x, double y, Long timestamp) { + List<PondingPo> list = readWaterJson(simu.getServiceName(), timestamp); + if (CollectionUtils.isEmpty(list)) return null; + + //DataPo data = JSONUtil.toBean(simu.getData(), DataPo.class); + //SpatialReference sr = GdalHelper.createSpatialReference(data.getEpsg()); + + Geometry p = new Geometry(ogr.wkbPoint); + p.AddPoint_2D(x, y); + p.AssignSpatialReference(GdalHelper.SR4326); + + for (PondingPo po : list) { + if (StringHelper.isEmpty(po.getPolygon())) continue; + + Geometry polygon = Geometry.CreateFromWkt(po.getPolygon()); + polygon.AssignSpatialReference(GdalHelper.SR4326); + if (polygon.Contains(p)) { + return po.getArea(); + } + } + + return null; + } + + private List<PondingPo> readWaterJson(String serviceName, Long timestamp) { + String filePath = config.getOutPath() + File.separator + serviceName + File.separator + "waters" + File.separator + timestamp + File.separator + "water.json"; + String json = getText(filePath); + if (StringHelper.isEmpty(json)) { return null; } - //鑾峰彇璇ョ偣瀵瑰簲鐨勬尝娈电殑鍊� - Band band = hDataset.GetRasterBand(1); - double[] values = new double[1]; - band.ReadRaster(ColRow[0], ColRow[1], 1, 1, values); - double value = values[0]; - - //閲婃斁璧勬簮 - hDataset.delete(); - return value; + return JSONUtil.toList(json, PondingPo.class); } - /** - * 灏嗗湴鍥惧潗鏍囪浆鎹负鏍呮牸鍍忕礌鍧愭爣 - * @param gt 浠垮皠鍙樻崲鍙傛暟 - * @param X 妯潗鏍� - * @param Y 绾靛潗鏍� - * @return - */ - public int[] Coordinates2ColRow(double[] gt, double X, double Y){ - int[] ints = new int[2]; - //鍚戜笅鍙栨暣,濡傛灉鍚戜笂鍙栨暣浼氬鑷磋绠楃粨鏋滃亸澶э紝浠庤�屽湪鍚庨潰璇诲彇鍒伴偦杩戝儚鍏冪殑鏁版嵁 - double Yline = Math.floor(((Y - gt[3])*gt[1] - (X-gt[0])*gt[4]) / (gt[5]*gt[1]-gt[2]*gt[4])); - double Xpixel = Math.floor((X-gt[0] - Yline*gt[2])/gt[1]); - ints[0] = new Double(Xpixel).intValue(); - ints[1] = new Double(Yline).intValue(); - return ints; + public Double getWaterArea2(SimuPo simu, double x, double y, Long timestamp) { + String filePath = config.getOutPath() + File.separator + simu.getServiceName() + File.separator + "waters" + + File.separator + timestamp + File.separator + "water.geojson"; + 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); + double[] xy = GdalHelper.fromWgs84(layer.GetSpatialRef(), x, y); + + Geometry p = new Geometry(ogr.wkbPoint); + p.AddPoint_2D(xy[0], xy[1]); + p.AssignSpatialReference(layer.GetSpatialRef()); + + for (long i = 0, d = layer.GetFeatureCount(); i < d; i++) { + Feature f = layer.GetFeature(i); + if (f.GetGeometryRef().Intersects(p)) { + /*f.GetFieldAsDouble("val"); + Geometry g = f.GetGeometryRef(); + GdalHelper.fromWgs84(layer.GetSpatialRef(), g); + Double area= g.GetArea();*/ + + return f.GetGeometryRef().Area(); + } + } + + return null; + } catch (Exception ex) { + log.error(ex.getMessage(), ex); + return null; + } finally { + GdalHelper.delete(layer, dataSource, driver); + } + } + + public List<BuildingDepthVo> getBuildingDepthBySeid(String serviceName, String seid) { + List<BuildingDepthVo> list = readBuildingJson(serviceName); + if (CollectionUtils.isEmpty(list)) return null; + + return list.parallelStream() + .filter(b -> seid.equals(b.getId())) + .sorted((a, b) -> a.getTimestamp().compareTo(b.getTimestamp())) + .collect(Collectors.toList()); + } + + public List<BuildingDepthVo> getBuildingDepthByTime(String serviceName, Long timestamp) { + List<BuildingDepthVo> list = readBuildingJson(serviceName); + if (CollectionUtils.isEmpty(list)) { + return null; + } + + return list.parallelStream().filter(b -> timestamp.equals(b.getTimestamp())).collect(Collectors.toList()); + } + + private List<BuildingDepthVo> readBuildingJson(String serviceName) { + String filePath = config.getOutPath() + File.separator + serviceName + File.separator + "building.json"; + String json = getText(filePath); + if (StringHelper.isEmpty(json)) { + return null; + } + + return JSONUtil.toList(json, BuildingDepthVo.class); + } + + private String getText(String filePath) { + File file = new File(filePath); + if (!file.exists()) { + return null; + } + + return FileUtil.readUtf8String(file); } } -- Gitblit v1.9.3