Tuesday 16 July 2019

qgis - Why does iface.activeLayer() not work in a PyQGis standalone app?


It is my second day on PyQGIS. Not sure why layer doesn't return anything. Can anyone help me on this?


Code:


#!/usr/bin/python
from qgis.core import *
from qgis.utils import *

from PyQt4.QtCore import *

def main():
qgis_prefix="/usr"
QgsApplication.setPrefixPath(qgis_prefix, True)
QgsApplication.initQgis()
app = QgsApplication([], False)

uri = "elevp.csv?delimiter=%s&xField=%s&yField=%s&elevField=%s" % (";","x","y","elev")
vlayer = QgsVectorLayer(uri, "test", "delimitedtext")

QgsMapLayerRegistry.instance().addMapLayer(vlayer)
#print(QgsMapLayerRegistry.instance().mapLayers())


#iface.mapCanvas().setSelectionColor( QColor("red") )
layer = iface.activeLayer()
print(layer)


# Run main wrapper codes

main()


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...