LayerIteratorTest.py

Full python script code - Dan Jones, 2013-05-23 03:07 AM

Download (2.02 KB)

 
1
from PyQt4.QtCore import *
2
from PyQt4.QtGui import *
3
from qgis.core import *
4
from qgis.gui import *
5
from qgis.utils import *
6
from qgis.analysis import *
7
from qgis.networkanalysis import *
8
import sys
9

    
10
def run_script(iface):
11
    print sys.version
12
    
13
    layer = iface.activeLayer()
14
    lid = layer.getLayerID()
15
    print "Layer: %s" % lid
16
    lct = layer.featureCount()
17
    print "Features: %d" % lct
18
    prov = layer.dataProvider()
19
    feat = QgsFeature()
20
    attr = prov.attributeIndexes()
21
    amap = prov.fieldNameMap()
22
    imap = {v:k for k, v in amap.items()}
23
    
24
    for (k,attr) in imap.iteritems():
25
        print "Attribute %d: %s" % (k,imap[k])
26
    prov.select(attr)
27
    maxfeat = 10
28
    curfeat = 0
29
    rad = 500
30
    field = "COVERAGE"
31
    fix = prov.fieldNameIndex(field)
32
    while prov.nextFeature(feat):
33
        curfeat += 1
34
        if curfeat == maxfeat:
35
            break
36
        geom = feat.geometry()
37
        cent = geom.centroid().asPoint()
38
        x = cent.x()
39
        y = cent.y()
40
        rect = QgsRectangle(x-rad,y-rad,x+rad,y+rad)
41
        #freq = QgsFeatureRequest().setFilterRect(rect)
42
        #wind = layer.getFeatures(freq)
43
        layer = iface.activeLayer()
44
        wind = iface.activeLayer().getFeatures(QgsFeatureRequest().setFilterRect(rect))
45
        total = 0
46
        while wind.nextFeature(wfeat):
47
            attrs = wfeat.attributeMap()
48
            total += attrs[fix]
49
        print "Total in window: %d" % total
50
##        print "%d, %d" % (x,y)
51
        
52
        
53
##    print type(layer)
54
##        print type(geom)
55
##        print str(geom)
56
##        print "Feature ID %d@: "% feat.id() 
57
##        if geom.type() == QGis.Polygon:
58
##            x = geom.asPolygon()
59
##            numPts = 0
60
##            for ring in x:
61
##                numPts += len(ring)
62
##                print "Polygon: %d rings with %d points" % (len(x), numPts)
63
##        attrs = feat.attributeMap()
64
##        for (k,attr) in attrs.iteritems():
65
##            print "%d: %s" % (k, attr.toString())