6.4. Lesson: 액션

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!

이 강의의 목표: 사용자 지정 액션을 추가하는 방법을 배우기.

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.

6.4.1. basic Follow Along: 이미지 용 항목 추가

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. Layer Properties 대화 창을 엽니다.

  2. Fields 탭을 클릭합니다.

  3. 편집 모드를 엽니다.

  4. 다음 새 열을 추가합니다.

  5. 다음 값을 입력합니다.

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

  7. Set Widget Type to Attachment:

  8. Click OK in the Layer Properties dialog.

  9. Identify 도구를 사용해 school_property 레이어에 있는 세 피처 가운데 하나를 클릭합니다.

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

  10. 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. OK 를 클릭합니다.

  13. 이런 방법으로 모든 이미지들을 올바른 피처와 연계하십시오.

  14. 편집 내용을 저장하고 편집 모드를 끕니다.

6.4.2. basic Follow Along: 액션 생성

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

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


    다음 단계는 사용자의 OS에 따라 달라지므로, 다음 가운데 적합한 지침을 선택하십시오.

    • 윈도우

      Type 드롭다운 메뉴를 클릭하고 Open 을 선택합니다.

    • 우분투 리눅스

      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. Type 드롭다운 메뉴를 클릭하고 Mac 을 선택합니다.

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

    Now you can continue writing the command.

    우리는 이미지를 열기를 원하고, QGIS는 이미지의 경로를 알고 있습니다. 이제 남은 것은 Action 에 이미지의 경로를 알려주는 일뿐입니다.

  3. 다음 목록에서 image 를 선택하십시오.

  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.

  4. 버튼을 클릭해서 도구를 활성화합니다.

  5. 이 도구를 이용해서, 학교 부지 세 곳 가운데 하나를 클릭합니다.

    The image for that property should open.

6.4.3. moderate Follow Along: 인터넷 검색

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. landuse 레이어의 속성 테이블을 엽니다.

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

  2. 속성 테이블을 닫습니다.

  3. Layer PropertiesActions 로 돌아갑니다.

  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.

    다음 단계는 사용자의 OS에 따라 달라지므로, 다음 가운데 적합한 지침을 선택하십시오.

    • 윈도우

      Type 아래에 있는 Open 을 선택하면, 윈도우에 인터넷 익스플로러 같은 기본 브라우저에서 인터넷 주소를 열도록 명령합니다.

    • 우분투 리눅스

      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

    앞에 나열한 명령어 가운데 어떤 것을 이용하든, 이제 어떤 인터넷 주소를 열 것인지 알려줘야 합니다. 즉 구글 사이트로 가서 자동으로 지역명을 검색해야 합니다.

    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. name 항목을 선택합니다.

  11. Click Insert button:


    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

이제 새로운 액션을 테스트해봅시다.

  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.


액션이 제대로 실행되지 않을 경우, 모든 명령어를 제대로 입력했는지 확인해보십시오. 이런 작업 시 오타가 흔하게 날 수 있습니다!

6.4.4. hard Follow Along: 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:


레이어 액션을 생성하려면,

  1. Layer Properties 대화 창을 열고 Actions 탭을 선택합니다.

  2. 새 액션을 위한 다음 속성들을 이용해서 해당 액션을 설정하십시오.

    • 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()

    이 명령어에 대해 설명하겠습니다.

    • 모든 파이썬 코드는 한 줄로, 쌍반점으로 구분되는 명령어들로 이루어집니다. (일반적인 파이썬 명령어들처럼 행갈이를 하지 않습니다.)

    • [%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.

    이 예제가 조금 부자연스럽다는 점을 알아야 합니다. 파이썬은 의미론적으로 특별한 들여쓰기를 사용합니다. 따라서 쌍반점으로 명령어를 구분하는 것은 파이썬을 사용하는 최적의 방식이 아닙니다. 그러므로 실제 상황에서는 파이썬 모듈에서 사용자 로직을 불러들여, 속성 항목을 파라미터로 쓰는 함수를 호출하는 일이 더 많을 것입니다.

    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.

6.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!

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