管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2022-12-20 488677202ae47d556bb6f4a69c86c8892efc6b05
src/main/java/com/lf/server/helper/GdbHelper.java
@@ -10,6 +10,7 @@
import org.gdal.osr.SpatialReference;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
@@ -78,7 +79,7 @@
        try {
            driver = ogr.GetDriverByName("OpenFileGDB");
            if (null == driver) {
                log.error("GdbHelper.getTabNames: OpenFileGDB is null");
                log.error("GdbHelper.getTabNames.driver(OpenFileGDB) is null.");
                return list;
            }
@@ -326,17 +327,18 @@
    /**
     * 创建GDB
     */
    public static void createGdb(String filePath,  Map<String, List<?>> map) {
    public static void createGdb(String filePath,  Map<String, List<?>> map) throws Exception {
        Driver driver = null;
        DataSource dataSource = null;
        try {
            driver = ogr.GetDriverByName("OpenFileGDB");
            driver = ogr.GetDriverByName("FileGDB");
            if (null == driver) {
                log.error("GdbHelper.createGdb.driver(FileGDB) is null.");
                return;
            }
            dataSource = driver.CreateDataSource(filePath);
            dataSource = driver.CreateDataSource(filePath, null);
            if (null == dataSource) {
                log.error("GdbHelper.createGdb.dataSource is null. " + filePath);
                return;
            }
@@ -354,18 +356,16 @@
                    String className = ClassHelper.getClassName(baseMapper);
                    Class clazz = ClassHelper.getEntityClass(className);
                    Field gField = getGeomField(clazz);
                    if (null == gField) {
                    if (null == clazz) {
                        continue;
                    }
                    List<Field> fields = new ArrayList<>();
                    // fields.add(getGeomField(clazz))
                    getFields(clazz, fields);
                    addLayerField(layer, fields);
                    setLayerData(layer, fields, map.get(key));
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                } finally {
                    if (null != layer) {
                        layer.delete();
@@ -377,6 +377,7 @@
            dataSource.FlushCache();
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            throw ex;
        } finally {
            GdbHelper.delete(dataSource, driver);
        }
@@ -456,20 +457,12 @@
    private static void addLayerField(Layer layer, List<Field> list) {
        for (int i = 0, c = list.size(); i < c; i++) {
            Field f = list.get(i);
            FieldDefn fd = getLayerField(f);
            layer.CreateField(fd, i);
            int fieldType = getFieldType(f);
            FieldDefn fd = new FieldDefn(f.getName(), fieldType);
            layer.CreateField(fd, i + 1);
        }
    }
    private static FieldDefn getLayerField(Field f ) {
        int fieldType = getFieldType(f);
        FieldDefn fd = new FieldDefn(f.getName(), fieldType);
        if (fieldType == ogr.OFTString) {
            // fd.SetWidth(100)
        }
        return fd;
    }
    /**
@@ -507,34 +500,41 @@
            for (int i = 0, c = fields.size(); i < c; i++) {
                Field field = fields.get(i);
                Object val = field.get(t);
                if (null == val) {
                    continue;
                }
                switch (field.getType().getName()) {
                    case "java.math.BigDecimal":
                        BigDecimal big = (BigDecimal) val;
                        f.SetField(i, big.doubleValue());
                        break;
                    case "java.lang.Double":
                    case "double":
                        Double d = field.getDouble(t);
                        double d = (double) val;
                        f.SetField(i, d);
                        break;
                    case "java.lang.Long":
                    case "long":
                        Long l = field.getLong(t);
                        long l = (long) val;
                        f.SetField(i, l);
                        break;
                    case "java.lang.Integer":
                    case "int":
                        Integer n = field.getInt(t);
                        int n = (int) val;
                        f.SetField(i, n);
                        break;
                    case "java.sql.Timestamp":
                        Timestamp time = null == field.get(t) ? null : (Timestamp) field.get(t);
                        Timestamp time = (Timestamp) field.get(t);
                        setTimestamp(f, i, time);
                        break;
                    default:
                        String str = null == field.get(t) ? null : (String) field.get(t);
                        String str = (String) val;
                        f.SetField(i, str);
                        break;
                }
            }
            layer.CreateFeature(f);
        }
    }
@@ -542,15 +542,10 @@
    /**
     * 设置Timestamp
     */
    @SuppressWarnings("AlibabaRemoveCommentedCode")
    private static void setTimestamp(Feature f, int i, Timestamp time) {
        if (null == time) {
            return;
        }
        //Calendar now = Calendar.getInstance();
        //now.setTimeInMillis(time.getTime());
        // f.SetField(i, now.get(Calendar.YEAR), now.get(Calendar.MONTH) + 1, now.get(Calendar.DATE), now.get(Calendar.HOUR), now.get(Calendar.MINUTE), now.get(Calendar.SECOND), 8);
        LocalDateTime local = time.toLocalDateTime();
        f.SetField(i, local.getYear(), local.getMonthValue(), local.getDayOfMonth(), local.getHour(), local.getMinute(), local.getSecond(), 8);