QGIS-2.8-PyQGISDeveloperCookbook-en(11)
时间:2025-03-11
时间:2025-03-11
为开发人员准备的文档,涵盖QGIS的多种功能实现和代码。
CHAPTER3
LoadingLayers
Let’sopensomelayerswithdata.QGISrecognizesvectorandrasterlayers.Additionally,customlayertypesare
available,butwearenotgoingtodiscussthemhere.
3.1VectorLayers
Toloadavectorlayer,specifylayer’sdatasourceidenti er,nameforthelayerandprovider’sname:
Thedatasourceidenti erisastringanditisspeci yer’snameisusedinthe
layerlistwidget.Itisimportanttocheckwhetherthelayerhasbeenloadedsuccessfully.Ifitwasnot,aninvalid
layerinstanceisreturned.
ThequickestwaytoopenanddisplayavectorlayerinQGISistheaddVectorLayerfunctionofthe
QgisInterface:
,"ogr")Thiscreatesanewlayerandaddsittothemaplayerregistry(makingitappearinthelayerlist)inonestep.The
functionreturnsthelayerinstanceorNoneifthelayercouldn’tbeloaded.
Thefollowinglistshowshowtoaccessvariousdatasourcesusingvectordataproviders:
OGRlibrary(shape lesandmanyother leformats)—data
sourceisthepathtothe le
)
PostGISdatabase—datasourceisastringwithallinformationneededtocreateaconnectiontoPostgreSQL
database.QgsDataSourceURIclasscangeneratethisstringforyou.NotethatQGIShastobecompiled
withPostgressupport,otherwisethisproviderisn’tavailable.
CSVorotherdelimitedtext les—toopena lewithasemicolonasadelimiter,with eld“x”forx-
coordinateand eld“y”withy-coordinateyouwouldusesomethinglikethis