基于python的图属互操作

本文介绍如何使用 ArcGIS Python 脚本将空间要素的节点坐标存储到属性字段,并通过修改该字段来变更图形信息。具体包括处理点、多点、线和面等不同类型的地理要素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    在实际的业务需要中你可能有这样的要求将空间要素的所有节点按照一定的规则存储到属性字段中,然后使用后面的业务逻辑处理这些节点的坐标信息,最后在通过这些数据字段中节点信息的变化完成图形信息的变更操作。本人使用python对该种业务方式进行了初步研究。

1.首先要将节点信息写入属性字段

import string, os, sys, locale, arcgisscripting
gp = arcgisscripting.create()
gp.overwriteoutput = 1

msgNotEnoughParams = "Incorrect number of input parameters."
msgUseValidDecimalPointSep = "Please use one of the valid decimal point separators."

try:

    if len(sys.argv) < 2: raise Exception, msgNotEnoughParams
    inputFC = sys.argv[1]
    cf=sys.argv[2]
    tmp=str(cf)
    inDesc = gp.describe(inputFC)
    inRows = gp.UpdateCursor(inputFC)
    inRow = inRows.next()
    outLine=''
    flag=False
    partflag=True
    while inRow:
        feat = inRow.GetValue(inDesc.ShapeFieldName)
        if inDesc.ShapeType.lower() == "point":
            pnt = feat.getpart()
            outLine =  "{"+str(pnt.x) + "," + str(pnt.y) + "," + str(pnt.z) + "," + str(pnt.m)+";"
           
        elif inDesc.ShapeType.lower() == "multipoint":
            partnum = 0
            partcount = feat.partcount
            outLine=outLine+"{"
            while partnum < partcount:
                pnt = feat.getpart(partnum)
                outLine = outLine+ str(pnt.x) + "," + str(pnt.y) + "," + str(pnt.z) + "," + str(pnt.m)+";"
                partnum += 1
               
        else:
            partnum = 0
            partcount = feat.partcount
            while partnum < partcount:
                part = feat.getpart(partnum)
                if partflag:
                    outLine ="{"+outLine
                else :
                    outLine =outLine+"{"
                pnt = part.next()
                if flag:
                    tmp1=outLine[:-1]
                    outLine =tmp1+"];"
                    flag=False
                while pnt:
                    outLine =outLine+ str(pnt.x) + "," + str(pnt.y) + "," + str(pnt.z) + "," + str(pnt.m)+";"
                    pnt = part.next()
                    if not pnt:
                        pnt = part.next()
                        if pnt:
                            if flag:
                                tmp3=outLine[:-1]
                                outLine=tmp3+"];"
                            flag=True
                            outLine=outLine+"["+str(pnt.x) + "," + str(pnt.y) + "," + str(pnt.z) + "," + str(pnt.m)+";"
                            pnt = part.next()  
                if flag:
                    flag=False
                    tmp2=outLine[:-1]
                    outLine =tmp2+"];"
                else:
                    tmp5=outLine[:-1]
                    outLine =tmp5+"}"
                if partflag:
                    tmp4=outLine[:-1]
                    outLine =tmp4+"}"
                    partflag=False
                partnum += 1
  
        outLine=outLine[:-1]+"}"
        inRow.SetValue(tmp,outLine)
        inRows.UpdateRow(inRow)
        outLine=''
        inRow = inRows.next()
    del inRow
    del inRows
except Exception, ErrorDesc:
    gp.AddError(ErrorDesc[0])
    gp.AddError(gp.getmessages(2))

 

2.通过变更操作修改其中的坐标点信息,然后更新图形

