ArcGIS中使用python实现:将面要素类的所有节点信息存储在属性表的字段中

总结两种方法实现:将面要素类的所有节点信息,按节点顺序存储在属性表的字段中

一种是使用字段计算器的python代码块,在网上找到了实现代码,但在运行中报如下错误,最后确定原因在于所添加的字段长度不够导致的:

首先需要通过add field添加一个string类型的字段,注意:字段长度必须足够大(如果面的节点很多),修改如下截图中Length值。

然后右键字段打开字段计算器,勾选显示代码块,如下截图所示:

代码块内容如下:

def MySub(feat):    
     partnum = 0
     #multipart feature
     partcount = feat.partCount
     pntcount = 0
     str=''
     # Enter while loop for each part in the feature (if a singlepart feature
     # this will occur only once)
     while partnum < partcount:
          part = feat.getPart(partnum)
          pnt = part.next()

          # Enter while loop for each vertex
          #
          str=str+"["
          while pnt:
               pntcount += 1
               px='%f' %pnt.X
               py='%f' %pnt.Y
               str=str+px+","+py +";"
               #print px, py
               pnt = part.next()
               
               # If pnt is null, either the part is finished or there is an 
               # interior ring
               if not pnt:
                    str=str[:-1]
                    str=str+"]" 
                    pnt = part.next()
          partnum += 1
     return str

第二种方法是使用py脚本文件处理,代码块调试起来很费劲,所以在排查错误的时候使用py更方便,代码如下:

# -*- coding: utf-8 -*-

import arcpy

def MySub(feat):    
     partnum = 0
     #multipart feature
     partcount = feat.partCount
     pntcount = 0
     str=''
     # Enter while loop for each part in the feature (if a singlepart feature
     # this will occur only once)
     while partnum < partcount:
          part = feat.getPart(partnum)
          pnt = part.next()

          # Enter while loop for each vertex
          str=str+"["
          while pnt:
               pntcount += 1
               px='%f' %pnt.X
               py='%f' %pnt.Y
               str=str+px+","+py +";"
               #print px, py
               pnt = part.next()
               
               # If pnt is null, either the part is finished or there is an 
               # interior ring
               if not pnt:
                    str=str[:-1]
                    str=str+"]" 
                    pnt = part.next()
          partnum += 1
     return str


arcpy.env.workspace = "E:/ArcTutor/Editing/Zion.gdb"

# Create the update cursor
cursor = arcpy.UpdateCursor("Park_boundary")

for row in cursor:
    mystr= MySub(row.shape)
    row.setValue("aaa", mystr)
    cursor.updateRow(row)

# Delete cursor and row objects
del cursor, row

转载自:https://blog.csdn.net/zglybl/article/details/80113930

You may also like...