14.3. 属性テーブルの操作¶
属性テーブルは選択されたレイヤの地物の情報を表示します。各行は(ジオメトリを持つ持たないに関わらず)1つの地物を表し、各列はその地物のある特定の情報を持っています。属性テーブルの地物は検索、選択、移動および編集が可能です。
14.3.1. 序文: 空間情報のあるテーブル、空間情報のないテーブル¶
QGISでは空間情報のあるテーブル、ないテーブルの両方を読み込むことができます。現状ではOGR、デリミテッドテキストに加えてPostgreSQL、MSSQL、Spatialite、DB2、オラクルのプロバイダーがサポートされています。読み込まれた全てのレイヤは Layers に順番に格納されます。レイヤの空間情報が定義されているか否かによってマップ上で操作できるか否かが決まります。
空間情報のないテーブルは属性テーブルを使って閲覧、編集をすることができます。加えて、フィールド検索が可能です。例えば、空間情報がないテーブルでも、編集モード中に列を使って属性値や値の範囲を定義したりすることができます。詳しくは Attributes Form Properties を参考にしてください。
14.3.2. Introducing the attribute table interface¶
To open the attribute table for a vector layer, activate the layer by
clicking on it in the レイヤパネル. Then, from the main
menu, choose . It is also possible to right-click on the layer and choose
from the drop-down menu,
or to click on the
Open Attribute Table button
in the Attributes toolbar.
If you prefer shortcuts, F6 will open the attribute table.
Shift+F6 will open the attribute table filtered to selected features and
Ctrl+F6 will open the attribute table filtered to visible features.
This will open a new window that displays the feature attributes for the layer (figure_attributes_table). According to the setting in menu, the attribute table will open in a docked window or a regular window. The total number of features in the layer and the number of currently selected/filtered features are shown in the attribute table title, as well as if the layer is spatially limited.

