| | |
| | | package com.moon.server.service.data; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.moon.server.entity.all.StaticData; |
| | | import com.moon.server.entity.data.AnalysisResultEntity; |
| | | import com.moon.server.entity.data.MetaEntity; |
| | | import com.moon.server.entity.data.PublishEntity; |
| | | import com.moon.server.helper.GeoHelper; |
| | | import com.moon.server.helper.PathHelper; |
| | | import com.moon.server.service.all.WebSocketService; |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | | import org.gdal.gdal.Band; |
| | |
| | | import javax.annotation.Resource; |
| | | import java.awt.geom.Point2D; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.util.*; |
| | | |
| | | /** |
| | |
| | | PublishService publishService; |
| | | |
| | | private final static Log log = LogFactory.getLog(RasterAnalysisService.class); |
| | | |
| | | /** |
| | | * 测试 |
| | | */ |
| | | public List<AnalysisResultEntity> test(Geometry geo, Integer size) { |
| | | List<AnalysisResultEntity> rs = new ArrayList<>(); |
| | | AnalysisResultEntity entity = new AnalysisResultEntity(); |
| | | entity.setLayerName("Test"); |
| | | |
| | | openRaster(entity, "D:\\Moon\\data\\DOM\\Lunar_LRO_LOLA_ClrShade_Global_128ppd_v04_2.tif", geo, size); |
| | | rs.add(entity); |
| | | |
| | | return rs; |
| | | } |
| | | |
| | | /** |
| | | * 使用WKT查询分析,结果以消息推送 |
| | | */ |
| | | public void analysisForPost(Geometry geo, Integer size, String token) { |
| | | List<PublishEntity> pubs = publishService.selectRaster(); |
| | | if (null == pubs || pubs.isEmpty()) { |
| | | return; |
| | | } |
| | | |
| | | // for (PublishEntity pub : pubs) { |
| | | pubs.parallelStream().forEach(pub -> { |
| | | try { |
| | | AnalysisResultEntity entity = new AnalysisResultEntity(token); |
| | | entity.setLayerName(pub.getName()); |
| | | |
| | | List<MetaEntity> metas = publishService.selectMetasByPubid(pub.getId()); |
| | | if (null == metas || metas.isEmpty()) { |
| | | setError(entity, "找不到发布数据"); |
| | | postInfo(entity); |
| | | return; |
| | | } |
| | | |
| | | String filePath = pathHelper.getConfig().getUploadPath() + File.separator + metas.get(0).getPath(); |
| | | File file = new File(filePath); |
| | | if (!file.exists() || file.isDirectory()) { |
| | | setError(entity, "源数据不存在"); |
| | | postInfo(entity); |
| | | return; |
| | | } |
| | | |
| | | openRaster(entity, filePath, geo, size); |
| | | postInfo(entity); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 推送消息 |
| | | */ |
| | | private void postInfo(AnalysisResultEntity entity) throws IOException { |
| | | JSONObject map = new JSONObject(); |
| | | map.put("analysisForPost", entity); |
| | | |
| | | String json = JSONObject.toJSONString(map); |
| | | WebSocketService.broadCastInfo(json); |
| | | } |
| | | |
| | | /** |
| | | * 分析方法 |
| | |
| | | // double rotationX = transform[2]; double rotationY = transform[4] |
| | | double minX = transform[0], pixelWidth = transform[1], maxY = transform[3], pixelHeight = transform[5]; |
| | | |
| | | double len = 0; |
| | | int bandCount = ds.getRasterCount(); |
| | | double lineDis = getPolylineDistance(geo); |
| | | double distance = lineDis / Math.max(geo.GetPointCount(), size); |
| | | |
| | | List<double[]> points = getPointsByDistance(geo, distance); |
| | | for (double[] xy : points) { |
| | | for (int i = 0, c = points.size(); i < c; i++) { |
| | | double[] xy = points.get(i); |
| | | if (i > 0) { |
| | | double[] p = points.get(i - 1); |
| | | len += GeoHelper.getDistance(p[0], p[1], xy[0], xy[1]); |
| | | } |
| | | |
| | | int xPixel = (int) Math.floor((xy[0] - minX) / pixelWidth); |
| | | int yPixel = (int) Math.floor((maxY - xy[1]) / Math.abs(pixelHeight)); |
| | | |
| | | List<Double> vals = new ArrayList<>(); |
| | | for (int i = 1; i <= bandCount; i++) { |
| | | for (int j = 1; j <= bandCount; j++) { |
| | | double[] pixelValues = new double[1]; |
| | | ds.GetRasterBand(i).ReadRaster(xPixel, yPixel, 1, 1, pixelValues); |
| | | ds.GetRasterBand(j).ReadRaster(xPixel, yPixel, 1, 1, pixelValues); |
| | | vals.add(pixelValues[0]); |
| | | } |
| | | entity.addPoint(xy[0], xy[1], vals); |
| | | entity.addPoint(xy[0], xy[1], len, vals); |
| | | } |
| | | } |
| | | |
| | |
| | | int yMaxPixel = (int) Math.floor((maxY - env[2]) / pixelHeight); |
| | | |
| | | int bandCount = ds.getRasterCount(); |
| | | int geoWidth = Math.abs(xMaxPixel - xMinPixel); |
| | | int geoHeight = Math.abs(yMaxPixel - yMinPixel); |
| | | int width = Math.abs(xMaxPixel - xMinPixel); |
| | | int height = Math.abs(yMaxPixel - yMinPixel); |
| | | |
| | | if (width * height > StaticData.I64 * StaticData.I64) { |
| | | readRasterForBlocks(entity, ds, bandCount, xMinPixel, yMinPixel, width, height); |
| | | return; |
| | | } |
| | | |
| | | for (int i = 1; i <= bandCount; i++) { |
| | | double[] pixelValues = new double[geoWidth * geoHeight]; |
| | | ds.GetRasterBand(i).ReadRaster(xMinPixel, yMinPixel, geoWidth, geoHeight, pixelValues); |
| | | |
| | | double[] pixelValues = new double[width * height]; |
| | | ds.GetRasterBand(i).ReadRaster(xMinPixel, yMinPixel, width, height, pixelValues); |
| | | setBandVals(entity, pixelValues); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 按照块读取栅格数据 |
| | | */ |
| | | private void readRasterForBlocks(AnalysisResultEntity entity, Dataset ds, int bandCount, int xMinPixel, int yMinPixel, int width, int height) { |
| | | List<Integer> xList = getSamples(xMinPixel, width); |
| | | List<Integer> yList = getSamples(yMinPixel, height); |
| | | |
| | | double[] pixelValues = new double[1]; |
| | | for (int i = 1; i <= bandCount; i++) { |
| | | List<Double> list = new ArrayList<>(); |
| | | for (Integer x : xList) { |
| | | for (Integer y : yList) { |
| | | ds.GetRasterBand(i).ReadRaster(x, y, 1, 1, pixelValues); |
| | | list.add(pixelValues[0]); |
| | | } |
| | | } |
| | | setBandVals(entity, list); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取抽样列表 |
| | | */ |
| | | private List<Integer> getSamples(int start, int strip) { |
| | | List<Integer> list = new ArrayList<>(); |
| | | |
| | | double avg = 1.0 * strip / (StaticData.I64 - 1); |
| | | for (int i = 0; i < StaticData.I64; i++) { |
| | | list.add(start + (int) Math.ceil(avg * i)); |
| | | } |
| | | |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | |
| | | for (double val : pixelValues) { |
| | | list.add(val); |
| | | } |
| | | setBandVals(entity, list); |
| | | } |
| | | |
| | | /** |
| | | * 设置Band值 |
| | | */ |
| | | private void setBandVals(AnalysisResultEntity entity, List<Double> list) { |
| | | double min = Collections.min(list); |
| | | double max = Collections.max(list); |
| | | double avg = list.stream().mapToDouble(Double::valueOf).average().getAsDouble(); |