|
@@ -25,7 +25,8 @@ import numpy as np
|
|
|
from PyQt5.QtGui import QIcon
|
|
|
|
|
|
from qgis.PyQt.QtCore import QUrl, QCoreApplication
|
|
|
-from qgis._core import QgsCoordinateTransformContext
|
|
|
+from qgis._core import QgsCoordinateTransformContext, QgsCoordinateReferenceSystem, QgsCoordinateTransform, \
|
|
|
+ QgsVectorLayer, QgsWkbTypes, QgsFeature, QgsGeometry
|
|
|
|
|
|
from qgis.core import (QgsApplication,
|
|
|
QgsProject,
|
|
@@ -299,6 +300,33 @@ class GdalAlgorithm(QgsProcessingAlgorithm):
|
|
|
|
|
|
# 将VectorLayer导入到SpatiaLite数据库
|
|
|
def importLayerToSpatiaLite(self, layer, table_name):
|
|
|
+ # TODO wanger 强制进行坐标系转换 统一入库数据坐标系 增强分析结果准确性
|
|
|
+ target_crs = QgsCoordinateReferenceSystem("EPSG:4490")
|
|
|
+ # 创建坐标转换对象
|
|
|
+ transform_context = QgsProject.instance().transformContext()
|
|
|
+ transform = QgsCoordinateTransform(layer.crs(), target_crs, transform_context)
|
|
|
+ # 创建内存图层
|
|
|
+ geometry_type = layer.wkbType() # 保留原始几何类型
|
|
|
+ memory_layer = QgsVectorLayer(
|
|
|
+ f"{QgsWkbTypes.displayString(geometry_type)}", # 指定几何类型
|
|
|
+ "Transformed Layer",
|
|
|
+ "memory"
|
|
|
+ )
|
|
|
+ # 设置内存图层的 CRS 和属性字段
|
|
|
+ memory_layer.setCrs(target_crs)
|
|
|
+ memory_layer.dataProvider().addAttributes(layer.fields()) # 复制字段
|
|
|
+ memory_layer.updateFields()
|
|
|
+ # 遍历原图层的所有要素,逐个转换几何并添加到内存图层
|
|
|
+ features = []
|
|
|
+ for feature in layer.getFeatures():
|
|
|
+ new_feature = QgsFeature(feature) # 复制属性
|
|
|
+ geom = feature.geometry()
|
|
|
+ if geom: # 检查几何是否有效
|
|
|
+ geom.transform(transform) # 转换几何坐标
|
|
|
+ new_feature.setGeometry(geom)
|
|
|
+ features.append(new_feature)
|
|
|
+ # 将转换后的要素添加到内存图层
|
|
|
+ memory_layer.dataProvider().addFeatures(features)
|
|
|
# TODO 构造连接到Spatialite数据库的URI
|
|
|
uri = QgsDataSourceUri()
|
|
|
uri.setDatabase(self.spatialite_db_path)
|
|
@@ -307,10 +335,11 @@ class GdalAlgorithm(QgsProcessingAlgorithm):
|
|
|
options.driverName = "SQLite"
|
|
|
options.layerName = table_name
|
|
|
options.fileEncoding = "UTF-8"
|
|
|
+ # options.crs = target_crs
|
|
|
options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer
|
|
|
# TODO 将QgsVectorLayer导入到spatialite并指定表名
|
|
|
- result = QgsVectorFileWriter.writeAsVectorFormatV2(layer, self.spatialite_db_path,
|
|
|
- QgsCoordinateTransformContext(), options)
|
|
|
+ result = QgsVectorFileWriter.writeAsVectorFormatV2(memory_layer, self.spatialite_db_path,
|
|
|
+ QgsProject.instance().transformContext(), options)
|
|
|
return result
|
|
|
|
|
|
# 将VectorLayer导出到文件
|
|
@@ -335,6 +364,7 @@ class GdalAlgorithm(QgsProcessingAlgorithm):
|
|
|
QgsCoordinateTransformContext(), export_options)
|
|
|
if result[0] != QgsVectorFileWriter.NoError:
|
|
|
print(f"Failed to export processed layer to SHP file.")
|
|
|
+ print(result)
|
|
|
|
|
|
# 拓扑检查成功,没有错误项
|
|
|
def topologySuccessWithNone(self):
|
|
@@ -343,7 +373,7 @@ class GdalAlgorithm(QgsProcessingAlgorithm):
|
|
|
"结论": "符合"
|
|
|
}
|
|
|
|
|
|
- #拓扑检查成功
|
|
|
+ # 拓扑检查成功
|
|
|
def topologySuccess(self, count, reportpath):
|
|
|
return {
|
|
|
"状态": "拓扑检查成功",
|