Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

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))
share|improve this answer
1  
Thanks, it worked nicely from within the plugin. –  fccoelho Oct 1 '12 at 10:45
    
You're welcome! But can you please +1 me and mark it as accepted answer.. ;-) –  qräbnö Oct 1 '12 at 18:27
add comment

If you want to access a vector file path, on a active layer, this seems to work:

myfilepath= os.path.dirname( unicode( qgis.utils.iface.activeLayer().dataProvider().dataSourceUri() ) ) ;
fic = myfilepath + "[% "FILE" %]"

then to open a picture or text located in same directory as mylayer:

from PyQt4 import QtWebKit, QtCore ; vue=QtWebKit.QWebView() vue.setUrl( QtCore.QUrl( fic ) ) ; vue.show()
share|improve this answer
    
Useful answer but what about the file name? You use "[% "FILE" %]" but I don't understand what does it means... –  G M Aug 19 '13 at 20:59
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.