図 14.68 地域レイヤーに対する属性テーブル¶
属性テーブルウィンドウの上部にあるボタンは、次の機能を提供します。
アイコン |
ラベル |
目的 |
デフォルトのショートカット |
---|---|---|---|
編集モード切替 |
編集機能を有効にする |
Ctrl+E |
|
複数編集モード切替 |
多くの地物の複数のフィールドを更新 |
||
編集内容の保存 |
現在の修正を保存 |
||
テーブルを再ロード |
|||
地物を追加 |
新規ジオメトリなし地物を追加 |
||
選択地物を削除 |
レイヤーから選択された地物を削除します |
||
Cut selected features to clipboard |
Ctrl+X |
||
Copy selected features to clipboard |
Ctrl+C |
||
クリップボードから地物を貼り付ける |
コピーされたものから新しい地物を挿入 |
Ctrl+V |
|
式を使用して地物を選択します |
|||
全てを選択する |
レイヤー内の全地物を選択 |
Ctrl+A |
|
選択を反転する |
レイヤー内の現在の選択を反転 |
Ctrl+R |
|
全ての選択を解除する |
現在のレイヤー内のすべての地物の選択を解除 |
Ctrl+Shift+A |
|
フォームを使用して地物をフィルタ/選択 |
Ctrl+F |
||
選択されたものを一番上に |
選択行を最上に移動 |
||
選択した行の地物に地図をパンする |
Ctrl+P |
||
選択した行の地物に地図をズームする |
Ctrl+J |
||
新規フィールド |
データソースに新しいフィールドを追加します。 |
Ctrl+W |
|
フィールド削除 |
データソースからフィールドを削除します |
||
フィールド計算機を開く |
行の複数の地物に対してフィールドを更新します。 |
Ctrl+I |
|
条件付き書式 |
表の書式設定を有効にします |
||
Dock attribute table |
Allows to dock/undock the attribute table |
||
Actions |
Lists the actions related to the layer |
注釈
QGISバージョンで構築されたデータおよびOGRライブラリのフォーマットによっては、一部のツールが利用できない場合があります。
これらのボタンの下には、クイックフィールド計算バー( 編集モード でのみ有効)があり、レイヤー内の地物のすべてまたは一部に計算を迅速に適用できます。このバーは フィールド計算機 として、同じ 式 を使用します( 属性値を編集する 参照)。
14.3.2.1. テーブルビュー対フォームビュー¶
QGISは簡単属性テーブル内のデータを操作するために2つの表示モードが用意されています。
The
Table view, displays values of multiple features in a tabular mode, each row representing a feature and each column a field.
The
Form view shows feature identifiers in a first panel and displays only the attributes of the clicked identifier in the second one. There is a pull-down menu at the top of the first panel where the "identifier" can be specified using an attribute (Column preview) or an Expression. The pull-down also includes the last 10 expressions for re-use. Form view uses the layer fields configuration (see Attributes Form Properties). You can browse through the feature identifiers with the arrows on the bottom of the first panel. Once you markered the feature in yellow in the list it is selected in yellow on the canvas. Use the
on top of the attribute table to zoom to the feature. Clicking on an entry in the list (without using the rectangles) makes a feature flash in red color once so you can see where it is situated.
You can switch from one mode to the other by clicking the corresponding icon at the bottom right of the dialog.
デフォルトビュー モードを指定することもできます。 「最後のビューを保存する」、「テーブルビュー」または「フォームビュー」にすることができます。
メニューで、属性テーブルのオープン時に
図 14.69 Attribute table in table view (top) vs form view (bottom)¶
14.3.2.2. Configuring the columns¶
テーブルビューにいるときに列見出しを右クリックすると、属性テーブルで何がどのように表示できるかを設定するのに役立つツールにアクセスできます。
14.3.2.2.1. Hiding and organizing columns and enabling actions¶
列見出しを右クリックすると、それを属性テーブルから隠すことを選択できます。列の非表示を解除または列の順序を変更し、一度に複数の列の動作を変更するには、 列を整理... を選択します。新しいダイアログでは以下のことができます:
表示または非表示にする/チェックを外し列をチェック
属性テーブル内の列の順序を変更するには、ドラッグアンドドロップアイテム。この変更は、表のレンダリングのためのものであり、レイヤのデータソースのフィールドの順序を変更しないことに注意してください
各行の行ごとのアクションのドロップダウンボックスやボタン、リストを表示し、 アクション 列、以下を参照してください。アクションの詳細については アクションのプロパティ 新しい仮想を有効にします。
14.3.2.2.2. Resizing columns widths¶
列の幅を右クリックして列ヘッダーにいずれかを選択して設定できます:
幅を設定... 希望の値を入力します。デフォルトでは、現在の値がウィジェットに表示されます
自動サイズ では列に合わせて最高の状態でリサイズします。
また、列見出しの右側の境界をドラッグして変更できます。新しい列サイズは、レイヤーに維持され、次の開始時に属性テーブルに復元されます。
14.3.2.2.3. Sorting columns¶
テーブルは、列見出しをクリックすることにより、任意の列でソートできます。小さな矢印は、ソート順は、(下向きダウン一番上の行からの値を降順手段、上向きでは、先頭行から下上昇値を意味する)を示しています。列見出しのコンテキストメニューの sort オプションと式を記述、またして行をソートするように選択できます、例えば、複数の列を使用して行を並べ替えるには、 concat(col0, col1)
と書くことができます。
フォームビューでは、地物識別子は プレビュー式で並べ替える オプションを使用してソートできます。
ちなみに
Sorting based on columns of different types
Trying to sort an attribute table based on columns of string and numeric types
may lead to unexpected result because of the concat("USE", "ID")
expression
returning string values (ie, 'Borough105' < 'Borough6'
).
You can workaround this by using eg concat("USE", lpad("ID", 3, 0))
which
returns 'Borough105' > 'Borough006'
.
14.3.2.3. Formatting of table cells using conditions¶
Conditional formatting settings can be used to highlight in the attribute table features you may want to put a particular focus on, using custom conditions on feature's:
ジオメトリ(例えば、マルチパートの地物を、小面積の地物を、または定義された地図範囲内で、...、識別する)。
or field value (e.g., comparing values to a threshold, identifying empty cells...).
をクリックする条件付き書式パネルを有効にできます(フォームビューでは使用できません)テーブルビューの属性ウィンドウの右上にあります。
新しいパネルによって、ユーザーは フィールド または
行全体 のフォーマットレンダリングに新しい規則を追加できます。新しい規則を追加するには、定義するために、フォームを開きます。
規則の名前;
式ビルダー 関数のいずれかを使用する条件;
書式設定:事前定義された書式のリストから選択するか、以下のようなプロパティに基づいて作成できます。
背景とテキストの色。
アイコンの使用;
太字、斜体、下線、または取り消し線;
フォント。

