Parcourir la source

更新shp引入的jar版本

chenendian il y a 3 semaines
Parent
commit
a500f278ef

+ 35 - 48
siwei-modules/siwei-apply/pom.xml

@@ -117,76 +117,63 @@
             <systemPath>${project.basedir}/lib/json-simple-1.1.1.jar</systemPath>
         </dependency>
         <!--   ejml     -->
-        <!--   geotools     -->
+
+        <!--geotools-->
         <dependency>
-            <groupId>gt-api</groupId>
-            <artifactId>gt-api</artifactId>
-            <scope>system</scope>
-            <version>19.2</version>
-            <systemPath>${project.basedir}/lib/gt-api-19.2.jar</systemPath>
+            <groupId>org.geotools</groupId>
+            <artifactId>gt-main</artifactId>
         </dependency>
         <dependency>
-            <groupId>gt-data</groupId>
-            <artifactId>gt-data</artifactId>
-            <scope>system</scope>
-            <version>19.2</version>
-            <systemPath>${project.basedir}/lib/gt-data-19.2.jar</systemPath>
+            <groupId>org.geotools</groupId>
+            <artifactId>gt-cql</artifactId>
         </dependency>
         <dependency>
-            <groupId>gt-geotools</groupId>
-            <artifactId>geojson</artifactId>
-            <scope>system</scope>
-            <version>19.2</version>
-            <systemPath>${project.basedir}/lib/gt-geojson-19.2.jar</systemPath>
+            <groupId>org.geotools.jdbc</groupId>
+            <artifactId>gt-jdbc-postgis</artifactId>
         </dependency>
         <dependency>
-            <groupId>gt-main</groupId>
-            <artifactId>gt-main</artifactId>
-            <scope>system</scope>
-            <version>19.2</version>
-            <systemPath>${project.basedir}/lib/gt-main-19.2.jar</systemPath>
+            <groupId>org.geotools</groupId>
+            <artifactId>gt-coverage</artifactId>
         </dependency>
         <dependency>
-            <groupId>gt-metadata</groupId>
-            <artifactId>gt-metadata</artifactId>
-            <scope>system</scope>
-            <version>19.2</version>
-            <systemPath>${project.basedir}/lib/gt-metadata-19.2.jar</systemPath>
+            <groupId>org.geotools</groupId>
+            <artifactId>gt-render</artifactId>
         </dependency>
         <dependency>
-            <groupId>gt-opengis</groupId>
+            <groupId>org.geotools</groupId>
             <artifactId>gt-opengis</artifactId>
-            <scope>system</scope>
-            <version>19.2</version>
-            <systemPath>${project.basedir}/lib/gt-opengis-19.2.jar</systemPath>
         </dependency>
         <dependency>
-            <groupId>gt-referencing</groupId>
+            <groupId>org.geotools</groupId>
             <artifactId>gt-referencing</artifactId>
-            <scope>system</scope>
-            <version>19.2</version>
-            <systemPath>${project.basedir}/lib/gt-referencing-19.2.jar</systemPath>
         </dependency>
         <dependency>
-            <groupId>gt-render</groupId>
-            <artifactId>gt-render</artifactId>
-            <scope>system</scope>
-            <version>19.2</version>
-            <systemPath>${project.basedir}/lib/gt-render-19.2.jar</systemPath>
+            <groupId>org.geotools</groupId>
+            <artifactId>gt-geojson</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.geotools</groupId>
+            <artifactId>gt-geotiff</artifactId>
         </dependency>
         <dependency>
-            <groupId>gt-shapefile</groupId>
+            <groupId>org.geotools</groupId>
+            <artifactId>gt-swing</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.geotools</groupId>
             <artifactId>gt-shapefile</artifactId>
-            <scope>system</scope>
-            <version>19.2</version>
-            <systemPath>${project.basedir}/lib/gt-shapefile-19.2.jar</systemPath>
         </dependency>
         <dependency>
