In ArcMap Pro, how to get data from a selected layer

4511
19
Jump to solution
04-30-2019 11:38 AM
jasongraham
New Contributor III

I am using python in ArcPro and relearning a few things.

I have a feature selected in the dataframe.  I want to determine the name of that layer that has a selection and return the value of the selected object in the field called "GNIS_Name" .  

aprx = arcpy.mp.ArcGISProject("CURRENT")

#get selected layer 
m = aprx.listMaps(aprx)
refLyr = m.listLayers(aprx) 
sel_lyrs = refLyr.getSelectionSet(aprx)

# get name of selected stream    
Stream_Name = arcpy.da.SearchCursor(sel_lyrs, "GNIS_Name", "Any value", "")‍‍‍‍‍‍‍

currently I get the error on line 5: 'list' object has no attribute 'listLayers'

0 Kudos
19 Replies
RandyBurton
MVP Alum

You might try adding the following line just after CalculateField - at line 141.  GetMessages will return messages from the CalculateField tool (i.e., the tool last used) and you can see if it ran successfully.

arcpy.AddMessage(arcpy.GetMessages())
0 Kudos
jasongraham
New Contributor III

Thanks,  it didnt give my any despite running the whole script.  I will try another method.

0 Kudos
jasongraham
New Contributor III

For what is worth, I have tried all three methods.  They all run without error but do not fill in the index field and does not report any errors.

Method 1
index = ['Index']
rows = arcpy.UpdateCursor(fc, index)
for row in rows:
    if (row.wind >= 0 and row.wind <= 20):
        index = 5
    elif (row.wind > 20 and row.wind <= 70):
        index = 4
    elif (row.wind > 70 and row.wind <= 110):
        index = 3
    elif (row.wind > 110 and row.wind <=160):
        index = 2
    elif(row.wind > 160 and row.wind <=200):
        index = 1
    elif(row.wind > 200 and row.wind <=250):
        index = 2
    elif(row.wind > 250 and row.wind <=290):
        index = 3
    elif(row.wind > 290 and row.wind <=340):
        index = 4
    elif(row.wind > 340):
        index = 5
    rows.updateRow(row)   
del row
del rows    
    
method 2
fields = ['Index', 'wind']
with arcpy.da.UpdateCursor(fc,fields) as cursor:
    for row in cursor:
        #wind = row[1]
        #Index = row[0]

        if (row[1] >= 0 and row[1] <= 20):
            row[0] = 5
        elif (row[1] > 20 and row[1] <= 70):
            row[0] = 4
        elif (row[1] > 70 and row[1] <= 110):
            row[0] = 3
        elif (row[1] > 110 and row[1] <=160):
            row[0] = 2
        elif(row[1] > 160 and row[1] <=200):
            row[0] = 1
        elif(row[1] > 200 and row[1] <=250):
            row[0] = 2
        elif(row[1] > 250 and row[1] <=290):
            row[0] = 3
        elif(row[1] > 290 and row[1] <=340):
            row[0] = 4
        elif(row[1] > 340):
            row[0] = 5
    
        cursor.updateRow(row)'''
        
arcpy.AddMessage(arcpy.GetMessages())

method 3        
expressionReclass = "Reclass(!wind!)"
codeblock_wind ='''def Reclass(wind):
	if (wind >= 0 and wind <= 20):
		return 5
	elif (wind > 20 and wind <= 70):
		return 4
	elif (wind > 70 and wind <= 110):
		return 3
	elif (wind > 110 and wind <=160):
		return 2
	elif(wind > 160 and wind <=200):
		return 1
	elif(wind > 200 and wind <=250):
		return 2
	elif(wind > 250 and wind <=290):
		return 3
	elif(wind > 290 and wind <=340):
		return 4
	elif(wind > 340):
		return 5'''   
#cursor = arcpy.UpdateCursor(sel_lyr,'Index')

arcpy.CalculateField_management(stream + '_Buff', 'Index', expressionReclass, 
                                'PYTHON_9.3', codeblock_wind)
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Have you tried hard coding the parameters and then loading the script in the interactive Python window.  If it works there, then you know it has to do with running the script as a tool.

0 Kudos
jasongraham
New Contributor III

Yes, they all work.  I did an addmessage be for and after the function.  Before index is 0 and after it has a number but it does not seem to write to the table.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I am a bit confused.  I asked if you have run it in the interactive Python window.  Your response, "yes, they all work."  But, then you state "it does not seem to write to the table."  I will rephrase my question.  Do you get the exact same results if you run the script as a script tool vs running it in the interactive Python window?

0 Kudos
jasongraham
New Contributor III

Sorry, I will try to clarify.

In the version I used codeblocks, (method 3) I tested the code in the field calculator and that was successful.

In method two above I used addmessage to see the results in each stage and the output seems correct but no change in the feature class table.

The code at this point can not be test on the interactive withndow as it requires to get parameters from users.  If I put temp values in,  it works fine.   Off for holiday,  get back to it later. Thanks

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

If I put temp values in,  it works fine.

If you put temp values in and run it from the interactive Python window, as a script tool, or both?

0 Kudos
jasongraham
New Contributor III

as a script tool

0 Kudos
jasongraham
New Contributor III

When I use this to update a field:

fields = ['Index', 'wind']
arcpy.AddMessage(fc)

with arcpy.da.UpdateCursor(fc,fields) as cursor:
    for row in cursor:
        #wind = row[1]
        #Index = row[0]
        arcpy.AddMessage(row)
        if (row[1] >= 0 and row[1] <= 20):
            row[0] = 5
        elif (row[1] > 20 and row[1] <= 70):
            row[0] = 4
        elif (row[1] > 70 and row[1] <= 110):
            row[0] = 3
        elif (row[1] > 110 and row[1] <=160):
            row[0] = 2
        elif(row[1] > 160 and row[1] <=200):
            row[0] = 1
        elif(row[1] > 200 and row[1] <=250):
            row[0] = 2
        elif(row[1] > 250 and row[1] <=290):
            row[0] = 3
        elif(row[1] > 290 and row[1] <=340):
            row[0] = 4
        elif(row[1] > 340):
            row[0] = 5

        cursor.updateRow(row)
        arcpy.AddMessage(row)

and check the messages I get 

[None, 101]
[3, 101]
[None, 56]
[4, 56]
[None, 24]
[4, 24]
[None, 47]
[4, 47]
showing that Index has nothing and wind has data which is right

then the next add message has data in both. 

However the actual table never gets updated but the tool completes without error.

0 Kudos