図 14.70 属性テーブルの条件付き書式¶
14.3.3. 属性テーブルで地物とやりとりする¶
14.3.3.1. 地物を選択する¶
テーブルビューでは、属性テーブルの各行は、レイヤー中のユニークな地物の属性が表示されます。行を選択する地物を選択し、同様に、(ジオメトリ有効レイヤーの場合)地図キャンバスに地物を選択すると、属性テーブル内の行を選択します。地図キャンバス(または属性テーブル)で選択された地物のセットが変更された場合、選択は、それに応じて属性テーブル(又は地図キャンバス)で更新されます。
行は、行の左側にある行番号をクリックすると選択できます。 Ctrl キーを押していることによって 複数の行 をマークできます。 Shift キー保持と行の左側にいくつかの行ヘッダーをクリックすることによって 連続選択 を行うことができます。現在のカーソル位置とクリック行との間のすべての行が選択されます。属性テーブル内のカーソル位置を移動する、テーブル内のセルをクリックして、行選択を変更しません。メインキャンバスでの選択を変更すると、属性テーブルのカーソル位置を移動しません。
属性テーブルのフォームビューでは、地物が( 画面のプロパティ 参照)、その表示されたフィールドの値によって左側のパネルで特定され、デフォルトです。この識別子は、既存のフィールドを選択するか、カスタム式を使用するか、パネルの上部にあるドロップダウンリストを使用して交換できます。また、ドロップダウンメニューから地物のリストをソートすることもできます。
右のいずれかで地物の属性を表示するには、左のパネルで値をクリックします。地物を選択するには、識別子の左に正方形のシンボルの内側をクリックする必要があります。デフォルトでは、シンボルが黄色に変わります。テーブルビューのように、以前に公開キーボードの組み合わせを使用して、複数の地物選択を行うことができます。
マウスで地物を選択する以外に、このようななどの属性テーブルのツールバーで利用可能なツールを使用して、フィーチャの属性に基づいて自動選択を行うことができます(より多くの情報とユースケースについてはセクション 自動選択 および次のセクションを参照):
Filtering and selecting features using forms を使って地物を選択することもできます。
14.3.3.2. 地物をフィルタする¶
属性テーブルで地物を選択したら、テーブルにこれらのレコードのみを表示できます。これは、属性テーブル]ダイアログボックスの左下にあるドロップダウンリストから 選択した地物を表示 項目を使用して簡単に行うことができます。このリストは、次のフィルタを提供しています:
すべての地物を表示
選択地物を表示
地図上で見える地物を表示
新規または編集された地物を表示
フィールドフィルタ - ユーザーがフィールドの値に基づいてフィルタリングできるようにする:リストから列を選択し、値を入力し、フィルタリングするためにを押す Enter を押す。次に、一致する地物のみが属性テーブルに表示されます。
Advanced filter (Expression) - Opens the expression builder dialog. Within it, you can create complex expressions to match table rows. For example, you can filter the table using more than one field. When applied, the filter expression will show up at the bottom of the form.
It is also possible to filter features using forms.
注釈
レイヤーのうちの地物をフィルタしていない属性テーブルからレコードをフィルタします。それらは単に瞬間的にmomentaneouslyテーブルから隠されており、地図キャンバスから、またはフィルタを除去することによってアクセスできます。レイヤーから非表示地物を行うフィルタでは、 クエリビルダー 使用。
ちなみに
地図上で見える地物を表示
で 更新データソースのフィルタリング
性能上の理由から、属性表に示される地物は、オープン時にキャンバスの範囲に空間的に制限されています(方法については データソースオプション 参照)。新しいキャンバス範囲で 地図に表示される地物を表示する と空間制限を更新します。
14.3.3.3. Filtering and selecting features using forms¶
Clicking the Filter/Select features using form or
pressing Ctrl+F will make the attribute table dialog switch to form view
and replace each widget with its search variant.
From this point onwards, this tool functionality is similar to the one described in 値による地物選択, where you can find descriptions of all operators and selecting modes.

