管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2022-11-23 2d2d9138fbe5c9c43f070be59a2d10f18fbf4c3b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package com.lf.server.helper;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.gdal.ogr.*;
 
import java.lang.reflect.Field;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * GDB帮助类
 * @author WWW
 */
public class GdbHelper {
    private final static Log log = LogFactory.getLog(GdbHelper.class);
 
    public static List<String> excludeFields = new ArrayList<String>();
 
    static {
        excludeFields.add("gid");
        excludeFields.add("shape_leng");
    }
 
    /**
     * 获取表名
     */
    public static List<String> getTabNames(String filePath) {
        List<String> list = new ArrayList<>();
        try {
            org.gdal.ogr.Driver driver = ogr.GetDriverByName("OpenFileGDB");
            if (null == driver) {
                return list;
            }
 
            DataSource dataSource = driver.Open(filePath, 0);
            if (null == dataSource) {
                return list;
            }
 
            for (int i = 0, count = dataSource.GetLayerCount(); i < count; i++) {
                Layer layer = dataSource.GetLayer(i);
                list.add(layer.GetName());
 
                layer.delete();
            }
 
            dataSource.delete();
            driver.delete();
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
 
        return list;
    }
 
    /**
     * 读取数据
     */
    public static <T> List<T> readData(Class clazz, String filePath, String layerName) {
        List<T> list = new ArrayList<>();
        try {
            org.gdal.ogr.Driver driver = ogr.GetDriverByName("OpenFileGDB");
            if (null == driver) {
                return list;
            }
 
            DataSource dataSource = driver.Open(filePath, 0);
            if (null == dataSource) {
                return list;
            }
 
            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);
                    break;
                }
 
                layer.delete();
            }
 
            dataSource.delete();
            driver.delete();
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
 
        return list;
    }
 
    /**
     * 读取图层
     */
    public static <T> void readLayer(Class clazz, Layer layer, List<T> list) {
        try {
            Field gField = getGeomField(clazz);
            Map<Integer, Field> map = getFieldMapper(clazz, layer);
            if (map.size() == 0) {
                return;
            }
            if (1 > layer.GetFeatureCount()) {
                return;
            }
 
            do {
                Feature f = layer.GetNextFeature();
                if (null == f) {
                    break;
                }
 
                T t = (T) clazz.newInstance();
                if (readFeature(t, f, map, gField)) {
                    list.add(t);
                }
            } while (true);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        } finally {
            if (null != layer) {
                layer.delete();
            }
        }
    }
 
    /**
     * 获取 geom 字段
     */
    private static Field getGeomField(Class clazz) {
        try {
            Field gField = clazz.getDeclaredField("geom");
            gField.setAccessible(true);
 
            return gField;
        } catch (Exception ex) {
            return null;
        }
    }
 
    /**
     * 获取字段映射
     */
    private static <T> Map<Integer, Field> getFieldMapper(Class clazz, Layer layer) {
        Map<Integer, Field> map = new HashMap<>(3);
 
        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();
                    if (excludeFields.contains(name)){
                        continue;
                    }
 
                    Field field = clazz.getDeclaredField(name);
                    field.setAccessible(true);
 
                    map.put(i, field);
                } catch (Exception e) {
                    //
                }
            }
        } catch (Exception ex) {
            //
        }
 
        return map;
    }
 
    /**
     * 读取Feature
     */
    private static <T> boolean readFeature(T t, Feature f, Map<Integer, Field> map, Field gField) {
        try {
            for (Integer i : map.keySet()) {
                Field field = map.get(i);
                setValue(t, f, field, i);
            }
 
            if (null != gField) {
                setGeom(t, f, gField);
            }
 
            return true;
        } catch (Exception ex) {
            return false;
        }
    }
 
    /**
     * 设置值
     */
    private static <T> void setValue(T t, Feature f, Field field, Integer i) throws Exception {
        switch (field.getType().toString()) {
            case "class java.math.BigDecimal":
            case "class java.lang.Double":
            case "double":
                field.set(t, f.GetFieldAsDouble(i));
                break;
            case "class java.lang.Long":
            case "long":
                field.set(t, f.GetFieldAsInteger64(i));
                break;
            case "class java.lang.Integer":
            case "int":
                field.set(t, f.GetFieldAsInteger(i));
                break;
            case "class java.sql.Timestamp":
                field.set(t, getTimestamp(f, i));
                break;
            default:
                field.set(t, f.GetFieldAsString(i));
                break;
        }
    }
 
    /**
     * 设置 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) throws Exception {
        Geometry geometry = f.GetGeometryRef();
        if (null != geometry) {
            String wkt = geometry.ExportToWkt();
            switch (geometry.GetGeometryType()) {
                case 2:
                    wkt = wkt.replace("LINESTRING (", "MULTILINESTRING ((") + ")";
                    break;
                case 3:
                    wkt = wkt.replace("POLYGON (", "MULTIPOLYGON ((") + ")";
                    break;
                default:
                    break;
            }
 
            gField.set(t, String.format("ST_GeomFromText('%s')", wkt));
        }
    }
 
    /**
     * 获取 Timestamp
     */
    private static Timestamp getTimestamp(Feature f, int index) {
        int[] pnYear = new int[1];
        int[] pnMonth = new int[1];
        int[] pnDay = new int[1];
        int[] pnHour = new int[1];
        int[] pnMinute = new int[1];
        float[] pfSecond = new float[1];
        int[] pnTzFlag = new int[1];
 
        f.GetFieldAsDateTime(index, pnYear, pnMonth, pnDay, pnHour, pnMinute, pfSecond, pnTzFlag);
 
        float fSecond = pfSecond[0];
        int s = (int) fSecond;
        int ns = (int) (1000000000 * fSecond - s);
 
        LocalDateTime localDateTime = LocalDateTime.of(
                LocalDate.of(pnYear[0], pnMonth[0], pnDay[0]),
                LocalTime.of(pnHour[0], pnMinute[0], s, ns)
        );
 
        Timestamp timestamp = Timestamp.valueOf(localDateTime);
 
        return timestamp;
    }
}