首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Arcpy SelectLayerByLocation出现错误

Arcpy SelectLayerByLocation出现错误
EN

Stack Overflow用户
提问于 2014-09-26 04:53:34
回答 2查看 923关注 0票数 0

此脚本应根据要素的某些特性在两个图层中选择距离内的要素一个要素将获得评分(例如:穿越河流等自然敏感区域的水管,该河流的类型及其永久性在评分中很重要,因此将选择每种类型,然后在按泌乳功能选择中使用,以给距离内的水管评分

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

执行中:清华water_mains WITHIN_A_DISTANCE Just_selected“2.5M”NEW_SELECTION开始时间:清华Sep 25 15:21:09 2014年错误999999:执行函数出错。指定的列不存在。指定的列不存在。无法执行(SelectLayerByLocation)。

按位置选择图层位于由另一个脚本(主脚本)调用的脚本中

主脚本:

代码语言:javascript
复制
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()

调用的函数是:

代码语言:javascript
复制
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())
EN

回答 2

Stack Overflow用户

发布于 2014-09-26 23:21:54

请在地理信息系统StackExchange上查看此问题:Points in Polygon Count: Error with arcpy.selectLayerByLocation_management。他们在调用MakeFeatureLayer_management时犯了一个错误,但SelectLayerByLocation_management抛出了错误。您可能会遇到类似的情况。

在您的示例中,您是否确信存储在dataPath + "\\ENVIRONMENTAL\\OHN_WaterCourse.shp"中的要素类具有名为WARTERCOURS的字段?是不是有个打字错误?( WARTERCOURS这个词引起了我的注意;Google says你是互联网上第一个使用这个词的人。)

票数 2
EN

Stack Overflow用户

发布于 2014-09-26 23:40:46

你的listOfClassTuples是不是应该是你的creekLayer中的一组字段(dataPath + "\ENVIRONMENTAL\OHN_WaterCourse.shp)?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26047921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档