Select to view content in your preferred language

Update a single attribute field of manually selected features (not sel by location)

1418
9
06-07-2011 09:46 AM
ThomasBooth
Deactivated User
Hello,
     I am trying to write a python script that will populate an attribute field of a selected feature.  I see that it is possible with select features by location by..

Use makefeaturelayer.
Feed the feature layer into the selectbylocation.
then use calculate field.

However I am selecting one point feature at a time and I am only trying to update one field with my script..

I thought the right tool was the script below but it updates every feature in that field instead of the selected feature. (Manhole is the feature class, Elevation is the field name, x is the sum of a mathematical function from another part of the code.)

gp.CalculateField_management("Manhole","Elevation", "x")

Any help will be appreciated.

Thanks,
Tom
Tags (2)
0 Kudos
9 Replies
RDHarles
Regular Contributor
You have it right, just replace SelectLayerByLocation with SelectLayerByAttribute, similar to this:

print "Creating lyr.. "
gp.MakeFeatureLayer_management("az019s.shp", "lyr")
print "Selecting..."    
gp.SelectLayerByAttribute_management("lyr", "New_Selection", "Func_class" = -1)
print "Calculating..."
gp.CalculateField_management("lyr", "FROMLEFT", 0)
0 Kudos
ThomasBooth
Deactivated User
Here is what I have added.  I see what you are getting me to do but in the I am getting the error in select by attributes.  I intend to select a single point feature, run this script by entering an elevation and click enter and have this script do the math and update the answers into the attribute table.  It should only update one field.  I think in the selectlayerbyattribute there is no option for continue with current selection as I dont want to create a new selection I just want to update a field from a feature that I have already selected.


import arcgisscripting, sys
gp = arcgisscripting.create(9.3)

print "Creating lyr.. "
gp.MakeFeatureLayer_management("stations.shp", "lyr")
print "Selecting..."   
gp.SelectLayerByAttribute_management("lyr", "New_Selection","ANN_PREC")
print "Calculating..."
gp.CalculateField_management("stations.shp","ANN_PREC", x)
#print "Manhole elevation calculated..."
#gp.CalculateField_management("Manhole","InteriorDrop", y)


thanks for your help!
Tom
0 Kudos
LoganPugh
Frequent Contributor
If you are running this as an in-process script tool from within ArcMap, pass in the name of the feature layer that has the selection to CalculateField_management and it will honor that selection. No need for Make Feature Layer or Select by Attributes/Location in this case.

This won't work if you are running the script standalone, outside of ArcMap.
0 Kudos
ThomasBooth
Deactivated User
Thanks for the idea,
I tried this first thinking it woukd recognize the skeceted feature but it didnt, it just updated every feature in the specified field inside the feature class... It did not recognize the seleceted feature.. I tried this on a dummy shapefile and not on my actuak database bc i dont want to risk compromising the data... Any other options or ideas??

Thanks!
0 Kudos
LoganPugh
Frequent Contributor
It should work, post the code you are using. Also, did you create a script tool in ArcToolbox for it? As I mentioned it won't recognize feature layers in ArcMap if you run the script standalone.
0 Kudos
ThomasBooth
Deactivated User
Hey Logan,
    This was the demo tool I used to see if it would update a single field within an attribute table.... the result was all the features in the specified field had the number 23.......


Sorry I will give you my whole code when I return


import arcgisscripting, sys
gp = arcgisscripting.create(9.3)


#ManholeElevation = sys.argv[1]

gp.workspace = "Q:/63_Projects/State/SC/SC_DNR_2010/Hampton/ANALYSIS/DATA/SHP/LiDAR_QA"


#x = (ManholeElevation - 1.7)

gp.CalculateField_management("05_11_2011_3.shp","Altitude", "23")
   

    # If an error occurred while running a tool, then print the messages
    #print gp.GetMessages()
0 Kudos
ThomasBooth
Deactivated User
Here is my whole code with comments on each line

import arcgisscripting, sys
gp = arcgisscripting.create(9.3)

##a = input ("Enter Manhole Elevation: ")
##c = (a - 1.7)
##b = input ("Enter Inv Out: ")
##z = (a - b)
##print "Manhole Elevation is: " ,c
##print "Interior Drop is: " ,z

ManholeElevation = sys.argv[1]
InvertOut = sys.argv[2]
gp.workspace =  #this is not complete b/c it will be a file GDB (I need the server name  etc)
try:
  
    x = (ManholeElevation - 1.7)      #math function
    y = (ManholeElevation - InvertOut)  #math function
    print "Creating lyr.. "  
    gp.MakeFeatureLayer_management("manhole", "lyr")     #create temp lyr from manhole fetaure class with selected features
    print "Selecting..."   
    gp.SelectLayerByAttribute_management("lyr", "New_Selection", "Elevation")   #select the feature in that temporary layer???
    print "Calculating..."
    gp.CalculateField_management("manhole","Elevation", x)    # in the manhole feature class, under the field name Elevation populate the selected feature with the sum of variable x
    "Manhole elevation calculated..."
#gp.CalculateField_management("Manhole","InteriorDrop", y) # in the manhole feature class, under the Elevation field populate the sum of variable y .
  
except:
    #If an error occurred while running a tool, then print the messages
     print gp.GetMessages()

any help will be appreciated, thanks.
0 Kudos
RDHarles
Regular Contributor
Thomas,

Are you updating every RECORD in the table?  In other words, is there any reason to do manual selections in ArcMap?
If you're updating every row, this could quite easily be done running a stand-alone script using a cursor that runs down through the table.

If you need to do manual selections inside ArcMap, then that's outside of my expertise.
0 Kudos
ThomasBooth
Deactivated User
Yes, I need to do an manual selection inside arcmap and only populate one field of that selected feature, not of all the features in that one field.

Thank you for your help though.
0 Kudos