-            <groupId>gt-jts</groupId>
-            <artifactId>gt-jts</artifactId>
-            <scope>system</scope>
-            <version>1.13</version>
-            <systemPath>${project.basedir}/lib/jts-1.13.jar</systemPath>
+            <groupId>org.geotools</groupId>
+            <artifactId>gt-tile-client</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.geotools</groupId>
+            <artifactId>gt-epsg-hsql</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.locationtech.jts</groupId>
+            <artifactId>jts-core</artifactId>
         </dependency>
         <dependency>
             <groupId>gt-jsr</groupId>

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

@@ -10,23 +10,20 @@ import com.siwei.apply.service.cadastre.CadastreManageService;
 
 import com.siwei.apply.utils.FileExtractUtil;
 import com.siwei.apply.utils.MdbUtil;
-import com.siwei.common.core.domain.R;
 import com.siwei.common.core.exception.ServiceException;
-import com.vividsolutions.jts.io.ParseException;
-import com.vividsolutions.jts.io.WKBReader;
-import com.vividsolutions.jts.io.WKTWriter;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.collections4.MapUtils;
 import org.apache.commons.lang3.StringUtils;
-import org.apache.ibatis.annotations.Param;
 import org.gdal.ogr.*;
 import org.gdal.osr.SpatialReference;
+import org.locationtech.jts.io.ParseException;
+import org.locationtech.jts.io.WKBReader;
+import org.locationtech.jts.io.WKTWriter;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Propagation;
 import org.springframework.transaction.annotation.Transactional;
-import org.springframework.web.bind.annotation.RequestParam;
 
 import java.io.File;
 import java.nio.file.Files;
