| | |
| | | package com.lf.server.helper; |
| | | |
| | | import com.lf.server.entity.all.StaticData; |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | | import org.gdal.ogr.DataSource; |
| | | import org.gdal.ogr.Layer; |
| | | import org.gdal.ogr.ogr; |
| | | import org.gdal.ogr.*; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | 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帮助类 |
| | |
| | | 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 { |
| | | org.gdal.ogr.Driver driver = ogr.GetDriverByName("OpenFileGDB"); |
| | | if (driver == null) { |
| | | driver = ogr.GetDriverByName("OpenFileGDB"); |
| | | if (null == driver) { |
| | | return list; |
| | | } |
| | | |
| | | DataSource dataSource = driver.Open(filePath, 0); |
| | | 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(); |
| | | } |
| | | } 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) { |
| | | public static <T> List<T> readData(Class clazz, String filePath, String layerName) { |
| | | List<T> list = new ArrayList<>(); |
| | | try { |
| | | |
| | | 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); |
| | | } |
| | | } |