使用ArcPy在中点分割多边形?


14

我正尝试在其中点(垂直于最长轴)(即在中点的宽度上)周围的4000个多边形中进行分割,如下图所示。

在此处输入图片说明

理想情况下,我想自动执行此操作,避免手动分割每个多边形。我已经通过转换可以在每个多边形中绘制的最长线来提取多边形的中点,我只需要确定一种自动在该点上绘制宽度线的方法即可。

多边形的宽度各不相同,因此,通过定义一定长度的宽度线来分割多边形的工具并不是我真正想要的。

有任何想法吗?


所有的多边形都是凸的吗?
AnserGIS

是的,它们的形状或多或少类似于上图所示。
马特

按照gis.stackexchange.com/questions/201867/…所述创建垂直线。使用它们和原图作为面要素的输入。这将有助于在边界点附近做
FelixIP

@Matt我的答案解决了您的问题吗?如果可以,您是否可以通过复选框将其标记为已回答?
BERA

Answers:


23

下面的脚本将输出一个新的分割面要素类和用于分割面的线。需要高级许可证。

多边形将像这样分割: 在此处输入图片说明

在此处输入图片说明

使用“最小边界几何”矩形的质心作为中点,并在该矩形上拆分。

import arcpy
print 'Running'
arcpy.env.workspace = r'C:\TEST.gdb'    #Change to match your data
infc = r'polygons123'                   #Change to match your data
outfc_splitlines = r'splitlines'        
outfc_splitpolygons=r'splitpolygons'    

spatial_ref = arcpy.Describe(infc).spatialReference
arcpy.CreateFeatureclass_management(out_path=arcpy.env.workspace, out_name=outfc_splitlines, geometry_type='POLYLINE',spatial_reference=spatial_ref) #Creates a new feature class to hold the split lines

with arcpy.da.SearchCursor(infc,['SHAPE@','SHAPE@X','SHAPE@Y']) as cursor: #For each input polygon create a minimum bounding rectangle
    for row in cursor:
        arcpy.MinimumBoundingGeometry_management(row[0],r'in_memory\bounding','RECTANGLE_BY_WIDTH')
        arcpy.SplitLine_management(r'in_memory\bounding', r'in_memory\splitline') #Split the rectangle into four lines, one for each side
        linelist=[]
        with arcpy.da.SearchCursor(r'in_memory\splitline',['SHAPE@LENGTH','SHAPE@']) as cursor2:
            for row2 in cursor2:
                linelist.append(row2) #Store the lines lenghts and geometries in a list
            linelist=sorted(linelist,key=lambda x: x[0]) #Sort shortest to longest (the two shortest sides of the rectangles come first and second in list)
        arcpy.CopyFeatures_management(in_features=linelist[0][1], out_feature_class=r'in_memory\templine') #Copy the first line to memory
        with arcpy.da.UpdateCursor(r'in_memory\templine',['SHAPE@X','SHAPE@Y']) as cursor3:
            for row3 in cursor3:
                newcentroidx=row[1] #Find x coord of bounding rectangle centroid
                newcentroidy=row[2] #Find y..
                row3[0]=newcentroidx #Assign this to the shortest line
                row3[1]=newcentroidy #Assign this to the shortest line
                cursor3.updateRow(row3) #Move the line to the centroid of bounding rectangle
        arcpy.Append_management(inputs=r'in_memory\templine', target=outfc_splitlines) #Save this line in splitline feature class
#After all split lines are created convert input polygons to lines, merge with split lines and create new polygons from lines.

arcpy.FeatureToLine_management(in_features=infc, out_feature_class=r'in_memory\polytemp')
arcpy.Merge_management(inputs=[r'in_memory\polytemp',outfc_splitlines], output=r'in_memory\templines')
arcpy.FeatureToPolygon_management(in_features=r'in_memory\templines', out_feature_class=outfc_splitpolygons)
print 'Done'

在此处输入图片说明

属性将丢失,但是您可以使用空间连接再次添加它们。


6
很好的解决方案。我认为应该注意,执行此操作(splitline,featureToLine和featureToPolygon)需要高级许可证。此外,我认为在代码中添加一些注释将有助于新的python用户理解每一行的内容。
Fezter

嗨@BERA,抱歉回复缓慢。脚本似乎无法正常工作,并输出以下错误:错误000466:in_memory \ templine与目标分割线的架构不匹配。无法执行(追加)。
马特

1
尝试将附加行更改为:arcpy.Append_management(inputs = r'in_memory \ templine',target = outfc_splitlines,schema_type ='NO_TEST')
BERA

这次似乎出现另一个错误:解析错误IndentationError:unindent与任何外部缩进级别都不匹配(第28行)
Matt

您需要在arcpy.Append_manag ...之前有8个空格。–
BERA
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.