⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mpointtospoint.py

📁 AdvPythonTechWS 是esri公司提供的一种编程实现语言,本程序是其中的一个小例子!
💻 PY
字号:
import os, sys, traceback
import win32com.client
gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")

def AddPrintMessage(msg, severity):
    print msg
    if severity == 0: gp.AddMessage(msg)
    elif severity == 1: gp.AddWarning(msg)
    elif severity == 2: gp.AddError(msg)

try:
    gp.overwriteoutput = 1

    #input multipoint fc
    inFC = sys.argv[1]
##    inFC = r"C:\Demos\UC2005\AdvPython\ErrorHandling\Reedley_GDB.mdb\The_FD\Polygon_FC"
    print inFC
    #output singlepoint fc
    outFC = sys.argv[2]
##    outFC = r"C:\Temp\junkout.shp"

    #boolean option to set the field values of the multipoint fc to the resulting single point fc.
    bCF = sys.argv[3]
##    bCF = "#"

    if bCF.lower() == "true":
        bCFopt = 1
    else:
        bCFopt = 0

    if not gp.Exists(inFC):
        raise "ExistenceError"

    inFCdesc = gp.describe(inFC)

    if inFCdesc.ShapeType.lower() != "multipoint":
        raise "ShapeTypeError"
    
    pSpatialReference = inFCdesc.SpatialReference
    pInRows = gp.SearchCursor(inFC)
    pInRow = pInRows.next()

    sPath, sName = os.path.split(outFC)
    gp.CreateFeatureClass(sPath, sName, "Point", inFC, "SAME_AS_TEMPLATE", "SAME_AS_TEMPLATE", pSpatialReference)
    outFCdesc = gp.describe(outFC)

    if bCFopt:
        FldList = gp.ListFields(inFC)
        failedfields = []

    pOutRows = gp.InsertCursor(outFC)

    while pInRow:
        
        for a in range(0, pInRow.GetValue("Shape").PartCount):
            pOutRow = pOutRows.NewRow()
            pnt_a = pInRow.Shape.GetPart(a)

            CopiedPoint = gp.createobject("Point")
            CopiedPoint = pnt_a
            pOutRow.shape = CopiedPoint
            
            if bCFopt:
                FldList.reset()
                Fld = FldList.next()

                while Fld:
                    if Fld.Name != outFCdesc.OIDFieldName and Fld.Name != outFCdesc.ShapeFieldName:
                        try:
                            pOutRow.SetValue(Fld.Name, pInRow.GetValue(Fld.Name))
                        except:
                            if Fld.Name not in failedfields:
                                failedfields.append(Fld.Name)
                            pass
                    Fld = FldList.next()
                    
            pOutRows.InsertRow(pOutRow)
        pInRow = pInRows.next()
            
    if bCFopt:
        for x in failedfields:
            AddPrintMessage("Field " + str(x) + " failed to copy", 1)

    del pOutRow
    del pOutRows
    del pInRow
    del pInRows

except "ExistenceError":
    AddPrintMessage("Input Feature Class does not exist", 2)

except "ShapeTypeError":
    AddPrintMessage("Input Features must be of type Multi-Point", 2)

except:
    tb = sys.exc_info()[2]
    tbinfo = traceback.format_tb(tb)[0]
    pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n    " + \
            str(sys.exc_type)+ ": " + str(sys.exc_value) + "\n"

    msgs = "GP ERRORS:\n" + gp.GetMessages(2) + "\n"

    AddPrintMessage(msgs,2)
    AddPrintMessage(pymsg,2)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -