TestTriangulation.py

Antoine Lafranchis, 2019-03-17 07:08 PM

Download (1.88 KB)

 
1
# -*- coding: utf-8 -*-
2

    
3
from qgis.core import (QgsProcessing, QgsProcessingAlgorithm, QgsProcessingParameterFeatureSource, QgsGeometry, QgsMultiPoint)
4

    
5
class TestTriangulation(QgsProcessingAlgorithm):
6
    POINTS = 'POINTS'
7
    # OUTPUT = 'OUTPUT'
8

    
9
    def createInstance(self):
10
        return TestTriangulation()
11

    
12
    def name(self):
13
        return 'testtriangulation'
14

    
15
    def displayName(self):
16
        return 'Test Triangulation'
17
    
18
    def flags(self):
19
        return QgsProcessingAlgorithm.FlagNoThreading
20

    
21
    def group(self):
22
        return 'Test'
23

    
24
    def groupId(self):
25
        return 'test'
26

    
27
    def shortHelpString(self):
28
        return ''
29

    
30
    def initAlgorithm(self, config = None):
31
        self.addParameter(QgsProcessingParameterFeatureSource(self.POINTS, 'Points', [QgsProcessing.TypeVectorPoint]))
32
        # self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, 'Triangles', QgsProcessing.TypeVectorPolygon))
33

    
34
    def processAlgorithm(self, parameters, context, feedback):
35
        pointLayer = self.parameterAsSource(parameters, self.POINTS, context)
36
        multiPoint = QgsMultiPoint()
37
        for pointFeature in pointLayer.getFeatures():
38
            multiPoint.addGeometry(pointFeature.geometry().vertexAt(0))
39
        multiPointGeometry = QgsGeometry(multiPoint)
40
        triangulation = multiPointGeometry.delaunayTriangulation(1e-3)
41
        multiTriangle = triangulation.get()
42
        # (sink, dest) = self.parameterAsSink(parameters, self.OUTPUT, context, QgsFields(), QgsWkbTypes.PolygonZ, context.project().crs())
43
        for i in range(multiTriangle.numGeometries()):
44
            triangle = multiTriangle.geometryN(i)
45
            triangleGeometry = QgsGeometry(triangle) # removing this line prevents the crash
46
            # feature = QgsFeature()
47
            # feature.setGeometry(triangleGeometry)
48
            # sink.addFeature(feature)
49
        return {} # {self.OUTPUT: dest}