arcpy游标使用(一)——Cursor

本文介绍了arcpy的Cursor对象,用于在GIS数据表中进行迭代、插入和更新操作。通过搜索游标读取字段值,更新游标修改属性,以及插入游标创建新行。
摘要由CSDN通过智能技术生成

摘要

Cursor是一种数据访问对象,可用于在表中迭代一组行或者向表中插入新行。游标有三种形式:搜索插入更新。游标通常用于读取和更新属性。

方法概述

方法说明
deleteRow (row)

删除数据库中的某一行。将删除与游标当前所在位置相对应的行。

insertRow (row)

向数据库中插入新行。

newRow ()

创建空行对象。

next ()

返回当前索引中的下一个对象。

reset ()

将当前枚举索引(由 next 方法使用)设置回第一个元素。

updateRow (row)

updateRow 方法可用于对更新游标当前所在的行进行更新。

 

 

 

 

 

代码示例

Cursor 示例 1

使用搜索游标查看行中的字段值。

import arcpy

# Set the workspace
arcpy.env.workspace = "c:/base/data.gdb"

# Create the search cursor
cursor = arcpy.SearchCursor("roads", '"TYPE" <> 4')

# Iterate through the rows in the cursor
for row in cursor:
    print("Name: {0},  CFCC code: {1}".format(row.NAME, row.CFCC))

del cursor, row

Cursor 示例 2

使用更新游标更改行中的字段值。

import arcpy

# Set the workspace
arcpy.env.workspace = "c:/base/data.gdb"

# Create the update cursor
cursor = arcpy.UpdateCursor("roads")

# Update the road buffer distance field based on road type.
#   Road type is either 1,2,3,4  Distance is in meters.
for row in cursor:
    row.setValue("BUFFER_DIST", row.getValue("TYPE") * 100)
    cursor.updateRow(row)

# Delete cursor and row objects
del cursor, row

Cursor 示例 3

使用插入游标在表中新建行。

import datetime
import arcpy

# Create insert cursor for table
cursor = arcpy.InsertCursor("c:/base/data.gdb/roads_maint")

# Create 25 new rows. Set default values on distance and CFCC code
for i in range(1000, 1025):
    row = cursor.newRow()
    row.setValue('rowid', i)
    row.setValue('distance', 100)
    row.setValue('CFCC', 'A10')
    row.setValue('LastInsp', datetime.datetime.now())
    cursor.insertRow(row)

# Delete cursor and row objects
del cursor, row
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值