working_with_vector_patch.diff

Giovanni Manghi, 2012-08-12 02:45 PM

Download (2.75 KB)

View differences:

working_with_vector_patched.rst 2012-08-12 22:43:30.116823653 +0100
1070 1070

  
1071 1071
You can think of all kinds of uses for actions. For example, if you have a point layer containing locations of images or photos along with a file name, you could create an action to launch a viewer to display the image. You could also use actions to launch web-based reports for an attribute field or combination of fields, specifying them in the same way we did in our Google search example.
1072 1072

  
1073
We can also make more complex examples, for instance on how to use **Python** actions.
1074

  
1075
Usually when we create an action to open a file with an external application we can use absolute paths, or eventually relative paths, in the second case the path is relative to the location of the external program executable file. But what about we need to use relative paths, relative to the selected layer (a file based one, like a shapefile or spatialite)? The following code will do the trick:
1076

  
1077
::
1078

  
1079

  
1080
	command = "firefox";
1081
	imagerelpath = "images_test/test_image.jpg"; 
1082
	layer = qgis.utils.iface.activeLayer(); 
1083
	import os.path; 
1084
	layerpath = layer.source() if layer.providerType() == 'ogr' else (qgis.core.QgsDataSourceURI(layer.source()).database() if layer.providerType() == 'spatialite' else None); 
1085
	path = os.path.dirname(str(layerpath)); 
1086
	image = os.path.join(path,imagerelpath); 
1087
	import subprocess; 
1088
	subprocess.Popen( [command, image ] );
1089

  
1090
we have to just remember that the action is one of type *Python* and to change the *command* and *imagerelpath* variables to fit our needs. 
1091

  
1092
But what about if the relative path need to be relative to the (saved) project file? The code of the Python action would be:
1093

  
1094
::
1095

  
1096

  
1097
	command="firefox"; 
1098
	imagerelpath="images/test_image.jpg"; 
1099
	projectpath=qgis.core.QgsProject.instance().fileName(); 
1100
	import os.path; path=os.path.dirname(str(projectpath)) if projectpath != '' else None; 
1101
	image=os.path.join(path, imagerelpath); 
1102
	import subprocess;
1103
	subprocess.Popen( [command, image ] );
1104

  
1105
Another Python actions example if the one that allows us to add new layers to the project. For instance the following examples will add to the project respectively a vector and a raster. The name of files to be added to the project and the name to be given to the layer are data driven (*filename* and *layname* are column names of the table of attributes of the vector where the action was created):
1106

  
1107
::
1108

  
1109

  
1110
	qgis.utils.iface.addVectorLayer('/yourpath/[% "filename" %].shp','[% "layername" %]', 'ogr')
1111
	
1112

  
1113
To add a raster (a tif image in this example) it becomes:
1114

  
1115
::
1116

  
1117

  
1118
	qgis.utils.iface.addRasterLayer('/yourpath/[% "filename" %].tif','[% "layername" %]')
1119

  
1073 1120
.. _`sec_joins`:
1074 1121

  
1075 1122
Joins Tab