From b6b0cb226fcf184525ee7b36af3a09471e9c0057 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期一, 25 三月 2024 11:29:33 +0800 Subject: [PATCH] 修改数据统计的查询条件 --- src/main/java/com/lf/server/helper/GdbHelper.java | 176 ++++++++++++++++++++++++++++++++++++++-------------------- 1 files changed, 115 insertions(+), 61 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..d11badd 100644 --- a/src/main/java/com/lf/server/helper/GdbHelper.java +++ b/src/main/java/com/lf/server/helper/GdbHelper.java @@ -2,12 +2,15 @@ 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; @@ -106,7 +109,7 @@ /** * 璇诲彇鏁版嵁 */ - 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; @@ -126,7 +129,7 @@ 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; } @@ -144,7 +147,7 @@ /** * 璇诲彇鍥惧眰 */ - 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); @@ -154,6 +157,8 @@ return; } + CoordinateTransformation ct = getCoordinateTransformation(layer, isTransform); + do { Feature f = layer.GetNextFeature(); if (null == f) { @@ -161,15 +166,41 @@ } 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); } /** @@ -193,14 +224,13 @@ 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); @@ -213,34 +243,32 @@ 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); @@ -259,10 +287,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 +318,41 @@ * 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]; @@ -326,6 +367,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 +382,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 +392,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 +420,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 +436,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,22 +460,25 @@ /** * 鍒涘缓鍥惧眰 */ - 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); } /** @@ -436,7 +486,7 @@ */ private static Integer getGeomType(String geomType) { if (StringHelper.isEmpty(geomType)) { - return ogr.wkbUnknown; + return ogr.wkbPoint; } switch (geomType) { @@ -460,11 +510,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 +523,7 @@ } if (!StaticData.OBJECT.equals(clazz.getSuperclass().getName())) { - getFields(clazz.getSuperclass(), list); + getFields(clazz.getSuperclass(), list, excludeFields); } } catch (Exception ex) { // @@ -525,9 +575,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