LoginSignup
0
0

More than 3 years have passed since last update.

pythonでベクタレイヤに新しいフィールドを作る方法

Last updated at Posted at 2021-02-07

新しいフィールドを作る

#レイヤの指定
lyr = iface.activeLayer()

#フィールドの定義

new_field = QgsField(name='fieldName', type=QVariant.String, typeName='String', len=254)

#定義したフィールドの適応
lyr.dataProvider().addAttributes([new_field])
lyr.updateFields()
  • QgsFieldで作成するフィールドの設定内容。
    • name:フィールド名
    • type:フィールドタイプ。stringの場合はQVariant.String、integerならQVariant.Int、realならQVariant.Double
    • len:フィールドの長さ
    • prec:精度(小数点以下の数値長さ)の指定

フィールドに値を入力する

for f in lyr.getFeatures():
    f.setAttribute("fieldName", value)
    lyr.updateFeature(f)

lyr.commitChanges()

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0