码迷,mamicode.com
首页 > 其他好文 > 详细

ArcPy函数-更新游标UpdateCursor

时间:2021-02-09 11:44:54      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:sys   返回   more   end   gis   col   一个   cee   arcpy   

ArcGIS 帮助 10.2

摘要

UpdateCursor 函数创建一个用于更新或删除指定要素类、shapefile 和表中的行的游标。该游标将数据锁定保留至脚本完成或更新游标对象被删除时。

讨论

以迭代方式更新游标的方式有两种:for 循环或者 while 循环(通过游标的 next 方法返回下一行)。如果要使用游标的 next 方法来检索行数为 N 的表中的所有行,脚本必须调用 next N 次。在检索完结果集的最后一行后调用 next 将返回 None,它是一种 Python 数据类型,此处用作占位符。

通过 for 循环使用 UpdateCursor。

import arcpy

fc = "c:/data/base.gdb/roads"
field1 = "field1"
field2 = "field2"

cursor = arcpy.UpdateCursor(fc)
for row in cursor:
    # field2 will be equal to field1 multiplied by 3.0
    row.setValue(field2, row.getValue(field1) * 3.0)
    cursor.updateRow(row)

 

通过 while 循环使用 UpdateCursor。

import arcpy

fc = "c:/data/base.gdb/roads"
field1 = "field1"
field2 = "field2"

cursor = arcpy.UpdateCursor(fc)
row = cursor.next()
while row:
    # field2 will be equal to field1 multiplied by 3.0
    row.setValue(field2, row.getValue(field1) * 3.0)
    cursor.updateRow(row)
    row = cursor.next()

语法

UpdateCursor (dataset, {where_clause}, {spatial_reference}, {fields}, {sort_fields})
参数 说明 数据类型
dataset

The feature class, shapefile, or table containing the rows to be updated or deleted.

String
where_clause

An optional expression that limits the rows returned in the cursor. For more information on WHERE clauses and SQL statements, see About_building_an_SQL_expression.

String
spatial_reference

Coordinates are specified in the spatial_reference provided and converted on the fly to the coordinate system of the dataset.

SpatialReference
fields
[fields,...]

The fields to be included in the cursor. By default, all fields are included.

String
sort_fields

Fields used to sort the rows in the cursor. Ascending and descending order for each field is denoted by A and D.

String
返回值
数据类型 说明
Cursor

游标对象可分发行对象。

代码实例

UpdateCursor 示例

根据另一个字段值更新要素类中的字段值。

 1 import arcpy
 2 
 3 # Create update cursor for feature class
 4 rows = arcpy.UpdateCursor("c:/data/base.gdb/roads")
 5 
 6 # Update the field used in buffer so the distance is based on the
 7 # road type. Road type is either 1, 2, 3 or 4. Distance is in meters.
 8 for row in rows:
 9     # Fields from the table can be dynamically accessed from the
10     # row object. Here fields named BUFFER_DISTANCE and ROAD_TYPE
11     # are used
12     row.setValue("BUFFER_DISTANCE", row.getValue("ROAD_TYPE") * 100)
13     rows.updateRow(row)
14 
15 # Delete cursor and row objects to remove locks on the data
16 del row
17 del rows

 

ArcPy函数-更新游标UpdateCursor

标签:sys   返回   more   end   gis   col   一个   cee   arcpy   

原文地址:https://www.cnblogs.com/erickoh/p/14386877.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!