図 14.71 フィルタフォームでフィルタされた属性テーブル¶
When selecting / filtering features from the attribute table, there is a Filter features button that allows defining and refining filters. Its use triggers the Advanced filter (Expression) option and displays the corresponding filter expression in an editable text widget at the bottom of the form.
すでにフィルタされた地物がある場合は、 地物をフィルタ ボタンの隣にあるドロップダウンリストを使用してフィルタを絞り込むことができます。オプションは次のとおりです。
フィルタの絞り込み ("AND")
フィルタを拡げる ("OR")
To clear the filter, either select the Show all features option from the bottom left pull-down menu, or clear the expression and click Apply or press Enter.
14.3.4. Using action on features¶
ユーザーはコンテキストメニューで地物を操作するためのいくつかの可能性を持っています、以下のように:
Select all (Ctrl+A) the features;
Copy the content of a cell in the clipboard with Copy cell content;
Zoom to feature without having to select it beforehand;
Pan to feature without having to select it beforehand;
Flash feature, to highlight it in the map canvas;
Open form: it toggles attribute table into form view with a focus on the clicked feature.

図 14.72 [セルの内容をコピー]ボタン¶
外部プログラム(Excel、LibreOffice、QGIS、カスタムWebアプリケーションなど)で属性データを使用する場合は、1つまたは複数の行を選択し、 選択した行をクリップボードにコピーする ボタンを押すか、 Ctrl+C を押します。
形式を選択して地物をコピー ドロップダウンリスト:
メニューで貼り付ける形式を定義できますプレーンテキスト、ジオメトリなし、
プレーンテキスト、WKTジオメトリ、
GeoJSON
このコンテキストメニューでアクションのリストを表示することもできます。これは アクションのプロパティ を参照。
タブで有効になります。アクションの詳細については14.3.4.1. Saving selected features as new layer¶
The selected features can be saved as any OGR-supported vector format and
also transformed into another coordinate reference system (CRS). In the
contextual menu of the layer, from the Layers panel, click on
to define the name of
the output dataset, its format and CRS (see section 既存のレイヤから新しいレイヤを作成する). You'll
notice that is checked.
It is also possible to specify OGR creation options within the dialog.
14.3.5. 属性値を編集する¶
属性値の編集は以下の方法で行うことができます。
属性テーブルがテーブルビューであるか、フォームビューであるかにかかわらず、セルに直接新しい値を入力します。したがって変更は、個々のセルや地物ごとに行われます。
フィールド計算機 を使用して、あるフィールド列全体に対して連続して更新を行います。フィールドは既存のものでも、新しく作られたものでもよいですが、更新は複数の地物に対して行われます。これは仮想フィールドを作るためにも使用することができます。
クイックフィールド 計算バー を使用します。上と同じですが既存のフィールドに対してのみ使用できます。
または 複数編集 モードを使用します。複数の地物の複数のフィールドを連続して更新します。
14.3.5.1. フィールド計算機を使用する¶
属性テーブルの フィールド計算機 ボタンは、例えばジオメトリ地物の長さや面積を計算するために、既存の属性値もしくは定義された関数に基づいて計算を行うことを可能にします。計算の結果は既存のフィールドを更新するために、あるいは新しいフィールド(これは virtual なフィールドでもいいです)に書き込むために使うことができます。
フィールド計算機 は、編集をサポートするすべてのレイヤーで利用可能です。フィールド計算機アイコンをクリックするとダイアログが表示されます( figure_field_calculator を参照)。レイヤーが編集モードでない場合は、警告が表示され、フィールド計算機を使用すると、計算が行われる前にレイヤーが編集モードに置かれることになります。
式ビルダー ダイアログを基に構築されたフィールド計算機ダイアログは、式を定義し、それを既存のもしくは新しく作られたフィールドに適用するための、完璧なインターフェイスを提供します。フィールド計算機ダイアログを使うには、計算によって以下のどちらをしたいのかを選ぶ必要があります。
レイヤの全体に計算を適用したいのか、それとも選択した地物のみに適用したいのか
計算によって新しいフィールドを作りたいのか、それとも既存のフィールドを更新したいのか

