管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2022-12-19 85fbc3fe04a0a2eff185c75687b316d99acd5a04
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package com.lf.server.helper;
 
import com.lf.server.entity.all.BaseGeoEntity;
import com.lf.server.entity.all.StaticData;
import com.lf.server.mapper.all.GeomBaseMapper;
import com.lf.server.service.all.BaseQueryService;
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.*;
 
/**
 * GDB帮助类
 * @author WWW
 */
public class GdbHelper {
    private final static Log log = LogFactory.getLog(GdbHelper.class);
 
    private final static String OBJECT ="java.lang.Object";
 
    /**
     * 销毁资源
     */
    public static void delete(Layer layer) {
        try {
            if (null != layer) {
                layer.delete();
            }
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
    }
 
    /**
     * 销毁资源
     */
    public static void delete(DataSource dataSource, Driver driver) {
        try {
            if (null != dataSource) {
                dataSource.delete();
            }
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
 
        try {
            if (null != driver) {
                driver.delete();
            }
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
    }
 
    /**
     * 销毁资源
     */
    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<>();
 
        Driver driver = null;
        DataSource dataSource = null;
        try {
            driver = ogr.GetDriverByName("OpenFileGDB");
            if (null == driver) {
                log.error("GdbHelper.getTabNames: OpenFileGDB is null");
                return list;
            }
 
            dataSource = driver.Open(filePath, 0);
            if (null == dataSource) {
                log.error("GdbHelper.getTabNames.dataSource is null. " + filePath);
                return list;
            }
 
            for (int i = 0, count = dataSource.GetLayerCount(); i < count; i++) {
                Layer layer = dataSource.GetLayer(i);
                list.add(layer.GetName());
 
                layer.delete();
            }
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        } finally {
            GdbHelper.delete(dataSource, driver);
        }
 
        return list;
    }
 
    /**
     * 读取数据
     */
    public static <T> List<T> readData(Class clazz, String filePath, String layerName) {
        List<T> list = new ArrayList<>();
 
        Driver driver = null;
        DataSource dataSource = null;
        try {
            driver = ogr.GetDriverByName("OpenFileGDB");
            if (null == driver) {
                return list;
            }
 
            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();
            }
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        } finally {
            GdbHelper.delete(dataSource, driver);
        }
 
        return list;
    }
 
    /**
     * 读取图层
     */
    public static <T> void readLayer(Class clazz, Layer layer, List<T> list) {
        try {
            Field gField = getGeomField(clazz);
 
            Map<Integer, Field> map = new HashMap<>(3);
            getFieldMapper(clazz, layer, map);
            if (map.isEmpty() || 0 == 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 {
            GdbHelper.delete(layer);
        }
    }
 
    /**
     * 获取 geom 字段
     */
    private static Field getGeomField(Class clazz) {
        try {
            Field gField = clazz.getSuperclass().getDeclaredField("geom");
            gField.setAccessible(true);
 
            return gField;
        } catch (Exception ex) {
            return null;
        }
    }
 
    /**
     * 获取字段映射
     */
    private static <T> 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();
                    if (StaticData.READ_EXCLUDE_FIELDS.contains(name)) {
                        continue;
                    }
 
                    Field field = clazz.getDeclaredField(name);
                    field.setAccessible(true);
 
                    map.put(i, field);
                } catch (Exception e) {
                    //
                }
            }
 
            if (OBJECT != clazz.getSuperclass().getName()) {
                getFieldMapper(clazz.getSuperclass(), layer, map);
            }
        } catch (Exception ex) {
            //
        }
    }
 
    /**
     * 读取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().getName()) {
            case "java.math.BigDecimal":
            case "java.lang.Double":
            case "double":
                field.set(t, f.GetFieldAsDouble(i));
                break;
            case "java.lang.Long":
            case "long":
                field.set(t, f.GetFieldAsInteger64(i));
                break;
            case "java.lang.Integer":
            case "int":
                field.set(t, f.GetFieldAsInteger(i));
                break;
            case "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 {
        String geo = "null";
        if (null != f.GetGeometryRef()) {
            String wkt = f.GetGeometryRef().ExportToWkt();
            switch (f.GetGeometryRef().GetGeometryType()) {
                case 2:
                    wkt = wkt.replace("LINESTRING (", "MULTILINESTRING ((") + ")";
                    break;
                case 3:
                    wkt = wkt.replace("POLYGON (", "MULTIPOLYGON ((") + ")";
                    break;
                default:
                    break;
            }
            geo = String.format("ST_GeomFromText('%s')", wkt);
        }
 
        gField.set(t, geo);
    }
 
    /**
     * 获取 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)
        );
 
        return Timestamp.valueOf(localDateTime);
    }
 
    /**
     * 创建GDB
     */
    public static void createGdb(String filePath,  Map<String, List<?>> map){
        Driver driver = null;
        DataSource dataSource = null;
        try{
            driver = ogr.GetDriverByName("OpenFileGDB");
            if (null == driver) {
                return;
            }
 
            dataSource = driver.CreateDataSource(filePath);
            if (null == dataSource) {
                return;
            }
 
            for(String key : map.keySet()){
                GeomBaseMapper baseMapper = ClassHelper.getGeoBaseMapper(key);
                if (null == baseMapper) {
                    continue;
                }
 
                String tab = BaseQueryService.getTabName(baseMapper);
                if (StringHelper.isNull(tab)) {
                    continue;
                }
 
                String className = ClassHelper.getClassName(baseMapper);
                Class clazz = ClassHelper.getEntityClass(className);
 
                List<?> list = map.get(key);
 
            }
        }catch (Exception ex){
            log.error(ex.getMessage(), ex);
        }finally {
            GdbHelper.delete(dataSource, driver);
        }
    }
 
    private static <T> String getGeomType(GeomBaseMapper baseMapper, List<T> list) {
        for (T t : list){
            if (!(t instanceof BaseGeoEntity)){
                return null;
            }
 
            BaseGeoEntity entity = (BaseGeoEntity)t;
            if (StringHelper.isEmpty(entity.getGeom())){
                continue;
            }
 
 
        }
 
        return null;
    }
}