需要注意:
1.只有面元素才可以计算面积,所以如果手上数据是边界线的文件,则需要将之转为面文件,参见Python(arcpy) 批量将线元素转化为面元素
2.地理坐标系无法计算面积,需要在投影坐标系下方可
#-*- coding: UTF-8 -*-
''' 计算子流域面积 '''
import arcpy
import glob
import os
import xlrd
import rearcpy.env.workspace = "F:WATA_polygon_shp\\"
outpath = "F:WATA_shp_with_area_field\\"
shps = arcpy.ListFiles("*.shp")for shp in shps:oldname = [substr.start() for substr in re.finditer('shp', shp)] #提取文件名newname = shp[0:int(oldname[0])-1]+'_with_area_field.shp' #去除旧文件名后缀,生成新文件名Input_Feature_Class = arcpy.env.workspace+'\\'+shp #原文件路径Output_Feature_Class = outpath+newname #新文件路径print(Input_Feature_Class)print(Output_Feature_Class)arcpy.CalculateAreas_stats(Input_Feature_Class,Output_Feature_Class)
print('All done!')
参考文献:
Calculate Areas帮助|文档
WARNING000916: 输入要素类似乎不包含投影数据
后记:
写博客的初衷是分享经验,同时是算是自己对思路和代码的整理,方便日后处理数据,应该可以帮到很多人。
我已免费分享我的心得,如果看官还有其他问题的,那么:知识付费,我的时间和经验正好可以解决你的问题。
咨询问题请添加QQ:819369354
2022年4月20日