5.4. Lesson: Azioni

Now that you have seen a default action in the previous lesson, it is time to define your own actions.

An action is something that happens when you click on a feature. It can add a lot of extra functionality to your map, allowing you to retrieve additional information about an object, for example. Assigning actions can add a whole new dimension to your map!

Obiettivo di questa lezione: Imparare come aggiungere azioni personalizzate.

In this lesson you will use the school_property layer you created previously. The sample data include photos of each of the three properties you digitized. What we are going to do is to associate each property with its image. Then we will create an action that will open the image for a property when clicking on the property.

5.4.1. basic Follow Along: Aggiungi un campo per le immagini

The school_property layer has no way to associate an image with a property yet. First we will create a field for this purpose.

  1. Apri la finestra Proprietà vettore.

  2. Spostati sulla scheda Campi.

  3. Attiva modifica:

    ../../../_images/toggle_editing_mode.png
  4. Aggiungi una nuova colonna

    ../../../_images/add_new_column.png
  5. Inserisci i seguenti valori:

    ../../../_images/column_settings.png
  6. After the field has been created, move to the Attributes Form tab and select the image field.

  7. Set Widget Type to Attachment:

    ../../../_images/select_file_name.png
  8. Click OK in the Layer Properties dialog.

  9. Usa il Informazione elementi cliccando su una delle tre geometrie del layer school_property.

    Since you are still in edit mode, the dialog should be active and look like this:

    ../../../_images/school_property_no_image.png
  10. Clicca il bottone di selezione ( il vicino al campo image).

  11. Select the path for your image. The images are in exercise_data/school_property_photos/ and are named the same as the features they should be associated with.

  12. Click OK.

  13. In questa maniera associa tutte le immagini con gli oggetti corretti.

  14. Salva le modifiche ed esci da modalità modifica.

5.4.2. basic Follow Along: Creare un’azione

  1. Open the Actions tab for the school_property layer, and click on the signPlus Add a new action button.

    ../../../_images/layer_actions.png
  2. In the Add New Action dialog, enter the words Show Image into the Description field:

    ../../../_images/show_image_action.png

    Cosa fare dopo varia a seconda del sistema operativo:

    • Windows

      Clicca Type sulla tendina e scegli Apri.

    • Ubuntu Linux

      Under Action, write eog for the Gnome Image Viewer, or write display to use ImageMagick. Remember to put a space after the command!

    • MacOS

      1. Clicca Type sulla tendina e scegli Mac.

      2. Under Action, write open. Remember to put a space after the command!

    Now you can continue writing the command.

    Vuoi aprire l’immagine, e QGIS sa dove è. Tutto ciò che devi fare è dire a Azioni dove è l’immagine.

  3. Seleziona dalla lista image.

    ../../../_images/select_image.png
  4. Click the Insert field button. QGIS will add the phrase [% "image" %] in the Action Text field.

  5. Click the OK button to close the Add New Action dialog

  6. Click OK to close the Layer Properties dialog

Now it is time to test the new action:

  1. Click on the school_property layer in the Layers panel so that it is highlighted.

  2. Find the actionRun Run feature action button (in the Attributes Toolbar).

  3. Click on the down arrow to the right of this button. There is only one action defined for this layer so far, which is the one you just created.

    ../../../_images/run_feature_action_select.png
  4. Clicca il pulsante per attivare lo strumento.

  5. Utilizzando questo strumento, clicca su uno dei tre oggetti della scuola.

    The image for that property should open.

5.4.3. moderate Follow Along: Cercare in internet

