2014-09-25 116 views
0

這個腳本應該根據它們的一些特徵選擇兩層之間距離的特徵,一個特徵將得到一個分數(例如:水管橫跨自然敏感區域,如河流,河流及其類型在評分永久性物質,因此每個類型將被選擇,則在所選擇的泌乳功能用來給水管是一個距離其得分範圍內的Arcpy SelectLayerByLocation給出了一個錯誤

這是我的錯誤當我運行這些代碼:

執行:SelectLayerByLocation water_mains WITHIN_A_DISTANCE Just_selected「2.5 Meters」NEW_SELECTION 開始時間:星期四九月25 15:21:09 2014 錯誤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

在GIS StackExchange上看到這個問題:Points in Polygon Count: Error with arcpy.selectLayerByLocation_management。他們在調用MakeFeatureLayer_management時犯了一個錯誤,但SelectLayerByLocation_management引發了錯誤。你可能有類似的情況。

對於您的情況,您是否確信存儲在dataPath + "\\ENVIRONMENTAL\\OHN_WaterCourse.shp"中的要素類具有名爲WARTERCOURS的字段?那裏可能有拼寫錯誤嗎? (字WARTERCOURS引起我的注意。Google says你是第一個人在互聯網上使用它)

+0

先生,你對於細節有着敏銳的眼光,確實是一個錯字,但是現在我得到了無法運行的消息,這意味着第二個代碼無法運行,儘管我會看看這個問題,謝謝! – Solaire 2014-09-26 15:36:59

0

是您listOfClassTuples,這是由你的小溪變喂,應該是你的creekLayer中設置字段( dataPath +「\ ENVIRONMENTAL \ OHN_WaterCourse.shp)?

+0

是的,前兩列小河(類型和永久性)確實是從小河層獲得的信息,但寬度和分數是從克里克圖層通過匹配前兩個 – Solaire 2014-09-26 15:47:45