図 14.73 フィールド計算機¶
If you choose to add a new field, you need to enter a field name, a field type (integer, real, date or string) and if needed, the total field length and the field precision. For example, if you choose a field length of 10 and a field precision of 3, it means you have 7 digits before the dot, and 3 digits for the decimal part.
短い例で、 式 タブを使用している場合、フィールドの計算がどのように機能するかを示しています。QGISサンプルデータセットから railroads
レイヤーのキロメートルの長さを計算したいです:
Set Output field name to
length_km
Select
Decimal number (real)
as Output field typeSet the Output field length to
10
and the Precision to3
Double click on
$length
in the Geometry group to add the length of the geometry into the Field calculator expression box.Complete the expression by typing
/ 1000
in the Field calculator expression box and click OK.You can now find a new length_km field in the attribute table.
14.3.5.2. Creating a Virtual Field¶
A virtual field is a field based on an expression calculated on the fly, meaning
that its value is automatically updated as soon as an underlying parameter
changes. The expression is set once; you no longer need to recalculate the field
each time underlying values change.
For example, you may want to use a virtual field if you need area to be evaluated
as you digitize features or to automatically calculate a duration between dates
that may change (e.g., using now()
function).
注釈
仮想フィールドの使用
仮想フィールドはレイヤーの属性で永続的ではありません、つまりそれらはそれらが作成されてきたプロジェクトファイルで保存され利用可能であるだけということを意味します。
A field can be set virtual only at its creation. Virtual fields are marked with a purple background in the fields tab of the layer properties dialog to distinguish them from regular physical or joined fields. Their expression can be edited later by pressing the expression button in the Comment column. An expression editor window will be opened to adjust the expression of the virtual field.
14.3.5.3. Using the Quick Field Calculation Bar¶
While Field calculator is always available, the quick field calculation bar on top of the attribute table is only visible if the layer is in edit mode. Thanks to the expression engine, it offers a quicker access to edit an already existing field:
Select the field to update in the drop-down list.
Fill the textbox with a value, an expression you directly write or build using the
expression button.
Click on Update All, Update Selected or Update Filtered button according to your need.

