Thursday, 2 June 2016

Getting path of project, or layer file in PyQGIS?


I am generating new raster files from vector layers on a project. I would like to save them in the same directory as the project or layer files.



How do I find this path using PyQGIS?



Answer



For python console:


QgsProject.instance().readPath("./") # also try "../"

Or with


dir(QgsProject.instance())

you find something like


fileName()




For python plugin (not tested!):


from PyQt4.QtGui import QMessageBox
from qgis.core import QgsProject
path_absolute = QgsProject.instance().readPath("./")
QMessageBox.information(None, "Title", "AP: " + unicode(path_absolute))

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...