重要

翻訳は あなたが参加できる コミュニティの取り組みです。このページは現在 62.50% 翻訳されています。

17.11. ベクター計算機

注釈

このレッスンでは、ベクター演算を使用して、数式に基づいてベクターレイヤーに新しい属性を追加する方法について説明します。

We already know how to use the raster calculator to create new raster layers using mathematical expressions. A similar algorithm is available for vector layers, and generates a new layer with the same attributes of the input layer, plus an additional one with the result of the expression entered. The algorithm is called Field calculator and has the following parameters dialog.

../../../_images/field_calculator.png

注釈

インターフェイスが大幅に変更されたプロセッシングの新しいバージョンでは、より強力で使いやすいです。

ここでは、そのアルゴリズムを使用してのいくつかの例があります。

First, let's calculate the population density of white people in each polygon, which represents a census. We have two fields in the attributes table that we can use for that, namely WHITE and SHAPE_AREA. We just have to divide them and multiply by one million (to have density per square km), so we can use the following formula in the corresponding field:

( "WHITE" / "SHAPE_AREA" ) * 1000000

以下に示すようにパラメーターダイアログが満たされる必要があります。

../../../_images/density1.png

This will generate a new field named WHITE_DENS.

今度は、男性人口が女性人口に比して数値的に優位にあるかどうかを示す新しいものを作成するために、 MALESFEMALES フィールド間の比率を計算してみましょう。

Enter the following formula:

"MALES" / "FEMALES"

This time the parameters window should look like this before pressing the OK button.

../../../_images/ratio.png

両方のフィールドが整数型であるため、以前のバージョンでは、結果が整数に切り捨てられることになります。この場合、式は次のようになります。私たちは浮動小数点数に結果を望んでいることを示すために 1.0 * "MALES" / "FEMALES"

次の式を使用して、 male または female テキスト文字列の代わりに、それらの比の値で新しいフィールドを持つように、条件付きの関数を使用できます:

CASE WHEN  "MALES" > "FEMALES"  THEN 'male' ELSE 'female' END

パラメーターウィンドウは、次のようになります。

../../../_images/predominance.png

A python field calculator is available in the Advanced Python field calculator, which will not be detailed here.

../../../_images/advanced.png