Arcpy基础入门-6、arcpy要素属性操作

空间数据具有三大特征:空间特征、属性特征和时间特征,同样组成一个几何要素的对象也包括空间几何、属性以及时间表达.

这一节我们以一个实例来说一下arcpy对几何对象属性的操作,属性的操作和数据库的操作基本相同,包括查询、添加、更改和删除,添加和删除主要是针对整个属性添加或者删除一个字段,查询和更改是针对一个要素的属性进行操作,我们来先看一个实例:

import time,os
import arcpy

print '程序开始:' + str(time.ctime())
arcpy.env.workspace = r"C:\Users\ljb\Desktop\高程数据"
#输入要素
inFeatures = "TERLK_LN.shp"
#添加一个字段用来标记高程是否可以整除10
fieldName1 = "Mark"
fieldPrecision = 2
fieldAlias = "整除10标记"

#列出所有字段
fieldObjList = arcpy.ListFields(inFeatures)
# Create an empty list that will be populated with field names        
fieldNameList = []
# For each field in the object list, add the field name to the
#  name list.  If the field is required, exclude it, to prevent errors
for field in fieldObjList:
    if not field.required:
        fieldNameList.append(field.name)
print fieldNameList
if fieldName1 in fieldNameList:
    arcpy.DeleteField_management(inFeatures, fieldName1)
    print"删除已有字段"

#添加字段函数
arcpy.AddField_management(inFeatures, fieldName1, "LONG", fieldPrecision, "", "",
                          fieldAlias, "NULLABLE")

field1 = "Elev"
field2 = "Mark"

#更新查询
cursor = arcpy.UpdateCursor(inFeatures)
for row in cursor:
    # field2 will be equal to field1 multiplied by 3.0
    if((int)(row.getValue(field1))%10 == 0):
        row.setValue(field2, 1)
    else:
        row.setValue(field2, 0)
    cursor.updateRow(row)
print '程序结束:' + str(time.ctime())


添加和删除就是简单的功能函数的问题,其中删除的时候要判断一下现有的字段中是否已经存在。最为关键的是查询,arcpy包括了两种方式的查询,一种是arcpy包里自带的三种游标InsertCursor、SearchCursor、UpdateCursor,另外一种是arcpy数据获取模块也就是arcpy.da模块里带的InsertCursor、SearchCursor、UpdateCursor。两种的主要区别:

1、arcpy.insertcursor只能对表进行插入,不能插入相应的几何要素;而arcpy.da.insertCursor即可以插入表,也可以插入几何要素(如点、线、面);

2、arcpy.updateCursor只能通过row.setValue(字段名,值)和row.getValue(字段名)这两个函数对要素的值进行获取和更改,而arcpy.da.updateCursor可以功过下标的方式对要素的值进行获取和更新如row[0],而且可以查询出要素的坐标、M、Z以及面积长度等等;

3、arcpy.da.SearchCursor可以通过属性字段设置的方式查询要素的坐标、M、Z以及面积长度等,还可以通过下标进行值得获取,arcpy.SearchCursor只能进行普通的属性进行查询,不能使用下标;


至于两者的性能区别,我们后面再说,这里先简单的进行一个入门。

程序和实验数据

  • 5
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
12: Arc GIS API for Pythonb'Chapter 12: Arc GIS API for Python'b'Introduction to the Arc GIS API for Python'b'Creating a Jupyter Notebook'b'Starting the Arc GIS API for Python'b'Adding an item to a web map'b'Importing a CSV with pandas'b'Summary'6: The arcpy.mapping Moduleb'Chapter 6: The arcpy.mapping Module'b'Using Arc Py with map documents \xc3\x82\xc2\xa0'b'Summary'7: Advanced Analysis Topicsb'Chapter 7: Advanced Analysis Topics'b'Using Network Analyst'b'The Network Analyst module'b'Accessing the Spatial Analyst extension'b'Summary'8: Introduction to Arc GIS Onlineb'Chapter 8: Introduction to Arc GIS Online'b'Arc GIS Online'b'Summary'9: Arc Py and Arc GIS Onlineb'Chapter 9: Arc Py and Arc GIS Online'b'Arc GIS Online REST services'b'URL parameters'b'Feature sets'b'Arc GIS Online tokens'b'Putting it all together'b'Summary'10: Arc REST Python Packageb'Chapter 10: Arc REST Python Package'b'Introducing the Arc REST module'b'Arc GIS Online administration'b'Querying hosted feature services'b'Summary'11: Arc Py and Arc GIS Prob'Chapter 11: Arc Py and Arc GIS Pro'b'Introducing\xc3\x82\xc2\xa0Arc GIS Pro'b'Installing and configuring Arc GIS Pro'b'The Arc GIS Pro Python window'b'Python 2.7 and Python 3.5 with Arc Pro'b'Conda and Arc GIS Pro'b'Reviewing Conda basics'b'Summary'1: Introduction to Python for Arc GISb'Chapter 1: Introduction to Python for Arc GIS'b'Python as a programming language'b'The basics of Python programming'b'Data types'b'Other important concepts'b'Important Python modules'b'How Python executes a script'b'Integrated Development Environments (IDEs)'b'Python folder structure'b'Summary'2: Creating the First Python Scriptb'Chapter 2: Creating the First Python Script'b'Prerequisites'b'Model Builder'b'Exporting the model and adjusting the script'b'String manipulation'b'The Arc Py tools'b'The final script'b'Summary'3: Arc Py Cursors - Search, Insert, and Updateb'Chapter 3: Arc Py Cursors - Search, Insert, and Update'b'Pythonfunctions\xc3\x82\xc2\xa0\xc3\xa2\xc

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值