중요

Translation is a community effort you can join. This page is currently translated at 85.71%.

12. 읽기 및 저장하기 설정

힌트

여러분이 PyQGIS 콘솔을 사용하지 않는 경우 이 페이지에 있는 코드 조각들을 다음과 같이 가져와야 합니다:

1from qgis.core import (
2  QgsProject,
3  QgsSettings,
4  QgsVectorLayer
5)

플러그인에 몇몇 변수를 저장하는 것이 유용한 경우가 많습니다. 여러분이 다음에 해당 플러그인을 실행할 때 변수들을 다시 입력하거나 선택하지 않아도 되기 때문입니다.

Qt 및 QGIS API 덕분에 이런 변수들을 저장하고 가져올 수 있습니다. 각 변수에 대해 해당 변수에 접근하는 데 사용할 키를 선택해야 합니다 – 예를 들면 여러분이 가장 선호하는 색상에 대해 “favourite_color” 또는 비슷한 의미를 가진 다른 어떤 문자열도 사용할 수 있습니다. 키를 명명할 때 어떤 기준을 따를 것을 권장합니다.

설정을 다음 몇 가지 서로 다른 유형으로 구분할 수 있습니다:

  • 전체 수준 설정(global settings) – 이 설정은 특정 머신의 사용자에 종속됩니다. QGIS 자체도 수많은, 예를 들면 주 윈도우의 크기 또는 기본 스냅 작업 허용 오차와 같은 전체 수준 설정을 저장합니다. 예를 들어 setValue()value() 메소드를 통해 QgsSettings 클래스를 사용해서 설정을 처리합니다.

    다음은 이런 메소드들을 어떻게 사용하는지 보여주는 예시입니다.

     1def store():
     2  s = QgsSettings()
     3  s.setValue("myplugin/mytext", "hello world")
     4  s.setValue("myplugin/myint",  10)
     5  s.setValue("myplugin/myreal", 3.14)
     6
     7def read():
     8  s = QgsSettings()
     9  mytext = s.value("myplugin/mytext", "default text")
    10  myint  = s.value("myplugin/myint", 123)
    11  myreal = s.value("myplugin/myreal", 2.71)
    12  nonexistent = s.value("myplugin/nonexistent", None)
    13  print(mytext)
    14  print(myint)
    15  print(myreal)
    16  print(nonexistent)
    

    value() 메소드의 두 번째 파라미터는 선택적으로, 전송된 설정 이름에 대해 이전에 설정한 값이 없을 경우 반환하는 기본값을 지정합니다.

    For a method to pre-configure the default values of the global settings through the qgis_global_settings.ini file, see 조직 내에서 QGIS 활용하기 for further details.

  • 프로젝트 수준 설정 – 프로젝트에 따라 달라지기 때문에 프로젝트 파일과 연동됩니다. 맵 캔버스 배경색 또는 대상 좌표계 등이 프로젝트 수준 설정의 예시입니다. 어떤 프로젝트에서는 하얀 배경 및 WGS84가 어울리는 반면 다른 프로젝트에서는 노란 배경 및 UTM 투영 좌표계가 어울리는 경우가 있는 법이죠.

    다음은 프로젝트 수준 설정 사용의 예시입니다.

     1proj = QgsProject.instance()
     2
     3# store values
     4proj.writeEntry("myplugin", "mytext", "hello world")
     5proj.writeEntry("myplugin", "myint", 10)
     6proj.writeEntryDouble("myplugin", "mydouble", 0.01)
     7proj.writeEntryBool("myplugin", "mybool", True)
     8
     9# read values (returns a tuple with the value, and a status boolean
    10# which communicates whether the value retrieved could be converted to
    11# its type, in these cases a string, an integer, a double and a boolean
    12# respectively)
    13
    14mytext, type_conversion_ok = proj.readEntry("myplugin",
    15                                            "mytext",
    16                                            "default text")
    17myint, type_conversion_ok = proj.readNumEntry("myplugin",
    18                                              "myint",
    19                                              123)
    20mydouble, type_conversion_ok = proj.readDoubleEntry("myplugin",
    21                                                    "mydouble",
    22                                                    123)
    23mybool, type_conversion_ok = proj.readBoolEntry("myplugin",
    24                                                "mybool",
    25                                                123)
    

    여러분도 알 수 있듯이, 많은 (정수, 문자열, 목록) 데이터 유형에 대해 writeEntry() 메소드를 사용하지만, 설정값을 다시 읽어오기 위한 메소드가 몇 가지 존재하며 각 데이터 유형에 대응하는 메소드를 선택해야 합니다.

  • 맵 레이어 수준 설정 – 프로젝트의 맵 레이어 하나의 특정 인스턴스와 관련된 설정입니다. 이 설정은 레이어의 기저 데이터소스와 연결되지 않기 때문에, shapefile 1개의 맵 레이어 인스턴스를 2개 생성하는 경우 이 두 인스턴스는 설정을 공유하지 않을 것입니다. 맵 레이어 수준 설정은 프로젝트 파일 내부에 저장되기 때문에 여러분이 프로젝트를 다시 열면 레이어 관련 설정도 그대로 불러올 것입니다. writeEntry() 메소드를 사용해서 지정한 설정의 값을 가져오고, setCustomProperty() 메소드를 사용해서 설정할 수 있습니다.

    1vlayer = QgsVectorLayer()
    2# save a value
    3vlayer.setCustomProperty("mytext", "hello world")
    4
    5# read the value again (returning "default text" if not found)
    6mytext = vlayer.customProperty("mytext", "default text")