Forráskód Böngészése

新增 坐标系转换接口

欧阳劲驰 3 hete
szülő
commit
bf2f3e826a

+ 41 - 0
src/main/java/com/shkpr/service/alambizplugin/apiparam/GisSurveyCRSParams.java

@@ -0,0 +1,41 @@
+package com.shkpr.service.alambizplugin.apiparam;
+
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.shkpr.service.alambizplugin.controllerserializer.GeometryDeserializer;
+import com.shkpr.service.alambizplugin.controllerserializer.GeometrySerializer;
+import com.shkpr.service.alambizplugin.controllervalid.CommonParamValidSK;
+import lombok.Data;
+import org.locationtech.jts.geom.Geometry;
+
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Size;
+
+/**
+ * 坐标系参数
+ *
+ * @author 欧阳劲驰
+ * @since 1.0.0
+ */
+@Data
+public class GisSurveyCRSParams {
+    /**
+     * 源crc编码
+     */
+    @NotNull(groups = {CommonParamValidSK.class})
+    @Size(max = 64, groups = {CommonParamValidSK.class})
+    private String sourceCRSCode;
+    /**
+     * 目标crc编码
+     */
+    @NotNull(groups = {CommonParamValidSK.class})
+    @Size(max = 64, groups = {CommonParamValidSK.class})
+    private String targetCRSCode;
+    /**
+     * 地理对象
+     */
+    @JsonDeserialize(using = GeometryDeserializer.class)
+    @JsonSerialize(using = GeometrySerializer.class)
+    private Geometry geometry;
+
+}

+ 38 - 10
src/main/java/com/shkpr/service/alambizplugin/bizservice/GisSurveyCRSBizService.java

@@ -1,25 +1,27 @@
-package com.shkpr.service.alambizplugin.bizservice;
+package com.shkpr.service.alambizplugin.commtools;
 
 import com.global.base.log.LogLevelFlag;
 import com.global.base.log.LogPrintMgr;
-import com.shkpr.service.alambizplugin.commtools.BeanUtil;
 import com.shkpr.service.alambizplugin.constants.LogFlagBusiType;
 import com.shkpr.service.alambizplugin.dto.CommCRSBoundingBox;
 import com.shkpr.service.alambizplugin.dto.CommCRSInfo;
 import lombok.AllArgsConstructor;
 import lombok.Data;
+import org.geotools.geometry.jts.JTS;
 import org.geotools.metadata.iso.extent.GeographicBoundingBoxImpl;
 import org.geotools.referencing.CRS;
 import org.geotools.referencing.ReferencingFactoryFinder;
 import org.geotools.referencing.crs.DefaultGeographicCRS;
 import org.geotools.referencing.factory.epsg.DirectEpsgFactory;
+import org.locationtech.jts.geom.Geometry;
 import org.opengis.metadata.Identifier;
 import org.opengis.referencing.FactoryException;
 import org.opengis.referencing.crs.CRSAuthorityFactory;
 import org.opengis.referencing.crs.CoordinateReferenceSystem;
 import org.opengis.referencing.datum.GeodeticDatum;
+import org.opengis.referencing.operation.MathTransform;
+import org.opengis.referencing.operation.TransformException;
 import org.opengis.util.GenericName;
-import org.springframework.stereotype.Component;
 
 import java.util.ArrayList;
 import java.util.Collections;
@@ -28,17 +30,16 @@ import java.util.Set;
 import java.util.stream.Collectors;
 
 /**
- * 坐标系管理service
+ * crs工具
  *
  * @author 欧阳劲驰
  * @since 1.0.0
  */