@@ -1330,7 +1327,7 @@ public class CadastreManageServiceImpl implements CadastreManageService {
            res.forEach(s -> {
                 if(s.get("geom") != null){
                     WKBReader reader = new WKBReader();
-                    com.vividsolutions.jts.geom.Geometry geom = null;
+                    org.locationtech.jts.geom.Geometry geom = null;
                     try {
                         geom = reader.read(WKBReader.hexToBytes(s.get("geom").toString()));
                     } catch (ParseException e) {
@@ -1346,7 +1343,6 @@ public class CadastreManageServiceImpl implements CadastreManageService {
         return res;
     }
 
-
     @Override
     public List<String> getTableFields(String tableName) {
         List<String> res = cadastreFileMapper.selectTableCollum(tableName);
@@ -1368,7 +1364,6 @@ public class CadastreManageServiceImpl implements CadastreManageService {
     }
 
 
-
     @Override
     public String tableRowUpdate(String tableName, List<Map<String,Object>> list) {
          int res = cadastreFileMapper.batchUpdateByBsm(tableName,list);
@@ -1418,7 +1413,6 @@ public class CadastreManageServiceImpl implements CadastreManageService {
     }
 
 
-
     /**
      *
      * 生成一个mdb文件

+ 59 - 29
siwei-modules/siwei-apply/src/main/java/com/siwei/apply/service/impl/ConvergeServiceImpl.java

@@ -15,17 +15,13 @@ import com.siwei.common.core.exception.ServiceException;
 import com.siwei.common.core.utils.DateUtils;
 import com.siwei.common.core.utils.bean.BeanUtils;
 import com.siwei.common.core.utils.uuid.IdUtils;
-import com.vividsolutions.jts.geom.Geometry;
-import com.vividsolutions.jts.geom.MultiPolygon;
-import com.vividsolutions.jts.geom.Polygon;
-import com.vividsolutions.jts.io.ParseException;
-import com.vividsolutions.jts.io.WKBReader;
-import com.vividsolutions.jts.io.WKTReader;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.collections4.CollectionUtils;
 import org.apache.commons.lang3.ArrayUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.geotools.data.DefaultTransaction;
+import org.geotools.data.FeatureSource;
+import org.geotools.data.FeatureWriter;
 import org.geotools.data.Transaction;
 import org.geotools.data.collection.ListFeatureCollection;
 import org.geotools.data.shapefile.ShapefileDataStore;
@@ -36,7 +32,12 @@ import org.geotools.data.simple.SimpleFeatureStore;
 import org.geotools.feature.simple.SimpleFeatureBuilder;
 import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
 import org.geotools.referencing.CRS;
-
+import org.locationtech.jts.geom.Geometry;
+import org.locationtech.jts.geom.MultiPolygon;
+import org.locationtech.jts.geom.Polygon;
+import org.locationtech.jts.io.ParseException;
+import org.locationtech.jts.io.WKBReader;
+import org.locationtech.jts.io.WKTReader;
 import org.opengis.feature.simple.SimpleFeature;
 import org.opengis.feature.simple.SimpleFeatureType;
 import org.opengis.referencing.crs.CoordinateReferenceSystem;
@@ -50,7 +51,9 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.PrintWriter;
 import java.io.Serializable;
+import java.math.BigDecimal;
 import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -315,6 +318,7 @@ public class ConvergeServiceImpl implements ConvergeService {
                 try {
                     operationFileStrategy(convergeTableList);
                 } catch (Exception e) {
+                    e.printStackTrace();
                     throw new ServiceException("汇交失败,数据错误"+e);
                 }
 
@@ -408,7 +412,22 @@ public class ConvergeServiceImpl implements ConvergeService {
             // SHP 字段名长度限制为 10 字符
             String shpColName = colName.length() > 10 ? colName.substring(0, 10) : colName;
             Class<?> javaType = mapDatabaseTypeToJavaClass(dataType);
-            typeBuilder.add(shpColName, javaType);
+            
+            // 明确指定字段长度和精度,防止 GeoTools 默认长度过小
+            if (javaType == String.class) {
+                // 设置为 254 字节,但数据输入时会截断到 80 字符以防万一
+                typeBuilder.length(254).add(shpColName, javaType);
+            } else if (Number.class.isAssignableFrom(javaType)) {
+                if (javaType == Double.class || javaType == Float.class || javaType == BigDecimal.class) {
+                    // GeoTools SimpleFeatureTypeBuilder 不直接支持 precision 方法
+                    // 需通过 userData 传递给 ShapefileDataStore
+                    typeBuilder.length(31).userData("precision", 6).add(shpColName, javaType);
+                } else {
+                    typeBuilder.length(20).add(shpColName, javaType);
+                }
+            } else {
+                typeBuilder.add(shpColName, javaType);
+            }
             columnTypeMap.put(colName, shpColName);
         }
 
@@ -461,7 +480,18 @@ public class ConvergeServiceImpl implements ConvergeService {
 
                 // 添加属性字段
                 for (String originColName : columnTypeMap.keySet()) {
-                    featureBuilder.add(data.get(originColName));
+                    Object val = data.get(originColName);
+                    if (val instanceof String) {
+                        String sVal = (String) val;
+                        // DBF 字符串最大长度为 254 字节,而非字符数
+                        // 这里使用更保守的 80 字符,以兼容 UTF-8 多字节情况
+                        if (sVal.length() > 80) {
+                            sVal = sVal.substring(0, 80);
+                        }
+                        featureBuilder.add(sVal);
+                    } else {
+                        featureBuilder.add(val);
+                    }
                 }
 
                 features.add(featureBuilder.buildFeature(null));
@@ -486,25 +516,20 @@ public class ConvergeServiceImpl implements ConvergeService {
         dataStore.setCharset(Charset.forName("UTF-8"));
         dataStore.createSchema(featureType);
 
-        Transaction transaction = new DefaultTransaction("create");
-        String typeName = dataStore.getTypeNames()[0];
-        SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
-
-        if (featureSource instanceof SimpleFeatureStore) {
-            SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
-            SimpleFeatureCollection collection = new ListFeatureCollection(featureType, features);
-            featureStore.setTransaction(transaction);
-            try {
-                featureStore.addFeatures(collection);
-                transaction.commit();
-            } catch (Exception e) {
-                transaction.rollback();
-                throw e;
-            } finally {
-                transaction.close();
+        // 使用 FeatureWriter 代替 SimpleFeatureStore.addFeatures,更稳定且易于调试
+        try (FeatureWriter<SimpleFeatureType, SimpleFeature> writer = dataStore.getFeatureWriterAppend(Transaction.AUTO_COMMIT)) {
+            for (SimpleFeature feature : features) {
+                try {
+                    SimpleFeature next = writer.next();
+                    next.setAttributes(feature.getAttributes());
+                    writer.write();
+                } catch (Exception e) {
+                    log.error("写入要素失败,跳过: {}", e.getMessage());
+                }
             }
+        } finally {
+            dataStore.dispose();
         }
-        dataStore.dispose();
 
         // 6. 生成 .cpg 文件
         File cpgFile = new File(filePath + File.separator + fileName + ".cpg");
@@ -518,6 +543,8 @@ public class ConvergeServiceImpl implements ConvergeService {
         dataType = dataType.toLowerCase();
         if (dataType.contains("char") || dataType.contains("text")) {
             return String.class;
+        } else if (dataType.contains("int8") || dataType.contains("bigint")) {
+            return Long.class;
         } else if (dataType.contains("int")) {
             return Integer.class;
         } else if (dataType.contains("numeric") || dataType.contains("decimal") || dataType.contains("double") || dataType.contains("float") || dataType.contains("real")) {
@@ -534,7 +561,7 @@ public class ConvergeServiceImpl implements ConvergeService {
         try {
             // 1. 构造完整的文件路径
             String mdbPath = filePath;
-            if (!mdbPath.endsWith(File.separator)) {
+            if (!mdbPath.endsWith("/") && !mdbPath.endsWith("\\")) {
                 mdbPath += File.separator;
             }
             mdbPath += fileName;
@@ -542,9 +569,12 @@ public class ConvergeServiceImpl implements ConvergeService {
                 mdbPath += ".mdb";
             }
 
-            // 2. 调用工具类生成 MDB 文件并写入数据
-            MdbUtil.writeMdbByTable(mdbPath, tableName, columns, dataList);
+            // 统一路径分隔符,防止 Linux 环境下出现反斜杠导致的路径错误
+            mdbPath = mdbPath.replace("\\", File.separator).replace("/", File.separator);
 
+            // 4. 调用工具类生成 MDB 文件并写入数据
+            MdbUtil.writeMdbByTable(mdbPath, tableName, columns, dataList);
+            
             log.info("MDB 文件生成成功: {}", mdbPath);
         } catch (Exception e) {
             log.error("生成 MDB 文件失败: {}", tableName, e);

+ 42 - 19
siwei-modules/siwei-spatial/src/main/java/com/siwei/spatial/service/file/impl/SpatialFilesDbServiceImpl.java

@@ -12,6 +12,7 @@ import com.siwei.spatial.service.file.ITGeomDbService;
 import com.siwei.spatial.utils.FileUtils;
 import org.geotools.data.DefaultTransaction;
 import org.geotools.data.FeatureSource;
+import org.geotools.data.FeatureWriter;
 import org.geotools.data.Transaction;
 import org.geotools.data.collection.ListFeatureCollection;
 import org.geotools.data.shapefile.ShapefileDataStore;
@@ -835,7 +836,21 @@ public class SpatialFilesDbServiceImpl implements ISpatialFilesDbService {
             // SHP 字段名长度限制为 10 字符
             String shpColName = colName.length() > 10 ? colName.substring(0, 10) : colName;
             Class<?> javaType = mapDatabaseTypeToJavaClass(dataType);
-            typeBuilder.add(shpColName, javaType);
+            
+            // 明确指定字段长度和精度,防止 GeoTools 默认长度过小
+            if (javaType == String.class) {
+                typeBuilder.length(254).add(shpColName, javaType);
+            } else if (Number.class.isAssignableFrom(javaType)) {
+                if (javaType == Double.class || javaType == Float.class || javaType == BigDecimal.class) {
+                    // GeoTools SimpleFeatureTypeBuilder 不直接支持 precision 方法
+                    // 需通过 userData 传递给 ShapefileDataStore
+                    typeBuilder.length(31).userData("precision", 6).add(shpColName, javaType);
+                } else {
+                    typeBuilder.length(20).add(shpColName, javaType);
+                }
+            } else {
+                typeBuilder.add(shpColName, javaType);
+            }
             columnTypeMap.put(colName, shpColName);
         }
 
@@ -888,7 +903,18 @@ public class SpatialFilesDbServiceImpl implements ISpatialFilesDbService {
 
                 // 添加属性字段
                 for (String originColName : columnTypeMap.keySet()) {
-                    featureBuilder.add(data.get(originColName));
+                    Object val = data.get(originColName);
+                    if (val instanceof String) {
+                        String sVal = (String) val;
+                        // DBF 字符串最大长度为 254 字节,而非字符数
+                        // 这里使用更保守的 80 字符,以兼容 UTF-8 多字节情况
+                        if (sVal.length() > 80) {
+                            sVal = sVal.substring(0, 80);
+                        }
+                        featureBuilder.add(sVal);
+                    } else {
+                        featureBuilder.add(val);
+                    }
                 }
 
                 features.add(featureBuilder.buildFeature(null));
@@ -906,25 +932,20 @@ public class SpatialFilesDbServiceImpl implements ISpatialFilesDbService {
         dataStore.setCharset(Charset.forName("UTF-8"));
         dataStore.createSchema(featureType);
 
-        Transaction transaction = new DefaultTransaction("create");
-        String typeName = dataStore.getTypeNames()[0];
-        SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
-
-        if (featureSource instanceof SimpleFeatureStore) {
-            SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
-            SimpleFeatureCollection collection = new ListFeatureCollection(featureType, features);
-            featureStore.setTransaction(transaction);
-            try {
-                featureStore.addFeatures(collection);
-                transaction.commit();
-            } catch (Exception e) {
-                transaction.rollback();
-                throw e;
-            } finally {
-                transaction.close();
+        // 使用 FeatureWriter 代替 SimpleFeatureStore.addFeatures
+        try (FeatureWriter<SimpleFeatureType, SimpleFeature> writer = dataStore.getFeatureWriterAppend(Transaction.AUTO_COMMIT)) {
+            for (SimpleFeature feature : features) {
+                try {
+                    SimpleFeature next = writer.next();
+                    next.setAttributes(feature.getAttributes());
+                    writer.write();
+                } catch (Exception e) {
+                    log.error("写入要素失败,跳过: {}", e.getMessage());
+                }
             }
+        } finally {
+            dataStore.dispose();
         }
-        dataStore.dispose();
 
         // 6. 生成 .cpg 文件
         File cpgFile = new File(filePath + File.separator + fileName + ".cpg");
@@ -938,6 +959,8 @@ public class SpatialFilesDbServiceImpl implements ISpatialFilesDbService {
         dataType = dataType.toLowerCase();
         if (dataType.contains("char") || dataType.contains("text")) {
             return String.class;
+        } else if (dataType.contains("int8") || dataType.contains("bigint")) {
+            return Long.class;
         } else if (dataType.contains("int")) {
             return Integer.class;
         } else if (dataType.contains("numeric") || dataType.contains("decimal") || dataType.contains("double") || dataType.contains("float") || dataType.contains("real")) {