| | |
| | | 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; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * GDB帮助类 |
| | |
| | | public class GdbHelper { |
| | | private final static Log log = LogFactory.getLog(GdbHelper.class); |
| | | |
| | | public static List<String> excludeFields = new ArrayList<String>(); |
| | | public static List<String> excludeFields = new ArrayList<>(Arrays.asList("gid", "shape_leng")); |
| | | |
| | | static { |
| | | excludeFields.add("gid"); |
| | | excludeFields.add("shape_leng"); |
| | | /** |
| | | * 销毁资源 |
| | | */ |
| | | 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"); |
| | | 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; |
| | | } |
| | |
| | | |
| | | layer.delete(); |
| | | } |
| | | |
| | | dataSource.delete(); |
| | | driver.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 { |
| | | org.gdal.ogr.Driver driver = ogr.GetDriverByName("OpenFileGDB"); |
| | | 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); |
| | | |
| | | 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); |
| | | } finally { |
| | | GdbHelper.delete(dataSource, driver); |
| | | } |
| | | |
| | | return list; |
| | |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | } finally { |
| | | if (null != layer) { |
| | | layer.delete(); |
| | | } |
| | | GdbHelper.delete(layer); |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | private static Field getGeomField(Class clazz) { |
| | | try { |
| | | Field gField = clazz.getDeclaredField("geom"); |
| | | Field gField = clazz.getSuperclass().getDeclaredField("geom"); |
| | | gField.setAccessible(true); |
| | | |
| | | return gField; |
| | |
| | | * 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()) { |
| | | String geo = "null"; |
| | | if (null != f.GetGeometryRef()) { |
| | | String wkt = f.GetGeometryRef().ExportToWkt(); |
| | | switch (f.GetGeometryRef().GetGeometryType()) { |
| | | case 2: |
| | | wkt = wkt.replace("LINESTRING (", "MULTILINESTRING ((") + ")"; |
| | | break; |
| | |
| | | default: |
| | | break; |
| | | } |
| | | |
| | | gField.set(t, String.format("ST_GeomFromText('%s')", wkt)); |
| | | geo = String.format("ST_GeomFromText('%s')", wkt); |
| | | } |
| | | |
| | | gField.set(t, geo); |
| | | } |
| | | |
| | | /** |
| | |
| | | LocalTime.of(pnHour[0], pnMinute[0], s, ns) |
| | | ); |
| | | |
| | | Timestamp timestamp = Timestamp.valueOf(localDateTime); |
| | | |
| | | return timestamp; |
| | | return Timestamp.valueOf(localDateTime); |
| | | } |
| | | } |