図 14.74 Quick Field Calculation Bar¶
14.3.5.4. Editing multiple fields¶
これまでのツールとは異なり、複数編集モードでは、異なる地物の複数の属性を同時に編集できます。編集のためにレイヤーを切り替えると、複数編集機能にアクセスできます。
注釈
属性テーブルのツールとは異なり、
オプションでは、属性の変更を記入するためのモーダルダイアログが出ます。したがって、実行前に地物の選択が必要です。行の複数のフィールドを編集するには:
Select the features you want to edit.
From the attribute table toolbar, click the
button. This will toggle the dialog to its form view. Feature selection could also be made at this step.
At the right side of the attribute table, fields (and values) of selected features are shown. New widgets appear next to each field allowing for display of the current multi edit state:
The field contains different values for selected features. It's shown empty and each feature will keep its original value. You can reset the value of the field from the drop-down list of the widget.
All selected features have the same value for this field and the value displayed in the form will be kept.
The field has been edited and the entered value will be applied to all the selected features. A message appears at the top of the dialog, inviting you to either apply or reset your modification.
これらのウィジェットのいずれかをクリックすると、フィールドの現在の値を設定するか、元の値にリセットできます。つまり、フィールド単位で変更をロールバックできます。
図 14.75 複数の地物の編集フィールド¶
Make the changes to the fields you want.
Click on Apply changes in the upper message text or any other feature in the left panel.
すべての選択された地物 に変更が適用されます。地物が選択されていない場合、テーブル全体が変更内容で更新されます。変更は単一の編集コマンドとして行われます。したがって、 取り消し を押すと、選択したすべての地物の属性変更を一度にロールバックします。
注釈
複数編集モードは、自動生成かつドラッグ&ドロップのフォームでのみ利用可能です( Customizing a form for your data 参照)。それは、カスタムUIフォームではサポートされていません。
14.3.6. 1対多または多対多の関係を作成する¶
関係とは、データベースでよく使用される技術です。その概念は、異なるレイヤー(テーブル)の地物(行)はお互いに属することができるというものです。
14.3.6.1. Introducing 1-N relations¶
例として、アラスカのすべての地域(ポリゴン)を有するレイヤーがあり、その名前と領域タイプと固有のID(主キーとして機能する)についていくつかの属性を持っているとします。
それから、地域に位置し、またこれらのトラックを保持したい空港についての情報を有する別のポイントレイヤーまたはテーブルを取得します。領域レイヤーにそれらを追加したい場合、ほとんどの地域には空港が複数あるため、外部キーを使用して1対多関係を作成する必要があります。

図 14.76 空港とアラスカ地域¶
14.3.6.1.1. Layers in 1-N relations¶
QGISではテーブルとベクターレイヤーの間に違いはありません。基本的に、ベクターレイヤーはジオメトリを持つテーブルです。テーブルをベクターレイヤーとして追加できます。 1対n関係を示すために、 regions
シェープファイルと airports
シェープファイルを読み込みます。シェープファイルには、レイヤー領域に外部キーフィールド( fk_region
)があります。つまり、各空港はそれぞれただ1つの地域に属し、各地域は空港をいくつでも持つことができます(典型的な1対多関係)。
14.3.6.1.2. Foreign keys in 1-N relations¶
空港属性テーブル中の既存の属性に加えて、外部キーとして動作する別のフィールドfk_regionが必要でしょう(データベースを持っている場合はおそらくそれに制約を定義することになるでしょう)。
このフィールドfk_regionには常に地域のIDが含まれているでしょう。それは、それが属する地域へのポインタのように見ることができます。そして、編集のためのカスタム編集フォームを設計でき、QGISではその設定についての処理をします。それはさまざまなプロバイダーで動作し(だからそれはシェープやCSVファイルで使用できます)、しなければならないのはQGISにテーブル間の関係を伝えることだけです。
14.3.6.1.3. Defining 1-N relations (Relation Manager)¶
The first thing we are going to do is to let QGIS know about the relations between the layers. This is done in Relations tab and click on Add Relation.
. Open theName is going to be used as a title. It should be a human readable string, describing, what the relation is used for. We will just call say airport_relation in this case.
Referenced Layer (Parent) also considered as parent layer, is the one with the primary key, pointed to, so here it is the
regions
layer. You can define the primary key of the referenced layer, so it isID
. For this layer you can define multiple referenced fields by using thebutton.
Referencing Layer (Child) also considered as child layer, is the one with the foreign key field on it. In our case, this is the
airports
layer. For this layer you need to add a referencing field which points to the other layer, so this isfk_region
. When using multiple field relations you can add another referencing field by using thebutton.
Id will be used for internal purposes and has to be unique. You may need it to build custom forms. If you leave it empty, one will be generated for you but you can assign one yourself to get one that is easier to handle
Relationship strength sets the strength of the relation between the parent and the child layer. The default Association type means that the parent layer is simply linked to the child one while the Composition type allows you to duplicate also the child features when duplicating the parent ones.

