本文整理汇总了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}
示例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