Select to view content in your preferred language

Calculate Field problem Please Help

1219
5
04-04-2013 09:06 AM
ScottMacDonald
Deactivated User
Hi,

I've written a script that narrows down a selection of points (cities) and then adds a field called CITYNAME to the feature class.  I need to update the new CITYNAME field with the same values as the NAME field in the same feature class but it doesn't update them although it shows as having run successfully.

I need to do this so that I can spatially join this points (cities) dataset to polygons (counties) and lines (roads) because all three of them have the same fieldname called NAME. 

I'm using ArcGIS v10.0 and any help would be appreciated as I've tried various methods like CalculateField and UpdateCursor to no avail.

Thanks,

Scotaidh

# ExtractFeaturesByLocationAndAttribute.py EFLA.py
# Description: Extract features to a new feature class based on a Location and an attribute query
# then add field called CITYNAME and update it with the NAME field from the cities layer

# import system modules
import arcpy
from arcpy import env
import os

# set overwrite option
arcpy.env.overwriteOutput = True

# Put in error trapping in case an error occurs when running tool
try:
    # Make a layer from the cities feature class
    arcpy.MakeFeatureLayer_management("H:/working/Findsites.gdb/cities","citiesL")

    # Select all cities that are within 30km of an interstate
    arcpy.SelectLayerByLocation_management("citiesL", "WITHIN_A_DISTANCE", "H:/working/Findsites.gdb/interstates", "30 KILOMETERS", "NEW_SELECTION")

    # Within the selection (done above) further select only those cities that have a university and low crime rate
    arcpy.SelectLayerByAttribute_management("citiesL", "SUBSET_SELECTION", "UNIVERSITY = 1 AND CRIME_INDE <= 0.02")
    
    # Make a layer from the counties feature class
    arcpy.MakeFeatureLayer_management("H://working/Findsites.gdb/counties","countiesL")
    
    # From the counties layer select only those counties with a large enough workforce and number of farms
    arcpy.SelectLayerByAttribute_management("countiesL", "NEW_SELECTION", "AGE_18_64 >= 25000 AND NO_FARMS87 >= 500")

    # Select all cities that intersect counties that meet work force and number of farms criteria
    arcpy.SelectLayerByLocation_management("citiesL", "INTERSECT", "countiesL", "", "SUBSET_SELECTION")
    
    # Write the selected features to a new featureclass
    arcpy.CopyFeatures_management("citiesL", "H:/working/Findsites.gdb/CityList")
    
    # create a new field called CITYNAME to replace NAME because other datasets have a NAME field
    arcpy.AddField_management("H:/working/Findsites.gdb/CityList", "CITYNAME", "TEXT", "", "", "25")
    
    expression1 = '!MP_VAL!'
    arcpy.CalculateField_management(CityList, CITYNAME, expression1, "PYTHON", "")
    
    #Calculate Field - update new CITYNAME field with NAME field
    # arcpy.AddMessage("Calculating Updated Field...")
    # Expression = "'{0}'".format(arcpy.GetValue(NAME))
    # single quoted string literal
    # arcpy.CalculateField_management(CityList, CITYNAME, Expression, "PYTHON")
    
except:
    # If an error occurred print the message to the screen
    print arcpy.GetMessages()
Tags (2)
0 Kudos
5 Replies
RobertBorchert
Honored Contributor
Do you have to do it with a script?

Just click on the new column and use the field calculator.  Take a few seconds.  Also depending on your environment it may not update them outside of an edit session.

Hi,

I've written a script that narrows down a selection of points (cities) and then adds a field called CITYNAME to the feature class.  I need to update the new CITYNAME field with the same values as the NAME field in the same feature class but it doesn't update them although it shows as having run successfully.

I need to do this so that I can spatially join this points (cities) dataset to polygons (counties) and lines (roads) because all three of them have the same fieldname called NAME. 

I'm using ArcGIS v10.0 and any help would be appreciated as I've tried various methods like CalculateField and UpdateCursor to no avail.

Thanks,

Scotaidh

# ExtractFeaturesByLocationAndAttribute.py EFLA.py
# Description: Extract features to a new feature class based on a Location and an attribute query
# then add field called CITYNAME and update it with the NAME field from the cities layer

# import system modules
import arcpy
from arcpy import env
import os

# set overwrite option
arcpy.env.overwriteOutput = True

