当前位置: 首页>>代码示例>>Python>>正文


Python QgsGeometry.polygonize方法代码示例

本文整理汇总了Python中qgis.core.QgsGeometry.polygonize方法的典型用法代码示例。如果您正苦于以下问题:Python QgsGeometry.polygonize方法的具体用法?Python QgsGeometry.polygonize怎么用?Python QgsGeometry.polygonize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在qgis.core.QgsGeometry的用法示例。


在下文中一共展示了QgsGeometry.polygonize方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: processAlgorithm

# 需要导入模块: from qgis.core import QgsGeometry [as 别名]
# 或者: from qgis.core.QgsGeometry import polygonize [as 别名]
    def processAlgorithm(self, parameters, context, feedback):
        source = self.parameterAsSource(parameters, self.INPUT, context)
        if source is None:
            raise QgsProcessingException(self.invalidSourceError(parameters, self.INPUT))

        if self.parameterAsBool(parameters, self.KEEP_FIELDS, context):
            fields = source.fields()
        else:
            fields = QgsFields()

        (sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
                                               fields, QgsWkbTypes.Polygon, source.sourceCrs())
        if sink is None:
            raise QgsProcessingException(self.invalidSinkError(parameters, self.OUTPUT))

        allLinesList = []
        features = source.getFeatures(QgsFeatureRequest().setSubsetOfAttributes([]))
        feedback.pushInfo(QCoreApplication.translate('Polygonize', 'Processing lines…'))
        total = (40.0 / source.featureCount()) if source.featureCount() else 1
        for current, inFeat in enumerate(features):
            if feedback.isCanceled():
                break

            if inFeat.geometry():
                allLinesList.append(inFeat.geometry())
            feedback.setProgress(int(current * total))

        feedback.setProgress(40)

        feedback.pushInfo(QCoreApplication.translate('Polygonize', 'Noding lines…'))
        allLines = QgsGeometry.unaryUnion(allLinesList)
        if feedback.isCanceled():
            return {}

        feedback.setProgress(45)
        feedback.pushInfo(QCoreApplication.translate('Polygonize', 'Polygonizing…'))
        polygons = QgsGeometry.polygonize([allLines])
        if polygons.isEmpty():
            feedback.reportError(self.tr('No polygons were created!'))
        feedback.setProgress(50)

        if not polygons.isEmpty():
            feedback.pushInfo(QCoreApplication.translate('Polygonize', 'Saving polygons…'))
            total = 50.0 / polygons.constGet().numGeometries()
            for i in range(polygons.constGet().numGeometries()):
                if feedback.isCanceled():
                    break

                outFeat = QgsFeature()
                geom = QgsGeometry(polygons.constGet().geometryN(i).clone())
                outFeat.setGeometry(geom)
                sink.addFeature(outFeat, QgsFeatureSink.FastInsert)
                feedback.setProgress(50 + int(current * total))

        return {self.OUTPUT: dest_id}
开发者ID:phborba,项目名称:QGIS,代码行数:57,代码来源:Polygonize.py

示例2: processAlgorithm

# 需要导入模块: from qgis.core import QgsGeometry [as 别名]
# 或者: from qgis.core.QgsGeometry import polygonize [as 别名]
    def processAlgorithm(self, parameters, context, feedback):
        vlayer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
        output = self.getOutputFromName(self.OUTPUT)
        if self.getParameterValue(self.FIELDS):
            fields = vlayer.fields()
        else:
            fields = QgsFields()
        if self.getParameterValue(self.GEOMETRY):
            fieldsCount = fields.count()
            fields.append(QgsField('area', QVariant.Double, 'double', 16, 2))
            fields.append(QgsField('perimeter', QVariant.Double,
                                   'double', 16, 2))
        allLinesList = []
        features = QgsProcessingUtils.getFeatures(vlayer, context, QgsFeatureRequest().setSubsetOfAttributes([]))
        feedback.pushInfo(self.tr('Processing lines...'))
        total = 40.0 / QgsProcessingUtils.featureCount(vlayer, context)
        for current, inFeat in enumerate(features):
            if inFeat.geometry():
                allLinesList.append(inFeat.geometry())
            feedback.setProgress(int(current * total))

        feedback.setProgress(40)

        feedback.pushInfo(self.tr('Noding lines...'))
        allLines = QgsGeometry.unaryUnion(allLinesList)

        feedback.setProgress(45)
        feedback.pushInfo(self.tr('Polygonizing...'))
        polygons = QgsGeometry.polygonize([allLines])
        if polygons.isEmpty():
            raise GeoAlgorithmExecutionException(self.tr('No polygons were created!'))
        feedback.setProgress(50)

        feedback.pushInfo('Saving polygons...')
        writer = output.getVectorWriter(fields, QgsWkbTypes.Polygon, vlayer.crs(), context)
        total = 50.0 / polygons.geometry().numGeometries()
        for i in range(polygons.geometry().numGeometries()):
            outFeat = QgsFeature()
            geom = QgsGeometry(polygons.geometry().geometryN(i).clone())
            outFeat.setGeometry(geom)
            if self.getParameterValue(self.GEOMETRY):
                outFeat.setAttributes([None] * fieldsCount + [geom.geometry().area(),
                                                              geom.geometry().perimeter()])
            writer.addFeature(outFeat, QgsFeatureSink.FastInsert)
            feedback.setProgress(50 + int(current * total))
        del writer
开发者ID:ndavid,项目名称:QGIS,代码行数:48,代码来源:Polygonize.py


注:本文中的qgis.core.QgsGeometry.polygonize方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。