| | |
| | | import org.gdal.gdal.Band; |
| | | import org.gdal.gdal.Dataset; |
| | | import org.gdal.gdal.gdal; |
| | | import org.gdal.gdalconst.gdalconst; |
| | | import org.gdal.gdalconst.gdalconstConstants; |
| | | import org.gdal.ogr.*; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.util.CollectionUtils; |
| | |
| | | @Resource |
| | | PropertiesConfig config; |
| | | |
| | | public final static double MAX_X_OFFSET = 0; |
| | | public static final double MAX_X_OFFSET = 0; |
| | | |
| | | public final static SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm"); |
| | | public static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm"); |
| | | |
| | | public void process(DataPo data) throws Exception { |
| | | String basePath = config.getInPath() + File.separator + data.getInPath() + File.separator; |
| | |
| | | public void process(ResultDto dto, LayerDto layer) throws Exception { |
| | | try { |
| | | copeTerrain(dto, layer); |
| | | copeBuilding(dto, layer); |
| | | copeBuilding(dto); |
| | | List<BuildingDepthVo> buildings = copeWater(dto, layer); |
| | | copeFlow(dto, layer); |
| | | copeLayerJson(dto, layer); |
| | | copeRainFallJson(dto, layer); |
| | | copeRainFallJson(dto); |
| | | copeBuildingDepthJson(dto, buildings); |
| | | } finally { |
| | | File dir = new File(dto.getTemp()); |
| | |
| | | public void copeTerrain(ResultDto dto, LayerDto layer) { |
| | | Dataset ds = null; |
| | | try { |
| | | ds = gdal.Open(dto.getTerrainFile(), gdalconst.GA_ReadOnly); |
| | | ds = gdal.Open(dto.getTerrainFile(), gdalconstConstants.GA_ReadOnly); |
| | | if (null == ds || 0 == ds.getRasterCount()) return; |
| | | |
| | | setTerrainInfo(ds, layer); |
| | |
| | | if (!new File(tif).exists()) continue; |
| | | |
| | | String png = terrainPath + File.separator + sizes[0] + "_" + sizes[1] + ".png"; |
| | | Terrain2Png(layer, tif, png, sizes[0], sizes[1]); |
| | | 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, gdalconst.GA_ReadOnly); |
| | | ds = gdal.Open(tif, gdalconstConstants.GA_ReadOnly); |
| | | if (null == ds || 0 == ds.getRasterCount()) return; |
| | | |
| | | Band band = ds.GetRasterBand(1); |
| | | float[] buffer = new float[width * height]; |
| | | //band.ReadRaster(0, 0, width, height, buffer, width, height, 0, 0); |
| | | band.ReadRaster(0, 0, width, height, buffer); |
| | | 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 { |
| | |
| | | } |
| | | |
| | | Color color = new Color(r, g, b, 127); |
| | | //graphic.drawImage(image, x, y, 1, 1, color, null); |
| | | image.setRGB(x, y, color.getRGB()); |
| | | } |
| | | } |
| | |
| | | } |
| | | } |
| | | |
| | | public void copeBuilding(ResultDto dto, LayerDto layer) { |
| | | public void copeBuilding(ResultDto dto) { |
| | | Driver driver = null; |
| | | DataSource dataSource = null; |
| | | Layer shpLayer = null; |
| | |
| | | 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); |
| | |
| | | |
| | | 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); |
| | | |
| | |
| | | public List<String> getFiles(String path, String suffix) { |
| | | List<String> files = new ArrayList<>(); |
| | | ComHelper.getFiles(files, new File(path), suffix); |
| | | files.sort((a, b) -> a.compareToIgnoreCase(b)); |
| | | files.sort(String::compareToIgnoreCase); |
| | | |
| | | return files; |
| | | } |
| | |
| | | files.parallelStream().forEach(file -> { |
| | | Dataset ds = null; |
| | | try { |
| | | ds = gdal.Open(file, gdalconst.GA_ReadOnly); |
| | | ds = gdal.Open(file, gdalconstConstants.GA_ReadOnly); |
| | | if (null == ds || 0 == ds.getRasterCount()) return; |
| | | |
| | | double[] mm = new double[2]; |
| | |
| | | if (null != ds) ds.delete(); |
| | | } |
| | | }); |
| | | //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)); |
| | |
| | | for (int i = 0, c = files.size(); i < c; i++) { |
| | | Dataset ds = null; |
| | | try { |
| | | ds = gdal.Open(files.get(i), gdalconst.GA_ReadOnly); |
| | | ds = gdal.Open(files.get(i), gdalconstConstants.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)); |
| | | if (config.getCopyTif()) copyWaterTif(dto, ds, layer.getWaters().getData().get(i)); |
| | | createVectors(dto, ds, layer, layer.getWaters().getData().get(i)); |
| | | createVectors(dto, ds, layer.getWaters().getData().get(i)); |
| | | } finally { |
| | | if (null != ds) ds.delete(); |
| | | } |
| | |
| | | if (!new File(tif).exists()) continue; |
| | | |
| | | String png = waterPath + File.separator + sizes[0] + "_" + sizes[1] + ".png"; |
| | | water2Png(dto, layer, tif, png, sizes[0], sizes[1]); |
| | | water2Png(layer, tif, png, sizes[0], sizes[1]); |
| | | } |
| | | } |
| | | |
| | | public void water2Png(ResultDto dto, LayerDto layer, String tif, String png, int width, int height) { |
| | | public void water2Png(LayerDto layer, String tif, String png, int width, int height) { |
| | | Dataset ds = null; |
| | | try { |
| | | ds = gdal.Open(tif, gdalconst.GA_ReadOnly); |
| | | ds = gdal.Open(tif, gdalconstConstants.GA_ReadOnly); |
| | | if (null == ds || 0 == ds.getRasterCount()) return; |
| | | |
| | | Band band = ds.GetRasterBand(1); |
| | | float[] buffer = new float[width * height]; |
| | | band.ReadRaster(0, 0, width, height, buffer); |
| | | double[] transform = ds.GetGeoTransform(); |
| | | |
| | | 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); |
| | | for (int x = 0; x < width; x++) { |
| | | for (int y = 0; y < height; y++) { |
| | |
| | | //BuildingDto building = intersects(dto, X, Y); |
| | | //if (null != building) continue; |
| | | |
| | | int r = 0, g, b; |
| | | int r = 0; |
| | | int g; |
| | | int b; |
| | | if (buffer[offset] + ts[offset] > maxHeight) { |
| | | g = b = 255; |
| | | } else { |
| | |
| | | for (int i = 0, c = files.size(); i < c; i++) { |
| | | Dataset ds = null; |
| | | try { |
| | | ds = gdal.Open(files.get(i), gdalconst.GA_ReadOnly); |
| | | ds = gdal.Open(files.get(i), gdalconstConstants.GA_ReadOnly); |
| | | if (null == ds || 0 == ds.getRasterCount()) continue; |
| | | if (null == ds.GetSpatialRef()) ds.SetSpatialRef(dto.getSpatialReference()); |
| | | |
| | |
| | | |
| | | 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]); |
| | | double minX = transform[0]; |
| | | double pixelWidth = transform[1]; |
| | | double maxY = transform[3]; |
| | | double pixelHeight = Math.abs(transform[5]); |
| | | |
| | | for (BuildingDto building : dto.getBuildingList()) { |
| | | Double val = getValue(ds, building, xSize, ySize, minX, maxY, pixelWidth, pixelHeight); |
| | | Double val = getValue(ds, building, 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) { |
| | | public Double getValue(Dataset ds, BuildingDto building, double minX, double maxY, double pixelWidth, double pixelHeight) { |
| | | double[] env = new double[4]; |
| | | building.getGeom().GetEnvelope(env); |
| | | |
| | |
| | | FileUtil.copyFile(source, target); |
| | | } |
| | | |
| | | public void createVectors(ResultDto dto, Dataset ds, LayerDto layer, Long ticks) { |
| | | public void createVectors(ResultDto dto, Dataset ds, 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); |
| | | List<PondingPo> list = copePonding(ds, geometries); |
| | | if (CollectionUtils.isEmpty(list)) return; |
| | | |
| | | try { |
| | |
| | | } |
| | | |
| | | public List<Geometry> getGeometries(String filePath) { |
| | | if (!FileUtil.exist(filePath)) return null; |
| | | if (!FileUtil.exist(filePath)) return Collections.emptyList(); |
| | | |
| | | Driver driver = null; |
| | | DataSource dataSource = null; |
| | | org.gdal.ogr.Layer layer = null; |
| | | try { |
| | | driver = ogr.GetDriverByName("GeoJSON"); |
| | | if (null == driver) return null; |
| | | if (null == driver) return Collections.emptyList(); |
| | | |
| | | DataSource ds = driver.Open(filePath); |
| | | if (null == ds) return null; |
| | | if (null == ds) return Collections.emptyList(); |
| | | |
| | | layer = ds.GetLayer(0); |
| | | List<Geometry> list = new ArrayList<>(); |
| | |
| | | return list; |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | return null; |
| | | return Collections.emptyList(); |
| | | } finally { |
| | | GdalHelper.delete(layer, dataSource, driver); |
| | | } |
| | | } |
| | | |
| | | public List<PondingPo> copePonding(ResultDto dto, Dataset ds, LayerDto layer, List<Geometry> geometries) { |
| | | public List<PondingPo> copePonding(Dataset ds, 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); |
| | | PointDto point = getValues(ds, geometry, transform); |
| | | 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) { |
| | | public PointDto getValues(Dataset ds, Geometry g, double[] transform) { |
| | | double[] env = new double[4]; |
| | | g.GetEnvelope(env); |
| | | |
| | |
| | | for (int i = 0, c = files.size(); i < c; i++) { |
| | | Dataset ds = null; |
| | | try { |
| | | ds = gdal.Open(files.get(i), gdalconst.GA_ReadOnly); |
| | | ds = gdal.Open(files.get(i), gdalconstConstants.GA_ReadOnly); |
| | | if (null == ds || 0 == ds.getRasterCount()) return; |
| | | if (null == ds.GetSpatialRef()) ds.SetSpatialRef(dto.getSpatialReference()); |
| | | |
| | |
| | | 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, gdalconst.GA_ReadOnly); |
| | | 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]; |
| | | float[] vxBuffer = new float[width * height]; |
| | | float[] vyBuffer = new float[width * height]; |
| | | ds.GetRasterBand(1).ReadRaster(0, 0, width, height, vxBuffer); |
| | | ds.GetRasterBand(2).ReadRaster(0, 0, width, height, vyBuffer); |
| | | |
| | |
| | | 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"; |
| | | |
| | | ComHelper.writeJson(filePath, json); |
| | | } |
| | | |
| | | public void copeRainFallJson(ResultDto dto, LayerDto layer) throws IOException, ParseException { |
| | | public void copeRainFallJson(ResultDto dto) throws IOException, ParseException { |
| | | String rainGageFilePath = config.getInPath() + File.separator + dto.getServiceName() + File.separator + "RainGage.dat"; |
| | | String filePath = dto.getOutPath() + File.separator + "rainfall.json"; |
| | | |
| | | Map<String, Double> map = new LinkedHashMap<>(); |
| | | FileReader fr = new FileReader(rainGageFilePath); |
| | | BufferedReader br = new BufferedReader(fr); |
| | | try (FileReader fr = new FileReader(rainGageFilePath); |
| | | BufferedReader br = new BufferedReader(fr)) { |
| | | |
| | | String line = br.readLine(); |
| | | while ((line = br.readLine()) != null) { |
| | | String[] rainFall = line.split(" "); |
| | | if (rainFall.length < 7) continue; |
| | | String line = br.readLine(); |
| | | while ((line = br.readLine()) != null) { |
| | | String[] rainFall = line.split(" "); |
| | | if (rainFall.length < 7) continue; |
| | | |
| | | String sdt = rainFall[1] + "-" + rainFall[2] + "-" + rainFall[3] + " " + rainFall[4] + ":" + rainFall[5]; |
| | | BigDecimal num = new BigDecimal(rainFall[6]); |
| | | map.put("" + SDF.parse(sdt).getTime(), num.setScale(2, RoundingMode.HALF_UP).doubleValue()); |
| | | String sdt = rainFall[1] + "-" + rainFall[2] + "-" + rainFall[3] + " " + rainFall[4] + ":" + rainFall[5]; |
| | | BigDecimal num = new BigDecimal(rainFall[6]); |
| | | map.put("" + SDF.parse(sdt).getTime(), num.setScale(2, RoundingMode.HALF_UP).doubleValue()); |
| | | } |
| | | } |
| | | br.close(); |
| | | fr.close(); |
| | | |
| | | String json = JSON.toJSONString(map); |
| | | ComHelper.writeJson(filePath, json); |
| | | } |