# Put in error trapping in case an error occurs when running tool
try:
    # Make a layer from the cities feature class
    arcpy.MakeFeatureLayer_management("H:/working/Findsites.gdb/cities","citiesL")

    # Select all cities that are within 30km of an interstate
    arcpy.SelectLayerByLocation_management("citiesL", "WITHIN_A_DISTANCE", "H:/working/Findsites.gdb/interstates", "30 KILOMETERS", "NEW_SELECTION")

    # Within the selection (done above) further select only those cities that have a university and low crime rate
    arcpy.SelectLayerByAttribute_management("citiesL", "SUBSET_SELECTION", "UNIVERSITY = 1 AND CRIME_INDE <= 0.02")
    
    # Make a layer from the counties feature class
    arcpy.MakeFeatureLayer_management("H://working/Findsites.gdb/counties","countiesL")
    
    # From the counties layer select only those counties with a large enough workforce and number of farms
    arcpy.SelectLayerByAttribute_management("countiesL", "NEW_SELECTION", "AGE_18_64 >= 25000 AND NO_FARMS87 >= 500")

    # Select all cities that intersect counties that meet work force and number of farms criteria
    arcpy.SelectLayerByLocation_management("citiesL", "INTERSECT", "countiesL", "", "SUBSET_SELECTION")
    
    # Write the selected features to a new featureclass
    arcpy.CopyFeatures_management("citiesL", "H:/working/Findsites.gdb/CityList")
    
    # create a new field called CITYNAME to replace NAME because other datasets have a NAME field
    arcpy.AddField_management("H:/working/Findsites.gdb/CityList", "CITYNAME", "TEXT", "", "", "25")
    
    expression1 = '!MP_VAL!'
    arcpy.CalculateField_management(CityList, CITYNAME, expression1, "PYTHON", "")
    
    #Calculate Field - update new CITYNAME field with NAME field
    # arcpy.AddMessage("Calculating Updated Field...")
    # Expression = "'{0}'".format(arcpy.GetValue(NAME))
    # single quoted string literal
    # arcpy.CalculateField_management(CityList, CITYNAME, Expression, "PYTHON")
    
except:
    # If an error occurred print the message to the screen
    print arcpy.GetMessages()
0 Kudos
ChrisSnyder
Honored Contributor
You need to put quotes around "CITYNAME" since it is text and not a variable. So instead of:

arcpy.CalculateField_management(CityList, CITYNAME, expression1, "PYTHON", "")

it needs to be:

arcpy.CalculateField_management(CityList, "CITYNAME", expression1, "PYTHON", "")
0 Kudos
ScottMacDonald
Deactivated User
Thanks Chris for your quick reply. I changed CityList to "CityList" (code below) but it still doesn't update the new CITYNAME field with the NAME field.

Do you perhaps have any other suggestions?
# ExtractFeaturesByLocationAndAttribute.py EFLA.py
# Description: Extract features to a new feature class based on a Location and an attribute query
# then add field called CITYNAME and update it with the NAME field from the cities layer

# import system modules
import arcpy
from arcpy import env
import os

# set overwrite option
arcpy.env.overwriteOutput = True

# Put in error trapping in case an error occurs when running tool
try:
    # Make a layer from the cities feature class
    arcpy.MakeFeatureLayer_management("H:/working/Findsites.gdb/cities","citiesL")

    # Select all cities that are within 30km of an interstate
    arcpy.SelectLayerByLocation_management("citiesL", "WITHIN_A_DISTANCE", "H:/working/Findsites.gdb/interstates", "30 KILOMETERS", "NEW_SELECTION")

    # Within the selection (done above) further select only those cities that have a university and low crime rate
    arcpy.SelectLayerByAttribute_management("citiesL", "SUBSET_SELECTION", "UNIVERSITY = 1 AND CRIME_INDE <= 0.02")
    
    # Make a layer from the counties feature class
    arcpy.MakeFeatureLayer_management("H://working/Findsites.gdb/counties","countiesL")
    
    # From the counties layer select only those counties with a large enough workforce and number of farms
    arcpy.SelectLayerByAttribute_management("countiesL", "NEW_SELECTION", "AGE_18_64 >= 25000 AND NO_FARMS87 >= 500")

    # Select all cities that intersect counties that meet work force and number of farms criteria
    arcpy.SelectLayerByLocation_management("citiesL", "INTERSECT", "countiesL", "", "SUBSET_SELECTION")
    
    # Write the selected features to a new featureclass
    arcpy.CopyFeatures_management("citiesL", "H:/working/Findsites.gdb/CityList")
    
    # create a new field called CITYNAME to replace NAME because other datasets have a NAME field
    arcpy.AddField_management("H:/working/Findsites.gdb/CityList", "CITYNAME", "TEXT", "", "", "25")
    
    expression1 = '!NAME!'
    arcpy.CalculateField_management(CityList, "CITYNAME", expression1, "PYTHON") 
        
except:
    # If an error occurred print the message to the screen
    print arcpy.GetMessages()
0 Kudos
ScottMacDonald
Deactivated User
Thanks Chris for your quick reply.
I put quotes around "CITYNAME" but the field still does not update with the NAME field.
The edited code is shown below and I wondered if you had any other ideas?
Thanks again
Scott

