QGIS-2.8-PyQGISDeveloperCookbook-en(19)
时间:2025-03-11
时间:2025-03-11
为开发人员准备的文档,涵盖QGIS的多种功能实现和代码。
CHAPTER5
UsingVectorLayers
Thissectionsummarizesvariousactionsthatcanbedonewithvectorlayers.
5.1Retrievinginformationsaboutattributes
Youcanretrieveinformationsaboutthe eldsassociatedwithavectorlayerbycallingpendingFields()on
aQgsVectorLayerinstance:
5.2Selectingfeatures
InQGISdesktop,featurescanbeselectedindifferentways,theusercanclickonafeature,drawarectangleon
themapcanvasoruseanexpression lter.Selectedfaturesarenormallyhiglightedinadifferentcolor(defaultis
yellow)todrawuser’sattentionontheselection.Sometimescanbeusefultoprogrammaticallyselectfeaturesor
tochangethedefaultcolor.
TochangetheselectioncoloryoucanusesetSelectionColor()methodof
QgsMapCanvasasshownin
thefollowingexample:
Toaddaddfeaturestotheselectedfeatureslistforagivenlayer,youcancallsetSelectedFeatures()
passingtoitthelistoffeaturesIDs:
Tocleartheselection,justpassanemptylist:
5.3IteratingoverVectorLayer
Iteratingoverthefeaturesinavectorlayerisoneofthemostcommontasks.Belowisanexampleofthesimple
basiccodetoperformthistaskandshowingsomeinformationabouteachfeature.thelayervariableisassumed