name is not defined for query

1981
6
Jump to solution
05-15-2012 02:52 PM
ElaineKuo
Occasional Contributor
System: ArcGIS 9.3

Problem:

I tried to calculate the latitude of north and south limit of polygons specified as S in a shapefiles.
The specification of S is using query.
However, an error jumped,
saying "query = "\"%s\" = 'S'" % field.Name
NameError: name 'field' is not defined".

Please kindly advise modification and thank you.
##Script Name: latitude of north or south limit ##Description: Extract latitude of north and south boundary of B or NB range size ##Created By: Elaine Kuo ##Date: 12/05/2012   #Import standard library modules import arcgisscripting import os  #Create the Geoprocessor object gp = arcgisscripting.create(9.3)  #Set the input workspace #GP.workspace = sys.argv[1] #Set the workspace. gp.Workspace= "H:/temp/test1"  #Set the output workspace #outWorkspace = sys.argv[2] #Set the workspace. List all of the feature classes in the dataset outWorkspace= "H:/temp"   #Get a list of the featureclasses in the input folder fcs = gp.ListFeatureClasses()   # Loop through every item in the list that was just generated for fc in fcs:      # Break out the name, no path or extension, using the describe object.     desc = gp.describe(fc)     featureName = desc.name      # Add a field to this shapefile, of type LONG     gp.AddField (fc, "NorthLat", "double", 6,6)     gp.AddField (fc, "SouthLat", "double", 6,6)          # Make temporary featureclasses     query = "\"%s\" = 'S'" % field.Name     gp.Select_analysis(fc,"output.shp",query)      # get latitude of north and south limits     ext = gp.describe("output.shp").extent     NorthLat = ext.YMax     SouthLat = ext.YMin             gp.CalculateField_management("output.shp", "NorthLat",str(NorthLat))     gp.CalculateField_management("output.shp", "SouthLat",str(SouthLat))      # copy temporary shapefile to fc     gp.Select_analysis("output.shp",fc)          # clear memory of temporary shapefile     gp.Delete("output.shp")      gp.AddMessage(gp.GetMessages()) print gp.GetMessages()
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
markdenil
Occasional Contributor III
Duncan explains what I had tried to ask about above: that you do indeed know the field name, it is the file name.

My second observation concerned the the field types of the added fields.
You add the NorthLat field as double precision:
gp.AddField (fc, "NorthLat", "double", 6,6)

and you get a number to put in that field:
NorthLat = ext.YMax

but when you calculate the field value, you change the input number into a string:     
gp.CalculateField_management("output.shp", "NorthLat",str(NorthLat))

str(NorthLat) makes a string out of your number, and you cannot put a string into a numeric field.

My third observation, about Select_analysis, refered to your SECOND use of Select_analysis:
# copy temporary shapefile to fc     gp.Select_analysis("output.shp",fc)

in this case, the output fc already exists.

View solution in original post

0 Kudos
6 Replies
markdenil
Occasional Contributor III
Well, when you try to set the variable query to the string "\"%s\" = 'S'" % field.Name
the variable 'field' is not defined, and an undefined variable cannot have Name property.

You know a lot about the shapefiles you are processing, do you not know the field name of the field with the 'S' in it?

I also notice you add the NorthLat and SouthLat fields as double, and then try to pass them a string in the calculation step.

Are you attempting to overwrite the fc with Select_analysis, or maybe append the data, or insert the update into the fc somehow?
Whichever way, I really don't think that will work.
Making an in-memory layer file instead of a temporary shapefile would be a better approach. Layer files are just views of the base data: changing the record in the layer file will be changing it in the base fc.
0 Kudos
ElaineKuo
Occasional Contributor
Well, when you try to set the variable query to the string "\"%s\" = 'S'" % field.Name
the variable 'field' is not defined, and an undefined variable cannot have Name property.

You know a lot about the shapefiles you are processing, do you not know the field name of the field with the 'S' in it?
=> they are C****, such as C5738 and C9875. That's why I put a query here.

I also notice you add the NorthLat and SouthLat fields as double, and then try to pass them a string in the calculation step.
=> Please kindly explain why it is better to put it in the "double "way

"gp.CalculateField_management("output.shp", "NorthLat",NorthLat)


Are you attempting to overwrite the fc with Select_analysis, or maybe append the data, or insert the update into the fc somehow?
=> I used select_analysis because I wanted to make a temporary dataset to make the code below work.
 ext = gp.describe("output.shp").extent


Making an in-memory layer file instead of a temporary shapefile would be a better approach.
=> in-memory layer cannot work in the gp.describe.extent in ArcGIS 9.3
Please kindly advise any modification  and thanks again.
0 Kudos
DuncanHornby
MVP Notable Contributor
This line is:

query = "\"%s\" = 'S'" % field.Name


is failing as you do not declare what field is. But in your reponse you say the field names are C5738 and C9875. If you know them then your query could be simplified to:

query = "\"C5738\" = 'S'"


But there is a question about your logic as you give TWO field names, which one do you want to process or do you want to select all records where both of those fields equal S? If so the query statement should be:

query = "\"C5738\" = 'S' OR \"C9875\" = 'S'"


Duncan
0 Kudos
ElaineKuo
Occasional Contributor
Thanks for the response.
However, there are 500 shapefiels.
Each of them has a name starting C.
The whole name is C****.
The corresponding field.name is C****, for example, C7839 (field name) for C7839 shapefile.
The purpose is to select the "S" of each field name of each shapefile.


But there is a question about your logic as you give TWO field names, which one do you want to process or do you want to select all records where both of those fields equal S? If so the query statement should be:

query = "\"C5738\" = 'S' OR \"C9875\" = 'S'"


Duncan
0 Kudos
DuncanHornby
MVP Notable Contributor
If you have a field that has the same name as the shapefile then you could alter the query to this:

desc = gp.describe(fc)  
 query = "\"%s\" = 'S'" % desc.baseName


So if your Shapefile is called C1234.shp then the query string will end up being query = "C1234" = 'S'

Duncan
0 Kudos
markdenil
Occasional Contributor III
Duncan explains what I had tried to ask about above: that you do indeed know the field name, it is the file name.

My second observation concerned the the field types of the added fields.
You add the NorthLat field as double precision:
gp.AddField (fc, "NorthLat", "double", 6,6)

and you get a number to put in that field:
NorthLat = ext.YMax

but when you calculate the field value, you change the input number into a string:     
gp.CalculateField_management("output.shp", "NorthLat",str(NorthLat))

str(NorthLat) makes a string out of your number, and you cannot put a string into a numeric field.

My third observation, about Select_analysis, refered to your SECOND use of Select_analysis:
# copy temporary shapefile to fc     gp.Select_analysis("output.shp",fc)

in this case, the output fc already exists.
0 Kudos