Arcpy SelectLayerByLocation发出错误

时间:2014-09-25 20:53:34

标签: python arcgis arcpy

这个脚本应该根据一些特征得到分数的特征来选择两层距离内的特征(例如:水管穿过自然敏感的区域,如河流,河流的类型及其在评分中的永久性物质,所以每种类型都会被选中,然后用于泌乳功能的选择,以给出距其得分的水管

这是我运行这些代码时遇到的错误:

执行:SelectLayerByLocation water_mains WITHIN_A_DISTANCE Just_selected“2.5米”NEW_SELECTION 开始时间:2014年9月25日星期四15:21:09 ERROR 999999:执行功能错误。 指定了一个不存在的列。 指定了一个不存在的列。 执行失败(SelectLayerByLocation)。

按位置选择图层位于另一个脚本(主脚本)

调用的脚本中

主要剧本:

def main():

    try:

        import arcpy
        from arcpy import env
        # pathing to avoid retyping 
        env.workspace = "C:/Users/abusheikan/Desktop/prev_files/RiskAnalysisModel"
        dataPath = 'C:\\Users\\abusheikan\\Desktop\\prev_files\\RiskAnalysisModel\\ToolData2'

        arcpy.env.overwriteOutput = True

        import imp

        ##Defines INPUT variables
        #some variable wont be used but are there for future use, I'm starting off as simple as possible
        creekLayer = dataPath + "\\ENVIRONMENTAL\\OHN_WaterCourse.shp"
        PipeLayer=dataPath + "\\SERVICES\\water_mains.shp"

        nameField = 'ROW_1'
        scoreField = 'ROW_SCORE1'
        crossingField = 'CROSS_ROW1'
        ROWfield = 'ROW_TRUE1'

        diaField='INTERNAL_D'
        rangeVal= 416

        Field = 'WARTERCOURS'
        Field2='PERMANENCY'

        arcpy.MakeFeatureLayer_management(PipeLayer,"water_mains")
        inFeatures = "water_mains"

        #The following lists contain road classes. Format is (a, b, c,d) such that a is the creek class name,
        #b is an average permencnacy of flow, c is the width, nd d is the xscore to be given .
        #Lower value of c means lower criticality.

        creeks = [('Vertual Flow','intermittent',10,1),
                  ('Vertual Connector','intermittent', 10,2),
                  ('Vertual Flow','Permanent', 10,1),
                  ('Vertual Connector', 'Permanent', 10,2),
                  ('Ditch','Intermittent',5,3),
                  ('Ditch','Permanent',5,4),
                  ('Stream','Intermittent',5,3),
                  ('Stream','Intermittent',5,4)]
       ## the following isnt used yet
        creeks2 = [('Vertual Flow','intermittent',10,1),
                  ('Vertual Connector','intermittent', 10,2),
                  ('Vertual Flow','Permanent', 10,1),
                  ('Vertual Connector', 'Permanent', 10,2),
                  ('Ditch','Intermittent',5,3),
                  ('Ditch','Permanent',5,4),
                  ('Stream','Intermittent',5,3),
                  ('Stream','Intermittent',5,4)]        


  ## This codeblock isnt utilized yet and will always return row_score, it is supposed to adjusts the value of ROW_SCORE
  ##based on whether the water main crosses a creek, by looking up the value in CROSS_ROW1 feild that is obtained later on 
        expression = "crossing(!CROSS_ROW1!,!ROW_SCORE1!)"

        codeblock = """def crossing(crosses, row_score):
            if crosses != 0:
                return 5
            else:
                return row_score"""



    except:
        arcpy.AddError("Definitions failed")
        arcpy.AddMessage(arcpy.GetMessages())

    try:
        ## pathing to a funtion to be called
        fpath = 'C:\\Users\\abusheikan\\Desktop\\prev_files\\RiskAnalysisModel\\Scripts\\'
        ## defining the function pathing we retyped anyway for debugging purpuses.
        functions = imp.load_source('functions', 'C:\\Users\\abusheikan\\Desktop\\prev_files\\RiskAnalysisModel\\Scripts\\functions_creeks.py')
         ## check check :-p
        arcpy.AddMessage("Funtions Loaded")

    except:
        arcpy.AddMessage("Functions not loaded")

    try:


    ##Clear all selections, because otherwise commands will be applied only to selected features, why? I ont know pls explain where this is
        ## supposed to be used and where not to. THANKs!

        arcpy.SelectLayerByAttribute_management(inFeatures, "CLEAR_SELECTION")
        arcpy.AddMessage("Selected")

    ##This new field will show the road overlying the pipe. Default value of "no Creek" will be assigned.
        arcpy.AddField_management(inFeatures, nameField, "TEXT")

        arcpy.CalculateField_management(inFeatures, nameField, '"No Creek"')

    ##This field will contain a score for the highest creek class over the pipe.
    ##  Default of 0 means no creeks 
        arcpy.AddField_management(inFeatures, scoreField, "SHORT")

        arcpy.CalculateField_management(inFeatures, scoreField, 1)

        arcpy.AddField_management(inFeatures, crossingField, "SHORT")


##        arcpy.AddField_management(mainRoadLayer, ROWfield, "FLOAT",3,1)
##        arcpy.CalculateField_management("t_Pavement_Line", ROWfield, expressionROW, "PYTHON_9.3", codeblockROW)

    except:

        #Report error
        arcpy.AddError("Could not create new fields")

        #Report error messages
        arcpy.AddMessage(arcpy.GetMessages())

    try:

##        functions.roadclass_loop is a function that loops through all creek classes in
##        a list, selects the water mains within a distance of each one, and assigns the
##        appropriate score. Full script is in the called function.


## the following s a failed test so never mind that commented out line, it may ciome in handy so left it in there
       ## arcpy.MakeFeatureLayer_management(PipeLayer, 
           ## "JUST_MADE",str(dialField) + " <= "+ str(rangeVal)) 


         ## calls creek_loop funtion() i think here is where the error is created pls check the inputs they may be where problem is! but i cant see anything wrong with them.
        functions.roadclass_loop(creeks, creekLayer, Field, inFeatures, "WITHIN_A_DISTANCE",
                                                 nameField, scoreField)
        arcpy.AddMessage("small pipes")            

        ## same as b4 but with the second tuple list.       
        functions.roadclass_loop(creeks2, creekLayer, Field, inFeatures, "WITHIN_A_DISTANCE",
                                 nameField, scoreField)
        arcpy.AddMessage("BIG PIPES")


       ## functions.roadclass_loop(provincial, provincialLayer, Field3, inFeatures, "INTERSECT",
       ##                          "", crossingField)

##        If the CROSS_ROW field has a nonzero value (i.e. if the water main crosses a large road)
##        the road class score will be increased to 5(the maximum). 
        ## inserts the scores into the 
        arcpy.CalculateField_management(inFeatures, scoreField, expression, "PYTHON_9.3", codeblock)

    except:
        arcpy.AddMessage("Could not run")
        arcpy.AddMessage(arcpy.GetMessages())


if __name__== "__main__":
    main()

被叫函数是:

def test():
##    import arcpy
    arcpy.AddMessage("This function works")

##def roadclass_loop(listOfClassTuples, sourceLayer, fieldName, targetLayer,
##                   outputField1, outputField2):

def roadclass_loop(listOfClassTuples, sourceLayer, fieldName, targetLayer, crossingType,
                   outputField1, outputField2):

    import arcpy
    from arcpy import env

    env.workspace = "C:/data/"

    ##try: 

    for creekclass in listOfClassTuples:

            (classname, Permanency, creekWidth, score) = creekclass

            bufferDistance = creekWidth*0.5
    try:
            if crossingType == "INTERSECT":
                stringBuffer = ""
            else: 
                stringBuffer = "%s Meters" % str(bufferDistance)
    except:
            arcpy.AddMessage("its here")
    arcpy.MakeFeatureLayer_management(sourceLayer, "Just_selected",
                                      fieldName + "= '"+ classname + "'")
    #arcpy.MakeFeatureLayer_management("Just_Selected", "JUST_SELECTED", FieldName2+" = '"+ Permanency + "'") 

    arcpy.SelectLayerByLocation_management(targetLayer, crossingType,
                                           "Just_selected", stringBuffer, "NEW_SELECTION")

    classname = classname.lower()

    if outputField1!= "":                                        

                arcpy.CalculateField_management(targetLayer, outputField1,   classname )

                arcpy.CalculateField_management(targetLayer, outputField2,   score )


    arcpy.Delete_management("Just_selected")
    arcpy.SelectLayerByAttribute_management(targetLayer, "CLEAR_SELECTION")

    ##except:

       # arcpy.AddMessage("Function failed")
        #arcpy.AddMessage(arcpy.GetMessages())

2 个答案:

答案 0 :(得分:2)

在GIS StackExchange上查看此问题:Points in Polygon Count: Error with arcpy.selectLayerByLocation_management。调用MakeFeatureLayer_management时他们犯了一个错误,但SelectLayerByLocation_management抛出了错误。你可能有类似的情况。

在您的情况下,您确信dataPath + "\\ENVIRONMENTAL\\OHN_WaterCourse.shp"中存储的要素类有一个名为WARTERCOURS的字段吗?那里可能有拼写错误吗? (WARTERCOURS这个词引起了我的注意; Google says你是互联网上第一个使用它的人。)

答案 1 :(得分:0)

您的listOfClassTuples是否由您的creeks变量提供,应该是您的creekLayer中的字段集(dataPath +“\ ENVIRONMENTAL \ OHN_WaterCourse.shp)?”