使用arcpy对shp文件进行操作

本文介绍如何使用Python和ArcPy库批量加载指定目录下的所有shp文件到ArcMap中,并展示了一种方法,利用Python读取Excel文件中的坐标数据,生成多边形并保存为shp文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将某一文件夹下的shp文件全部加载:

import arcpy
mxd = arcpy.mapping.MapDocument(r"D:\tes\Operation.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

# set workspace to directory of interest
arcpy.env.workspace = r"D:\data"
# create list of all files ending in .shp
list_shapefiles = arcpy.ListFiles("*.shp")

targetGroupLayer = arcpy.mapping.ListLayers(mxd, "Actual", df)[0]
# loop through list, adding each shapefile to group layer
for shapefile in list_shapefiles:
    addLayer = shapefile
    arcpy.mapping.AddLayerToGroup(df, targetGroupLayer, addLayer, "TOP")

mxd.saveACopy(r"D:\tes\Operation_2.mxd")

python结合Arcpy处理EXCEL数据生成多边形

import xlrd
import xlwt
import arcpy
 
xlsPath = r"C:\Users\Administrator\Desktop\Polygon.xls"
data = xlrd.open_workbook(xlsPath)
 
table = data.sheets()[0]#通过索引顺序获取
cols = table.col_values(5)
nrows = table.nrows
point = arcpy.Point()
array = arcpy.Array()
 
polygonGeometryList = []
for i in range(1,nrows):
    str = table.cell(i,5).value
    points = str.split(u';')
    for j in points:
        xy = j.split(',')
        print xy[0]
        print xy[1]
        print '\n'
        point.X = float(xy[0]);point.Y = float(xy[1])
        array.add(point)
    polygon = arcpy.Polygon(array)
    polygonGeometryList.append(polygon)
    array.removeAll()
arcpy.CopyFeatures_management(polygonGeometryList, "d:\\polygon.shp")
 
print 'over'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值