Let’s say we are looking at the map and want to know more about the area that a farm is in. Suppose you know nothing of the area in question and want to find general information about it. Your first impulse, considering that you’re using a computer right now, would probably be to Google the name of the area. So let’s tell QGIS to do that automatically for us!

  1. Aprire la tabella attributi per il layer landuse.

    We will be using the name field for each of our landuse areas to search Google.

  2. Chiudi la tabella attributi

  3. Torna a Azioni nella finestra Proprietà vettore.

  4. Click on the Create Default Actions button to add a number of pre-defined actions.

  5. Remove all the actions but the Open URL action with the short name Search Web using the signMinus Remove the selected action button below.

  6. Double-click on the remaining action to edit it

  7. Change the Description to Google Search, and remove the content of the Short Name field.

  8. Make sure that Canvas is among the checked Action scopes.

    Cosa fare dopo varia a seconda del sistema operativo:

    • Windows

      Sotto: guilabel:Tipo, scegli Apri. Questo dirà a Windows di aprire un indirizzo Internet nel browser predefinito, ad esempio Internet Explorer.

    • Ubuntu Linux

      Under Action, write xdg-open. This will tell Ubuntu to open an Internet address in your default browser, such as Chrome or Firefox.

    • MacOS

      Under Action, write open. This will tell MacOS to open an Internet address in your default browser, such as Safari.

    Now you can continue writing the command

    Qualunque sia il comando che usasti in precedenza, è necessario dire l’indirizzo Internet. Vuoi usare Google per la ricerca di una frase automaticamente.

    Usually when you use Google, you enter your search phrase into the Google Search bar. But in this case, you want your computer to do this for you. The way you tell Google to search for something (if you don’t want to use its search bar directly) is by giving your Internet browser the address https://www.google.com/search?q=SEARCH_PHRASE, where SEARCH_PHRASE` is what you want to search for. Since we don’t know what phrase to search for yet, we will just enter the first part (without the search phrase).

  9. In the Action field, write https://www.google.com/search?q=. Remember to add a space after your initial command before writing this in!

    Now you want QGIS to tell the browser to tell Google to search for the value of name for any feature that you could click on.

  10. Selaziona il campo noame.

  11. Click Insert button:

    ../../../_images/google_search_action.png

    What this means is that QGIS is going to open the browser and send it to the address https://www.google.com/search?q=[% "name" %]. [% "name" %] tells QGIS to use the contents of the name field as the phrase to search for.

    So if, for example, the landuse area you click on is named Marloth Nature Reserve, QGIS is going to send the browser to https://www.google.com/search?q=Marloth%20Nature%20Reserve, which will cause your browser to visit Google, which will in turn search for «Marloth Nature Reserve».

  12. If you have not done so already, set everything up as explained above.

  13. Click the OK button to close the Add New Action dialog

  14. Click OK to close the Layer Properties dialog

Ora testa la nuova azione:

  1. With the landuse layer active in the Layers panel, click on the down arrow to the right of the actionRun Run feature action button, and select the only action (Google Search) defined for this layer.

  2. Click on any landuse area you can see on the map. Your browser will now open, and will start a Google search for the place that is recorded as that area’s name value.

Nota

Se l’azione non funziona, controlla che tutto sia stato inserito correttamente; errori di battitura sono comuni con questo tipo di lavoro!

5.4.4. hard Follow Along: Apri un sito direttamente in QGIS

Above, you’ve seen how to open a webpage in an external browser. There are some shortcomings with this approach in that it adds an unknowable dependency – will the end-user have the software required to execute the action on their system? As you’ve seen, they don’t necessarily even have the same kind of base command for the same kind of action, if you don’t know which OS they will be using. With some OS versions, the above commands to open the browser might not work at all. This could be an insurmountable problem.

However, QGIS sits on top of the incredibly powerful and versatile Qt library. Also, QGIS actions can be arbitrary, tokenized (i.e. using variable information based on the contents of a field attribute) Python commands!

Now you will see how to use a python action to show a web page. It is the same general idea as opening a site in an external browser, but it requires no browser on the user’s system since it uses the Qt QWebView class (which is a webkit based html widget) to display the content in a pop-up window.

Let us use Wikipedia this time. So the URL you request will look like this:

https://wikipedia.org/wiki/SEARCH_PHRASE

Per creare l’azione sul layer

  1. Apri la finestra Proprietà vettore e vai sulla scheda Azioni.

  2. Configura una nuova azione usando le seguenti proprietà:

    • Type: Python

    • Description: Wikipedia

    • Action Text (all on one line):

      from qgis.PyQt.QtCore import QUrl; from qgis.PyQt.QtWebKitWidgets import QWebView; myWV = QWebView(None); myWV.load(QUrl('https://wikipedia.org/wiki/[%name%]')); myWV.show()
      
    ../../../_images/python_action_example.png

    Ci sono un paio di cose da dire:

    • Tutto il codice Python è in una sola riga con punti e virgola che separano i comandi (al posto di a capo)

    • [%name%] will be replaced by the actual attribute value when the action is invoked (as before).

    • The code simply creates a new QWebView instance, sets its URL, and then calls show() on it to make it visible as a window on the user’s desktop.

    Nota che questo è un esempio un po “forzato”. Python lavora con indentazione semanticamente significante, così separare le cose con un punto e virgola non è il modo migliore per scriverlo. Così, nel mondo reale, devi essere più propenso a importare la logica da un modulo Python e quindi chiamare una funzione con un attributo campo come parametro.

    You could also use this approach to display an image without requiring that the users have a particular image viewer on their system.

  3. Try to use the methods described above to load a Wikipedia page using the Wikipedia action you just created.

5.4.5. In Conclusion

Actions allow you to give your map extra functionality, useful to the end-user who views the same map in QGIS. Due to the fact that you can use shell commands for any operating system, as well as Python, the sky is the limit in terms of the functions you could incorporate!

5.4.6. What’s Next?

Now that you’ve done all kinds of vector data creation, you will learn how to analyze the data to solve problems. That is the topic of the next module.