Arcpy脚本根据矢量点提取矢量图层
软件环境
Win11 + ArcGis10.2(叶赫那拉版)
主要函数
MakeFeatureLayer_management()
#使用了MakeFeatureLayer_management方法将数据保存为要素图层数据
arcpy.MakeFeatureLayer_management(Points, "village")
SelectLayerByLocation_management()
# 通过arcpy包的SelectLayerByLocation_management方法进行按位置选择,
# 参数有(被选择的图层,空间关系类型,另一空间关系依赖图层)
# 其中第一个参数必须为要素图层,不能直接使用绝对路径上的文件数据,否则会报错,
# 为此前面使用了MakeFeatureLayer_management方法将数据保存为要素图层数据
arcpy.SelectLayerByLocation_management("village", "intersect", Range)
GetCount_management()
返回值类型是result:
arcpy.GetCount_management("village")
要获取具体的数字使用:
arcpy.GetCount_management("village").getOutput(0)
CopyFeatures_management()
arcpy.CopyFeatures_management(Range, res)
CreateThiessenPolygons_analysis()
#输出泰森多边形要素类的外部边界是点输入要素的范围另加 10%。如果范围环境设置为特定的范围窗口,则该工具将使用环境设置来设置其外部边界。
tempEnvironment0 = arcpy.env.extent
arcpy.env.extent = "121.634233474731 40.8602123260497 131.31520652771 46.3030948638917"
arcpy.CreateThiessenPolygons_analysis(outpoints_shp, outFeatureClass, outFields)
arcpy.env.extent = tempEnvironment0 #改变环境后再改回来
完整代码
# -*- coding:utf-8 -*-
import arcpy
import os
arcpy.env.workspace = "E:\dev\get_point" #自己设置工作路径,文件夹都在这里
Points = "raw_points\村.shp" #原始点数据
shp = r'Jilin\Jilin.shp' #原始边界数据
outpath = r'output_region' #输出乡镇分片数据
#使用了MakeFeatureLayer_management方法将数据保存为要素图层数据
arcpy.MakeFeatureLayer_management(Points, "village")
#SHAPE@指代单个要素,lake_id是一个字段,该字段也是我们想要作为每个polygon命名的值,也可以改为其他的字段如name
with arcpy.da.SearchCursor(shp,["SHAPE@",'FID','Town','County','City','Province'])as cursor:
for row in cursor:
outname = 'output' + str(row[1])+'.shp'
#print("输出"+outname)
arcpy.FeatureClassToFeatureClass_conversion(row[0],outpath,outname)
#读取刚提取的乡镇区域
Range = os.path.join(outpath, outname)
# 通过arcpy包的SelectLayerByLocation_management方法进行按位置选择,
# 参数有(被选择的图层,空间关系类型,另一空间关系依赖图层)
# 其中第一个参数必须为要素图层,不能直接使用绝对路径上的文件数据,否则会报错,
# 为此前面专门使用了MakeFeatureLayer_management方法将数据保存为要素图层数据
selected_featuresByLocation = arcpy.SelectLayerByLocation_management("village", "intersect", Range)
ct = int(arcpy.GetCount_management("village").getOutput(0))
#print(ct)
#判断,区域内无点就跳过
if ct > 1:
#提取选中点
outpoints_shp = os.path.join('output_points', 'out_points' + str(row[1])+'.shp')
#print("输出选中点"+'out_points' + str(row[1])+'.shp')
arcpy.CopyFeatures_management(selected_featuresByLocation, outpoints_shp)
#原始泰森多边形
outFeatureClass = os.path.join('ThiessenPolygons', 'thiessen' + str(row[1])+'.shp')
#print("输出原始泰森"+'thiessen' + str(row[1])+'.shp')
outFields = "ALL"
res = os.path.join('result', 'res' + str(row[1])+'.shp')
# Execute CreateThiessenPolygons
tempEnvironment0 = arcpy.env.extent
arcpy.env.extent = "121.634233474731 40.8602123260497 131.31520652771 46.3030948638917"
arcpy.CreateThiessenPolygons_analysis(outpoints_shp, outFeatureClass, outFields)
arcpy.env.extent = tempEnvironment0
#用乡镇区域提取原始泰森多边形
arcpy.Clip_analysis(outFeatureClass, Range, res)
#print("输出结果图"+'res' + str(row[1])+'.shp')
else:
print('FID:'+str(row[1])+"该区域点位过少")
# 保存为Shapefile
res = os.path.join('result', 'res' + str(row[1])+'.shp')
arcpy.CopyFeatures_management(Range, res)
参考
Arcgis将一个shp依照属性表导出为多个shp
Arcgis提取指定面矢量内点或线数据
ArcGIS中的Python入门知识点整理
创建泰森多边形
盗版软件害死人,疯狂闪退气死人~