[Qgis-community-team] python actions examples (for the manual)

Giovanni Manghi giovanni.manghi at gmail.com
Wed Jan 26 15:37:23 EST 2011


Hi all,

recently Giuseppe Sucameli, a qgis dev, show a few exemples of
(advanced) qgis actions that I believe are very nice for the manual.


The first two are about opening files with external programs: usually
you have to use absolute paths, or relative paths... relative to the
position of the external program executable.

What about if you need to use relative paths relative to the selected
layer (file based)?

command = "eog"; imagerelpath = "immagini/tav_01.jpg"; layer =
qgis.utils.iface.activeLayer(); import os.path; layerpath =
layer.source() if layer.providerType() == 'ogr' else
(qgis.core.QgsDataSourceURI(layer.source()).database() if
layer.providerType() == 'spatialite' else None); path =
os.path.dirname(str(layerpath)); image = os.path.join(path,
imagerelpath); import subprocess; subprocess.Popen( [command, image ] );



and what about if the relative path need to be relative to the (saved)
project file?

command = "eog"; imagerelpath = "immagini/tav_01.jpg"; projectpath =
qgis.core.QgsProject.instance().fileName(); import os.path; path =
os.path.dirname(str(projectpath)) if projectpath != '' else None; image
= os.path.join(path, imagerelpath); import subprocess;
subprocess.Popen( [command, image ] );


He also suggested me a way to add new layers to a project trough an
action:

qgis.utils.iface.addVectorLayer('/home/user/%column_name.shp','%
NAME', 'ogr')

qgis.utils.iface.addRasterLayer('/home/user/%column_name.tif','%NAME')


cheers

-- Giovanni --



More information about the Qgis-community-team mailing list