¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.se.nsl.service; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.metadata.OrderItem; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.bc.zarr.ZarrArray; |
| | | import com.bc.zarr.ZarrGroup; |
| | | import com.se.nsl.config.PropertiesConfig; |
| | | import com.se.nsl.domain.po.Simu; |
| | | import com.se.nsl.domain.vo.SimuResult; |
| | | import com.se.nsl.domain.vo.SimuVo; |
| | | import com.se.nsl.helper.StringHelper; |
| | | import com.se.nsl.mapper.SimuMapper; |
| | | import com.se.nsl.utils.CoordinateTransformer; |
| | | 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; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.time.Instant; |
| | | import java.time.LocalDateTime; |
| | | import java.time.ZoneId; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | |
| | | @Slf4j |
| | | @Service |
| | | @SuppressWarnings("ALL") |
| | | public class SimuService { |
| | | public static final String TIF_EXTSION = ".tif"; |
| | | @Resource |
| | | SimuMapper simuMapper; |
| | | |
| | | @Resource |
| | | PropertiesConfig config; |
| | | |
| | | /** |
| | | * å页æ¥è¯¢æ¨æ¼æ¨¡æ |
| | | * |
| | | * @param pageNum 页ç |
| | | * @param pageSize æ¯é¡µæ°é |
| | | * @return å页åçæ¨æ¼æ¨¡æ |
| | | */ |
| | | public IPage<Simu> selectPage(SimuVo vo, int pageNum, int pageSize) { |
| | | QueryWrapper<Simu> wrapper = getPageWrapper(vo, pageNum, pageSize); |
| | | |
| | | Page<Simu> page = new Page<>(pageNum, pageSize); |
| | | page.addOrder(OrderItem.desc("id")); |
| | | |
| | | IPage<Simu> paged = simuMapper.selectPage(page, wrapper); |
| | | |
| | | return paged; |
| | | } |
| | | |
| | | private QueryWrapper<Simu> getPageWrapper(SimuVo vo, int pageNum, int pageSize) { |
| | | QueryWrapper<Simu> wrapper = new QueryWrapper<>(); |
| | | if (null != vo.getId()) { |
| | | wrapper.eq("id", vo.getId()); |
| | | } |
| | | if (!StringHelper.isEmpty(vo.getName())) { |
| | | wrapper.like("lower(name)", vo.getName().trim().toLowerCase()); |
| | | } |
| | | if (!StringHelper.isEmpty(vo.getServiceName())) { |
| | | wrapper.like("service_name", vo.getServiceName().trim()); |
| | | } |
| | | if (null != vo.getType()) { |
| | | wrapper.eq("type", vo.getType()); |
| | | } |
| | | if (null != vo.getAreaType()) { |
| | | wrapper.eq("area_type", vo.getAreaType()); |
| | | } |
| | | if (null != vo.getStatus()) { |
| | | wrapper.eq("status", vo.getStatus()); |
| | | } |
| | | |
| | | return wrapper; |
| | | } |
| | | |
| | | /** |
| | | * æ ¹æ®IDæ¹éå 餿¨æ¼æ¨¡æ |
| | | * |
| | | * @param ids è¦å é¤çåºåIDå表 |
| | | * @return å 餿åçè®°å½æ° |
| | | */ |
| | | public int deleteByIds(List<Integer> ids) { |
| | | return simuMapper.deleteBatchIds(ids); |
| | | } |
| | | |
| | | /** |
| | | * æ°å¢æ¨æ¼æ¨¡æ |
| | | * |
| | | * @param Simu æ¨æ¼æ¨¡æå¯¹è±¡ |
| | | * @return æ°å¢æåçè®°å½æ° |
| | | */ |
| | | public int insert(Simu simu) { |
| | | return simuMapper.insert(simu); |
| | | } |
| | | |
| | | public int inserts(List<Simu> list) { |
| | | return simuMapper.inserts(list); |
| | | } |
| | | |
| | | /** |
| | | * æ ¹æ®IDæ¥è¯¢ |
| | | * |
| | | * @param id ID |
| | | * @return Simu |
| | | */ |
| | | public Simu selectById(Integer id) { |
| | | return simuMapper.selectById(id); |
| | | } |
| | | |
| | | /** |
| | | * ä¿®æ¹æ¨æ¼æ¨¡æ |
| | | * |
| | | * @param Simu æ¨æ¼æ¨¡æå¯¹è±¡ |
| | | * @return ä¿®æ¹æåçè®°å½æ° |
| | | */ |
| | | public int updateById(Simu simu) { |
| | | return simuMapper.updates(Arrays.asList(simu)); |
| | | } |
| | | |
| | | public SimuResult queryByPosition(double lon, double lat, long time, String serviceName) { |
| | | //transform coordiante from 4326 to 4548 |
| | | double[] xy = CoordinateTransformer.transform(4326, 4548, lon, lat); |
| | | // System.out.println(String.format("转æ¢åçåæ ï¼x:%s,y:%s", lon, lat)); |
| | | // System.out.println(String.format("转æ¢åçåæ ï¼x:%s,y:%s", xy[0], xy[1])); |
| | | //read from zarr |
| | | // return queryByZarr(xy, time, serviceName); |
| | | |
| | | //read from tif file |
| | | return queryByTif(xy, time, serviceName); |
| | | |
| | | } |
| | | |
| | | private SimuResult queryByZarr(double[] xy, long time, String serviceName) { |
| | | double x = xy[0]; |
| | | double y = xy[1]; |
| | | String prefix = formatTime(time); |
| | | File inPath = new File(config.getInPath()); |
| | | // File tifDir = new File(inPath, serviceName + File.separator + "depth"); |
| | | // int index = 0; |
| | | // File[] files = tifDir.listFiles(); |
| | | // for (File file : files) { |
| | | // String name = file.getName(); |
| | | // if (!name.endsWith(TIF_EXTSION)) continue; |
| | | // if (name.equals(prefix + TIF_EXTSION)) { |
| | | // break; |
| | | // } |
| | | // index++; |
| | | // } |
| | | File dem = new File(inPath, serviceName + File.separator + "DEM.tif"); |
| | | ColumnRow cr = getColumnRow(dem.getAbsoluteFile(), x, y); |
| | | if (cr == null) return null; |
| | | |
| | | File zarr = new File(inPath, serviceName + File.separator + "result.zarr"); |
| | | try { |
| | | ZarrGroup group = ZarrGroup.open(zarr.toPath()); |
| | | ZarrArray depthArray = group.openArray("depth"); |
| | | int[] shape = new int[] {60, 637, 351}; |
| | | // int[] offset = new int[]{210, 384}; |
| | | float[] depth = (float[]) depthArray.read(shape); |
| | | System.out.println("depth:" + depth.length); |
| | | } 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; |
| | | File tifFile = new File(inPath, child); |
| | | 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); |
| | | return result; |
| | | } |
| | | |
| | | private static ColumnRow getColumnRow(File tifFile, double x, double y) { |
| | | Dataset dataset = gdal.Open(tifFile.getAbsolutePath(), gdalconstConstants.GA_ReadOnly); |
| | | // è·åå°ç忢忰ï¼6å
ç´ æ°ç»ï¼ |
| | | // [0]: å·¦ä¸è§Xåæ , [1]: åå
宽度, [2]: Xæ¹åæè½¬, |
| | | // [3]: å·¦ä¸è§Yåæ , [4]: Yæ¹åæè½¬, [5]: åå
é«åº¦ï¼è´å¼è¡¨ç¤ºYè½´åä¸ï¼ |
| | | 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("è¡åå·ä¸å¨tifèå´å
"); |
| | | 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]; |
| | | } |
| | | |
| | | private String formatTime(long time) { |
| | | // Instant instant = Instant.ofEpochMilli(time); |
| | | // LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); |
| | | // DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); |
| | | // return localDateTime.format(formatter); |
| | | return TimeFormatUtil.formatTime(time, "yyyyMMddHHmmss"); |
| | | } |
| | | |
| | | } |