月球大数据地理空间分析展示平台-【后端】-月球后台服务
1
13693261870
2024-11-13 024e90554d19c2342f27a26f91bbea378f84da82
src/main/java/com/moon/server/helper/GdbHelper.java
@@ -8,7 +8,9 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.gdal.ogr.*;
import org.gdal.osr.CoordinateTransformation;
import org.gdal.osr.SpatialReference;
import org.gdal.osr.osr;
import java.lang.reflect.Field;
import java.math.BigDecimal;
@@ -18,16 +20,10 @@
import java.time.LocalTime;
import java.util.*;
/**
 * GDB帮助类
 * @author WWW
 */
@SuppressWarnings("ALL")
public class GdbHelper {
    private final static Log log = LogFactory.getLog(GdbHelper.class);
    /**
     * 销毁资源
     */
    public static void delete(Layer layer) {
        try {
            if (null != layer) {
@@ -38,9 +34,6 @@
        }
    }
    /**
     * 销毁资源
     */
    public static void delete(DataSource dataSource, Driver driver) {
        try {
            if (null != dataSource) {
@@ -59,17 +52,11 @@
        }
    }
    /**
     * 销毁资源
     */
    public static void delete(Layer layer, DataSource dataSource, Driver driver) {
        delete(layer);
        delete(dataSource, driver);
    }
    /**
     * 获取表名
     */
    public static List<String> getTabNames(String filePath) {
        List<String> list = new ArrayList<>();
@@ -104,10 +91,7 @@
        return list;
    }
    /**
     * 读取数据
     */
    public static <T> List<T> readData(Class clazz, String filePath, String layerName) {
    public static <T> List<T> readData(Class clazz, String filePath, String layerName, boolean isTransform) {
        List<T> list = new ArrayList<>();
        Driver driver = null;
@@ -127,7 +111,7 @@
            for (int i = 0, count = dataSource.GetLayerCount(); i < count; i++) {
                Layer layer = dataSource.GetLayer(i);
                if (layer.GetName().equals(layerName)) {
                    GdbHelper.readLayer(clazz, layer, list);
                    GdbHelper.readLayer(clazz, layer, list, isTransform);
                    break;
                }
@@ -142,10 +126,7 @@
        return list;
    }
    /**
     * 读取图层
     */
    public static <T> void readLayer(Class clazz, Layer layer, List<T> list) {
    public static <T> void readLayer(Class clazz, Layer layer, List<T> list, boolean isTransform) {
        try {
            Field gField = getGeomField(clazz);
@@ -155,6 +136,8 @@
                return;
            }
            CoordinateTransformation ct = getCoordinateTransformation(layer, isTransform);
            do {
                Feature f = layer.GetNextFeature();
                if (null == f) {
@@ -162,7 +145,7 @@
                }
                T t = (T) clazz.newInstance();
                readFeature(t, f, map, gField);
                readFeature(t, f, map, gField, ct);
                list.add(t);
            } while (true);
        } catch (Exception ex) {
@@ -172,9 +155,30 @@
        }
    }
    /**
     * 获取 geom 字段
     */
    private static CoordinateTransformation getCoordinateTransformation(Layer layer, boolean isTransform) {
        if (!isTransform) {
            return null;
        }
        String epsg = layer.GetSpatialRef().GetAttrValue("AUTHORITY", 1);
        if (StringHelper.isEmpty(epsg)) {
            return null;
        }
        int epsgId = Integer.parseInt(epsg);
        if (StaticData.I4326 == epsgId || StaticData.I4490 == epsgId || StaticData.I104903 == epsgId) {
            return null;
        }
        SpatialReference srTarget = new SpatialReference();
        srTarget.ImportFromEPSG(StaticData.I4490);
        layer.GetSpatialRef().SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER);
        srTarget.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER);
        return CoordinateTransformation.CreateCoordinateTransformation(layer.GetSpatialRef(), srTarget);
    }
    private static Field getGeomField(Class clazz) {
        try {
            Field gField = clazz.getSuperclass().getDeclaredField("geom");
@@ -186,21 +190,17 @@
        }
    }
    /**
     * 获取字段映射
     */
    private static void getFieldMapper(Class clazz, Layer layer, Map<Integer, Field> map) {
        try {
            FeatureDefn fd = layer.GetLayerDefn();
            for (int i = 0, count = fd.GetFieldCount(); i < count; i++) {
                FieldDefn fieldDefn = fd.GetFieldDefn(i);
                try {
                    String name = fieldDefn.GetName().toLowerCase();
                    String name = fd.GetFieldDefn(i).GetName().toLowerCase();
                    if (StaticData.READ_EXCLUDE_FIELDS.contains(name)) {
                        continue;
                    }
                    Field field = clazz.getDeclaredField(name);
                    Field field = clazz.getDeclaredField("gbcode".equals(name) ? "gb" : name);
                    field.setAccessible(true);
                    map.put(i, field);
@@ -213,14 +213,11 @@
                getFieldMapper(clazz.getSuperclass(), layer, map);
            }
        } catch (Exception ex) {
            //
            log.error(ex);
        }
    }
    /**
     * 读取Feature
     */
    private static <T> void readFeature(T t, Feature f, Map<Integer, Field> map, Field gField) {
    private static <T> void readFeature(T t, Feature f, Map<Integer, Field> map, Field gField, CoordinateTransformation ct) {
        for (Integer i : map.keySet()) {
            try {
                Field field = map.get(i);
@@ -231,13 +228,10 @@
        }
        if (null != gField) {
            setGeom(t, f, gField);
            setGeom(t, gField, f.GetGeometryRef(), ct);
        }
    }
    /**
     * 设置值
     */
    public static <T> void setValue(T t, Feature f, Field field, Integer i) throws Exception {
        switch (field.getType().getName()) {
            case "java.math.BigDecimal":
@@ -274,46 +268,37 @@
        }
    }
    /**
     * 设置 geom 字段值
     * <p>
     * wkbUnknown = 0,
     * wkbPoint = 1,
     * wkbLineString = 2,
     * wkbPolygon = 3,
     * wkbMultiPoint = 4,
     * wkbMultiLineString = 5,
     * wkbMultiPolygon = 6,
     * wkbGeometryCollection = 7,
     * wkbNone = 100,
     * wkbLinearRing = 101
     */
    private static <T> void setGeom(T t, Feature f, Field gField) {
    private static <T> void setGeom(T t, Field gField, Geometry geometry, CoordinateTransformation ct) {
        try {
            String geo = "null";
            if (null != f.GetGeometryRef()) {
                String wkt = f.GetGeometryRef().ExportToWkt();
                // f.GetGeometryRef().GetGeometryType()
                if (wkt.contains(StaticData.LINESTRING) && !wkt.contains(StaticData.MULTILINESTRING)) {
                    wkt = wkt.replace("LINESTRING (", "MULTILINESTRING ((") + ")";
                }
                if (wkt.contains(StaticData.POLYGON) && !wkt.contains(StaticData.MULTIPOLYGON)) {
                    wkt = wkt.replace("POLYGON (", "MULTIPOLYGON ((") + ")";
                }
                wkt = wkt.replace(" 0,", ",").replace(" 0)", ")");
                geo = String.format("ST_GeomFromText('%s')", wkt);
            if (null == geometry) {
                gField.set(t, "null");
                return;
            }
            if (null != ct) {
                int flag = geometry.Transform(ct);
            }
            gField.set(t, geo);
            String wkt = geometry.ExportToWkt();
            if (wkt.contains(StaticData.LINESTRING) && !wkt.contains(StaticData.MULTILINESTRING)) {
                wkt = wkt.replace("LINESTRING (", "MULTILINESTRING ((") + ")";
            }
            if (wkt.contains(StaticData.POLYGON) && !wkt.contains(StaticData.MULTIPOLYGON)) {
                wkt = wkt.replace("POLYGON (", "MULTIPOLYGON ((") + ")";
            }
            wkt = wkt.replace(" 0,", ",").replace(" 0)", ")");
            if (wkt.contains(StaticData.MULTICURVE)) {
                wkt = wkt.replace("MULTICURVE (", "MULTILINESTRING (").replace("CIRCULARSTRING ", "");
                if (wkt.contains(StaticData.COMPOUNDCURVE)) {
                    wkt = wkt.replace("COMPOUNDCURVE (", "").replace(")))", "))");
                }
            }
            gField.set(t, String.format("ST_GeomFromText('%s')", wkt));
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
    }
    /**
     * 获取 Timestamp
     */
    public static Timestamp getTimestamp(Feature f, int index) {
        int[] pnYear = new int[1];
        int[] pnMonth = new int[1];
@@ -341,9 +326,6 @@
        return Timestamp.valueOf(localDateTime);
    }
    /**
     * 获取 LocalDate
     */
    public static LocalDate getLocalDate(Feature f, int index) {
        int[] pnYear = new int[1];
        int[] pnMonth = new int[1];
@@ -361,9 +343,6 @@
        return LocalDate.of(pnYear[0], pnMonth[0], pnDay[0]);
    }
    /**
     * 创建GDB
     */
    public static void createGdb(String filePath,  Map<String, List<?>> map) throws Exception {
        Driver driver = null;
        DataSource dataSource = null;
@@ -419,9 +398,6 @@
        }
    }
    /**
     * 创建图层
     */
    private static Layer createLayer(DataSource dataSource, BasicMapper baseMapper ) {
        String tab = BaseQueryService.getTabName(baseMapper);
        if (StringHelper.isNull(tab)) {
@@ -437,15 +413,18 @@
            srid = geomMapper.selectSrid(tab);
        }
        SpatialReference sr = new SpatialReference();
        sr.ImportFromEPSG(null == srid ? 4490 : srid);
        // SpatialReference sr = new SpatialReference(); sr.ImportFromEPSG(null == srid || 0 == srid ? StaticData.I104903 : srid)
        SpatialReference sr;
        if (null == srid || StaticData.DEFAULT_EPSG.contains(srid)) {
            sr = new SpatialReference(StaticData.MOON_2000_WKT);
        } else {
            sr = new SpatialReference();
            sr.ImportFromEPSG(srid);
        }
        return dataSource.CreateLayer(tab.replace(".", "_"), sr, getGeomType(geomType), null);
    }
    /**
     * 获取Geom类别
     */
    private static Integer getGeomType(String geomType) {
        if (StringHelper.isEmpty(geomType)) {
            return ogr.wkbPoint;
@@ -469,9 +448,6 @@
        }
    }
    /**
     * 获取字段
     */
    public static void getFields(Class clazz, List<Field> list, List<String> excludeFields) {
        try {
            Field[] fields = clazz.getDeclaredFields();
@@ -492,9 +468,6 @@
        }
    }
    /**
     * 添加图层字段
     */
    public static void addLayerField(Layer layer, List<Field> list) {
        for (int i = 0, c = list.size(); i < c; i++) {
            Field f = list.get(i);
@@ -506,9 +479,6 @@
        }
    }
    /**
     * 获取字段类型
     */
    private static Integer getFieldType(Field f) {
        switch (f.getType().getName()) {
            case "java.math.BigDecimal":
@@ -530,9 +500,6 @@
        }
    }
    /**
     * 设置图层数据
     */
    private static <T> void setLayerData(Layer layer,  List<Field> fields, List<T> list) throws Exception {
        for (T t : list) {
            Feature f = new Feature(layer.GetLayerDefn());
@@ -550,9 +517,6 @@
        }
    }
    /**
     * 设置要素的数据
     */
    public static <T> void setFeatureData(Feature f, List<Field> fields, T t) throws Exception {
        for (int i = 0, c = fields.size(); i < c; i++) {
            Field field = fields.get(i);
@@ -597,9 +561,6 @@
        }
    }
    /**
     * 设置Timestamp
     */
    private static void setTimestamp(Feature f, int i, Timestamp time) {
        if (null == time) {
            return;
@@ -609,9 +570,6 @@
        f.SetField(i, local.getYear(), local.getMonthValue(), local.getDayOfMonth(), local.getHour(), local.getMinute(), local.getSecond(), 8);
    }
    /**
     * 设置LocalDate
     */
    private static void setLocalDate(Feature f, int i, LocalDate local) {
        if (null == local) {
            return;