From 71726a1cb05790d6841595ef7daef5173f2cddfa Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期三, 29 三月 2023 09:43:11 +0800 Subject: [PATCH] 1 --- src/main/java/com/lf/server/helper/GdbHelper.java | 120 ++++++++++++++++++++++++++++++++++-------------------------- 1 files changed, 68 insertions(+), 52 deletions(-) diff --git a/src/main/java/com/lf/server/helper/GdbHelper.java b/src/main/java/com/lf/server/helper/GdbHelper.java index 31ddbd5..eaa458e 100644 --- a/src/main/java/com/lf/server/helper/GdbHelper.java +++ b/src/main/java/com/lf/server/helper/GdbHelper.java @@ -2,6 +2,7 @@ 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; @@ -161,9 +162,8 @@ } T t = (T) clazz.newInstance(); - if (readFeature(t, f, map, gField)) { - list.add(t); - } + readFeature(t, f, map, gField); + list.add(t); } while (true); } catch (Exception ex) { log.error(ex.getMessage(), ex); @@ -220,27 +220,25 @@ /** * 璇诲彇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) { + 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, f, gField); } } /** * 璁剧疆鍊� */ - 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); @@ -259,10 +257,16 @@ 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)); @@ -284,34 +288,33 @@ * 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: + private static <T> void setGeom(T t, Feature f, Field gField) { + try { + String geo = "null"; + if (null != f.GetGeometryRef()) { + String wkt = f.GetGeometryRef().ExportToWkt(); + // f.GetGeometryRef().GetGeometryType() + if (wkt.contains(StaticData.LINESTRING) && !wkt.contains(StaticData.MULTILINESTRING)) { wkt = wkt.replace("LINESTRING (", "MULTILINESTRING ((") + ")"; - break; - case 3: + } + if (wkt.contains(StaticData.POLYGON) && !wkt.contains(StaticData.MULTIPOLYGON)) { wkt = wkt.replace("POLYGON (", "MULTIPOLYGON ((") + ")"; - break; - default: - break; - } - geo = String.format("ST_GeomFromText('%s')", wkt); - } + } + wkt = wkt.replace(" 0,", ",").replace(" 0)", ")"); - gField.set(t, geo); + geo = String.format("ST_GeomFromText('%s')", wkt); + } + + gField.set(t, geo); + } 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]; @@ -326,6 +329,10 @@ 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) @@ -337,7 +344,7 @@ /** * 鑾峰彇 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]; @@ -347,6 +354,9 @@ 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]); } @@ -372,7 +382,7 @@ for (String key : map.keySet()) { Layer layer = null; try { - GeomBaseMapper baseMapper = ClassHelper.getGeoBaseMapper(key); + BasicMapper baseMapper = ClassHelper.getBasicMapper(key); if (null == baseMapper) { continue; } @@ -388,8 +398,7 @@ } 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)); @@ -413,18 +422,21 @@ /** * 鍒涘缓鍥惧眰 */ - 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); @@ -436,7 +448,7 @@ */ private static Integer getGeomType(String geomType) { if (StringHelper.isEmpty(geomType)) { - return ogr.wkbUnknown; + return ogr.wkbPoint; } switch (geomType) { @@ -460,11 +472,11 @@ /** * 鑾峰彇瀛楁 */ - 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; } @@ -473,7 +485,7 @@ } if (!StaticData.OBJECT.equals(clazz.getSuperclass().getName())) { - getFields(clazz.getSuperclass(), list); + getFields(clazz.getSuperclass(), list, excludeFields); } } catch (Exception ex) { // @@ -525,9 +537,13 @@ 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); -- Gitblit v1.9.3