<< Click to Display Table of Contents >>

 

Cursor

 

 

Cursor是一種用來處理feature Class 或table資料的類別, 本身可透過迴圈來取得Cursor中每筆資料的row 物件,再透過row物件, 對每筆資料逐一做處理, Cursor自身有三種主要方法,可以直接對feature Class資料進行新增修改予刪除的動作。

Cursor 類函數

類別函數

函數說明

deleteRow(row)

刪除Couser中的row物件, 被刪除的row物件,必須是目前Couser所指向的那一筆, row被刪除時, 來源feature class 中的資料也會被刪除

insertRow(row)

新增新的row物件到Couser中, row被新增時, 來源feature class 中的資料也會被新增

updateRow(row)

更新Couser中的row物件, 被更新的row物件,必須是目前Couser所指向的那一筆, row被更新時, 來源feature class 中的資料也會被更新

newRow()

建立一個空的row物件 (可透過insertRow新增屬性資料)

Next()

將Couser指標指向下一筆資料

getCount()

取得Cursor中的資料總筆數

Reset()

將Couser指標指向第一筆資料

範例:

1. 查詢資料範例

import sgpy

#  Input: C:/temp/Line2.shp

#  Fields: prop1; prop2; prop3

#  Sort fields: prop2 A

#建立查詢用cursor物件,取得依指定欄位排序,保留特定欄位資訊的查詢成果

rows = sgpy.SearchCursor("D:/temp/Line2.shp", where_clause="prop2 = 'abc'")

# 透過迴圈,逐一取得查詢成果中,每筆資料的屬性值

for row in rows:

   print("prop1: {0}, prop2: {1}, prop3: {2}".format(

       row.getValue("prop1"),

       row.getValue("prop2"),

       row.getValue("prop3")))

2. 更新資料範例

import sgpy

# 建立更新資料用Cursor

rows = sgpy.UpdateCursor("D://temp// Line2.shp")

for row in rows:

      #取得特定欄位的資料,經計算後,將結果寫入特定欄位中

      row.setValue("prop1", str(row.getValue("prop2"))+"a")

      rows = sgpy.UpdateCursor("D:/temp/Line2.shp", where_clause="prop2 = 'abc'") #對資料進行更新

      del row

del rows

 


©2015 Supergeo Technologies Inc. All rights reserved.