Arcpy实现图层点顺时针编号

Arcpy实现一个点图层按照一个面图层的边界走向来选取任意一个点为起点后顺时针排序怎么写?就像下面这个图一样,点图层在面图层的边界上,选了左上角为第一个点后顺时针排序。

img

最开始想的是找到这个面图层的中心点然后求取每个点到中心点的向量与标准向量的角度比较来排序,但是这必须要凸边形才能成功,比如下面这种按角度排序就不行了。

img

img

import arcpy

##  input parameters
mxd = arcpy.mapping.MapDocument("CURRENT")
polygons = arcpy.mapping.ListLayers(mxd,"PGONS")[0]
points = arcpy.mapping.ListLayers(mxd,"POINTS")[0]

##truncated coordinate as string
def truncate(f, n):
    s = '{}'.format(f)
    i, p, d = s.partition('.')
    return '.'.join([i, (d+'0'*n)[:n]])        

with arcpy.da.SearchCursor(polygons,["SHAPE@","MB2013"]) as cursor:
    for shp,idL in cursor:
##      get extent NW corner
        ext=shp.extent
        UL=arcpy.PointGeometry(ext.upperLeft)
        Q='"MB2013" = %s'%("'"+idL+"'")
        points.definitionQuery=Q
        aList=[];pLine=shp.boundary()
##      order points along boundary at new start
        dMin=1e6
        with arcpy.da.SearchCursor(points,"SHAPE@") as pCur:
            for line in pCur:
                pnt=line[0].firstPoint
                L=pLine.measureOnLine(pnt)
                d=UL.distanceTo(pnt)
                if d<dMin: dMin=d;lMin=L
                aList.append([L,pnt])
        for i,(L,pnt) in enumerate(aList):
            if L>=lMin: aList[i][0]=L-lMin
            else:aList[i][0]=L+pLine.length-lMin
##     dictionary of points signatures and chainage
        newList=sorted(aList)
        aDict={}
        for i,(L,pnt) in enumerate(newList):
            aKey=truncate(pnt.X,2)+truncate(pnt.Y,2)
            aDict[aKey]=i+1
##      transfer new order to points table
        with arcpy.da.UpdateCursor(points,("SHAPE@","GroupNo")) as pCur:
            for pnt,no in pCur:
                aKey=truncate(pnt.firstPoint.X,2)+truncate(pnt.firstPoint.Y,2)
                pCur.updateRow((pnt,aDict[aKey]))

怕你访问不了 请参考。

我觉得这篇博文【【arcpy】创建点、线、面(孔洞、环、多部件)要素、要素类】对你编写程序应该有所帮助:http://www.manongjc.com/detail/24-ygxbhjdubznoqvr.html