LP01.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. # -*- coding: utf-8 -*-
  2. """
  3. ***************************************************************************
  4. LP01.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.StringUtils import getUUID
  55. from processing.tools.FileUtils import getParentFolderPath
  56. from processing.tools.topology.read import (getTopoCheckSQL, getTopoCheckDescription, getTopoCheckNote)
  57. from processing.tools.SpatiaLite.SpatiaLite import SpatiaLiteUtils
  58. import sqlite3
  59. pluginPath = os.path.dirname(os.path.abspath(__file__))
  60. gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES")
  61. gdal.SetConfigOption("SHAPE_ENCODING", "GBK")
  62. class LP01(GdalAlgorithm):
  63. INPUTVECTOR = "INPUTVECTOR"
  64. INPUTTARGETVECTOR = "INPUTTARGETVECTOR"
  65. OUTPUTVECTOR = 'OUTPUTVECTOR'
  66. TOPOLOGYTYPE = "LP01"
  67. uid = getUUID()
  68. in_table = "topology_in_table"
  69. in_table_t = "topology_in_table_t"
  70. out_table = "topology_out_table"
  71. TOPOLOGYPARAMS = {
  72. "intable_s": f"{in_table}_{uid}",
  73. "intable_t": f"{in_table_t}_{uid}",
  74. "outtable": f"{out_table}_{uid}"
  75. }
  76. def __init__(self):
  77. super().__init__()
  78. def initAlgorithm(self, config=None):
  79. self.addParameter(QgsProcessingParameterFeatureSource(self.INPUTVECTOR,
  80. self.tr('待检查数据'),
  81. types=[QgsProcessing.TypeVectorLine]))
  82. self.addParameter(QgsProcessingParameterFeatureSource(self.INPUTTARGETVECTOR,
  83. self.tr('目标表'),
  84. types=[QgsProcessing.TypeVectorPoint]))
  85. self.addParameter(QgsProcessingParameterVectorDestination(self.OUTPUTVECTOR,
  86. self.tr('输出位置(矢量数据和报告)')))
  87. def name(self):
  88. return self.TOPOLOGYTYPE
  89. def icon(self):
  90. return QIcon(os.path.join(pluginPath, 'topology.png'))
  91. def displayName(self):
  92. return self.tr(getTopoCheckNote(self.TOPOLOGYTYPE))
  93. def shortDescription(self):
  94. return self.tr(getTopoCheckDescription(self.TOPOLOGYTYPE))
  95. def tags(self):
  96. t = self.tr('import,into,postgis,database,vector').split(',')
  97. t.extend(super().tags())
  98. return t
  99. def group(self):
  100. return self.tr('拓扑检查')
  101. def groupId(self):
  102. return 'topology'
  103. def topologycheck(self, parameters, context, feedback, executing=True):
  104. print("拓扑检查开始啦")
  105. # TODO 参数设置
  106. spatialite_db_path = self.spatialite_db_path # Spatialite 数据库路径
  107. table_name = f"{self.in_table}_{self.uid}" # 导入后的表名
  108. table_t_name = f"{self.in_table_t}_{self.uid}" # 导入后的目标表名
  109. outtable_name = f"{self.out_table}_{self.uid}" # 分析执行后的表名
  110. output_vector_path = self.parameterAsOutputLayer(parameters, self.OUTPUTVECTOR, context) # 输出的 SHP 文件路径
  111. export_layer_name = outtable_name
  112. # TODO 将 vectorlayer导入到Spatialite
  113. input_layer = self.parameterAsVectorLayer(parameters, self.INPUTVECTOR, context)
  114. if not input_layer.isValid():
  115. return {
  116. "状态": "拓扑检查失败",
  117. "错误信息": f"图层数据不可用。"
  118. }
  119. result = self.importLayerToSpatiaLite(layer=input_layer, table_name=table_name)
  120. if result[0] != QgsVectorFileWriter.NoError:
  121. return {
  122. "状态": "拓扑检查失败",
  123. "错误信息": f"图层数据导出到SpatiaLite数据库失败。"
  124. }
  125. input_target_layer = self.parameterAsVectorLayer(parameters, self.INPUTTARGETVECTOR, context)
  126. if not input_target_layer.isValid():
  127. return {
  128. "状态": "拓扑检查失败",
  129. "错误信息": f"图层数据不可用。"
  130. }
  131. result = self.importLayerToSpatiaLite(layer=input_target_layer, table_name=table_t_name)
  132. if result[0] != QgsVectorFileWriter.NoError:
  133. return {
  134. "状态": "拓扑检查失败",
  135. "错误信息": f"图层数据导出到SpatiaLite数据库失败。"
  136. }
  137. print(f"file imported to Spatialite as table: {table_name}")
  138. dbutils = SpatiaLiteUtils(spatialite_db_path)
  139. # TODO SQL语句
  140. sql = getTopoCheckSQL(self.TOPOLOGYTYPE, self.TOPOLOGYPARAMS)
  141. try:
  142. dbutils.executescript(f"{sql}", self.TOPOLOGYPARAMS)
  143. except sqlite3.Error as e:
  144. return {
  145. "状态": "拓扑检查失败",
  146. "错误信息": f"SpatiaLite数据库执行SQL语句失败: {e}"
  147. }
  148. # TODO 获取结果集条目
  149. feature_count = dbutils.queryCount(outtable_name)
  150. if feature_count == 0:
  151. dbutils.resetDb()
  152. dbutils.closeDb()
  153. return self.topologySuccessWithNone()
  154. # TODO 将数据导出为本地矢量文件
  155. processed_layer = QgsVectorLayer(f"{spatialite_db_path}|layername={export_layer_name}", "processed_layer",
  156. "ogr")
  157. path = getParentFolderPath(output_vector_path)
  158. csv_path = f"{path}\\report.csv"
  159. if not processed_layer.isValid():
  160. dbutils.closeDb()
  161. return {
  162. "状态": "拓扑检查失败",
  163. "错误信息": "拓扑检查结果图层加载失败。"
  164. }
  165. else:
  166. self.vectorLayerExport(layer=processed_layer, type="CSV", path=csv_path)
  167. self.vectorLayerExportToVector(layer=processed_layer, path=output_vector_path)
  168. os.startfile(path)
  169. dbutils.resetDb()
  170. dbutils.closeDb()
  171. return self.topologySuccess(count=feature_count, reportpath=csv_path)
  172. # 判断数据是否为字符串
  173. def is_string(self, var):
  174. return isinstance(var, str)
  175. def getConsoleCommands(self, parameters, context, feedback, executing=True):
  176. return []
  177. def contains_keys(self, obj, keys):
  178. if isinstance(obj, dict):
  179. return all(key in obj.keys() for key in keys)
  180. elif hasattr(type(obj), '__dict__'):
  181. return all(key in obj.__dict__ for key in keys)
  182. else:
  183. raise ValueError("Invalid object type")
  184. def commandName(self):
  185. return "ogr2ogr"