FishNet.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # -*- coding: utf-8 -*-
  2. __author__ = 'wanger'
  3. __description__ = '处理渔网数据 判断网格是否为自由 增加标签'
  4. __date__ = '2024-11-25'
  5. __copyright__ = '(C) 2024 by siwei'
  6. __revision__ = '1.0'
  7. import os
  8. from qgis.PyQt.QtCore import QCoreApplication
  9. from qgis.core import (QgsProcessing,
  10. QgsVectorLayer,
  11. QgsFeatureSink,
  12. QgsProcessingException,
  13. QgsProcessingAlgorithm,
  14. QgsProcessingParameterFile,
  15. QgsProcessingParameterFeatureSource,
  16. QgsProcessingParameterFeatureSink)
  17. from qgis import processing
  18. class FishNetProcessingAlgorithm(QgsProcessingAlgorithm):
  19. INPUT = 'INPUT'
  20. def tr(self, string):
  21. return QCoreApplication.translate('Processing', string)
  22. def createInstance(self):
  23. return FishNetProcessingAlgorithm()
  24. def name(self):
  25. return 'makeFishNet'
  26. def displayName(self):
  27. return self.tr('处理网格数据')
  28. def group(self):
  29. return self.tr('栅格数据')
  30. def groupId(self):
  31. return 'metadata'
  32. def shortHelpString(self):
  33. return self.tr("将输入的网格数据进行检查,将接边自由的要素做标记。")
  34. def initAlgorithm(self, config=None):
  35. self.addParameter(QgsProcessingParameterFile(
  36. self.INPUT,
  37. '网格数据',
  38. extension='shp' # 设置文件过滤器,只允许选择shp文件
  39. ))
  40. # 定义检查一个网格单元格上下左右是否有相邻的网格单元
  41. def check_neighbors(self, feature, layer):
  42. # 获取当前单元格的几何
  43. geom = feature.geometry()
  44. bbox = geom.boundingBox() # 获取网格单元的包围盒(bounding box)
  45. # 提取当前网格的中心点位置
  46. center_x, center_y = geom.centroid().asPoint()
  47. # 定义上下左右偏移量(单位为网格的大小,假设网格是正方形或矩形)
  48. tolerance = 1e-5 # 设定一个微小的容忍误差,用于比较几何形状
  49. neighbors = {
  50. 'north': None,
  51. 'south': None,
  52. 'west': None,
  53. 'east': None
  54. }
  55. for other_feature in layer.getFeatures():
  56. if other_feature.id() == feature.id():
  57. continue # 跳过自己
  58. other_geom = other_feature.geometry()
  59. # 检查上、下、左、右的邻接
  60. if abs(center_y - other_geom.centroid().asPoint().y()) < tolerance and other_geom.centroid().asPoint().x() < center_x:
  61. neighbors['west'] = other_feature
  62. elif abs(
  63. center_y - other_geom.centroid().asPoint().y()) < tolerance and other_geom.centroid().asPoint().x() > center_x:
  64. neighbors['east'] = other_feature
  65. elif abs(
  66. center_x - other_geom.centroid().asPoint().x()) < tolerance and other_geom.centroid().asPoint().y() < center_y:
  67. neighbors['south'] = other_feature
  68. elif abs(
  69. center_x - other_geom.centroid().asPoint().x()) < tolerance and other_geom.centroid().asPoint().y() > center_y:
  70. neighbors['north'] = other_feature
  71. return neighbors
  72. # 执行
  73. def processAlgorithm(self, parameters, context, feedback):
  74. shp_file_path = self.parameterAsString(parameters, self.INPUT, context)
  75. # 检查文件路径是否有效
  76. if not os.path.exists(shp_file_path):
  77. feedback.reportError(f"指定的文件路径 {shp_file_path} 不存在!")
  78. return {}
  79. # 加载Shapefile为QgsVectorLayer
  80. layer = QgsVectorLayer(shp_file_path, "Vector Layer", 'ogr')
  81. print(layer)
  82. layer.startEditing()
  83. # 获取所有要素(假设网格单元是矩形或正方形)
  84. features = list(layer.getFeatures())
  85. # 遍历所有网格单元,检查相邻的网格
  86. for feature in features:
  87. neighbors = self.check_neighbors(feature, layer)
  88. for direction, neighbor in neighbors.items():
  89. if neighbor:
  90. print(f"")
  91. else:
  92. # 更新字段值
  93. feature.setAttribute(direction, "自由")
  94. # 更新要素
  95. layer.updateFeature(feature)
  96. # 保存编辑
  97. layer.commitChanges()
  98. return {
  99. "状态": "处理成功"
  100. }