| | |
| | | import com.moon.server.entity.data.PublishEntity; |
| | | import com.moon.server.helper.GeoHelper; |
| | | import com.moon.server.helper.PathHelper; |
| | | import com.moon.server.helper.StringHelper; |
| | | import com.moon.server.helper.WebHelper; |
| | | import com.moon.server.service.all.WebSocketService; |
| | | import org.apache.commons.logging.Log; |
| | |
| | | import org.gdal.gdal.gdal; |
| | | import org.gdal.ogr.Geometry; |
| | | import org.gdal.ogr.ogr; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.awt.geom.Point2D; |
| | | import java.io.File; |
| | | import java.io.FileWriter; |
| | | import java.io.IOException; |
| | | import java.util.*; |
| | | |
| | |
| | | */ |
| | | @Service |
| | | public class RasterAnalysisService { |
| | | @Value("${sys.gdal_path}") |
| | | String gdalPath; |
| | | |
| | | @Resource |
| | | PathHelper pathHelper; |
| | | |
| | |
| | | |
| | | /** |
| | | * 测试 |
| | | * <p> |
| | | * POINT (26.72 -48.58) |
| | | * LINESTRING(18.44 -46.69,20.14 -45.93) |
| | | * POLYGON ((5.11 -41.64,7.68 -43.59,4.73 -43.3,5.11 -41.64)) |
| | | * POLYGON ((42.6 -40.3,49.1 -40.4,49.1 -65.7,46.3 -65.6,42.6 -40.3)) |
| | | */ |
| | | public List<AnalysisResultEntity> test(Geometry geo, Integer size) { |
| | | List<AnalysisResultEntity> rs = new ArrayList<>(); |
| | |
| | | return; |
| | | } |
| | | |
| | | // for (PublishEntity pub : pubs) { |
| | | //for (PublishEntity pub : pubs) { |
| | | pubs.parallelStream().forEach(pub -> { |
| | | try { |
| | | AnalysisResultEntity entity = new AnalysisResultEntity(token); |
| | | entity.setLayerName(pub.getName()); |
| | | entity.setUnit(pub.getUnit()); |
| | | |
| | | List<MetaEntity> metas = publishService.selectMetasByPubid(pub.getId()); |
| | | if (null == metas || metas.isEmpty()) { |
| | |
| | | return; |
| | | } |
| | | |
| | | String filePath = pathHelper.getConfig().getUploadPath() + File.separator + metas.get(0).getPath(); |
| | | String filePath = getFilePath(metas); |
| | | File file = new File(filePath); |
| | | if (!file.exists() || file.isDirectory()) { |
| | | setError(entity, "源数据不存在"); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 获取文件路径 |
| | | */ |
| | | private String getFilePath(List<MetaEntity> metas) throws IOException { |
| | | String basePath = pathHelper.getConfig().getUploadPath() + File.separator; |
| | | if (metas.size() == 1) { |
| | | return basePath + metas.get(0).getPath(); |
| | | } |
| | | |
| | | String tempPath = pathHelper.getTempPath() + File.separator, guid = StringHelper.getGuid(); |
| | | String fileList = tempPath + guid + ".txt"; |
| | | FileWriter fw = new FileWriter(fileList); |
| | | for (int i = 0, c = metas.size(); i < c; i++) { |
| | | String str = (i > 0 ? "\r\n" : "") + basePath + metas.get(i).getPath(); |
| | | fw.write(str.toCharArray()); |
| | | } |
| | | fw.close(); |
| | | |
| | | String vrt = tempPath + guid + ".vrt"; |
| | | String cmd = String.format("\"%s\\gdalbuildvrt.exe\" -input_file_list \"%s\" \"%s\"", gdalPath, fileList, vrt); |
| | | WebHelper.exec(cmd); |
| | | |
| | | return vrt; |
| | | } |
| | | |
| | | /** |
| | | * 推送消息 |
| | | */ |
| | | private void postInfo(AnalysisResultEntity entity) throws IOException { |
| | |
| | | map.put("analysisForPost", entity); |
| | | |
| | | String json = JSONObject.toJSONString(map); |
| | | // System.out.println(json) |
| | | //System.out.println(json) |
| | | |
| | | WebSocketService.broadCastInfo(json); |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | for (PublishEntity pub : pubs) { |
| | | AnalysisResultEntity entity = new AnalysisResultEntity(); |
| | | entity.setLayerName(pub.getName()); |
| | | try { |
| | | AnalysisResultEntity entity = new AnalysisResultEntity(); |
| | | entity.setLayerName(pub.getName()); |
| | | entity.setUnit(pub.getUnit()); |
| | | |
| | | List<MetaEntity> metas = publishService.selectMetasByPubid(pub.getId()); |
| | | if (null == metas || metas.isEmpty()) { |
| | | setError(entity, "找不到发布数据"); |
| | | continue; |
| | | List<MetaEntity> metas = publishService.selectMetasByPubid(pub.getId()); |
| | | if (null == metas || metas.isEmpty()) { |
| | | setError(entity, "找不到发布数据"); |
| | | continue; |
| | | } |
| | | |
| | | String filePath = getFilePath(metas); |
| | | File file = new File(filePath); |
| | | if (!file.exists() || file.isDirectory()) { |
| | | setError(entity, "源数据不存在"); |
| | | continue; |
| | | } |
| | | |
| | | openRaster(entity, filePath, geo, size); |
| | | rs.add(entity); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | } |
| | | |
| | | String filePath = pathHelper.getConfig().getUploadPath() + File.separator + metas.get(0).getPath(); |
| | | File file = new File(filePath); |
| | | if (!file.exists() || file.isDirectory()) { |
| | | setError(entity, "源数据不存在"); |
| | | continue; |
| | | } |
| | | |
| | | openRaster(entity, filePath, geo, size); |
| | | rs.add(entity); |
| | | } |
| | | |
| | | return rs; |
| | | } |
| | | |
| | |
| | | for (int j = 1; j <= bandCount; j++) { |
| | | double[] pixelValues = new double[1]; |
| | | ds.GetRasterBand(j).ReadRaster(xPixel, yPixel, 1, 1, pixelValues); |
| | | if (!Double.isNaN(pixelValues[0])) { |
| | | if (isValid(pixelValues[0])) { |
| | | vals.add(WebHelper.round(pixelValues[0], 3)); |
| | | } |
| | | } |
| | |
| | | for (Integer x : xList) { |
| | | for (Integer y : yList) { |
| | | ds.GetRasterBand(i).ReadRaster(x, y, 1, 1, pixelValues); |
| | | if (!Double.isNaN(pixelValues[0])) { |
| | | if (isValid(pixelValues[0])) { |
| | | list.add(pixelValues[0]); |
| | | } |
| | | } |
| | |
| | | |
| | | List<Double> list = new ArrayList<>(); |
| | | for (double val : pixelValues) { |
| | | if (!Double.isNaN(val)) { |
| | | if (isValid(val)) { |
| | | list.add(val); |
| | | } |
| | | } |
| | |
| | | } |
| | | |
| | | /** |
| | | * 值是否有效 |
| | | */ |
| | | public static boolean isValid(Double val) { |
| | | return !Double.isNaN(val) && val > Integer.MIN_VALUE; |
| | | } |
| | | |
| | | /** |
| | | * 设置Band值 |
| | | */ |
| | | private void setBandVals(AnalysisResultEntity entity, List<Double> list) { |