topology.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from qgis._core import QgsCoordinateTransformContext
  2. from qgis.core import (
  3. QgsVectorLayer,
  4. QgsDataSourceUri,
  5. QgsVectorFileWriter,
  6. QgsApplication
  7. )
  8. from PyQt5.QtSql import QSqlDatabase, QSqlQuery
  9. # 初始化 QGIS 应用程序(如果在外部运行脚本需要添加)
  10. qgs = QgsApplication([], False)
  11. qgs.initQgis()
  12. # 1. 将 SHP 文件导入到 Spatialite
  13. shp_file_path = "D:\\gisdata\\hainanShp\\building4326\\Building.shp" # 输入的 SHP 文件路径
  14. spatialite_db_path = "D:\\temp\\output.sqlite" # Spatialite 数据库路径
  15. table_name = "building_table" # 导入后的表名
  16. output_shp_path = "D:\\gisdata\\hainanShp\\building4326\\test\\sqlliteexport.shp" # 输出的 SHP 文件路径
  17. export_layer_name = table_name
  18. # 加载 SHP 文件
  19. shp_layer = QgsVectorLayer(shp_file_path, "shp_layer", "ogr")
  20. if not shp_layer.isValid():
  21. print("图层数据不可用。")
  22. exit()
  23. # 获取 SHP 文件的 CRS(坐标参考系)
  24. shp_crs = shp_layer.crs().authid()
  25. # 构造连接到 Spatialite 数据库的 URI
  26. uri = QgsDataSourceUri()
  27. uri.setDatabase(spatialite_db_path)
  28. # 将 SHP 文件写入 Spatialite 数据库
  29. options = QgsVectorFileWriter.SaveVectorOptions()
  30. options.driverName = "SQLite"
  31. options.layerName = table_name
  32. options.fileEncoding = "UTF-8"
  33. options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer
  34. # 将QgsVectorLayer导入到spatialite并指定表名
  35. result = QgsVectorFileWriter.writeAsVectorFormatV2(shp_layer, spatialite_db_path, QgsCoordinateTransformContext(), options)
  36. if result[0] != QgsVectorFileWriter.NoError:
  37. print("Failed to import SHP file to Spatialite.")
  38. exit()
  39. print(f"SHP file imported to Spatialite as table: {table_name}")
  40. # 2. 执行 SQL 语句
  41. db = QSqlDatabase.addDatabase("QSQLITE")
  42. db.setDatabaseName(spatialite_db_path)
  43. if not db.open():
  44. print("Failed to open Spatialite database.")
  45. exit()
  46. query = QSqlQuery(db)
  47. sql = f"""
  48. -- 在这里输入你的 SQL 语句
  49. SELECT * FROM {table_name};
  50. """
  51. if not query.exec(sql):
  52. print("SQL execution failed:", query.lastError().text())
  53. exit()
  54. print("SQL execution completed.")
  55. # 3. 将数据导出为本地 SHP 文件
  56. # 加载处理后的表
  57. processed_layer = QgsVectorLayer(f"{spatialite_db_path}|layername={export_layer_name}", "processed_layer", "ogr")
  58. if not processed_layer.isValid():
  59. print("拓扑检查结果图层加载失败。")
  60. exit()
  61. # 导出为 SHP 文件
  62. export_options = QgsVectorFileWriter.SaveVectorOptions()
  63. export_options.driverName = "ESRI Shapefile"
  64. export_options.fileEncoding = "UTF-8"
  65. result = QgsVectorFileWriter.writeAsVectorFormatV2(processed_layer, output_shp_path, QgsCoordinateTransformContext(), export_options)
  66. if result[0] != QgsVectorFileWriter.NoError:
  67. print("Failed to export processed layer to SHP file.")
  68. exit()
  69. print(f"Processed layer exported to SHP file: {output_shp_path}")
  70. # 释放资源
  71. db.close()
  72. qgs.exitQgis()