図 14.77 関係マネージャ¶
14.3.6.1.4. Forms for 1-N relations¶
今QGISが関係について知っていること、生成フォームを改善するために使用されます。我々は(自動生成)デフォルトのフォームのメソッドを変更していないとして、それは私たちの形で新しいウィジェットを追加します。それでは、凡例にレイヤー領域を選択してみましょうと、特定のツールを使用します。設定に応じて、フォームが直接開くこともありますし、アクションの下で識別ダイアログでそれを選択して開く必要があることもあります。

図 14.78 空港への関係を持つ識別]ダイアログ領域¶
As you can see, the airports assigned to this particular region are all shown in a table. And there are also some buttons available. Let's review them shortly:
ボタンは編集モードを切り替えるためのものです。私たちは地域レイヤーから地物の地物の形であるが、それは空港レイヤーの編集モードを切り替えていることに注意してください。しかしこの表は、空港レイヤーの地物を表しています。
The
button will add a new record to the airport layer attribute table. And it will assign the new airport to the current region by default.
記号では、現在の領域に割り当てられる、既存の空港を選択できる新しいダイアログが開きます。たまたま間違った領域の上に空港を作成してしまった場合、これは便利かもしれません。
シンボルは、効果的に(外部キーがNULLに設定されている)、未割り当て、それらを残し、現在の領域から選択された空港のリンクを解除します。
With the
button you can zoom the map to the selected child features.
The two buttons
and
to the right switch between table view and form view where the later let's you view all the airports in their respective form.
In the above example the referencing layer has geometries (so it isn't just an alphanumeric table) so the above steps will create an entry in the layer attribute table that has no corresponding geometric feature. To add the geometry:
Select the record that has been added previously within the feature form of the referenced layer.
Use the
Add Part digitizing tool to attach a geometry to the selected attributes table record.
If you work on the airport table, the widget Relation Reference is automatically
set up for the fk_region
field (the one used to create the relation), see
Relation Reference widget.
In the airport form you will see the button at the right side of the
fk_region
field: if you click on the button the form of the region layer will
be opened. This widget allows you to easily and quickly open the forms of the
linked parent features.

図 14.79 地域との関係で識別]ダイアログ空港¶
The Relation Reference widget has also an option to embed the form of the parent
layer within the child one. It is available in the fk_region
field and check the
Show embedded form
option.
今地物ダイアログを見れば、地域のフォームが空港のフォーム内に埋め込まれても、別の領域に、現在の空港を割り当てることができますコンボボックスを、持っていること、わかります。

Moreover if you toggle the editing mode of the airport layer, the fk_region
field has also an autocompleter function: while typing you will see all the
values of the id
field of the region layer.
Here it is possible to digitize a polygon for the region layer using the button
if you chose the option
Allow adding new features
in the
menu of the airport layer.
The child layer can also be used in the 値による地物選択 tool in order to select features of the parent layer based on attributes of their children.
In 図 14.81, all the regions where the mean altitude of the airports is greater than 500 meters above sea level are selected.
You will find that many different aggregation functions are available in the form.

