ArcGIS二次开发——面要素等面积分割
本文使用的ArcGIS版本为10.5,python为ArcGIS10.5自带的python(32位2.7.12)。读者如果不想自己写代码,可以修改代码中的polygon、spiltNum和step三个参数,运行结果将保存在当前脚本所在目录下,文件名为result.shp。本教程示例代码运行的结果有一定误差,但可以将误差减小最小,详见输入参数介绍。
本教程所使用的示例代码可能不是很专业,因在实际工作中需要这样的功能,想到的办法。如果你有更好的面要素等面积分割方法欢迎在下方留言。
polygon表示需要拆分的要素,可以是一个shp文件中包含多个polygon。
spiltNum表示将每个多边形分成多少份。
step的含义是:示例代码主要原理是对多边形从左向右扫描的过程,指针不是匀速向右进行,step代表了每次向右的距离,step值越小等面积分割的误差越小,程序运行时间越长。具体step的取值还要参考输入要素的坐标系统,不同坐标系统及地理坐标和投影的情况下差异较大,当然step粗略值的获取对于我们学地理信息专业的人来说几乎没有难度。
# -*- coding: utf-8 -*-
import os
import shapefile
def polygonsplit(polygon, spiltNum, step, outputFile, path_now):
try:
arcpy.AddField_management(polygon, "area", "DOUBLE", 18)
except:
print(u"面积字段已经存在")
arcpy.CalculateField_management(polygon, "area", "!shape.geodesicArea@SQUAREKILOMETERS!", "PYTHON_9.3")
area = 0
with arcpy.da.SearchCursor(polygon, "area") as cursor:
for row in cursor:
area = row[0]
print(u"总面积:" + str(area))
print(u"每块面积:" + str(area / spiltNum))
file = shapefile.Reader(polygon)
shapes = file.shapes()
print(file.bbox)
topx = file.bbox[0]
topy = file.bbox[3]
buttonx = file.bbox[0]
buttony = file.bbox[1]
ltopx = file.bbox[0]
ltopy = file.bbox[3]
lbuttonx = file.bbox[0]
lbuttony = file.bbox[1]
tmparea = 0
topx = file.bbox[0]
topy = file.bbox[3]
buttonx = file.bbox[0]
buttony = file.bbox[1]
for i in xrange(spiltNum):
tmponesplitarea = 0
print(tmponesplitarea)
print(tmparea)
if i < spiltNum -