123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- # -*- coding: utf-8 -*-
- __author__ = 'wanger'
- __description__ = '处理渔网数据 判断网格是否为自由 增加标签'
- __date__ = '2024-11-25'
- __copyright__ = '(C) 2024 by siwei'
- __revision__ = '1.0'
- import os
- from qgis.PyQt.QtCore import QCoreApplication
- from qgis.core import (QgsProcessing,
- QgsVectorLayer,
- QgsFeatureSink,
- QgsProcessingException,
- QgsProcessingAlgorithm,
- QgsProcessingParameterFile,
- QgsProcessingParameterFeatureSource,
- QgsProcessingParameterFeatureSink)
- from qgis import processing
- class FishNetProcessingAlgorithm(QgsProcessingAlgorithm):
- INPUT = 'INPUT'
- def tr(self, string):
- return QCoreApplication.translate('Processing', string)
- def createInstance(self):
- return FishNetProcessingAlgorithm()
- def name(self):
- return 'makeFishNet'
- def displayName(self):
- return self.tr('处理网格数据')
- def group(self):
- return self.tr('栅格数据')
- def groupId(self):
- return 'metadata'
- def shortHelpString(self):
- return self.tr("将输入的网格数据进行检查,将接边自由的要素做标记。")
- def initAlgorithm(self, config=None):
- self.addParameter(QgsProcessingParameterFile(
- self.INPUT,
- '网格数据',
- extension='shp' # 设置文件过滤器,只允许选择shp文件
- ))
- # 定义检查一个网格单元格上下左右是否有相邻的网格单元
- def check_neighbors(self, feature, layer):
- # 获取当前单元格的几何
- geom = feature.geometry()
- bbox = geom.boundingBox() # 获取网格单元的包围盒(bounding box)
- # 提取当前网格的中心点位置
- center_x, center_y = geom.centroid().asPoint()
- # 定义上下左右偏移量(单位为网格的大小,假设网格是正方形或矩形)
- tolerance = 1e-5 # 设定一个微小的容忍误差,用于比较几何形状
- neighbors = {
- 'north': None,
- 'south': None,
- 'west': None,
- 'east': None
- }
- for other_feature in layer.getFeatures():
- if other_feature.id() == feature.id():
- continue # 跳过自己
- other_geom = other_feature.geometry()
- # 检查上、下、左、右的邻接
- if abs(center_y - other_geom.centroid().asPoint().y()) < tolerance and other_geom.centroid().asPoint().x() < center_x:
- neighbors['west'] = other_feature
- elif abs(
- center_y - other_geom.centroid().asPoint().y()) < tolerance and other_geom.centroid().asPoint().x() > center_x:
- neighbors['east'] = other_feature
- elif abs(
- center_x - other_geom.centroid().asPoint().x()) < tolerance and other_geom.centroid().asPoint().y() < center_y:
- neighbors['south'] = other_feature
- elif abs(
- center_x - other_geom.centroid().asPoint().x()) < tolerance and other_geom.centroid().asPoint().y() > center_y:
- neighbors['north'] = other_feature
- return neighbors
- # 执行
- def processAlgorithm(self, parameters, context, feedback):
- shp_file_path = self.parameterAsString(parameters, self.INPUT, context)
- # 检查文件路径是否有效
- if not os.path.exists(shp_file_path):
- feedback.reportError(f"指定的文件路径 {shp_file_path} 不存在!")
- return {}
- # 加载Shapefile为QgsVectorLayer
- layer = QgsVectorLayer(shp_file_path, "Vector Layer", 'ogr')
- print(layer)
- layer.startEditing()
- # 获取所有要素(假设网格单元是矩形或正方形)
- features = list(layer.getFeatures())
- # 遍历所有网格单元,检查相邻的网格
- for feature in features:
- neighbors = self.check_neighbors(feature, layer)
- for direction, neighbor in neighbors.items():
- if neighbor:
- print(f"")
- else:
- # 更新字段值
- feature.setAttribute(direction, "自由")
- # 更新要素
- layer.updateFeature(feature)
- # 保存编辑
- layer.commitChanges()
- return {
- "状态": "处理成功"
- }
|