# -*- 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

多边形节点编码python脚本的更多相关文章

  1. 关于Python脚本开头两行的:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用 – 指定文件编码类型

    #!/usr/bin/python指定用什么解释器运行脚本以及解释器所在的位置 # -*- coding: utf-8 -*-用来指定文件编码为utf-8的PEP 0263 -- Defining P ...

  2. 【转】关于Python脚本开头两行的:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用 – 指定文件编码类型

    原文网址:http://www.crifan.com/python_head_meaning_for_usr_bin_python_coding_utf-8/ #!/usr/bin/python 是用 ...

  3. 【转载】关于Python脚本开头两行的:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用 – 指定文件编码类型

    1.#!/usr/bin/python 是用来说明脚本语言是 python 的 是要用 /usr/bin下面的程序(工具)python,这个解释器,来解释 python 脚本,来运行 python 脚 ...

  4. arcgis python脚本工具实例教程—栅格范围提取至多边形要素类

    arcgis python脚本工具实例教程-栅格范围提取至多边形要素类 商务合作,科技咨询,版权转让:向日葵,135-4855_4328,xiexiaokui#qq.com 功能:提取栅格数据的范围, ...

  5. win下python脚本以unix风格换行保存将会报错为编码问题 SyntaxError: encoding problem:gbk

    utf-8与gbk编码都报错 从别人的github拉下来一个python脚本. 直接运行,python报错如下: File ".\drag_files_do_event.py", ...

  6. Python脚本开头两行的:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用

    #!/usr/bin/Python指定用什么解释器运行脚本以及解释器所在的位置 # -*- coding: utf-8 -*-用来指定文件编码为utf-8的 估计有不少人注意过一些python脚本开头 ...

  7. python脚本实现集群检测和管理

    python脚本实现集群检测和管理 场景是这样的:一个生产机房,会有很多的测试机器和生产机器(也就是30台左右吧),由于管理较为混乱导致了哪台机器有人用.哪台机器没人用都不清楚,从而产生了一个想法-- ...

  8. Python脚本控制的WebDriver 常用操作 <十七> 获取测试对象的属性及内容

    测试用例场景 获取测试对象的内容是前端自动化测试里一定会使用到的技术.比如我们要判断页面上是否显示了一个提示,那么我们就需要找到这个提示对象,然后获取其中的文字,再跟我们的预期进行比较.在webdri ...

  9. python 脚本查看微信把你删除的好友--win系统版

    PS:目测由于微信改动,该脚本目前不起作用 下面截图来自原作者0x5e 相信大家在微信上一定被上面的这段话刷过屏,群发消息应该算是微信上流传最广的找到删除好友的方法了.但群发消息不仅仅会把通讯录里面所 ...

随机推荐

  1. Parallel for-each loops in .NET C# z

    An IEnumerable object An Action of T which is used to process each item in the list List<string&g ...

  2. sonar之安装篇

    sonar 是一个很好的质量度量平台,安装方式有很多种.下面我教大家使用j2ee 容器的方式安装,我们使用tomcat 1.准备: 1.1 环境redhat linux1.2 下载sonar 从htt ...

  3. Selenium的PageFactory在大型项目中的应用

    出路出路,走出去了,总是会有路的:困难苦难,困在家里就是难. 因为最近遇到的技术问题一直没找到可行的解决办法,一直在翻看selenium的源代码,之前写测试代码的时候就是拿来即用,写什么功能啊,就按手 ...

  4. 导入showb时候出错--2015-12-4

    [root@cache-02 ~]# /opt/coreseek/csftweb-bash: /opt/coreseek/csftweb: is a directory[root@cache-02 ~ ...

  5. TCP 滑动窗口的简介

    TCP 滑动窗口的简介 POSTED BY ADMIN ON AUG 1, 2012 IN FLOWS34ARTICLES | 0 COMMENTS TCP的滑动窗口主要有两个作用,一是提供TCP的可 ...

  6. jenkins api调用

    在使用jenkins的时候,有时候需要其他外部调用,下面是调用方法,不定期更新 job调用 使用user和password: curl -X POST "jobPath/buildWithP ...

  7. Hdu 5289-Assignment 贪心,ST表

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Assignment Time Limit: 4000/2000 MS (Java/Others) ...

  8. C#通过DllImport引入dll中的C++非托管类(转)

    http://blog.sina.com.cn/s/blog_70a144580100tmj8.html

  9. 50道经典的JAVA编程题(21-25)

    50道经典的JAVA编程题(21-25),明天早上java考试了,还是坚持做题吧...这题比老师的题好多了! [程序21]TestJieCheng.java题目:求1+2!+3!+...+20!的和1 ...

  10. [OC Foundation框架 - 2] NSString 的创建

    A. 不可变字符串 void stringCreate() { //Don't need to release memory by this way NSString *str1 = @"S ...