topologycheck.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. # -*- coding: utf-8 -*-
  2. """
  3. ***************************************************************************
  4. TopologyCheck.py
  5. ---------------------
  6. Date : November 2012
  7. Copyright : (C) 2012 by Victor Olaya
  8. Email : volayaf at gmail dot com
  9. ***************************************************************************
  10. * *
  11. * This program is free software; you can redistribute it and/or modify *
  12. * it under the terms of the GNU General Public License as published by *
  13. * the Free Software Foundation; either version 2 of the License, or *
  14. * (at your option) any later version. *
  15. * *
  16. ***************************************************************************
  17. """
  18. __author__ = 'wanger'
  19. __date__ = 'November 2024'
  20. __copyright__ = '(C) 2024, wanger'
  21. import os
  22. from PyQt5.QtSql import QSqlDatabase, QSqlQuery
  23. from osgeo import ogr, gdal
  24. from PyQt5.QtGui import QIcon
  25. from PyQt5.QtWidgets import QApplication
  26. from future.moves import sys
  27. from qgis.PyQt import QtWidgets
  28. from qgis._core import QgsProcessingParameterVectorDestination, QgsVectorLayer, QgsVectorFileWriter, \
  29. QgsCoordinateTransformContext
  30. from qgis.core import (QgsProcessing,
  31. QgsProcessingParameterFeatureSource,
  32. QgsProcessingParameterString,
  33. QgsProcessingParameterFile,
  34. QgsProcessingParameterDateTime,
  35. QgsProcessingParameterEnum,
  36. QgsProcessingParameterCrs,
  37. QgsProcessingParameterField,
  38. QgsProcessingParameterExtent,
  39. QgsProcessingParameterBoolean,
  40. QgsProcessingParameterProviderConnection,
  41. QgsProcessingParameterDatabaseSchema,
  42. QgsProcessingParameterDatabaseTable,
  43. QgsProviderRegistry,
  44. QgsProcessingException,
  45. QgsProcessingParameterDefinition,
  46. QgsProviderConnectionException,
  47. QgsDataSourceUri)
  48. from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
  49. from processing.algs.gdal.GdalUtils import GdalUtils
  50. from processing.tools.PrintUtils import printStr
  51. from processing.tools.StringUtils import (getConnectionStr, getNow)
  52. from processing.tools.GeoServer.Geoserver import Geoserver
  53. from processing.tools.PostgreSQL.PostgreSQL import PostgreSQL
  54. from processing.tools.system import isWindows
  55. pluginPath = os.path.normpath(os.path.join(
  56. os.path.split(os.path.dirname(__file__))[0], os.pardir))
  57. gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES")
  58. gdal.SetConfigOption("SHAPE_ENCODING", "GBK")
  59. class TopologyCheck(GdalAlgorithm):
  60. INPUTVECTOR = "INPUTVECTOR"
  61. TOPOLOGYTYPE = "TOPOLOGYTYPE"
  62. TOPOLOGYTYPES = ["必须不相交", "不能重叠", "不能相交", "不能有悬挂点", "不能有伪结点", "不能自重叠", "不能自相交",
  63. "不能相交或内部接触", "必须为单一部件", "不能有空隙"]
  64. OUTPUTVECTOR = 'OUTPUTVECTOR'
  65. def __init__(self):
  66. super().__init__()
  67. def initAlgorithm(self, config=None):
  68. self.addParameter(QgsProcessingParameterFeatureSource(self.INPUTVECTOR,
  69. self.tr('待检查数据'),
  70. types=[QgsProcessing.TypeVector]))
  71. EnumOption = QgsProcessingParameterEnum(name=self.TOPOLOGYTYPE,
  72. description=self.tr('拓扑检查类型'),
  73. options=self.TOPOLOGYTYPES)
  74. EnumOption.setAllowMultiple(True)
  75. self.addParameter(EnumOption)
  76. self.addParameter(QgsProcessingParameterVectorDestination(self.OUTPUTVECTOR,
  77. self.tr('输出位置')))
  78. def name(self):
  79. return 'topologycheck'
  80. def icon(self):
  81. return QIcon(os.path.join(pluginPath, 'images', 'dbms', 'topology.png'))
  82. def displayName(self):
  83. return self.tr('拓扑检查')
  84. def shortDescription(self):
  85. return self.tr('拓扑检查')
  86. def tags(self):
  87. t = self.tr('import,into,postgis,database,vector').split(',')
  88. t.extend(super().tags())
  89. return t
  90. def group(self):
  91. return self.tr('拓扑检查')
  92. def groupId(self):
  93. return 'topology'
  94. def topologycheck(self, parameters, context, feedback, executing=True):
  95. print("拓扑检查开始啦")
  96. types = print(parameters[self.TOPOLOGYTYPE])
  97. print(f"拓扑检查类型:{types}")
  98. # TODO 参数设置
  99. spatialite_db_path = "D:\\temp\\output.sqlite" # Spatialite 数据库路径
  100. table_name = "topology_table" # 导入后的表名
  101. output_vector_path = self.parameterAsOutputLayer(parameters, self.OUTPUTVECTOR, context) # 输出的 SHP 文件路径
  102. export_layer_name = table_name
  103. # TODO 将 vectorlayer导入到Spatialite
  104. input_layer = self.parameterAsVectorLayer(parameters, self.INPUTVECTOR, context)
  105. if not input_layer.isValid():
  106. print("图层数据不可用。")
  107. crs = input_layer.crs().authid()
  108. # TODO 构造连接到Spatialite数据库的URI
  109. uri = QgsDataSourceUri()
  110. uri.setDatabase(spatialite_db_path)
  111. # TODO 将vectorlayer写入Spatialite 数据库
  112. options = QgsVectorFileWriter.SaveVectorOptions()
  113. options.driverName = "SQLite"
  114. options.layerName = table_name
  115. options.fileEncoding = "UTF-8"
  116. options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer
  117. # TODO 将QgsVectorLayer导入到spatialite并指定表名
  118. result = QgsVectorFileWriter.writeAsVectorFormatV2(input_layer, spatialite_db_path,
  119. QgsCoordinateTransformContext(), options)
  120. if result[0] != QgsVectorFileWriter.NoError:
  121. print("Failed to import SHP file to Spatialite.")
  122. print(f"SHP file imported to Spatialite as table: {table_name}")
  123. # TODO 执行 SQL 语句
  124. db = QSqlDatabase.addDatabase("QSQLITE")
  125. db.setDatabaseName(spatialite_db_path)
  126. if not db.open():
  127. print("Failed to open Spatialite database.")
  128. query = QSqlQuery(db)
  129. # TODO SQL语句
  130. sql = f"""
  131. SELECT * FROM {table_name};
  132. """
  133. if not query.exec(sql):
  134. print("SQL execution failed:", query.lastError().text())
  135. print("SQL execution completed.")
  136. # TODO 将数据导出为本地矢量文件
  137. processed_layer = QgsVectorLayer(f"{spatialite_db_path}|layername={export_layer_name}", "processed_layer",
  138. "ogr")
  139. if not processed_layer.isValid():
  140. print("拓扑检查结果图层加载失败。")
  141. # TODO 导出矢量文件参数配置
  142. export_options = QgsVectorFileWriter.SaveVectorOptions()
  143. export_options.driverName = "ESRI Shapefile"
  144. export_options.fileEncoding = "UTF-8"
  145. result = QgsVectorFileWriter.writeAsVectorFormatV2(processed_layer, output_vector_path,
  146. QgsCoordinateTransformContext(), export_options)
  147. if result[0] != QgsVectorFileWriter.NoError:
  148. print("Failed to export processed layer to SHP file.")
  149. print(f"Processed layer exported to SHP file: {output_vector_path}")
  150. return {
  151. "状态": "拓扑检查成功",
  152. "错误项": "条记录",
  153. "矢量数据输出位置": output_vector_path,
  154. "报告输出位置": ""
  155. }
  156. # 判断数据是否为字符串
  157. def is_string(self, var):
  158. return isinstance(var, str)
  159. def getConsoleCommands(self, parameters, context, feedback, executing=True):
  160. inputvector = self.parameterAsVectorLayer(parameters, self.INPUTVECTOR, context)
  161. print(f"拓扑检查输入矢量图层:")
  162. print(inputvector)
  163. outFile = self.parameterAsOutputLayer(parameters, self.OUTPUTVECTOR, context)
  164. print(f"拓扑检查输出文件:{outFile}")
  165. return []
  166. def contains_keys(self, obj, keys):
  167. if isinstance(obj, dict):
  168. return all(key in obj.keys() for key in keys)
  169. elif hasattr(type(obj), '__dict__'):
  170. return all(key in obj.__dict__ for key in keys)
  171. else:
  172. raise ValueError("Invalid object type")
  173. def commandName(self):
  174. return "ogr2ogr"