arcpy 增删改查要素

1. 查询要素相关代码

 


# -*- coding: UTF-8 -*-
import os
import arcpy
from arcpy import env

# 根據條件查詢
fc_name = r'D:\data\arcpy\MapData\Points\pois.shp'

# 查詢方式1
def search():
where_clause = 'code = 2601'
fields = ["FID", "code", "name"]
cursor = arcpy.SearchCursor(fc_name, where_clause=where_clause, fields=";".join(fields))
for row in cursor:
for f in fields:
print f, row.getValue(f)
print '----------------'

# 查詢方式2
def da_search():
where_clause = "fclass = 'test'"
fields = ["FID", "code", 'fclass', 'SHAPE@XY']
cursor = arcpy.da.SearchCursor(fc_name, fields, where_clause=where_clause)
for row in cursor:
print row
print '----------------'

if __name__ == '__main__':
da_search()

2. 增加要素相关代码

 


# -*- coding: UTF-8 -*-

import os
import arcpy
from arcpy import env

# 插入要素
fc_name = r'D:\data\arcpy\MapData\Points\pois.shp'
fields = ['SHAPE@', "code", "name", "fclass"]

def insert():
cursor = arcpy.da.InsertCursor(fc_name, fields)

for x in xrange(1, 100):
row = [arcpy.Point(104 + x / 100.0, 30 + x / 100.0), x, 'test_name' + str(x), 'test']
cursor.insertRow(row)
del cursor

if __name__ == '__main__':
insert()

 

3. 删除要素相关代码

 


# -*- coding: UTF-8 -*-
import os
import arcpy
from arcpy import env

# 根据查詢条件删除要素
fc_name = r'D:\data\arcpy\MapData\Points\pois.shp'
where_clause = "fclass = 'test'"

def delete():
with arcpy.da.UpdateCursor(fc_name, ["OID@"], where_clause=where_clause) as cursor:
for row in cursor:
cursor.deleteRow()

print '删除成功'

if __name__ == '__main__':
delete()

4. 更新要素相关代码

 


# -*- coding: UTF-8 -*-
import os
import arcpy
from arcpy import env

# 根据查詢条件更新要素
fc_name = r'D:\data\arcpy\MapData\Points\pois.shp'
where_clause = "fclass = 'test'"

def update():
with arcpy.da.UpdateCursor(fc_name, ["OID@", "fclass"], where_clause=where_clause) as cursor:
for row in cursor:
row[1] = 'test2'
cursor.updateRow(row)

print '更新成功'

if __name__ == '__main__':
update()

5. 附其他功能代码:

5.1缓冲区分析

 


# -*- coding: UTF-8 -*-
import os
import arcpy

# 缓冲区分析
fc_name = r'D:\data\arcpy\MapData\Points\pois.shp'
output_name = r'D:\data\arcpy\MapData\Points\pois2.shp'

def buffer():
arcpy.Buffer_analysis(fc_name, output_name, "1 Meters")

if __name__ == '__main__':
buffer()

 

5.2使用pyshp生产多边形要素文件


import shapefile
w = shapefile.Writer('C:/zip/test2')
w.field('name', 'C')
polygon = [[[4.0459844793273926E7,2864882.480102539],[4.0461294993896484E7,2863781.5603027344],[4.0460110980895996E7,2862240.359313965],[4.045850363409424E7,2863841.091674804],[4.0459844793273926E7,2864882.480102539]]]

w.poly(polygon)
w.record('polygon1')
w.close()

留下评论

您的邮箱地址不会被公开。 必填项已用 * 标注