|
|
@@ -618,85 +618,86 @@ public class CadastreManageServiceImpl implements CadastreManageService {
|
|
|
}
|
|
|
|
|
|
|
|
|
- /**
|
|
|
- * 获取当前图层的所有字段名称和值
|
|
|
- *
|
|
|
- * @param layer
|
|
|
- * @return
|
|
|
- */
|
|
|
- private List<Map<String, Object>> getValueList(Layer layer, Map<String, String> fieldNameAndTypeMap, Boolean haveGeom) {
|
|
|
- List<Map<String, Object>> list = new ArrayList<>();
|
|
|
- Integer sourceSRID = 4326;
|
|
|
-
|
|
|
- // ========== 1. 获取源数据坐标系 EPSG:4527 ==========
|
|
|
+ private static final String[] DATE_PATTERNS = {
|
|
|
+ "yyyy-MM-dd HH:mm:ssX",
|
|
|
+ "yyyy-MM-dd HH:mm:ss",
|
|
|
+ "yyyy/MM/dd HH:mm:ssX",
|
|
|
+ "yyyy/MM/dd HH:mm:ss",
|
|
|
+ "yyyy-MM-dd",
|
|
|
+ "yyyy/MM/dd"
|
|
|
+ };
|
|
|
+
|
|
|
+ private Object convertFieldValue(Feature feature, int fieldIndex, String fieldName, String fieldType) {
|
|
|
+ if (feature.IsFieldNull(fieldIndex)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String strValue = feature.GetFieldAsString(fieldIndex);
|
|
|
+ if (StringUtils.isBlank(strValue)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
try {
|
|
|
- SpatialReference srcSR = layer.GetSpatialRef();
|
|
|
- if (srcSR != null) {
|
|
|
- srcSR.AutoIdentifyEPSG();
|
|
|
- String code = srcSR.GetAuthorityCode(null);
|
|
|
- if (code != null) {
|
|
|
- sourceSRID = Integer.parseInt(code);
|
|
|
- }
|
|
|
+ if (StringUtils.containsIgnoreCase(fieldType, "character") || StringUtils.containsIgnoreCase(fieldType, "text")) {
|
|
|
+ return strValue;
|
|
|
+ } else if (StringUtils.containsIgnoreCase(fieldType, "integer") || StringUtils.containsIgnoreCase(fieldType, "smallint") || StringUtils.containsIgnoreCase(fieldType, "int2") || StringUtils.containsIgnoreCase(fieldType, "int4")) {
|
|
|
+ return feature.GetFieldAsInteger(fieldIndex);
|
|
|
+ } else if (StringUtils.containsIgnoreCase(fieldType, "bigint") || StringUtils.containsIgnoreCase(fieldType, "int8")) {
|
|
|
+ return feature.GetFieldAsInteger64(fieldIndex);
|
|
|
+ } else if (StringUtils.containsIgnoreCase(fieldType, "timestamp")) {
|
|
|
+ return parseDate(strValue, fieldName);
|
|
|
+ } else if (StringUtils.containsIgnoreCase(fieldType, "date")) {
|
|
|
+ return parseDate(strValue, fieldName);
|
|
|
+ } else if (StringUtils.containsIgnoreCase(fieldType, "numeric") || StringUtils.containsIgnoreCase(fieldType, "double") || StringUtils.containsIgnoreCase(fieldType, "real")) {
|
|
|
+ return feature.GetFieldAsDouble(fieldIndex);
|
|
|
+ } else if (StringUtils.containsIgnoreCase(fieldType, "bytea") || StringUtils.containsIgnoreCase(fieldType, "blob") || StringUtils.containsIgnoreCase(fieldType, "binary")) {
|
|
|
+ return null;
|
|
|
+ } else if (StringUtils.containsIgnoreCase(fieldType, "boolean")) {
|
|
|
+ return feature.GetFieldAsInteger(fieldIndex) != 0;
|
|
|
+ } else {
|
|
|
+ return strValue;
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
- log.warn("获取图层坐标系SRID失败,使用默认4326", e);
|
|
|
+ log.warn("字段{}类型转换异常,值:{},类型:{},置空处理", fieldName, strValue, fieldType, e);
|
|
|
+ return null;
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- layer.ResetReading();
|
|
|
- Feature feature;
|
|
|
- while ((feature = layer.GetNextFeature()) != null) {
|
|
|
- Map<String, Object> map = new LinkedHashMap<>();
|
|
|
- for (int i = 0; i < feature.GetFieldCount(); i++) {
|
|
|
- String fieldName = feature.GetFieldDefnRef(i).GetName();
|
|
|
- if (fieldNameAndTypeMap.containsKey(fieldName.toLowerCase())) {
|
|
|
- String fieldType = fieldNameAndTypeMap.get(fieldName.toLowerCase());
|
|
|
- if (StringUtils.containsIgnoreCase(fieldType, "character")) {
|
|
|
- map.put(fieldName, feature.GetFieldAsString(i));
|
|
|
- } else if (StringUtils.containsIgnoreCase(fieldType, "integer")) {
|
|
|
- map.put(fieldName, feature.GetFieldAsInteger(i));
|
|
|
- } else if (StringUtils.containsIgnoreCase(fieldType, "date")) {
|
|
|
- //todo 日期转化
|
|
|
- map.put(fieldName, feature.GetFieldAsString(i));
|
|
|
- } else if (StringUtils.containsIgnoreCase(fieldType, "numeric")) {
|
|
|
- map.put(fieldName, feature.GetFieldAsDouble(i));
|
|
|
- } else {
|
|
|
- map.put(fieldName, feature.GetFieldAsString(i));
|
|
|
- }
|
|
|
- } else {
|
|
|
- //这里兼容图层可能存在多出的字段不处理
|
|
|
- //map.put(fieldName, feature.GetFieldAsString(i));
|
|
|
- log.warn("当前图层-{},多出字段-{}",layer.GetName(),fieldName);
|
|
|
- }
|
|
|
+ private java.sql.Date parseDate(String dateStr, String fieldName) {
|
|
|
+ if (StringUtils.isBlank(dateStr)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String cleaned = dateStr.trim();
|
|
|
+ for (String pattern : DATE_PATTERNS) {
|
|
|
+ try {
|
|
|
+ java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(pattern);
|
|
|
+ sdf.setLenient(false);
|
|
|
+ java.util.Date parsed = sdf.parse(cleaned);
|
|
|
+ return new java.sql.Date(parsed.getTime());
|
|
|
+ } catch (Exception ignored) {
|
|
|
}
|
|
|
- if (haveGeom.equals(true)) {
|
|
|
- String ewkt = null;
|
|
|
- Geometry geom = feature.GetGeometryRef();
|
|
|
- if (geom != null) {
|
|
|
- String wkt = geom.ExportToWkt();
|
|
|
- ewkt = "SRID=" + sourceSRID + ";" + wkt;
|
|
|
- ewkt = this.transforGeomTo4326(ewkt);
|
|
|
- }
|
|
|
- map.put("geom", ewkt);
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ String normalized = cleaned.replace("/", "-");
|
|
|
+ if (normalized.contains(" ") || normalized.contains("T")) {
|
|
|
+ normalized = normalized.substring(0, Math.min(normalized.indexOf(' ') > 0 ? normalized.indexOf(' ') : Integer.MAX_VALUE, normalized.indexOf('T') > 0 ? normalized.indexOf('T') : Integer.MAX_VALUE));
|
|
|
}
|
|
|
- list.add(map);
|
|
|
+ if (normalized.length() > 10) {
|
|
|
+ normalized = normalized.substring(0, 10);
|
|
|
+ }
|
|
|
+ return java.sql.Date.valueOf(normalized);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("日期字段{}解析失败,值:{},置空处理", fieldName, dateStr);
|
|
|
+ return null;
|
|
|
}
|
|
|
- return list;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
/**
|
|
|
- *
|
|
|
- * 调整坐标系导入系统展示不准问题
|
|
|
+ * 获取当前图层的所有字段名称和值
|
|
|
*
|
|
|
* @param layer
|
|
|
- * @param fieldNameAndTypeMap
|
|
|
- * @param haveGeom
|
|
|
* @return
|
|
|
*/
|
|
|
- private List<Map<String, Object>> getValueList2(Layer layer, Map<String, String> fieldNameAndTypeMap, Boolean haveGeom) {
|
|
|
- // 目标坐标系固定为 4326(WGS84),写入数据库用
|
|
|
- final int TARGET_SRID = 4326;
|
|
|
+ private List<Map<String, Object>> getValueList(Layer layer, Map<String, String> fieldNameAndTypeMap, Boolean haveGeom) {
|
|
|
+ List<Map<String, Object>> list = new ArrayList<>();
|
|
|
Integer sourceSRID = 4326;
|
|
|
|
|
|
// ========== 1. 获取源数据坐标系 EPSG:4527 ==========
|
|
|
@@ -713,54 +714,55 @@ public class CadastreManageServiceImpl implements CadastreManageService {
|
|
|
log.warn("获取图层坐标系SRID失败,使用默认4326", e);
|
|
|
}
|
|
|
|
|
|
- // ========== 2. 创建坐标转换对象(4527 → 4326) ==========
|
|
|
- CoordinateTransformation transform = null;
|
|
|
- try {
|
|
|
- if (sourceSRID != TARGET_SRID) {
|
|
|
- SpatialReference srcSR = new SpatialReference();
|
|
|
- srcSR.ImportFromEPSG(sourceSRID);
|
|
|
-
|
|
|
- SpatialReference targetSR = new SpatialReference();
|
|
|
- targetSR.ImportFromEPSG(TARGET_SRID);
|
|
|
-
|
|
|
- // 创建转换工具
|
|
|
- transform = CoordinateTransformation.CreateCoordinateTransformation(srcSR, targetSR);
|
|
|
- }
|
|
|
- } catch (Exception e) {
|
|
|
- log.error("创建坐标转换失败(源{}→目标{})", sourceSRID, TARGET_SRID, e);
|
|
|
- }
|
|
|
-
|
|
|
- List<Map<String, Object>> list = new ArrayList<>();
|
|
|
layer.ResetReading();
|
|
|
Feature feature;
|
|
|
-
|
|
|
while ((feature = layer.GetNextFeature()) != null) {
|
|
|
Map<String, Object> map = new LinkedHashMap<>();
|
|
|
- // 读取属性字段(原逻辑不变)
|
|
|
for (int i = 0; i < feature.GetFieldCount(); i++) {
|
|
|
String fieldName = feature.GetFieldDefnRef(i).GetName();
|
|
|
if (fieldNameAndTypeMap.containsKey(fieldName.toLowerCase())) {
|
|
|
String fieldType = fieldNameAndTypeMap.get(fieldName.toLowerCase());
|
|
|
if (StringUtils.containsIgnoreCase(fieldType, "character")) {
|
|
|
map.put(fieldName, feature.GetFieldAsString(i));
|
|
|
- } else if (StringUtils.containsIgnoreCase(fieldType, "integer")) {
|
|
|
+ } else if (StringUtils.containsIgnoreCase(fieldType, "integer") || StringUtils.containsIgnoreCase(fieldType, "smallint") || StringUtils.containsIgnoreCase(fieldType, "int2") || StringUtils.containsIgnoreCase(fieldType, "int4")) {
|
|
|
map.put(fieldName, feature.GetFieldAsInteger(i));
|
|
|
+ } else if (StringUtils.containsIgnoreCase(fieldType, "bigint") || StringUtils.containsIgnoreCase(fieldType, "int8")) {
|
|
|
+ map.put(fieldName, feature.GetFieldAsInteger64(i));
|
|
|
} else if (StringUtils.containsIgnoreCase(fieldType, "date")) {
|
|
|
- //todo 日期转化
|
|
|
- map.put(fieldName, feature.GetFieldAsString(i));
|
|
|
- } else if (StringUtils.containsIgnoreCase(fieldType, "numeric")) {
|
|
|
- map.put(fieldName, feature.GetFieldAsDouble(i));
|
|
|
+ String dateStr = feature.GetFieldAsString(i);
|
|
|
+ if (StringUtils.isNotBlank(dateStr)) {
|
|
|
+ try {
|
|
|
+ String normalized = dateStr.trim().replace("/", "-");
|
|
|
+ if (normalized.length() > 10) {
|
|
|
+ normalized = normalized.substring(0, 10);
|
|
|
+ }
|
|
|
+ map.put(fieldName, java.sql.Date.valueOf(normalized));
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("日期字段{}解析失败,值:{},置空处理", fieldName, dateStr);
|
|
|
+ map.put(fieldName, null);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ map.put(fieldName, null);
|
|
|
+ }
|
|
|
+ } else if (StringUtils.containsIgnoreCase(fieldType, "numeric") || StringUtils.containsIgnoreCase(fieldType, "double") || StringUtils.containsIgnoreCase(fieldType, "real")) {
|
|
|
+ if (feature.IsFieldNull(i)) {
|
|
|
+ map.put(fieldName, null);
|
|
|
+ } else {
|
|
|
+ map.put(fieldName, feature.GetFieldAsDouble(i));
|
|
|
+ }
|
|
|
+ } else if (StringUtils.containsIgnoreCase(fieldType, "bytea") || StringUtils.containsIgnoreCase(fieldType, "blob") || StringUtils.containsIgnoreCase(fieldType, "binary")) {
|
|
|
+ map.put(fieldName, null);
|
|
|
} else {
|
|
|
map.put(fieldName, feature.GetFieldAsString(i));
|
|
|
}
|
|
|
} else {
|
|
|
- map.put(fieldName, feature.GetFieldAsString(i));
|
|
|
+ //这里兼容图层可能存在多出的字段不处理
|
|
|
+ //map.put(fieldName, feature.GetFieldAsString(i));
|
|
|
+ log.warn("当前图层-{},多出字段-{}",layer.GetName(),fieldName);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- // ========== 3. 处理几何体:转换坐标 + 输出4326 ==========
|
|
|
if (haveGeom.equals(true)) {
|
|
|
- String ewkt = "";
|
|
|
+ String ewkt = null;
|
|
|
Geometry geom = feature.GetGeometryRef();
|
|
|
if (geom != null) {
|
|
|
String wkt = geom.ExportToWkt();
|
|
|
@@ -771,12 +773,6 @@ public class CadastreManageServiceImpl implements CadastreManageService {
|
|
|
}
|
|
|
list.add(map);
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
- // 关闭释放资源
|
|
|
- if (transform != null) {
|
|
|
- transform.delete();
|
|
|
- }
|
|
|
return list;
|
|
|
}
|
|
|
|