# ExtractFeaturesByLocationAndAttribute.py EFLA.py
# Description: Extract features to a new feature class based on a Location and an attribute query
# then add field called CITYNAME and update it with the NAME field from the cities layer

# import system modules
import arcpy
from arcpy import env
import os

# set overwrite option
arcpy.env.overwriteOutput = True

# Put in error trapping in case an error occurs when running tool
try:
    # Make a layer from the cities feature class
    arcpy.MakeFeatureLayer_management("H:/working/Findsites.gdb/cities","citiesL")

    # Select all cities that are within 30km of an interstate
    arcpy.SelectLayerByLocation_management("citiesL", "WITHIN_A_DISTANCE", "H:/working/Findsites.gdb/interstates", "30 KILOMETERS", "NEW_SELECTION")

    # Within the selection (done above) further select only those cities that have a university and low crime rate
    arcpy.SelectLayerByAttribute_management("citiesL", "SUBSET_SELECTION", "UNIVERSITY = 1 AND CRIME_INDE <= 0.02")
    
    # Make a layer from the counties feature class
    arcpy.MakeFeatureLayer_management("H://working/Findsites.gdb/counties","countiesL")
    
    # From the counties layer select only those counties with a large enough workforce and number of farms
    arcpy.SelectLayerByAttribute_management("countiesL", "NEW_SELECTION", "AGE_18_64 >= 25000 AND NO_FARMS87 >= 500")

    # Select all cities that intersect counties that meet work force and number of farms criteria
    arcpy.SelectLayerByLocation_management("citiesL", "INTERSECT", "countiesL", "", "SUBSET_SELECTION")
    
    # Write the selected features to a new featureclass
    arcpy.CopyFeatures_management("citiesL", "H:/working/Findsites.gdb/CityList")
    
    # create a new field called CITYNAME to replace NAME because other datasets have a NAME field
    arcpy.AddField_management("H:/working/Findsites.gdb/CityList", "CITYNAME", "TEXT", "", "", "25")
    
    expression1 = '!NAME!'
    arcpy.CalculateField_management(CityList, "CITYNAME", expression1, "PYTHON") 
        
except:
    # If an error occurred print the message to the screen
    print arcpy.GetMessages()
0 Kudos
ChrisSnyder
Honored Contributor
Your script has some issues with strings vs. variables and also how you are defining paths. For example:

1. In Python, cityList is not the same thing as 'cityList'.... the 1st referes to a variable since it isn't in quotes, but the second is a string object since it is in quotes

2. Python is picky how you define paths. All of these are okay:

path = r"H:\here\it\is"
path = "H:\\here\\it\is"
path = "H:/here/it/is"

This is not okay though:
path = "H://here//it//is"


I took the liberty to rewrite your code. Notice how the FCs are defined as variables up near the top, and they use a consistent path delimiter method. This code "should" work:

import os, sys, traceback. arcpy
arcpy.env.overwriteOutput = True
try:
    citiesFC = r"H:\working\Findsites.gdb\cities"
    interstatesFC = r"H:\working\Findsites.gdb\interstates"
    countiesFC = r"H:\working\Findsites.gdb\counties"
    cityListFC = r"H:\working\Findsites.gdb\city_list"
    arcpy.MakeFeatureLayer_management(citiesFC,"citiesL")
    arcpy.SelectLayerByLocation_management("citiesL", "WITHIN_A_DISTANCE", interstatesFC, "30 KILOMETERS", "NEW_SELECTION")
    arcpy.SelectLayerByAttribute_management("citiesL", "SUBSET_SELECTION", "UNIVERSITY = 1 AND CRIME_INDE <= 0.02")
    arcpy.MakeFeatureLayer_management(countiesFC,"countiesL")
    arcpy.SelectLayerByAttribute_management("countiesL", "NEW_SELECTION", "AGE_18_64 >= 25000 AND NO_FARMS87 >= 500")
    arcpy.SelectLayerByLocation_management("citiesL", "INTERSECT", "countiesL", "", "SUBSET_SELECTION")
    arcpy.CopyFeatures_management("citiesL", cityListFC)
    arcpy.AddField_management(cityListFC, "CITYNAME", "TEXT", "", "", "25")
    arcpy.CalculateField_management(cityListFC, "CITYNAME", "!NAME!", "PYTHON")  
except:
    print "\n*** LAST GEOPROCESSOR MESSAGE (may not be source of the error)***"; print arcpy.GetMessages()
    print "Python Traceback Info: " + traceback.format_tb(sys.exc_info()[2])[0]
    print "Python Error Info: " +  str(sys.exc_type)+ ": " + str(sys.exc_value)
0 Kudos