Feature request #4920
Add python actions examples to documentation
Status: | Closed | ||
---|---|---|---|
Priority: | Normal | ||
Assignee: | - | ||
Category: | Documentation and Help | ||
Pull Request or Patch supplied: | Yes | Resolution: | fixed |
Easy fix?: | No | Copied to github as #: | 14726 |
Description
I have two good examples of python actions that I believe are good to be included in 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')
History
#1 Updated by Giovanni Manghi about 12 years ago
- Pull Request or Patch supplied changed from No to Yes
- File working_with_vector_patch.diff added
#2 Updated by Giovanni Manghi about 12 years ago
- Tracker changed from Bug report to Feature request
#3 Updated by Giovanni Manghi about 12 years ago
- Resolution set to fixed
- Status changed from Open to Closed