delaunay.py

Patch for Delaunay triangulation - mesajs -, 2010-06-15 03:23 AM

Download (1.84 KB)

 
1
  def delaunay_triangulation( self ):
2
    import voronoi
3
    
4
    ptDict = {} #  New line
5
    ptNdx = -1  # New line
6
    
7
    vprovider = self.vlayer.dataProvider()
8
    allAttrs = vprovider.attributeIndexes()
9
    vprovider.select( allAttrs )
10
    fields = {
11
    0 : QgsField( "POINTA", QVariant.Double ),
12
    1 : QgsField( "POINTB", QVariant.Double ),
13
    2 : QgsField( "POINTC", QVariant.Double ) }
14
    writer = QgsVectorFileWriter( self.myName, self.myEncoding,
15
    fields, QGis.WKBPolygon, vprovider.crs() )
16
    inFeat = QgsFeature()
17
    points = []
18
    while vprovider.nextFeature( inFeat ):
19
      inGeom = QgsGeometry( inFeat.geometry() )
20
      point = inGeom.asPoint()
21
      points.append( point )
22
      
23
      ptNdx +=1  # new line
24
      ptDict[ptNdx] = inFeat.id() # New line
25
      
26
    vprovider.rewind()
27
    vprovider.select( allAttrs )
28
    triangles = voronoi.computeDelaunayTriangulation( points )
29
    feat = QgsFeature()
30
    nFeat = len( triangles )
31
    nElement = 0
32
    self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
33
    self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
34
    for triangle in triangles:
35
      indicies = list( triangle )
36
      indicies.append( indicies[ 0 ] )
37
      polygon = []
38
      step = 0
39
      for index in indicies:
40
        
41
        vprovider.featureAtId( ptDict[index], inFeat, True,  allAttrs ) # Changed line
42
        
43
        geom = QgsGeometry( inFeat.geometry() )
44
        point = QgsPoint( geom.asPoint() )
45
        polygon.append( point )
46
        if step <= 3: feat.addAttribute( step, QVariant( index ) )
47
        step += 1
48
      geometry = QgsGeometry().fromPolygon( [ polygon ] )
49
      feat.setGeometry( geometry )      
50
      writer.addFeature( feat )
51
      nElement += 1
52
      self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ),  nElement )
53
    del writer
54
    return True