管道基础大数据平台系统开发-【后端】-Server
13693261870
2024-02-29 36b2f13fff583e0dbc94ee40e9fe4ae6e75af4e0
修改JSHelper类
已修改4个文件
已删除2个文件
187 ■■■■ 文件已修改
pom.xml 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/lf/server/helper/JsHelper.java 17 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/lf/server/helper/PathHelper.java 13 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/lf/server/service/sys/ReportService.java 32 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/static/html/turf.html 35 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/static/js/turf.min.6.5.js 88 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pom.xml
@@ -260,7 +260,7 @@
            <artifactId>proj4j</artifactId>
            <version>0.1.0</version>
        </dependency>
        <!-- JTS -->
        <!--JTS-->
        <dependency>
            <groupId>org.locationtech.jts</groupId>
            <artifactId>jts-core</artifactId>
src/main/java/com/lf/server/helper/JsHelper.java
@@ -6,6 +6,8 @@
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.io.File;
import java.io.FileReader;
/**
 * JS帮助类
@@ -19,24 +21,27 @@
    /**
     * è°ƒç”¨JS方法
     */
    public Object callJsFn() {
    public static Object callJsFn(String jsFile, String function, String str) {
        try {
            // åˆ›å»º JavaScript å¼•擎管理器
            ScriptEngineManager manager = new ScriptEngineManager();
            // èŽ·å– JavaScript å¼•擎
            ScriptEngine engine = manager.getEngineByName("javascript");
            // å®šä¹‰è¦æ‰§è¡Œçš„ JavaScript è„šæœ¬
            String script = "function addNumbers(a, b){ return a + b; }";
            File f = new File(jsFile);
            if (!f.exists() || f.isDirectory()) {
                return null;
            }
            FileReader reader = new FileReader(f);
            // ç¼–译并运行 JavaScript è„šæœ¬
            engine.eval(script);
            engine.eval(reader);
            // è°ƒç”¨ JavaScript å‡½æ•°
            Invocable invocable = (Invocable) engine;
            Object obj = invocable.invokeFunction("addNumbers", 2, 3);
            return obj;
            return invocable.invokeFunction(function, str);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return null;
src/main/java/com/lf/server/helper/PathHelper.java
@@ -5,10 +5,12 @@
import com.lf.server.entity.all.StaticData;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.File;
import java.net.URL;
import java.util.Date;
/**
@@ -162,4 +164,15 @@
            log.error(ex.getMessage(), ex);
        }
    }
    /**
     * èŽ·å–static目录
     */
    public static String getStaticPath() {
        ClassLoader classLoader = PathHelper.class.getClassLoader();
        URL url = classLoader.getResource("");
        String path = url.getPath();
        return path.substring(0, path.indexOf("/target/classes")) + "/src/main/resources/static/";
    }
}
src/main/java/com/lf/server/service/sys/ReportService.java
@@ -13,6 +13,7 @@
import com.lf.server.mapper.sys.ReportMapper;
import com.lf.server.service.all.RedisService;
import com.lf.server.service.data.DownloadService;
import org.locationtech.jts.geom.Coordinate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@@ -124,10 +125,10 @@
    @Override
    public List<CountEntity> countExplorationPoints() {
        String key = RedisCacheKey.dataCountKey("countExplorationPoints");
        Object obj = redisService.get(key);
        if (obj instanceof List<?>) {
            return (List<CountEntity>) obj;
        }
//        Object obj = redisService.get(key);
//        if (obj instanceof List<?>) {
//            return (List<CountEntity>) obj;
//        }
        List<CountEntity> list = reportMapper.countExplorationPoints();
        if (null != list && list.size() > 0) {
@@ -157,12 +158,33 @@
                continue;
            }
            double area = JtsHelper.calcAreaByPoints(ces);
            StringBuilder sb = new StringBuilder();
            sb.append("[");
            for (CoordinateEntity e : ces) {
                sb.append(e.getX() + "," + e.getY() + ",");
            }
            sb.replace(sb.length() - 1, sb.length(), "]");
            // double area = JtsHelper.calcAreaByPoints(ces)
            double area = callJsFn(sb.toString());
            ce.setArea(area);
        }
    }
    /**
     * è°ƒç”¨JS方法
     */
    private double callJsFn(String str) {
        String staticPath = PathHelper.getStaticPath();
        Object obj = JsHelper.callJsFn(staticPath + "js/turf.min.6.5.js", "pointsToPolygon", str);
        if (null == obj) {
            return 0d;
        }
        return (double) obj;
    }
    /**
     * åˆ›å»ºæŠ¥å‘Š
     */
    public void createReport(UserEntity ue, ReportEntity re, HttpServletResponse res) throws Exception {
src/main/resources/static/html/turf.html
ÎļþÒÑɾ³ý
src/main/resources/static/js/turf.min.6.5.js
ÎļþÒÑɾ³ý