基於python的圖屬互操作

來源:互聯網
上載者:User

    在實際的業務需要中你可能有這樣的要求將空間要素的所有節點按照一定的規則儲存到屬性欄位中,然後使用後面的商務邏輯處理這些節點的座標資訊,最後在通過這些資料欄位中節點資訊的變化完成圖形資訊的變更操作。本人使用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包括環的情況,以上指令碼還在進一步測試希望大家多多指正

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.