| | |
| | | package com.se.simu.service; |
| | | |
| | | import cn.hutool.json.JSONUtil; |
| | | import com.se.simu.config.PropertiesConfig; |
| | | import com.se.simu.domain.po.DataPo; |
| | | import com.se.simu.domain.po.SimuPo; |
| | | import com.se.simu.domain.vo.*; |
| | | import com.se.simu.helper.GdalHelper; |
| | | 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.osr.SpatialReference; |
| | | import org.gdal.osr.osr; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | |
| | | */ |
| | | @Slf4j |
| | | @Service |
| | | @SuppressWarnings("ALL") |
| | | public class WaterService { |
| | | @Resource |
| | | PropertiesConfig config; |
| | |
| | | } |
| | | |
| | | /** |
| | | * 根据坐标查询积水深度 |
| | | * 根据坐标查询积水深度:gdalconst.GA_Update |
| | | */ |
| | | public Double getWaterHeight(String serviceName, Double x, Double y, Long timestamp) { |
| | | String filePath = config.getOutPath() + File.separator + serviceName + File.separator + "waters" |
| | | 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"; |
| | | |
| | | // 首先是所有gdal程序都需要的注册语句 |
| | | gdal.AllRegister(); |
| | | // 读取影像 |
| | | Dataset hDataset = gdal.Open(filePath, gdalconstConstants.GA_Update); |
| | | Dataset ds = null; |
| | | try { |
| | | ds = gdal.Open(filePath, gdalconst.GA_ReadOnly); |
| | | if (null == ds || ds.getRasterCount() < 1) { |
| | | return null; |
| | | } |
| | | if (null == ds.GetSpatialRef()) { |
| | | ds.SetSpatialRef(getSpatialRef(simu)); |
| | | } |
| | | |
| | | // 设置数据集的投影 |
| | | SpatialReference srs = new SpatialReference(); |
| | | srs.ImportFromEPSG(4548); |
| | | hDataset.SetProjection(srs.ExportToWkt()); |
| | | double[] gt = ds.GetGeoTransform(); |
| | | double[] xy = GdalHelper.fromWgs84(ds.GetSpatialRef(), x, y); |
| | | int[] XY = coordinates2ColRow(gt, xy[0], xy[1]); |
| | | |
| | | //获取栅格数量 |
| | | int numBands=hDataset.GetRasterCount(); |
| | | System.out.println("RasterCount: " + numBands); |
| | | //构造仿射变换参数数组,并获取数据 |
| | | double[] gt = new double[6]; |
| | | hDataset.GetGeoTransform(gt); |
| | | System.out.println("仿射变换参数"+ Arrays.toString(gt)); |
| | | if (XY[0] < 0 || XY[1] < 0 || XY[0] > ds.getRasterXSize() || XY[1] > ds.getRasterYSize()) { |
| | | return null; |
| | | } |
| | | |
| | | //经纬度转换为栅格像素坐标 |
| | | int[] ColRow=Coordinates2ColRow(gt,x,y); |
| | | double[] values = new double[1]; |
| | | ds.GetRasterBand(1).ReadRaster(XY[0], XY[1], 1, 1, values); |
| | | double val = values[0]; |
| | | |
| | | //判断是否坐标超出范围 |
| | | if(ColRow[0]<0||ColRow[1]<0||ColRow[0]>hDataset.getRasterXSize()||ColRow[1]>hDataset.getRasterYSize()){ |
| | | System.out.println(Arrays.toString(ColRow)+"坐标值超出栅格范围!"); |
| | | return isValid(val) ? val : null; |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | return null; |
| | | } finally { |
| | | if (null != ds) ds.delete(); |
| | | } |
| | | } |
| | | |
| | | //获取该点对应的波段的值 |
| | | Band band = hDataset.GetRasterBand(1); |
| | | double[] values = new double[1]; |
| | | band.ReadRaster(ColRow[0], ColRow[1], 1, 1, values); |
| | | double value = values[0]; |
| | | private SpatialReference getSpatialRef(SimuPo simu) { |
| | | DataPo data = JSONUtil.toBean(simu.getData(), DataPo.class); |
| | | |
| | | //释放资源 |
| | | hDataset.delete(); |
| | | return value; |
| | | SpatialReference sr = new SpatialReference(); |
| | | sr.ImportFromEPSG(data.getEpsg()); |
| | | sr.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER); |
| | | |
| | | return sr; |
| | | } |
| | | |
| | | /** |
| | | * 将地图坐标转换为栅格像素坐标 |
| | | * |
| | | * @param gt 仿射变换参数 |
| | | * @param X 横坐标 |
| | | * @param Y 纵坐标 |
| | | * @return |
| | | * @param x 横坐标 |
| | | * @param y 纵坐标 |
| | | * @return 像素坐标 |
| | | */ |
| | | public int[] Coordinates2ColRow(double[] gt, double X, double Y){ |
| | | 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; |
| | | |
| | | // 向下取整,如果向上取整会导致计算结果偏大,从而在后面读取到邻近像元的数据 |
| | | //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]); |
| | | |
| | | return new int[]{row.intValue(), col.intValue()}; |
| | | } |
| | | |
| | | public static boolean isValid(double val) { |
| | | return !Double.isNaN(val) && val > Integer.MIN_VALUE; |
| | | } |
| | | } |