import arcgisscripting
import fileinput
import os
import string
gp = arcgisscripting.create(9.3)
infile = sys.argv[1]
tmp=sys.argv[2]
# Create update cursor for feature class
inDesc = gp.describe(infile)
rows = gp.UpdateCursor(infile)
row = rows.Next()
gp.AddMessage(inDesc.ShapeType.lower())
flag=False
while row:
    lineArray = gp.CreateObject("Array")
    partArray=gp.CreateObject("Array")
    featureVertexArray = gp.CreateObject("Array")
    if inDesc.ShapeType.lower() == "polygon":
        txt=str(row.GetValue(tmp))
        strlist=string.split(txt,'}')
        temstr=''
        for value in strlist:
            arr=string.split(value,';')
            for pntstr in arr:
                if len(pntstr)>1:
                    if pntstr[0:1]=='{':
                       temstr=pntstr[1:]
                    elif pntstr[0:1]=='[':
                       temstr=pntstr[1:]
                    elif pntstr[-1]==']':
                        temstr=pntstr[:-1]
                        flag=True
                    else:
                       temstr=pntstr
                a=string.split(temstr,',')
                pnt = gp.createobject("Point")
                if len(a)==4:
                     pnt.x=string.atof(a[0])
                     pnt.y=string.atof(a[1])
                     pnt.z=string.atof(a[2])
                     pnt.m=string.atof(a[3])
                     lineArray.add(pnt)
                     gp.AddMessage(pnt.x)
                     gp.AddMessage(pnt.y)
                if flag==True:
                    flag=False
                    featureVertexArray.add(lineArray)
                    lineArray.removeAll()
            gp.AddMessage("newpart")
            featureVertexArray.add(lineArray)
            lineArray.removeAll()
        polyGeom = gp.createobject("geometry", "polygon", featureVertexArray)
        row.Shape=polyGeom
        rows.UpdateRow(row)
        row = rows.Next()  
    elif inDesc.ShapeType.lower() == "point":
        pnt = gp.CreateObject("Point")
        txt=str(row.GetValue(tmp))
        arrp=string.split(txt,",")
        x=arrp[0]
        m=arrp[3]
        pnt.x=string.atof(x[1:])
        pnt.y=string.atof(arrp[1])
        pnt.z=string.atof(arrp[2])
        pnt.m=string.atof(m[:-1])
        row.Shape=pnt
        rows.UpdateRow(row)
        row = rows.Next()
    elif inDesc.ShapeType.lower() == "multipoint":
        txt=str(row.GetValue(tmp))
        strlist=string.split(txt,'}')
        temstr=''
        for value in strlist:
            arr=string.split(value,';')
            for pntstr in arr:
                if pntstr[0:1]=='{':
                    temstr=pntstr[1:]
                else:
                    temstr=pntstr
                a=string.split(temstr,',')
                pnt = gp.createobject("Point")
                if len(a)==4:
                     pnt.x=string.atof(a[0])
                     pnt.y=string.atof(a[1])
                     pnt.z=string.atof(a[2])
                     pnt.m=string.atof(a[3])
                     lineArray.add(pnt)
                     gp.AddMessage(pnt.x)
                     gp.AddMessage(pnt.y)
            gp.AddMessage("newpart")
        row.Shape=lineArray
        rows.UpdateRow(row)
        row = rows.Next()
    elif inDesc.ShapeType.lower() == "polyline":
       
        txt=str(row.GetValue(tmp))
        strlist=string.split(txt,'}')
        temstr=''
        for value in strlist:
            arr=string.split(value,';')
            for pntstr in arr:
                if pntstr[0:1]=='{':
                    temstr=pntstr[1:]
                else:
                    temstr=pntstr
                a=string.split(temstr,',')
                pnt = gp.createobject("Point")
                if len(a)==4:
                     pnt.x=string.atof(a[0])
                     pnt.y=string.atof(a[1])
                     pnt.z=string.atof(a[2])
                     pnt.m=string.atof(a[3])
                     lineArray.add(pnt)
                     gp.AddMessage(pnt.x)
                     gp.AddMessage(pnt.y)
            gp.AddMessage("newpart")
            featureVertexArray.add(lineArray)
            lineArray.removeAll()
        polyGeomline = gp.createobject("geometry", "polyline", featureVertexArray)
        row.Shape=polyGeomline
        rows.UpdateRow(row)
        row = rows.Next()
# Delete cursor and row objects to remove locks on the data
del row
del rows


 

测试中我采用编辑图形的方式完成节点坐标信息的修改然后将图形通过上面的代码变更回原来的要素(应为第一步我已经将要素的节点信息存储到属性表中)

通过工具变更后为

 

通过以上的脚本最终完成数据的变更初步测试了最负责的面包括三种情况1:只有一个部分2:包括多个部分;3包括环的情况,以上脚本还在进一步测试希望大家多多指正

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值