The Vector Properties Dialog:Actions Tab

提供: OSGeo.JP Wiki
2009年8月17日 (月) 19:59時点におけるKayama (トーク | 投稿記録)による版

(差分) ←前の版 | 最新版 (差分) | 次の版→ (差分)
移動: 案内検索

Actions Tab

QGIS provides the ability to perform an action based on the attributes of a feature. This can be used to perform any number of actions, for example, running a program with arguments built from the attributes of a feature or passing parameters to a web reporting tool. <P> Actions are useful when you frequently want to run an external application or view a web page based on one or more values in your vector layer. An example is performing a search based on an attribute value. This concept is used in the following discussion. <P> Defining Actions<A NAME="410"></A> <P> Attribute actions are defined from the vector Layer Properties dialog. To define an action, open the vector Layer Properties dialog and click on the Actions tab. Provide a descriptive name for the action. The action itself must contain the name of the application that will be executed when the action is invoked. You can add one or more attribute field values as arguments to the application. When the action is invoked any set of characters that start with a % followed by the name of a field will be replaced by the value of that field. The special characters %% <A NAME="414"></A>will be replaced by the value of the field that was selected from the identify results or attribute table (see Using Actions below). Double quote marks can be used to group text into a single argument to the program, script or command. Double quotes will be ignored if preceded by a backslash. <P> If you have field names that are substrings of other field names (e.g., col1 and col10) you should indicate so, by surrounding the field name (and the % character) with square brackets (e.g., [%col10]). This will prevent the %col10 field name being mistaken for the %col1 field name with a 0 on the end. The brackets will be removed by QGIS when it substitutes in the value of the field. If you want the substituted field to be surrounded by square brackets, use a second set like this: %col10. <P> The Identify Results dialog box includes a (Derived) item that contains information relevant to the layer type. The values in this item can be accessed in a similar way to the other fields by using preceeding the derived field name by (Derived).. For example, a point layer has an X and Y field and the value of these can be used in the action with %(Derived).X and %(Derived).Y. The derived attributes are only available from the Identify Results dialog box, not the Attribute Table dialog box. <P> Two example actions are shown below:<A NAME="431"></A> <P>

<P> In the first example, the web browser konqueror is invoked and passed a URL to open. The URL performs a Google search on the value of the nam field from our vector layer. Note that the application or script called by the action must be in the path or you must provided the full path. To be sure, we could rewrite the first example as: /opt/kde3/bin/konqueror http://www.google.com/search?q=%nam. This will ensure that the konqueror application will be executed when the action is invoked.

<P> The second example uses the %% notation which does not rely on a particular field for its value. When the action is invoked, the %% will be replaced by the value of the selected field in the identify results or attribute table.

<P> Using Actions<A NAME="439"></A><A NAME="label_usingactions"></A>Actions can be invoked from either the Identify Results dialog or an

Attribute Table dialog. 

(Recall that these dialogs can be opened by clicking mActionOpenTableIdentify Features or mActionOpenTableOpen Table.) To invoke an action, right click on the record and choose the action from the popup menu. Actions are listed in the popup menu by the name you assigned when defining the actions. Click on the action you wish to invoke.

<P> If you are invoking an action that uses the %% notation, right-click on the field value in the Identify Results dialog or the Attribute Table dialog that you wish to pass to the application or script.

<P> Here is another example that pulls data out of a vector layer and inserts them into a file using bash and the echo command (so it will only work or perhaps ). The layer in question has fields for a species name taxon_name, latitude lat and longitude long. I would like to be able to make a spatial selection of a localities and export these field values to a text file for the selected record (shown in yellow in the QGIS map area). Here is the action to achieve this:

<P>

  bash -c "echo \"%taxon_name %lat %long\" >> /tmp/species_localities.txt"

<P> After selecting a few localities and running the action on each one, opening the output file will show something like this:

<P>

  Acacia mearnsii -34.0800000000 150.0800000000
  Acacia mearnsii -34.9000000000 150.1200000000
  Acacia mearnsii -35.2200000000 149.9300000000
  Acacia mearnsii -32.2700000000 150.4100000000

<P> As an exercise we create an action that does a Google search on the lakes layer. First we need to determine the URL needed to perform a search on a keyword. This is easily done by just going to Google and doing a simple search, then grabbing the URL from the address bar in your browser. From this little effort we see that the format is: <A NAME="tex2html72"

 HREF="http://google.com/search?q=qgis">http://google.com/search?q=qgis</A>,

where qgis is the search term. Armed with this information, we can proceed:

<P>

  1. Make sure the lakes layer is loaded.
  2. Open the Layer Properties dialog by double-clicking on the layer in the legend or right-click and choose Properties from the popup menu.
  3. Click on the Actions tab.
  4. Enter a name for the action, for example Google Search.
  5. For the action, we need to provide the name of the external program to run. In this case, we can use Firefox. If the program is not in your path, you need to provide the full path.
  6. Following the name of the external application, add the URL used for doing a Google search, up to but not included the search term: <A NAME="tex2html73" HREF="http://google.com/search?q=">http://google.com/search?q=</A>
  7. The text in the Action field should now look like this:
    firefox <A NAME="tex2html74" HREF="http://google.com/search?q=">http://google.com/search?q=</A>
  8. Click on the drop-down box containing the field names for the lakes layer. It's located just to the left of the Insert Field button.
  9. From the drop-down box, select NAMES and click Insert Field.
  10. Your action text now looks like this:
    firefox <A NAME="tex2html75" HREF="http://google.com/search?q=%NAMES">http://google.com/search?q=%NAMES</A>
  11. Fo finalize the action click the Insert action button.

<P> This completes the action and it is ready to use. The final text of the action should look like this:

<P>

<P> We can now use the action. Close the Layer Properties dialog and zoom in to an area of interest. Make sure the lakes layer is active and identify a lake. In the result box you'll now see that our action is visible:

<P>

<A NAME="fig:identify_action"></A><A NAME="484"></A>
Figure: Select feature and choose action

[clip=true, width=8cm]action_identifyaction

<P> When we click on the action, it brings up Firefox and navigates to the URL http://www.google.com/search?q=Tustumena . It is also possible to add further attribute fields to the action. Therefore you can add a ``+ to the end of the action text, select another field and click on Insert Field. In this example there is just no other field available that would make sense to search for.

<P> You can define multiple actions for a layer and each will show up in the Identify Results dialog. You can also invoke actions from the attribute table by selecting a row and right-clicking, then choosing the action from the popup menu.

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