| | |
| | | |
| | | import com.lf.server.entity.all.BaseGeoEntity; |
| | | import com.lf.server.entity.all.StaticData; |
| | | import com.lf.server.mapper.all.BasicMapper; |
| | | 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 org.gdal.osr.CoordinateTransformation; |
| | | import org.gdal.osr.SpatialReference; |
| | | import org.gdal.osr.osr; |
| | | |
| | | import java.lang.reflect.Field; |
| | | import java.math.BigDecimal; |
| | |
| | | Driver driver = null; |
| | | DataSource dataSource = null; |
| | | try { |
| | | driver = ogr.GetDriverByName("OpenFileGDB"); |
| | | // driver = ogr.GetDriverByName("OpenFileGDB") |
| | | driver = ogr.GetDriverByName("FileGDB"); |
| | | if (null == driver) { |
| | | log.error("GdbHelper.getTabNames.driver(OpenFileGDB) is null."); |
| | | log.error("GdbHelper.getTabNames.driver is null."); |
| | | 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; |
| | | DataSource dataSource = null; |
| | | try { |
| | | driver = ogr.GetDriverByName("OpenFileGDB"); |
| | | // driver = ogr.GetDriverByName("OpenFileGDB") |
| | | driver = ogr.GetDriverByName("FileGDB"); |
| | | if (null == driver) { |
| | | 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); |
| | | GdbHelper.readLayer(clazz, layer, list, isTransform); |
| | | break; |
| | | } |
| | | |
| | |
| | | /** |
| | | * 读取图层 |
| | | */ |
| | | 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); |
| | | |
| | |
| | | return; |
| | | } |
| | | |
| | | CoordinateTransformation ct = getCoordinateTransformation(layer, isTransform); |
| | | |
| | | do { |
| | | Feature f = layer.GetNextFeature(); |
| | | if (null == f) { |
| | |
| | | } |
| | | |
| | | T t = (T) clazz.newInstance(); |
| | | if (readFeature(t, f, map, gField)) { |
| | | list.add(t); |
| | | } |
| | | readFeature(t, f, map, gField, ct); |
| | | list.add(t); |
| | | } while (true); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | } finally { |
| | | GdbHelper.delete(layer); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取坐标转换器 |
| | | */ |
| | | 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) { |
| | | 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); |
| | | } |
| | | |
| | | /** |
| | |
| | | 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); |
| | |
| | | getFieldMapper(clazz.getSuperclass(), layer, map); |
| | | } |
| | | } catch (Exception ex) { |
| | | // |
| | | log.error(ex); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 读取Feature |
| | | */ |
| | | private static <T> boolean readFeature(T t, Feature f, Map<Integer, Field> map, Field gField) { |
| | | try { |
| | | for (Integer i : map.keySet()) { |
| | | 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); |
| | | setValue(t, f, field, i); |
| | | } catch (Exception e) { |
| | | log.error(e.getMessage(), e); |
| | | } |
| | | } |
| | | |
| | | if (null != gField) { |
| | | setGeom(t, f, gField); |
| | | } |
| | | |
| | | return true; |
| | | } catch (Exception ex) { |
| | | return false; |
| | | if (null != gField) { |
| | | setGeom(t, gField, f.GetGeometryRef(), ct); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 设置值 |
| | | */ |
| | | private static <T> void setValue(T t, Feature f, Field field, Integer i) throws Exception { |
| | | public static <T> void setValue(T t, Feature f, Field field, Integer i) throws Exception { |
| | | switch (field.getType().getName()) { |
| | | case "java.math.BigDecimal": |
| | | double dd = f.GetFieldAsDouble(i); |
| | |
| | | field.set(t, f.GetFieldAsInteger(i)); |
| | | break; |
| | | case "java.sql.Timestamp": |
| | | field.set(t, getTimestamp(f, i)); |
| | | Timestamp ts = getTimestamp(f, i); |
| | | if (null != ts) { |
| | | field.set(t, ts); |
| | | } |
| | | break; |
| | | case "java.time.LocalDate": |
| | | field.set(t, getLocalDate(f, i)); |
| | | LocalDate ld = getLocalDate(f, i); |
| | | if (null != ld) { |
| | | field.set(t, ld); |
| | | } |
| | | break; |
| | | default: |
| | | field.set(t, f.GetFieldAsString(i)); |
| | |
| | | * 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(); |
| | | // noinspection AlibabaRemoveCommentedCode |
| | | switch (f.GetGeometryRef().GetGeometryType()) { |
| | | //case 1: |
| | | // wkt = wkt.replace("POINT", "MULTIPOINT"); |
| | | // break; |
| | | case 2: |
| | | wkt = wkt.replace("LINESTRING (", "MULTILINESTRING ((") + ")"; |
| | | break; |
| | | case 3: |
| | | wkt = wkt.replace("POLYGON (", "MULTIPOLYGON ((") + ")"; |
| | | break; |
| | | default: |
| | | break; |
| | | private static <T> void setGeom(T t, Field gField, Geometry geometry, CoordinateTransformation ct) { |
| | | try { |
| | | if (null == geometry) { |
| | | gField.set(t, "null"); |
| | | return; |
| | | } |
| | | geo = String.format("ST_GeomFromText('%s')", wkt); |
| | | } |
| | | 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 |
| | | */ |
| | | private static Timestamp getTimestamp(Feature f, int index) { |
| | | public static Timestamp getTimestamp(Feature f, int index) { |
| | | int[] pnYear = new int[1]; |
| | | int[] pnMonth = new int[1]; |
| | | int[] pnDay = new int[1]; |
| | |
| | | int s = (int) fSecond; |
| | | int ns = (int) (1000000000 * fSecond - s); |
| | | |
| | | if (pnYear[0] > StaticData.I2050 || pnMonth[0] > StaticData.I12 || pnDay[0] > StaticData.I31 || pnHour[0] > StaticData.I24 || pnMinute[0] > StaticData.I60) { |
| | | return null; |
| | | } |
| | | |
| | | LocalDateTime localDateTime = LocalDateTime.of( |
| | | LocalDate.of(pnYear[0], pnMonth[0], pnDay[0]), |
| | | LocalTime.of(pnHour[0], pnMinute[0], s, ns) |
| | |
| | | /** |
| | | * 获取 LocalDate |
| | | */ |
| | | private static LocalDate getLocalDate(Feature f, int index) { |
| | | public static LocalDate getLocalDate(Feature f, int index) { |
| | | int[] pnYear = new int[1]; |
| | | int[] pnMonth = new int[1]; |
| | | int[] pnDay = new int[1]; |
| | |
| | | int[] pnTzFlag = new int[1]; |
| | | |
| | | f.GetFieldAsDateTime(index, pnYear, pnMonth, pnDay, pnHour, pnMinute, pfSecond, pnTzFlag); |
| | | if (pnYear[0] > StaticData.I2050 || pnMonth[0] > StaticData.I12 || pnDay[0] > StaticData.I31) { |
| | | return null; |
| | | } |
| | | |
| | | return LocalDate.of(pnYear[0], pnMonth[0], pnDay[0]); |
| | | } |
| | |
| | | for (String key : map.keySet()) { |
| | | Layer layer = null; |
| | | try { |
| | | GeomBaseMapper baseMapper = ClassHelper.getGeoBaseMapper(key); |
| | | BasicMapper baseMapper = ClassHelper.getBasicMapper(key); |
| | | if (null == baseMapper) { |
| | | continue; |
| | | } |
| | |
| | | } |
| | | |
| | | List<Field> fields = new ArrayList<>(); |
| | | // fields.add(getGeomField(clazz)) |
| | | getFields(clazz, fields); |
| | | getFields(clazz, fields, StaticData.GDB_EXCLUDE_FIELDS); |
| | | addLayerField(layer, fields); |
| | | |
| | | setLayerData(layer, fields, map.get(key)); |
| | |
| | | /** |
| | | * 创建图层 |
| | | */ |
| | | private static Layer createLayer(DataSource dataSource, GeomBaseMapper baseMapper ) { |
| | | private static Layer createLayer(DataSource dataSource, BasicMapper baseMapper ) { |
| | | String tab = BaseQueryService.getTabName(baseMapper); |
| | | if (StringHelper.isNull(tab)) { |
| | | return null; |
| | | } |
| | | |
| | | String geomType = baseMapper.selectGeometryType(tab); |
| | | if (StringHelper.isEmpty(geomType)) { |
| | | return null; |
| | | Integer srid = null; |
| | | String geomType = null; |
| | | if (baseMapper instanceof GeomBaseMapper) { |
| | | GeomBaseMapper geomMapper = (GeomBaseMapper) baseMapper; |
| | | geomType = geomMapper.selectGeometryType(tab); |
| | | |
| | | srid = geomMapper.selectSrid(tab); |
| | | } |
| | | |
| | | Integer srid = baseMapper.selectSrid(tab); |
| | | SpatialReference sr = new SpatialReference(); |
| | | sr.ImportFromEPSG(null == srid ? 4490 : srid); |
| | | |
| | | return dataSource.CreateLayer(tab, sr, getGeomType(geomType), null); |
| | | return dataSource.CreateLayer(tab.replace(".", "_"), sr, getGeomType(geomType), null); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | private static Integer getGeomType(String geomType) { |
| | | if (StringHelper.isEmpty(geomType)) { |
| | | return ogr.wkbUnknown; |
| | | return ogr.wkbPoint; |
| | | } |
| | | |
| | | switch (geomType) { |
| | |
| | | /** |
| | | * 获取字段 |
| | | */ |
| | | private static void getFields(Class clazz, List<Field> list) { |
| | | public static void getFields(Class clazz, List<Field> list, List<String> excludeFields) { |
| | | try { |
| | | Field[] fields = clazz.getDeclaredFields(); |
| | | for (Field f : fields) { |
| | | if (StaticData.GDB_EXCLUDE_FIELDS.contains(f.getName())) { |
| | | if (excludeFields.contains(f.getName())) { |
| | | continue; |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | if (!StaticData.OBJECT.equals(clazz.getSuperclass().getName())) { |
| | | getFields(clazz.getSuperclass(), list); |
| | | getFields(clazz.getSuperclass(), list, excludeFields); |
| | | } |
| | | } catch (Exception ex) { |
| | | // |
| | |
| | | for (T t : list) { |
| | | Feature f = new Feature(layer.GetLayerDefn()); |
| | | |
| | | BaseGeoEntity geoEntity = (BaseGeoEntity) t; |
| | | Geometry geom = Geometry.CreateFromWkt(geoEntity.getGeom()); |
| | | f.SetGeometry(geom); |
| | | if (t instanceof BaseGeoEntity) { |
| | | BaseGeoEntity geoEntity = (BaseGeoEntity) t; |
| | | if (!StringHelper.isEmpty(geoEntity.getGeom())) { |
| | | Geometry geom = Geometry.CreateFromWkt(geoEntity.getGeom()); |
| | | f.SetGeometry(geom); |
| | | } |
| | | } |
| | | |
| | | setFeatureData(f, fields, t); |
| | | layer.CreateFeature(f); |