zoukankan      html  css  js  c++  java
  • 9章代码

    9.1 修改字段长度

    #coding=utf8
    import arcpy
    
    import os
    import sys
    import math
    
    def Main():
    
        fm1 = arcpy.FieldMap()
    
        fms = arcpy.FieldMappings()
        for field in arcpy.ListFields(inFeature):
            if field.type!="Geometry" and field.type!="OID" and field.editable:
                fm = arcpy.FieldMap()
                myFieldname=field.name
                arcpy.AddMessage("myFieldname=="+myFieldname)
                fm.addInputField(inFeature, myFieldname)
                fm.mergeRule="First"
                outputField=fm.outputField
                outputField.name =myFieldname
                if myFieldname==FieldName:
                    outputField.length=Fieldlen #修改
                fm.outputField=outputField #必须的
                fms.addFieldMap(fm)
    
        outPath, outFC = os.path.split(outFeature)
    
        arcpy.FeatureClassToFeatureClass_conversion(inFeature, outPath, outFC, field_mapping=fms)
    
    inFeature  = arcpy.GetParameterAsText(0)
    FieldName  = arcpy.GetParameterAsText(1)
    Fieldlen=arcpy.GetParameter(2)
    outFeature=arcpy.GetParameterAsText(3)
    
    Main()

    9.2.2 修改字段类型

    #coding=utf8
    import arcpy
    
    import os
    import sys
    import math
    
    def Main():
    
        #fm1 = arcpy.FieldMap()
    
        fms = arcpy.FieldMappings()
        for field in arcpy.ListFields(inFeature):
            if field.type!="Geometry" and field.type!="OID" and field.editable:
                fm = arcpy.FieldMap()
                myFieldname=field.name
                arcpy.AddMessage("myFieldname=="+myFieldname)
                fm.addInputField(inFeature, myFieldname)
                fm.mergeRule="First"
                outputField=fm.outputField
                outputField.name =myFieldname
                if myFieldname==FieldName:
                    outputField.length=Fieldlen
                    outputField.type=Fieldtype
    
                    arcpy.AddMessage("Fieldlen=="+str(Fieldlen))
    
                fm.outputField=outputField #必须的
                fms.addFieldMap(fm)
    
        outPath, outFC = os.path.split(outFeature)
        try:
            arcpy.FeatureClassToFeatureClass_conversion(inFeature, outPath, outFC, field_mapping=fms)
        except Exception as e:
            #print e.message
            arcpy.AddError(e.message)
    
    inFeature  = arcpy.GetParameterAsText(0)
    FieldName  = arcpy.GetParameterAsText(1)
    Fieldtype=arcpy.GetParameterAsText(2)
    Fieldlen=arcpy.GetParameter(3)
    outFeature=arcpy.GetParameterAsText(4)
    
    Main()

    9.3  获得一个省有多少的城市

    #coding=utf8
    import arcpy
    
    import os
    import sys
    import math
    
    def getCount(inFeature):
        result = arcpy.GetCount_management(inFeature)
        count= int(result.getOutput(0))
        return count
    
    def Main():
        count=getCount(sFeature)
        if count < 1:
            arcpy.AddMessage(sFeature+u"没有数据")
            return
        yl_999=arcpy.env.scratchWorkspace+"/yl_999" #in_memory/yl_999" #
    
        fms = arcpy.FieldMappings()
        fms.addTable(sFeature)
        #fms.addTable(cityFeature) 考虑到和sFeature相同的字段
        newname=cityFieldName
        for field in arcpy.ListFields(cityFeature):
            if field.type!="Geometry" and field.type!="OID" and field.editable:
                fm = arcpy.FieldMap()
                myFieldname=field.name
                fm.addInputField(cityFeature, myFieldname)
    
                if myFieldname.lower()==cityFieldName.lower():
    
                    fm.mergeRule="Join"
                    fm.joinDelimiter=","
                else:
                    fm.mergeRule="First"
    
                outputField=fm.outputField
                if FieldExists(sFeature,myFieldname):
                    outputField.name =myFieldname+"_1"
                    if myFieldname.lower()==newname.lower():
                        newname=myFieldname+"_1"
                else:
                    outputField.name =myFieldname
                if myFieldname==cityFieldName:
                    outputField.length=99999
                fm.outputField=outputField #必须的
                fms.addFieldMap(fm)
    
        arcpy.SpatialJoin_analysis(sFeature, cityFeature, yl_999, "#", "#", fms)
    
        #arcpy.AddMessage("newname="+newname)
        scursor=arcpy.da.SearchCursor(yl_999, newname)
        with arcpy.da.UpdateCursor(sFeature, FieldName) as myupdatecursor:
            for row in myupdatecursor:
                srow=scursor.next()
                mystr=srow[0]
                if mystr==None:
                    mystr=""
    
                row[0]=mystr
                myupdatecursor.updateRow(row)
        del scursor
        arcpy.Delete_management(yl_999)
    
    sFeature  = arcpy.GetParameterAsText(0)
    FieldName  = arcpy.GetParameterAsText(1)
    cityFeature=arcpy.GetParameterAsText(2)
    cityFieldName=arcpy.GetParameterAsText(3)
    
    Main()

    9.4   按字段最长合并

    #coding=utf8
    import arcpy
    
    import os
    import sys
    import math
    
    
    def initProgress(hint,num):
        arcpy.SetProgressor("step", u"正在"+hint,0,num,1)
    def step():
        arcpy.SetProgressorLabel(u"正在进行....")
        arcpy.SetProgressorPosition()
    def freeProgress():
        arcpy.ResetProgressor()
    
    def getCount(inFeature):
        result = arcpy.GetCount_management(inFeature)
        count= int(result.getOutput(0))
        return count
    def getmapidx(fms,fieldname):
        idx=0
        for field in fms.fields:
    
            if field.name.lower()==fieldname.lower():
                return idx
    
            idx=idx+1
        return -1
    
    def getfms(fms):
        num=fms.fieldCount
        arcpy.AddMessage("fieldCount={0}".format(num))
        arcpy.AddMessage("fieldValidationWorkspace={0}".format(fms.fieldValidationWorkspace))
        arcpy.AddMessage("exportToString={0}".format(fms.exportToString()))
        for field in fms.fields:
            arcpy.AddMessage("===========outfieldname={0},length={1},type={2}".format(field.name,field.length,field.type))
        for fm in fms.fieldMappings:
            arcpy.AddMessage("inputFieldCount={0}".format(fm.inputFieldCount))
            arcpy.AddMessage("joinDelimiter={0}".format(fm.joinDelimiter))
            arcpy.AddMessage("mergeRule={0}".format(fm.mergeRule))
    
            field=fm.outputField
            arcpy.AddMessage("out fieldname={0},length={1},type={2}".format(field.name,field.length,field.type))
            num=fm.inputFieldCount
            for i in range(num):
                arcpy.AddMessage("{0}=InputFieldName={1}".format(i,fm.getInputFieldName(i)))
                arcpy.AddMessage("{0}=getInputTableName={1}".format(i,fm.getInputTableName(i)))
    def Main():
    
        #fm1 = arcpy.FieldMap()
    
        features=inFeatures.split(";")
        num=len(features)
        inFeature=features[0]
        if num==1:
            arcpy.Select_analysis(inFeature, outFeature)
            return
    
        arcpy.AddMessage("inFeature="+inFeature)
        fms = arcpy.FieldMappings()
        for Feature in features:
            fms.addTable(Feature) #如果这个样,把他的所有对照都自动加过
    
        for field in arcpy.ListFields(inFeature):
            if field.type!="Geometry" and field.type!="OID" and field.editable:
    
                myFieldname=field.name
    
                maxlen=-1;
                for i in range(num):
                    flen=getFieldlen(features[i],myFieldname)
    
                    if flen>maxlen:
                        maxlen=flen
                idx=getmapidx(fms,myFieldname)
                arcpy.AddMessage("idx=="+str(idx))
                if idx>-1:
                    fm=fms.getFieldMap(idx)
                    outputField=fm.outputField
                    if outputField.length!=maxlen:
                        outputField.length=maxlen
                        #outputField.length=900
                        fm.outputField=outputField #必须的
    
                        arcpy.AddMessage("myFieldname============="+myFieldname+":maxlen="+str(maxlen))
                        fms.replaceFieldMap(idx,fm)
        getfms(fms)
        #outPath, outFC = os.path.split(outFeature)
        try:
            arcpy.Merge_management(features, outFeature,fms) #一定要加对照,不然自动了,用了2个小时的教训
        except Exception as e:
            #print e.message
            arcpy.AddError(e.message)
    
    inFeatures  = arcpy.GetParameterAsText(0)
    outFeature=arcpy.GetParameterAsText(1)
    
    Main()

    9.6.1  联合

    #coding=utf8
    import arcpy
    
    import os
    import sys
    import math
    
    def getCount(inFeature):
        result = arcpy.GetCount_management(inFeature)
        count= int(result.getOutput(0))
        return count
    def getWorkspace(inFeature):
        desc = arcpy.Describe(inFeature)
        path=desc.path.lower()
        if path.endswith(".mdb") or path.endswith(".gdb"):
            return path
        if os.path.isdir(path):#shp
            return path
        outPath, outFC = os.path.split(path)
        return outPath
    
    
    def getValueTable(vt):
        arcpy.AddMessage("columnCount={}".format(vt.columnCount))
        arcpy.AddMessage("rowCount={}".format(vt.rowCount))
        arcpy.AddMessage("exportToString={}".format(vt.exportToString()))
        rowCount=vt.rowCount
        columnCount=vt.columnCount
        for i in range(rowCount):
            for j in range(columnCount):
                arcpy.AddMessage("row={0},col={1},value={2}".format(i,j,vt.getValue(i,j)))
                arcpy.AddMessage("row={0},col={1},Truevalue={2}".format(i,j,vt.getTrueValue(i,j)))
    
    def Main():
        # Set the workspace. List all of the feature classes in the dataset
        arcpy.env.workspace = inworkspace
        feature_classes = arcpy.ListFeatureClasses(feature_type="Polygon")
        num=len(feature_classes)
        # Create the value table for the Analysis Union tool with 2 columns
        value_table = arcpy.ValueTable(2) #多少列,2
        i=1
    
        # Iterate through the list of feature classes
        for fc in feature_classes:
            # Update the value table with a rank of 2 for each record, except
            #   for BigBog
            myfc=inworkspace+os.sep+fc
            value_table.addRow(myfc + " "+str(i))
            arcpy.AddMessage("myfc={}".format(myfc))
            i+=1
        getValueTable(value_table)
        arcpy.Union_analysis(value_table, outFeature)
    
    inworkspace  = arcpy.GetParameterAsText(0)
    outFeature=arcpy.GetParameterAsText(1)
    
    Main()

    9.6.2   融合列子

    #coding=utf8
    import arcpy
    
    import os
    import sys
    import math
    
    
    def initProgress(hint,num):
        arcpy.SetProgressor("step", u"正在"+hint,0,num,1)
    def step():
        arcpy.SetProgressorLabel(u"正在进行....")
        arcpy.SetProgressorPosition()
    def freeProgress():
        arcpy.ResetProgressor()
    
    def getCount(inFeature):
        result = arcpy.GetCount_management(inFeature)
        count= int(result.getOutput(0))
        return count
    def getValueTable(vt):
        arcpy.AddMessage("columnCount={}".format(vt.columnCount))
        arcpy.AddMessage("rowCount={}".format(vt.rowCount))
        arcpy.AddMessage("exportToString={}".format(vt.exportToString()))
        rowCount=vt.rowCount
        columnCount=vt.columnCount
        for i in range(rowCount):
            for j in range(columnCount):
                arcpy.AddMessage("row={0},col={1},value={2}".format(i,j,vt.getValue(i,j)))
                arcpy.AddMessage("row={0},col={1},Truevalue={2}".format(i,j,vt.getTrueValue(i,j)))
    def DissolveVt():
        dissolveFields = [groupField]
    
        if sumField=="":
            arcpy.Dissolve_management(inFeature, outFeature,
                              dissolveFields, "")
        else:
            statistics_fields= arcpy.ValueTable(2)
            statistics_fields.addRow(sumField+" "+summode)
            statistics_fields.addRow(sumField+" Count") #加统计个数
            arcpy.Dissolve_management(inFeature, outFeature,
                              dissolveFields, statistics_fields)
    
    
    def Main():
        DissolveVt()
    
    inFeature  = arcpy.GetParameterAsText(0)
    groupField  = arcpy.GetParameterAsText(1)
    sumField  = arcpy.GetParameterAsText(2)
    summode  = arcpy.GetParameterAsText(3)
    outFeature=arcpy.GetParameterAsText(4)
    
    Main()

    9.6.3 创建tin

    #coding=utf8
    import arcpy
    
    import os
    import sys
    import math
    
    def getValueTable(vt):
        arcpy.AddMessage("columnCount={}".format(vt.columnCount))
        arcpy.AddMessage("rowCount={}".format(vt.rowCount))
        arcpy.AddMessage("exportToString={}".format(vt.exportToString()))
        rowCount=vt.rowCount
        columnCount=vt.columnCount
        for i in range(rowCount):
            for j in range(columnCount):
                arcpy.AddMessage("row={0},col={1},value={2}".format(i,j,vt.getValue(i,j)))
                arcpy.AddMessage("row={0},col={1},Truevalue={2}".format(i,j,vt.getTrueValue(i,j)))
    def CreateTin():
    
        inValTab= arcpy.ValueTable(4)
        inValTab.addRow("{0} {1} {2} {3}".format(dgxFeature,gcField,"Hard_Line","<None>"))
    
        if fwFeature!="":
            inValTab.addRow("{0} {1} {2} {3}".format(fwFeature,"<None>","Soft_Clip","<None>"))
    
        arcpy.ddd.CreateTin(outtin, sr, inValTab)
    
    def Main():
        CreateTin()
    
    dgxFeature  = arcpy.GetParameterAsText(0)
    gcField  = arcpy.GetParameterAsText(1)
    fwFeature=arcpy.GetParameterAsText(2)
    outtin=arcpy.GetParameterAsText(3)
    sr=arcpy.Describe(dgxFeature).spatialReference
    arcpy.CheckOutExtension("3D")
    Main()
    arcpy.CheckInExtension("3D")
  • 相关阅读:
    1144 The Missing Number (20分)
    1145 Hashing
    1146 Topological Order (25分)
    1147 Heaps (30分)
    1148 Werewolf
    1149 Dangerous Goods Packaging (25分)
    TypeReference
    Supervisor安装与配置()二
    谷粒商城ES调用(十九)
    Found interface org.elasticsearch.common.bytes.BytesReference, but class was expected
  • 原文地址:https://www.cnblogs.com/gisoracle/p/13579731.html
Copyright © 2011-2022 走看看