図 14.81 Select parent features with child values¶
14.3.6.2. Introducing many-to-many (N-M) relations¶
N-M relations are many-to-many relations between two tables. For instance, the
airports
and airlines
layers: an airport receives several airline
companies and an airline company flies to several airports.
This SQL code creates the three tables we need for an N-M relationship in
a PostgreSQL/PostGIS schema named locations. You can run the code using the
pgAdmin. The airports table stores the airports
layer and the airlines
table stores the airlines
layer. In both tables few fields are used for
clarity. The tricky part is the airports_airlines
table. We need it to list all
airlines for all airports (or vice versa). This kind of table is known
as a pivot table. The constraints in this table force that an airport can be
associated with an airline only if both already exist in their layers.
CREATE SCHEMA locations;
CREATE TABLE locations.airports
(
id serial NOT NULL,
geom geometry(Point, 4326) NOT NULL,
airport_name text NOT NULL,
CONSTRAINT airports_pkey PRIMARY KEY (id)
);
CREATE INDEX airports_geom_idx ON locations.airports USING gist (geom);
CREATE TABLE locations.airlines
(
id serial NOT NULL,
geom geometry(Point, 4326) NOT NULL,
airline_name text NOT NULL,
CONSTRAINT airlines_pkey PRIMARY KEY (id)
);
CREATE INDEX airlines_geom_idx ON locations.airlines USING gist (geom);
CREATE TABLE locations.airports_airlines
(
id serial NOT NULL,
airport_fk integer NOT NULL,
airline_fk integer NOT NULL,
CONSTRAINT airports_airlines_pkey PRIMARY KEY (id),
CONSTRAINT airports_airlines_airport_fk_fkey FOREIGN KEY (airport_fk)
REFERENCES locations.airports (id)
ON DELETE CASCADE
ON UPDATE CASCADE
DEFERRABLE INITIALLY DEFERRED,
CONSTRAINT airports_airlines_airline_fk_fkey FOREIGN KEY (airline_fk)
REFERENCES locations.airlines (id)
ON DELETE CASCADE
ON UPDATE CASCADE
DEFERRABLE INITIALLY DEFERRED
);
Instead of PostgreSQL you can also use GeoPackage. In this case, the three tables can be created manually using the
. In GeoPackage there are no schemas so the locations prefix is not needed.Foreign key constraints in airports_airlines
table can´t be created using
or so they should be created using .
GeoPackage doesn't support ADD CONSTRAINT statements so the airports_airlines
table should be created in two steps:
Set up the table only with the
id
field usingUsing
, type and execute this SQL code:ALTER TABLE airports_airlines ADD COLUMN airport_fk INTEGER REFERENCES airports (id) ON DELETE CASCADE ON UPDATE CASCADE DEFERRABLE INITIALLY DEFERRED; ALTER TABLE airports_airlines ADD COLUMN airline_fk INTEGER REFERENCES airlines (id) ON DELETE CASCADE ON UPDATE CASCADE DEFERRABLE INITIALLY DEFERRED;
Then in QGIS, you should set up two one-to-many relations as explained above:
airlines
テーブルとピボットテーブルの間の関係。そして
airports
テーブルとピボットテーブルとの間の第2の関係。
An easier way to do it (only for PostgreSQL) is using the Discover Relations in . QGIS will automatically read all relations in your database and you only have to select the two you need. Remember to load the three tables in the QGIS project first.

図 14.82 Relations and autodiscover¶
In case you want to remove an airport
or an airline
, QGIS won't remove
the associated record(s) in airports_airlines
table. This task will be made by
the database if we specify the right constraints in the pivot table creation as
in the current example.
注釈
Combining N-M relation with automatic transaction group
このようなコンテキストで作業する場合は、
でトランザクションモードを有効にする必要があります。 QGISは、すべてのテーブル(航空会社、空港、ピボットテーブル)の行を追加または更新できる必要があります。Finally we have to select the right cardinalilty in the
airports
and
airlines
layers. For the first one we should choose the airlines (id) option
and for the second one the airports (id) option.

図 14.83 Set relationship cardinality¶
Now you can associate an airport with an airline (or an airline with an airport)
using Add child feature or Link existing child feature
in the subforms. A record will automatically be inserted in the airports_airlines
table.

図 14.84 N-M relationship between airports and airlines¶
注釈
Using Many to one relation cardinality
Sometimes hiding the pivot table in an N-M relationship is not desirable. Mainly because there are attributes in the relationship that can only have values when a relationship is established. If your tables are layers (have a geometry field) it could be interesting to activate the On map identification option ( ) for the foreign key fields in the pivot table.
注釈
Pivot table primary key
Avoid using multiple fields in the primary key in a pivot table. QGIS assumes a single
primary key so a constraint like constraint airports_airlines_pkey primary key (airport_fk, airline_fk)
will not work.