zoukankan      html  css  js  c++  java
  • 多边形节点编码python脚本

    # -*- coding: cp936 -*-
    #本脚以最左边、Y值最大的点为起始点按顺时针为多边形节点编码,生成一个包含记录编码值和多边形FID字段的点要素类

    #注意:
    #1.本脚本作为arcgis脚本工具使用、脚本测试版本为10.0中文,未装sp5补丁
    #2.暂不支持带环多边形,处理带环多边形程序直接崩溃
    #3.本工具脚本用到系统工具"多部分(Multipart)至单部分(Singlepart)",因该工具无法完美处理所有的CAD多边形要素类,
    #  所以本脚本工具只支持简单CAD多边形要素类。

    #Creater: ych
    #Create Time:2013年5月1日

    import arcpy
    import os

    #获取单部分多边形最右边且Y值最大的点(边线相交的多边形不适合)
    def getRightupperPoint(fc): 
        points = [point for point in fc.getPart(0)]
        XValues = [point.X for point in points]
        Xmax_indexs = [XValues.index(x) for x in XValues if x == max(XValues)]
        Ymax_index = [i for i in Xmax_indexs if points[i].Y == max([points[j].Y for j in Xmax_indexs ])][0]
        RightupperPoint = points[Ymax_index]
        return   RightupperPoint,Ymax_index

    #判断单部分多边形的方向(True 为顺时针,False为逆时针 圆、椭圆返回-1)     
    def isclockwise(fc): 
         #找出最右且Y值最大的节点
         points = [point for point in fc.getPart(0)]
         if len(points)!= 2:
            rightupperPoint,rightupperPoint_index = getRightupperPoint(fc)
        
            #如果最右且Y值最大的节点的后一个点(以在points列表中的顺序为参考顺序 )的Y值,比最右且Y值
            #最大的节点的前一个点的Y值大,则多边形为顺时针,否则为逆时针     
            if rightupperPoint_index == len(points)-1:
               BehindPt = points[1]
            else:
               BehindPt = points[rightupperPoint_index+1]
           
            prePt = points[rightupperPoint_index-1]
        
            if prePt.X == rightupperPoint.X:
                return True
            else:
                y = (prePt.Y - rightupperPoint.Y)*(BehindPt.X - rightupperPoint.X)/(prePt.X - rightupperPoint.X ) + rightupperPoint.Y

            if y <BehindPt.Y:
                 return True
            else:
                 return False
         else:
            return -1
             
               
           
    #将单部分多边形要素的起始点只设置为最右且Y值最大的点
    def changeStartPoint(fc):
          points = [point for point in fc.getPart(0)]
          if len(points)!= 2:
             #计算多边形最右边且Y值最大的点在Points中索引
             index_RightupperPoint = getRightupperPoint(fc)[1]
             #计算更改了起始点的多边形要素的newpoints
             newpoints =[]
             newpoints.extend(points[index_RightupperPoint:-1])
             newpoints.extend(points[:index_RightupperPoint+1])
             #创建新要素
             newPolygon = arcpy.Polygon(arcpy.Array(newpoints))
             return newPolygon
          #len(points) ==2为圆、椭圆 
          else:
             return fc
        
         


       
    #主程序
    def main():
      #设置参数
      fcl_input = arcpy.GetParameterAsText(0)  #输入多边形要素类
      out_PointClass = arcpy.GetParameterAsText(1) #输出点要素类


      #创建空的、包含用于存储节点编号的子段的点要素类
      name = os.path.basename(out_PointClass)
      path = out_PointClass[:-(len(name)+1)]
      desc = arcpy.Describe(fcl_input)
      pointClass = arcpy.CreateFeatureclass_management(path,name,"POINT","","","",desc.spatialReference)
     
      #添加字段
      arcpy.AddField_management(pointClass,"NodeCode","SHORT")
      arcpy.AddField_management(pointClass,"PolygonFID","LONG")

      #多部分至单部分
      scrathName = arcpy.CreateScratchName ("TEMP","","",path)
      fcl_input=arcpy.MultipartToSinglepart_management (fcl_input,scrathName)

      #创建cursor
      rows = arcpy.InsertCursor(pointClass)
      rows_fc = arcpy.SearchCursor(fcl_input)
     
     
      #输入多边形要素类的FID字段
      fcID = arcpy.ListFields(fcl_input,"","OID")
     

      for row_fc in rows_fc:
            fc = row_fc.shape
            #将多边形要素的起始点只设置为最右且Y值最大的点
            fc = changeStartPoint(fc)
            #获取多边形节点列表
            points = [point for point in fc.getPart(0)]
            #如果多边形为逆时针,则翻转节点列表,使多边形变成顺时针方向
            if isclockwise(fc)==False: 
               points.reverse()   
            else:
               pass
            #将顺时针方向的多边形点列表写到多边形要素类中
            for point in  points[:-1]:    
               newrow = rows.newRow()
               newrow.shape = point
               newrow.NodeCode =points.index(point)+1
               newrow.PolygonFID = eval("row_fc."+fcID[0].name)
               rows.insertRow(newrow)
            arcpy.AddMessage("Nodes of Polygon %s has been successfully decoded."% eval("row_fc."+fcID[0].name))    
      #删除游标
      del rows,rows_fc,row_fc
      #删除临时文件
      arcpy.Delete_management (fcl_input)

    if __name__ == "__main__":

        main()

    来自:http://bbs.esrichina-bj.cn/esri/viewthread.php?tid=127216
     

  • 相关阅读:
    《架构师》反思:系统可靠性
    表现力(转)
    4月反思
    30天敏捷结果(10) 强化你的一周
    认真对待学习(2月反思)
    Sort By Double
    12月反思 组内设计评审会议
    WPF框架的内存泄漏BUG
    OpenExpressApp 框架结构(2)
    绑定子类的泛型基类,反模式?
  • 原文地址:https://www.cnblogs.com/gisoracle/p/3427757.html
Copyright © 2011-2022 走看看