Преглед изворни кода

增加导出shp 时候投影检查以及兜底

ywf пре 2 недеља
родитељ
комит
7f9e90e049

+ 38 - 0
siwei-modules/siwei-apply/src/main/java/com/siwei/apply/config/GeoToolsEpsgRuntimeCheck.java

@@ -0,0 +1,38 @@
+package com.siwei.apply.config;
+
+import lombok.extern.slf4j.Slf4j;
+import org.geotools.referencing.CRS;
+import org.geotools.referencing.ReferencingFactoryFinder;
+import org.geotools.referencing.factory.epsg.hsql.ThreadedHsqlEpsgFactory;
+import org.opengis.referencing.crs.CoordinateReferenceSystem;
+import org.springframework.boot.ApplicationArguments;
+import org.springframework.boot.ApplicationRunner;
+import org.springframework.stereotype.Component;
+
+import java.util.Set;
+
+@Slf4j
+@Component
+public class GeoToolsEpsgRuntimeCheck implements ApplicationRunner {
+    private static final String EPSG_FACTORY_CLASS =
+            "org.geotools.referencing.factory.epsg.hsql.ThreadedHsqlEpsgFactory";
+
+    @Override
+    public void run(ApplicationArguments args) {
+        try {
+            Class.forName(EPSG_FACTORY_CLASS);
+            ThreadedHsqlEpsgFactory hsqlFactory = new ThreadedHsqlEpsgFactory();
+            ReferencingFactoryFinder.addAuthorityFactory(hsqlFactory);
+            ReferencingFactoryFinder.scanForPlugins();
+            Set<String> codes = CRS.getSupportedCodes("EPSG");
+            Object authorityFactory = CRS.getAuthorityFactory(true);
+            CoordinateReferenceSystem crs = CRS.decode("EPSG:4490");
+            CoordinateReferenceSystem hsqlCrs = hsqlFactory.createCoordinateReferenceSystem("4490");
+            log.info("GeoTools EPSG运行时检查通过:factoryClass={}, authorityFactory={}, has4490={}, crs={}, hsqlCrs={}",
+                    EPSG_FACTORY_CLASS, authorityFactory.getClass().getName(), codes.contains("4490"),
+                    crs.getName(), hsqlCrs.getName());
+        } catch (Exception e) {
+            log.error("GeoTools EPSG:4490运行时检查失败,请确认运行包包含gt-epsg-hsql、hsqldb,且没有通过loader.path或外部lib混入其他版本GeoTools依赖", e);
+        }
+    }
+}

+ 3 - 3
siwei-modules/siwei-apply/src/main/java/com/siwei/apply/service/cadastre/impl/CadastreManageServiceImpl.java

@@ -42,9 +42,9 @@ import java.util.stream.Collectors;
 @Slf4j
 @Service
 public class CadastreManageServiceImpl implements CadastreManageService {
-    static {
-        ogr.RegisterAll();
-    }
+//    static {
+//        ogr.RegisterAll();
+//    }
 
     @Autowired
     private CadastreFileMapper cadastreFileMapper;

+ 17 - 1
siwei-modules/siwei-apply/src/main/java/com/siwei/apply/service/impl/ProjectImpl.java

@@ -26,6 +26,7 @@ import org.geotools.data.shapefile.ShapefileDataStore;
 import org.geotools.data.shapefile.ShapefileDataStoreFactory;
 import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
 import org.geotools.referencing.CRS;
+import org.geotools.referencing.factory.epsg.hsql.ThreadedHsqlEpsgFactory;
 import org.locationtech.jts.geom.Geometry;
 import org.locationtech.jts.geom.MultiPolygon;
 import org.locationtech.jts.geom.Polygon;
@@ -1105,7 +1106,7 @@ public class ProjectImpl implements ProjectService {
     private void writeCycleShapefile(File shpFile, List<CycleShpRow> rows, List<String> fields) throws Exception {
         SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder();
         typeBuilder.setName("project_cycle");
-        CoordinateReferenceSystem crs = CRS.decode("EPSG:4490");
+        CoordinateReferenceSystem crs = decodeCgcs2000Crs();
         typeBuilder.setCRS(crs);
         typeBuilder.add("the_geom", MultiPolygon.class);
 
@@ -1152,6 +1153,21 @@ public class ProjectImpl implements ProjectService {
         }
     }
 
+    private CoordinateReferenceSystem decodeCgcs2000Crs() throws Exception {
+        try {
+            return CRS.decode("EPSG:4490");
+        } catch (Exception e) {
+            log.warn("CRS.decode解析EPSG:4490失败,改用gt-epsg-hsql权威库直接解析", e);
+            try {
+                return new ThreadedHsqlEpsgFactory().createCoordinateReferenceSystem("4490");
+            } catch (Exception hsqlException) {
+                hsqlException.addSuppressed(e);
+                log.error("项目生命周期SHP导出解析EPSG:4490失败,请确认运行包已加载gt-epsg-hsql和hsqldb,且运行时没有混入其他版本GeoTools依赖", hsqlException);
+                throw hsqlException;
+            }
+        }
+    }
+
     private Geometry parseEwkt(String ewkt, WKTReader reader) {
         if (StringUtils.isBlank(ewkt)) {
             return null;