月球大数据地理空间分析展示平台-【后端】-月球后台服务
1
13693261870
2024-11-17 796b44ea813a1133beae4f3a67f1c0263510c0c7
src/main/java/com/moon/server/service/data/SlopeAnalysisService.java
@@ -2,7 +2,7 @@
import com.moon.server.entity.all.StaticData;
import com.moon.server.entity.data.PointEntity;
import com.moon.server.helper.FileHelper;
import com.moon.server.helper.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.gdal.gdal.Dataset;
@@ -12,40 +12,33 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import java.util.*;
/**
 * 栅格分析服务
 * @author WWW
 * @date 2023-11-16
 */
@Service
@SuppressWarnings("ALL")
public class SlopeAnalysisService {
    @Value("${sys.path.slopFile}")
    private String filePath;
    @Resource
    protected PathHelper pathHelper;
    private String xlsTemplate;
    private final static Log log = LogFactory.getLog(SlopeAnalysisService.class);
    /**
     * 获取Excel模板
     */
    private String getXlsTemplate() {
    private String getXlsTemplate() throws IOException {
        if (null == xlsTemplate) {
            xlsTemplate = FileHelper.getClassPath() + File.separator + "config" + File.separator + "slop.xlsx";
            xlsTemplate = FileHelper.getClassPathForWar() + File.separator + "config" + File.separator + "slope.xlsx";
        }
        return xlsTemplate;
    }
    /**
     * 下载坡度分析Excel
     */
    public void downloadSlopXls(Geometry polygon, HttpServletResponse res) throws Exception {
        Dataset ds = null;
        try {
@@ -59,7 +52,13 @@
                throw new Exception("分析结果为空(查询范围无效)");
            }
            //
            String xlsFile = createXls(list, getXlsTemplate());
            if (null == xlsFile || !new File(xlsFile).exists()) {
                throw new Exception("创建坡度分析Excel失败");
            }
            String fileName = FileHelper.getFileName(xlsFile);
            WebHelper.download(xlsFile, fileName, res);
        } finally {
            if (null != ds) {
                ds.delete();
@@ -67,9 +66,6 @@
        }
    }
    /**
     * 分析多边形
     */
    public List<PointEntity> analysisPolygon(Geometry geo, Dataset ds) {
        double[] transform = ds.GetGeoTransform();
        int xSize = ds.getRasterXSize(), ySize = ds.getRasterYSize();
@@ -97,11 +93,22 @@
                ds.GetRasterBand(StaticData.I1).ReadRaster(x, y, 1, 1, values);
                if (!Double.isNaN(values[0])) {
                    list.add(new PointEntity(point.GetY(), point.GetX(), values[0]));
                    list.add(new PointEntity(point.GetX(), point.GetY(), values[0]));
                }
            }
        }
        return list;
    }
    public String createXls(List<PointEntity> list, String template) {
        String target = pathHelper.getTempPath() + File.separator + "slope_" + StringHelper.YMD2_FORMAT.format(new Date()) + ".xlsx";
        Map<String, List<PointEntity>> map = new HashMap<>(1);
        map.put("data", list);
        ExcelHelper.writeToTemplate(template, target, map);
        return target;
    }
}