使用ArcPy创建常用的要素文件时(shapefile),如、点、线面要素,其创建步骤基本相同。线、面要素与点要素创建的不同在于,点要素使用单个点坐标创建,线和面则是将一组点放入一个数组中,使用数组来创建线或者面。下面展示创建要素的基本步骤: 均为在PyCharm中进行
## 创建要素的基本步骤:
# 1.输入数据
# 2.定义空间参考
# 3.确定创建要素的路径、名称
# 4.创建要素类
# 5.创建要素字段
# 6.创建游标
# 7.创建点,遍历所有数据
# 8.新建要素、新建行
# 9.将新建的行写入游标中
点、线、面要素的创建如上所述。下面展示点线面要素创建的python代码以及相应的解释: 1.点要素的创建
#coding:utf-8
import arcpy
points=[[113.12345,34.3654],[113.12365,34.65894],[113.12389,34.6547]]
sp=arcpy.SpatialReference(4326)
path="H:\pyStudy\data_points" #输出创建文件的路径
filename="testPoint.shp" #所创建文件的名称
fc=arcpy.CreateFeatureclass_management(path,filename,"POINT","","","",sp) #创建要素类
arcpy.AddField_management(fc,"FIDX","TEXT",50) #创建字段,即属性表中的字段名
arcpy.AddField_management(fc,"X","TEXT",50)
arcpy.AddField_management(fc,"Y","TEXT",50)
cursor=arcpy.InsertCursor(fc) #创建游标
index=0
for pt in points:
point=arcpy.Point()
point.X=pt[0]
point.Y=pt[1]
row=cursor.newRow()
row.shape=point
row.FIDX=index #将字段数据写入行中
row.X=pt[0]
row.Y=pt[1]
cursor.insertRow(row)
index+=1
print "Finished!"
print "创建成功!"
##创建多点,即多个点为一条记录
pointArray=[
[[113.12345,34.3654],[113.12365,34.65894],[113.12389,34.6547]],
[[113.1235,34.3654],[113.12365,34.6894],[113.2389,34.6547]],
[[113.12345,34.654],[113.365,34.6894],[113.9,34.6547]],
]
sp=arcpy.SpatialReference(4326)
path="H:\pyStudy\data_points"
filename="testMultiPoints"
fc=arcpy.CreateFeatureclass_management(path,filename,"MULTIPOINT","","","",sp)
arcpy.AddField_management(fc,"FIDn","TEXT",50)
cursor=arcpy.InsertCursor(fc)
index=0
for coord in pointArray:
array=arcpy.Array()
for pt in coord:
point=arcpy.Point()
point.X=pt[0]
point.Y=pt[1]
array.add(point)
multipoint=arcpy.Multipoint(array)
row=cursor.newRow()
row.shape=multipoint
row.FIDn=index
cursor.insertRow(row)
index+=1
print "创建多点完成!"#创建单点
2.线要素的创建
###创建线要素
polylinePts=[
[[113.12345,34.3654],[113.12365,34.65894],[113.12389,34.6547]],
[[113.1235,34.3654],[113.12365,34.6894],[113.2389,34.6547]],
] #每组有三个点,每组为一条线段
sp=arcpy.SpatialReference(4326)
path="H:\pyStudy\data_polylines"
filename="test_polylines.shp"
fc=arcpy.CreateFeatureclass_management(path,filename,"POLYLINE","","","",sp)
arcpy.AddField_management(fc,"polyline","TEXT",50)
cursor=arcpy.InsertCursor(fc)
for coord in polylinePts:
array=arcpy.Array()
for pt in coord:
point=arcpy.Point()
point.X=pt[0]
point.Y=pt[1]
array.add(point)
polyline=arcpy.Polyline(array)
row=cursor.newRow()
row.shape=polyline
row.polyline="***123"
cursor.insertRow(row)
print "创建线要素完成!"
3.面要素的创建
###创建面要素
polygonPts=[
[[113.12345,34.3654],[114.12365,34.65894],[113.12389,34.6547]],
[[113.1235,34.3654],[113.12365,35.6894],[113.2389,34.6547]],
] #每组三个点,三个点组成一个面
sp=arcpy.SpatialReference(4326)
path="H:\pyStudy\data_polygon"
filename="test_polygons.shp"
fc=arcpy.CreateFeatureclass_management(path,filename,"POLYGON","","","",sp)
arcpy.AddField_management(fc,"Polygon","TEXT",50)
cursor=arcpy.InsertCursor(fc)
for coord in polygonPts:
array = arcpy.Array()
for pt in coord:
point=arcpy.Point()
point.X=pt[0]
point.Y=pt[1]
array.add(point)
polygon=arcpy.Polygon(array)
row=cursor.newRow()
row.shape=polygon
row.Polygon="Polygon"
cursor.insertRow(row)
print "面要素创建完成!"
|