-@Component
-public class GisSurveyCRSBizService {
+public class CRSUtil {
     /**
      * log
      */
-    private final static String mStrClassName = "GisSurveyCRSBizService";
+    private final static String mStrClassName = "CRSUtil";
     private final static String mBizType = LogFlagBusiType.BUSI_GIS_SURVEY.toStrValue();
 
     /**
@@ -59,7 +60,7 @@ public class GisSurveyCRSBizService {
      * @see DirectEpsgFactory#getAuthorityCodes
      * @see org.geotools.referencing.factory.epsg.AuthorityCodes
      */
-    public List<Item> getCRSList() {
+    public static List<Item> getList() {
         try {
             List<Item> results = new ArrayList<>();
 
@@ -80,14 +81,13 @@ public class GisSurveyCRSBizService {
         }
     }
 
-
     /**
      * 获取坐标系信息
      *
      * @param code 坐标系code
      * @return 坐标系信息
      */
-    public CommCRSInfo getCRSInfo(String code) {
+    public static CommCRSInfo getInfo(String code) {
         try {
             //获取code集合
             Set<String> codes = crsFactory.getAuthorityCodes(CoordinateReferenceSystem.class);
@@ -127,6 +127,34 @@ public class GisSurveyCRSBizService {
         }
     }
 
+    /**
+     * 转换
+     *
+     * @param geometry      源地理对象
+     * @param sourceCRSCode 源坐标系code
+     * @param targetCRSCode 目标坐标系code
+     * @return 转换后地理对象
+     */
+    public static Geometry transform(Geometry geometry, String sourceCRSCode, String targetCRSCode) {
+        try {
+            //获取crs
+            CoordinateReferenceSystem sourceCRS = CRS.decode(AUTHORITY + ":" + sourceCRSCode, true);
+            CoordinateReferenceSystem targetCRS = CRS.decode(AUTHORITY + ":" + targetCRSCode, true);
+            //获取转换器
+            MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS, true);
+            // 转换对象
+            Geometry transformedGeometry = JTS.transform(geometry, transform);
+            // 设置 CRS
+            transformedGeometry.setSRID(Integer.parseInt(targetCRSCode));
+            return transformedGeometry;
+        } catch (FactoryException | TransformException e) {
+            LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, mBizType, mStrClassName
+                    , String.format("geom转换异常: error:%s", e));
+            return null;
+        }
+    }
+
+
     @Data
     @AllArgsConstructor
     public static class Item {

+ 1 - 0
src/main/java/com/shkpr/service/alambizplugin/constants/ApiURI.java

@@ -39,6 +39,7 @@ public class ApiURI {
     public static final String URI_XXX_CAD_CONVERT_GET = "cad-convert-get";
     public static final String URI_XXX_CRS_GET_LIST = "crs-get-list";
     public static final String URI_XXX_CRS_GET_INFO = "crs-get-info";
+    public static final String URI_XXX_CRS_TRANSFORM = "crs-transform";
     public static final String URI_XXX_TEMP_FILES = "temp-files";
     public static final String URI_XXX_ASYNC_RESULTS = "async-results";
 

+ 90 - 10
src/main/java/com/shkpr/service/alambizplugin/controller/ApiGisSurveyController.java

@@ -2,16 +2,18 @@ package com.shkpr.service.alambizplugin.controller;
 
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.module.SimpleModule;
 import com.global.base.log.LogLevelFlag;
 import com.global.base.log.LogPrintMgr;
+import com.shkpr.service.alambizplugin.apiparam.GisSurveyCRSParams;
 import com.shkpr.service.alambizplugin.apiparam.GisSurveyCheckParams;
 import com.shkpr.service.alambizplugin.apiparam.GisSurveyThirdExportParams;
 import com.shkpr.service.alambizplugin.apiparam.GisSurveyThirdImportParams;
-import com.shkpr.service.alambizplugin.bizservice.GisSurveyCRSBizService;
 import com.shkpr.service.alambizplugin.bizservice.GisSurveyCadConvertBizService;
 import com.shkpr.service.alambizplugin.bizservice.GisSurveySystemCheckBizService;
 import com.shkpr.service.alambizplugin.bizservice.GisSurveyThirdExportBizService;
 import com.shkpr.service.alambizplugin.bizservice.GisSurveyThirdImportBizService;
+import com.shkpr.service.alambizplugin.commtools.CRSUtil;
 import com.shkpr.service.alambizplugin.commtools.CommTool;
 import com.shkpr.service.alambizplugin.constants.ApiURI;
 import com.shkpr.service.alambizplugin.constants.CadEnum;
@@ -21,6 +23,7 @@ import com.shkpr.service.alambizplugin.constants.GisSurveyImportStatusEnum;
 import com.shkpr.service.alambizplugin.constants.LogFlagBusiType;
 import com.shkpr.service.alambizplugin.constants.ResponseCode;
 import com.shkpr.service.alambizplugin.controllerfilter.TokenAuthenticationService;
+import com.shkpr.service.alambizplugin.controllerserializer.GeometrySerializer;
 import com.shkpr.service.alambizplugin.controllervalid.CommonParamValidSK;
 import com.shkpr.service.alambizplugin.dbdao.services.GisSurveyLayerApplyThirdCopyServiceImpl;
 import com.shkpr.service.alambizplugin.dbdao.services.intef.GisSurveyLayerApplyService;
@@ -34,6 +37,7 @@ import com.shkpr.service.alambizplugin.dto.ResponseRes;
 import com.shkpr.service.alambizplugin.exception.SelfException;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
+import org.locationtech.jts.geom.Geometry;
 import org.springframework.data.domain.Pageable;
 import org.springframework.data.web.PageableDefault;
 import org.springframework.validation.BindingResult;
@@ -76,7 +80,6 @@ public class ApiGisSurveyController {
 
     private final ObjectMapper objectMapper;
 
-    private final GisSurveyCRSBizService crsBizService;
     private final GisSurveySystemCheckBizService systemCheckBizService;
     private final GisSurveyThirdImportBizService thirdImportBizService;
     private final GisSurveyThirdExportBizService thirdExportBizService;
@@ -85,7 +88,6 @@ public class ApiGisSurveyController {
     private final GisSurveyLayerApplyThirdCopyServiceImpl layerApplyThirdCopyService;
 
     public ApiGisSurveyController(ObjectMapper objectMapper
-            , GisSurveyCRSBizService crsBizService
             , GisSurveySystemCheckBizService systemCheckBizService
             , GisSurveyThirdImportBizService thirdImportBizService
             , GisSurveyThirdExportBizService thirdExportBizService
@@ -108,7 +110,6 @@ public class ApiGisSurveyController {
         mSeqCrsGetListReq = new AtomicInteger(0);
         mSeqCrsGetInfoReq = new AtomicInteger(0);
         this.objectMapper = objectMapper;
-        this.crsBizService = crsBizService;
         this.systemCheckBizService = systemCheckBizService;
         this.thirdImportBizService = thirdImportBizService;
         this.thirdExportBizService = thirdExportBizService;
@@ -991,7 +992,7 @@ public class ApiGisSurveyController {
     }
 
     /**
-     * 获取crs列表
+     * 获取坐标系列表
      *
      * @param request       request
      * @param strClientType 客户端类型
@@ -1023,10 +1024,10 @@ public class ApiGisSurveyController {
         resResult.setResmsg(ResponseCode.RESULT_ASYNC_TASK_FAILED.toStrMsg());
 
         //获取crs列表
-        List<GisSurveyCRSBizService.Item> result = crsBizService.getCRSList();
+        List<CRSUtil.Item> result = CRSUtil.getList();
         String resultStr = null;
         try {
-            if (result != null) resultStr = objectMapper.writeValueAsString(result);
+            if (CollectionUtils.isNotEmpty(result)) resultStr = objectMapper.writeValueAsString(result);
         } catch (JsonProcessingException e) {
             LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, mBizType, mStrClassName
                     , String.format("Json序列化异常: error:%s", e));
@@ -1053,7 +1054,7 @@ public class ApiGisSurveyController {
     }
 
     /**
-     * 获取crs信息
+     * 获取坐标系信息
      *
      * @param request       request
      * @param strClientType 客户端类型
@@ -1092,8 +1093,8 @@ public class ApiGisSurveyController {
         resResult.setRescode(ResponseCode.RESULT_ASYNC_TASK_FAILED.toStrCode());
         resResult.setResmsg(ResponseCode.RESULT_ASYNC_TASK_FAILED.toStrMsg());
 
-        //获取crs列表
-        CommCRSInfo result = crsBizService.getCRSInfo(code);
+        //获取crs信息
+        CommCRSInfo result = CRSUtil.getInfo(code);
         String resultStr = null;
         try {
             if (result != null) resultStr = objectMapper.writeValueAsString(result);
@@ -1121,4 +1122,83 @@ public class ApiGisSurveyController {
                         , resResult.getTimestamp() - llReqBefore));
         return resResult;
     }
+
+    /**
+     * 坐标系转换
+     *
+     * @param request       request
+     * @param strClientType 客户端类型
+     * @param strUserAgent  用户信息
+     * @return crs信息
+     */
+    @PostMapping(value = ApiURI.URI_XXX_CRS_TRANSFORM)
+    public ResponseRes<String> crsTransform(HttpServletRequest request
+            , @RequestHeader(value = ApiURI.HEADER_CLIENT_TYPE, required = false) String strClientType
+            , @RequestHeader(value = ApiURI.HEADER_USER_AGENT, required = false) String strUserAgent
+            , @RequestBody(required = false) @Validated(value = {CommonParamValidSK.class}) GisSurveyCRSParams oJsonParam
+            , BindingResult bindRes) throws SelfException {
+        //入参校验
+        final String URI_PATH = request.getRequestURI();
+        final String strPlatform = CommTool.getPlatformByAgent(strClientType, strUserAgent);
+        final String strUserId = (String) request.getAttribute(TokenAuthenticationService.HEADER_USERID);
+
+        if (oJsonParam == null || bindRes.hasErrors() || oJsonParam.getGeometry()==null) {
+            throw new SelfException(ResponseCode.STATUS_ERROR_JSON_FORMAT.toStrCode()
+                    , String.format(ApiURI.EXCEPTION_FORMAT
+                    , strPlatform
+                    , URI_PATH
+                    , ResponseCode.STATUS_ERROR_JSON_FORMAT.toStrMsg()));
+        }
+
+        //begin
+        long llReqBefore = System.currentTimeMillis();
+        String strRunSeq = String.format("%d-%d", llReqBefore, mSeqCrsGetInfoReq.incrementAndGet());
+        LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName, strUserId
+                , String.format("%s:%s seq:{%s} begin====>"
+                        , strPlatform
+                        , URI_PATH
+                        , strRunSeq));
+
+        //构建result
+        ResponseRes<String> resResult = new ResponseRes<>();
+        resResult.setRescode(ResponseCode.RESULT_ASYNC_TASK_FAILED.toStrCode());
+        resResult.setResmsg(ResponseCode.RESULT_ASYNC_TASK_FAILED.toStrMsg());
+
+        //转换坐标系
+        Geometry result = CRSUtil.transform(oJsonParam.getGeometry(), oJsonParam.getSourceCRSCode(), oJsonParam.getTargetCRSCode());
+        String resultStr = null;
+        try {
+            if (result != null) {
+                //创建objectMapper,并注册geo序列化
+                ObjectMapper objectMapper = new ObjectMapper();
+                SimpleModule module = new SimpleModule();
+                module.addSerializer(Geometry.class, new GeometrySerializer());
+                objectMapper.registerModule(module);
+
+                resultStr = objectMapper.writeValueAsString(result);
+            }
+        } catch (JsonProcessingException e) {
+            LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_ERROR, mBizType, mStrClassName
+                    , String.format("Json序列化异常: error:%s", e));
+        }
+
+        //执行成功
+        if (resultStr != null) {
+            resResult.setRescode(ResponseCode.RESULT_NORMAL.toStrCode());
+            resResult.setResmsg(ResponseCode.RESULT_NORMAL.toStrMsg());
+            resResult.setResdata(resultStr);
+        }
+
+        //end
+        resResult.setTimestamp(System.currentTimeMillis());
+        LogPrintMgr.getInstance().printLogMsg(LogLevelFlag.LOG_INFO, mBizType, mStrClassName, strUserId
+                , String.format("%s:%s seq:{%s} rescode:{%s} resmsg:{%s} time:{%d ms} end<===="
+                        , strPlatform
+                        , URI_PATH
+                        , strRunSeq
+                        , resResult.getRescode()
+                        , resResult.getResmsg()
+                        , resResult.getTimestamp() - llReqBefore));
+        return resResult;
+    }
 }

+ 1 - 0
src/main/java/com/shkpr/service/alambizplugin/controllerfilter/third/ApiJWTGisSurveyBizFilter.java

@@ -33,6 +33,7 @@ public class ApiJWTGisSurveyBizFilter extends JWTAuthenticationFilter {
         msMapURI2Method.put(String.format("%s/%s", ApiURI.URI_GIS_SURVEY_H, ApiURI.URI_XXX_CAD_CONVERT_GET), "GET");
         msMapURI2Method.put(String.format("%s/%s", ApiURI.URI_GIS_SURVEY_H, ApiURI.URI_XXX_CRS_GET_LIST), "GET");
         msMapURI2Method.put(String.format("%s/%s", ApiURI.URI_GIS_SURVEY_H, ApiURI.URI_XXX_CRS_GET_INFO), "GET");
+        msMapURI2Method.put(String.format("%s/%s", ApiURI.URI_GIS_SURVEY_H, ApiURI.URI_XXX_CRS_TRANSFORM), "POST");
     }
 
     public ApiJWTGisSurveyBizFilter(String url, AuthenticationManager authenticationManager){

+ 0 - 2
src/main/java/com/shkpr/service/alambizplugin/controllerserializer/GeometrySerializer.java

@@ -22,8 +22,6 @@ public class GeometrySerializer extends JsonSerializer<Geometry> {
      */
     @Override
     public void serialize(Geometry geom, JsonGenerator gen, SerializerProvider serializers) throws IOException {
-        //设置坐标系
-        geom.setSRID(4490);
         //Geom转换为GeoJson
         String geoJson = new GeoJsonWriter().write(geom);
         //读取并输出