Эх сурвалжийг харах

提交spotoverlap微服务模块代码(WMTS服务相关功能接口)

wanger 8 сар өмнө
parent
commit
da8206399a

+ 2 - 2
onemap-modules/onemap-spotoverlap/src/main/java/com/onemap/spotoverlap/controller/yzt/YztController.java

@@ -47,9 +47,9 @@ public class YztController extends BaseController {
      * @return
      */
     @PostMapping("/import")
-    public RequestResult importIn(MultipartFile file, String path, String type, String name, String ufield, String style, String cfield) {
+    public RequestResult importIn(MultipartFile file, String path, String type, String name, String ufield, String style, String cfield, String servertype) {
         try {
-            return yztService.importIn(file, path, type, name, ufield, style, cfield);
+            return yztService.importIn(file, path, type, name, ufield, style, cfield, servertype);
         } catch (Exception e) {
             e.printStackTrace();
             return RequestResult.error("失败", null);

+ 11 - 0
onemap-modules/onemap-spotoverlap/src/main/java/com/onemap/spotoverlap/domain/yzt/tAnalyseVector.java

@@ -1,5 +1,6 @@
 package com.onemap.spotoverlap.domain.yzt;
 
+import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableName;
 
 import java.util.Date;
@@ -22,6 +23,16 @@ public class tAnalyseVector {
     private String servicetype;
     private String servicename;
     private String serviceproj;
+    @TableField(exist = false)
+    private wmtsConfig wmts;
+
+    public wmtsConfig getWmts() {
+        return wmts;
+    }
+
+    public void setWmts(wmtsConfig wmts) {
+        this.wmts = wmts;
+    }
 
     public String getCfield() {
         return cfield;

+ 11 - 0
onemap-modules/onemap-spotoverlap/src/main/java/com/onemap/spotoverlap/domain/yzt/tBaseVector.java

@@ -1,5 +1,6 @@
 package com.onemap.spotoverlap.domain.yzt;
 
+import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableName;
 
 import java.util.Date;
@@ -15,6 +16,16 @@ public class tBaseVector {
     private String servicename;
     private String serviceproj;
     private Date inserttime;
+    @TableField(exist = false)
+    private wmtsConfig wmts;
+
+    public wmtsConfig getWmts() {
+        return wmts;
+    }
+
+    public void setWmts(wmtsConfig wmts) {
+        this.wmts = wmts;
+    }
 
     public Date getInserttime() {
         return inserttime;

+ 32 - 0
onemap-modules/onemap-spotoverlap/src/main/java/com/onemap/spotoverlap/domain/yzt/wmtsConfig.java

@@ -0,0 +1,32 @@
+package com.onemap.spotoverlap.domain.yzt;
+
+//wmts服务的配置类
+public class wmtsConfig {
+    private String gridset;
+    private Integer zoomstart;
+    private Integer zoomend;
+
+    public String getGridset() {
+        return gridset;
+    }
+
+    public void setGridset(String gridset) {
+        this.gridset = gridset;
+    }
+
+    public Integer getZoomstart() {
+        return zoomstart;
+    }
+
+    public void setZoomstart(Integer zoomstart) {
+        this.zoomstart = zoomstart;
+    }
+
+    public Integer getZoomend() {
+        return zoomend;
+    }
+
+    public void setZoomend(Integer zoomend) {
+        this.zoomend = zoomend;
+    }
+}

+ 63 - 22
onemap-modules/onemap-spotoverlap/src/main/java/com/onemap/spotoverlap/service/impl/yzt/YztServiceImpl.java

@@ -68,41 +68,49 @@ public class YztServiceImpl implements IYztService {
     @Value("${dbschema}")
     private String dbschema;
     @Value("${file.temp}")
-    String localFilePath;
+    private String localFilePath;
     @Value("${file.proxy}")
-    String localFileProxy;
+    private String localFileProxy;
     @Value("${server.port}")
-    String serverport;
+    private String serverport;
     @Value("${server.host}")
-    String serverhost;
+    private String serverhost;
     @Value("${file.wkid}")
-    Integer shpWkid;
+    private Integer shpWkid;
     @Value("${file.prefix}")
-    String prefix;
+    private String prefix;
     @Value("${image.qsx}")
-    String qsx;
+    private String qsx;
     @Value("${image.hsx}")
-    String hsx;
+    private String hsx;
     @Value("${qgis}")
-    String qgis;
+    private String qgis;
     @Value("${swid}")
-    String swid;
+    private String swid;
     @Value("${swarea}")
-    String swarea;
+    private String swarea;
     @Value("${swtype}")
-    String swtype;
+    private String swtype;
     @Value("${exportFields}")
-    String exportFields;
+    private String exportFields;
     @Value("${mapExport.fixed}")
-    String mapExportFixed;
+    private String mapExportFixed;
     @Value("${mapExport.size}")
-    Integer mapExportSize;
+    private Integer mapExportSize;
     @Value("${checkedstyle}")
-    String checkedstyle;
+    private String checkedstyle;
     @Value("${spotstyle}")
-    String spotstyle;
+    private String spotstyle;
     @Value("${charset}")
-    String charset;
+    private String charset;
+    @Value("${wmts.gridset}")
+    private String wmtsgridset;
+    @Value("${wmts.seedtype}")
+    private String wmtsseedtype;
+    @Value("${wmts.zoomstart}")
+    private Integer wmtszoomstart;
+    @Value("${wmts.zoomend}")
+    private Integer wmtszoomend;
 
     @Override
     public RequestResult upload(MultipartFile file) {
@@ -157,10 +165,13 @@ public class YztServiceImpl implements IYztService {
     }
 
     @Override
-    public RequestResult importIn(MultipartFile file, String path, String type, String name, String ufield, String style, String cfield) {
+    public RequestResult importIn(MultipartFile file, String path, String type, String name, String ufield, String style, String cfield, String servertype) {
         if (StringUtils.isEmpty(style)) {
             style = spotstyle;
         }
+        if (StringUtils.isEmpty(servertype)) {
+            servertype = "WMS";
+        }
         if (file == null && path == null) {
             return RequestResult.error("参数未传递", null);
         }
@@ -257,8 +268,21 @@ public class YztServiceImpl implements IYztService {
             String geoserverusername = geoserverconfig.getUsername();
             String geoserverpassword = geoserverconfig.getPassword();
             String geoserverworkspace = geoserverconfig.getWorkspace();
+            //TODO wanger 有没有都删除一下数据存储,避免二次发布无效
+            try {
+                GeoServer.delete_datasource(geoserverurl, geoserverusername, geoserverpassword, geoserverworkspace, datasource);
+            } catch (Exception e) {
+                System.out.println("删除geoserver数据存储失败,可能不存在!");
+            }
             GeoServer.publishShp(geoserverurl, geoserverusername, geoserverpassword, geoserverworkspace, shpPath, style, datasource, charset);
+            //TODO wanger wmts切片处理
+            if ("WMTS".equals(servertype)) {
+                GeoServer.caching_layer(geoserverurl, geoserverusername, geoserverpassword, geoserverworkspace, datasource, wmtsgridset, wmtsseedtype, wmtszoomstart, wmtszoomend);
+            }
             String layerurl = geoserverurl + "/" + geoserverworkspace + "/wms";
+            if ("WMTS".equals(servertype)) {
+                layerurl = geoserverurl + "/gwc/service/wmts";
+            }
             String layername = geoserverworkspace + ":" + datasource;
             String epsg = GeoServer.getShpEPSG(shpPath);
             String uuid = StringUtils.getUUID();
@@ -269,7 +293,7 @@ public class YztServiceImpl implements IYztService {
                 baseVector.setLayername(name);
                 baseVector.setTablename(tablename);
                 baseVector.setServiceuri(layerurl);
-                baseVector.setServicetype("WMS");
+                baseVector.setServicetype(servertype);
                 baseVector.setServicename(layername);
                 baseVector.setServiceproj(epsg);
                 tBaseVectorMapper.insert(baseVector);
@@ -288,7 +312,7 @@ public class YztServiceImpl implements IYztService {
                 analyseVector.setTablename(tablename);
                 analyseVector.setPath(folderpath);
                 analyseVector.setServiceuri(layerurl);
-                analyseVector.setServicetype("WMS");
+                analyseVector.setServicetype(servertype);
                 analyseVector.setServicename(layername);
                 analyseVector.setServiceproj(epsg);
                 analyseVector.setCfield(cfield);
@@ -730,6 +754,10 @@ public class YztServiceImpl implements IYztService {
             QueryWrapper<tBaseVector> wrapper = new QueryWrapper();
             wrapper.orderByDesc("inserttime");
             List<tBaseVector> tBaseVectorList = tBaseVectorMapper.selectList(null);
+            wmtsConfig config = getWmtsConfig();
+            for (tBaseVector cur : tBaseVectorList) {
+                cur.setWmts(config);
+            }
             return RequestResult.success("查询成功!", tBaseVectorList);
         } catch (Exception e) {
             e.printStackTrace();
@@ -748,7 +776,12 @@ public class YztServiceImpl implements IYztService {
                 wrapper.eq("layername", name);
             }
             wrapper.orderByDesc("inserttime");
-            return RequestResult.success("查询成功!", tAnalyseVectorMapper.selectList(wrapper));
+            List<tAnalyseVector> res = tAnalyseVectorMapper.selectList(wrapper);
+            wmtsConfig config = getWmtsConfig();
+            for (tAnalyseVector cur : res) {
+                cur.setWmts(config);
+            }
+            return RequestResult.success("查询成功!", res);
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -1264,4 +1297,12 @@ public class YztServiceImpl implements IYztService {
         });
     }
 
+    public wmtsConfig getWmtsConfig() {
+        wmtsConfig config = new wmtsConfig();
+        config.setGridset(wmtsgridset);
+        config.setZoomstart(wmtszoomstart);
+        config.setZoomend(wmtszoomend);
+        return config;
+    }
+
 }

+ 1 - 1
onemap-modules/onemap-spotoverlap/src/main/java/com/onemap/spotoverlap/service/yzt/IYztService.java

@@ -13,7 +13,7 @@ import javax.servlet.http.HttpServletResponse;
 import java.util.Map;
 
 public interface IYztService {
-    RequestResult importIn(MultipartFile file, String path, String type, String name, String ufield, String style, String cfield);
+    RequestResult importIn(MultipartFile file, String path, String type, String name, String ufield, String style, String cfield, String servertype);
 
     RequestResult modellist();
 

+ 192 - 13
onemap-modules/onemap-spotoverlap/src/main/java/com/onemap/spotoverlap/utils/GeoServer.java

@@ -1,10 +1,7 @@
 package com.onemap.spotoverlap.utils;
 
 import com.google.gson.Gson;
-import org.apache.http.client.methods.CloseableHttpResponse;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.client.methods.HttpPut;
+import org.apache.http.client.methods.*;
 import org.apache.http.entity.StringEntity;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClients;
@@ -81,12 +78,8 @@ public class GeoServer {
      * @throws IOException
      */
     public static void publishShp(String geoserverurl, String username, String password, String workspace, String filepath, String stylename, String datasource, String charset) throws IOException {
-//        File file = new File(filepath);
-//        String fileName = file.getName();
-//        int dotIndex = fileName.lastIndexOf('.');
-//        String datasource = dotIndex != -1 ? fileName.substring(0, dotIndex) : fileName;
-
         CloseableHttpClient client = HttpClients.createDefault();
+
         // TODO 设置 Basic Authentication 头
         String auth = username + ":" + password;
         String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8));
@@ -122,13 +115,26 @@ public class GeoServer {
             String responseEntity = EntityUtils.toString(response.getEntity());
             System.out.println(responseEntity);
         }
+
+        // TODO 更新符号样式配置
         updateStyle(stylename, geoserverurl, username, password, workspace + ":" + datasource);
+
+        //TODO wanger 是否发布为WMTS服务  调用切片
         client.close();
     }
 
+    /**
+     * 更新服务的样式配置
+     *
+     * @param stylename
+     * @param geoserverurl
+     * @param username
+     * @param password
+     * @param datasource
+     */
     public static void updateStyle(String stylename, String geoserverurl, String username, String password, String datasource) {
+        CloseableHttpClient client = HttpClients.createDefault();
         try {
-            CloseableHttpClient client = HttpClients.createDefault();
             // TODO 创建图层样式的请求体
             String styleRequest = "<layer><defaultStyle>" +
                     "<name>" + stylename + "</name>" +
@@ -145,6 +151,11 @@ public class GeoServer {
             }
             client.close();
         } catch (Exception e) {
+            try {
+                client.close();
+            } catch (IOException ioException) {
+                ioException.printStackTrace();
+            }
             e.printStackTrace();
         }
     }
@@ -158,11 +169,8 @@ public class GeoServer {
     public static String getShpEPSG(String shpFilePath) {
         try {
             File shpFile = new File(shpFilePath);
-            // 通过Shapefile加载数据
             FileDataStore store = new ShapefileDataStoreFactory().createDataStore(shpFile.toURI().toURL());
-            // 获取CoordinateReferenceSystem
             CoordinateReferenceSystem crs = store.getSchema().getCoordinateReferenceSystem();
-            // 获取SRID
             Integer srid = CRS.lookupEpsgCode(crs, false);
             return "EPSG:" + srid;
         } catch (Exception e) {
@@ -170,4 +178,175 @@ public class GeoServer {
         }
         return "EPSG:4326";
     }
+
+    /**
+     * 创建服务的切片配置
+     *
+     * @param geoserverurl
+     * @param geoserverusername
+     * @param geoserverpassword
+     * @param geoserverworkspace
+     * @param datasource
+     * @param wmtsgridset
+     * @param wmtsseedtype
+     * @param wmtszoomstart
+     * @param wmtszoomend
+     */
+    public static void caching_layer(String geoserverurl,
+                                     String geoserverusername,
+                                     String geoserverpassword,
+                                     String geoserverworkspace,
+                                     String datasource,
+                                     String wmtsgridset,
+                                     String wmtsseedtype,
+                                     Integer wmtszoomstart,
+                                     Integer wmtszoomend) {
+
+        CloseableHttpClient client = HttpClients.createDefault();
+        try {
+            String layer_name = geoserverworkspace + ":" + datasource;
+            String url = String.format("%s/gwc/rest/layers/%s", geoserverurl, layer_name);
+            // TODO 创建图层样式的请求体
+            String styleRequest = "<GeoServerLayer>" +
+                    "                <enabled>true</enabled>" +
+                    "                <inMemoryCached>true</inMemoryCached>" +
+                    "                <name>" + layer_name + "</name>" +
+                    "                <mimeFormats>" +
+                    "                    <string>image/png</string>" +
+                    "                    <string>image/jpeg</string>" +
+                    "                </mimeFormats>" +
+                    "                <gridSubsets>" +
+                    "                    <gridSubset>" +
+                    "                        <gridSetName>" + wmtsgridset + "</gridSetName>" +
+                    "                        <zoomStart>" + wmtszoomstart + "</zoomStart>" +
+                    "                        <zoomStop>" + wmtszoomend + "</zoomStop>" +
+                    "                        <minCachedLevel>" + wmtszoomstart + "</minCachedLevel>" +
+                    "                        <maxCachedLevel>" + wmtszoomend + "</maxCachedLevel>" +
+                    "                    </gridSubset>" +
+                    "                </gridSubsets>" +
+                    "                <metaWidthHeight>" +
+                    "                    <int>4</int>" +
+                    "                    <int>4</int>" +
+                    "                </metaWidthHeight>" +
+                    "                <expireCache>0</expireCache>" +
+                    "                <expireClients>0</expireClients>" +
+                    "                <parameterFilters>" +
+                    "                </parameterFilters>" +
+                    "                <gutter>0</gutter>" +
+                    "                <autoCacheStyles>true</autoCacheStyles>" +
+                    "            </GeoServerLayer>";
+            String auth = geoserverusername + ":" + geoserverpassword;
+            String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8));
+            HttpPost putRequest = new HttpPost(url);
+            putRequest.setEntity(new StringEntity(styleRequest));
+            putRequest.setHeader("Authorization", "Basic " + encodedAuth);
+            putRequest.setHeader("Content-Type", "application/xml");
+            try (CloseableHttpResponse response = client.execute(putRequest)) {
+                String responseEntity = EntityUtils.toString(response.getEntity());
+                System.out.println(responseEntity);
+                seed_caching_layer(geoserverurl, geoserverusername, geoserverpassword, geoserverworkspace, datasource, wmtsgridset, wmtsseedtype, wmtszoomstart, wmtszoomend);
+            }
+            client.close();
+        } catch (Exception e) {
+            try {
+                client.close();
+            } catch (IOException ioException) {
+                ioException.printStackTrace();
+            }
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * 发送切片指令 seed
+     *
+     * @param geoserverurl
+     * @param geoserverusername
+     * @param geoserverpassword
+     * @param geoserverworkspace
+     * @param datasource
+     * @param wmtsgridset
+     * @param wmtsseedtype
+     * @param wmtszoomstart
+     * @param wmtszoomend
+     */
+    private static void seed_caching_layer(String geoserverurl,
+                                           String geoserverusername,
+                                           String geoserverpassword,
+                                           String geoserverworkspace,
+                                           String datasource,
+                                           String wmtsgridset,
+                                           String wmtsseedtype,
+                                           Integer wmtszoomstart,
+                                           Integer wmtszoomend) {
+        CloseableHttpClient client = HttpClients.createDefault();
+        try {
+            String layer_name = geoserverworkspace + ":" + datasource;
+            String url = String.format("%s/gwc/rest/seed/%s?threadCount=4&" +
+                    "type=%s" +
+                    "&gridSetId=%s" +
+                    "&tileFormat=image/png" +
+                    "&zoomStart=%s" +
+                    "&zoomStop=%s" +
+                    "&parameter_STYLES=" +
+                    "&minX=&minY=&maxX=&maxY=" +
+                    "&tileFailureRetryCount=-1" +
+                    "&tileFailureRetryWaitTime=100" +
+                    "&totalFailuresBeforeAborting=1000", geoserverurl, layer_name, wmtsseedtype, wmtsgridset, wmtszoomstart, wmtszoomend);
+            String auth = geoserverusername + ":" + geoserverpassword;
+            String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8));
+            HttpPost putRequest = new HttpPost(url);
+            putRequest.setHeader("Authorization", "Basic " + encodedAuth);
+            putRequest.setHeader("Content-Type", "application/json");
+            try (CloseableHttpResponse response = client.execute(putRequest)) {
+                String responseEntity = EntityUtils.toString(response.getEntity());
+                System.out.println(responseEntity);
+            }
+            client.close();
+        } catch (Exception e) {
+            try {
+                client.close();
+            } catch (IOException ioException) {
+                ioException.printStackTrace();
+            }
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * 删除指定数据存储
+     *
+     * @param geoserverurl
+     * @param geoserverusername
+     * @param geoserverpassword
+     * @param geoserverworkspace
+     * @param datasource
+     */
+    public static void delete_datasource(String geoserverurl,
+                                         String geoserverusername,
+                                         String geoserverpassword,
+                                         String geoserverworkspace,
+                                         String datasource) {
+
+        CloseableHttpClient client = HttpClients.createDefault();
+        try {
+            String url = String.format("%s/rest/workspaces/%s/datastores/%s?recurse=true", geoserverurl, geoserverworkspace, datasource);
+            String auth = geoserverusername + ":" + geoserverpassword;
+            String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8));
+            HttpDelete putRequest = new HttpDelete(url);
+            putRequest.setHeader("Authorization", "Basic " + encodedAuth);
+            try (CloseableHttpResponse response = client.execute(putRequest)) {
+                String responseEntity = EntityUtils.toString(response.getEntity());
+                System.out.println(responseEntity);
+            }
+            client.close();
+        } catch (Exception e) {
+            try {
+                client.close();
+            } catch (IOException ioException) {
+                ioException.printStackTrace();
+            }
+            e.printStackTrace();
+        }
+    }
 }