<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: name is not defined for query in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/name-is-not-defined-for-query/m-p/353947#M27780</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Well, when you try to set the variable query to the string "\"%s\" = 'S'" % field.Name&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;the variable 'field' is not defined, and an undefined variable cannot have Name property.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;=&amp;gt; they are C****, such as C5738 and C9875. That's why I put a query here.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I also notice you add the NorthLat and SouthLat fields as double, and then try to pass them a string in the calculation step.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;=&amp;gt; Please kindly explain why it is better to put it in the "double "way &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;"gp.CalculateField_management("output.shp", "NorthLat",NorthLat)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Are you attempting to overwrite the fc with Select_analysis, or maybe append the data, or insert the update into the fc somehow? &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;=&amp;gt; I used select_analysis because I wanted to make a temporary dataset to make the code below work.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt; ext = gp.describe("output.shp").extent&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Making an in-memory layer file instead of a temporary shapefile would be a better approach. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;=&amp;gt; in-memory layer cannot work in the gp.describe.extent in ArcGIS 9.3&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Please kindly advise any modification&amp;nbsp; and thanks again.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 17 May 2012 10:43:29 GMT</pubDate>
    <dc:creator>ElaineKuo</dc:creator>
    <dc:date>2012-05-17T10:43:29Z</dc:date>
    <item>
      <title>name is not defined for query</title>
      <link>https://community.esri.com/t5/python-questions/name-is-not-defined-for-query/m-p/353945#M27778</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;System: ArcGIS 9.3&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Problem:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I tried to calculate the latitude of north and south limit of polygons specified as S in a shapefiles.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The specification of S is using query.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;However, an error jumped, &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;saying "query = "\"%s\" = 'S'" % field.Name&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;NameError: name 'field' is not defined".&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Please kindly advise modification and thank you.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;##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&amp;nbsp;&amp;nbsp; #Import standard library modules import arcgisscripting import os&amp;nbsp; #Create the Geoprocessor object gp = arcgisscripting.create(9.3)&amp;nbsp; #Set the input workspace #GP.workspace = sys.argv[1] #Set the workspace. gp.Workspace= "H:/temp/test1"&amp;nbsp; #Set the output workspace #outWorkspace = sys.argv[2] #Set the workspace. List all of the feature classes in the dataset outWorkspace= "H:/temp"&amp;nbsp;&amp;nbsp; #Get a list of the featureclasses in the input folder fcs = gp.ListFeatureClasses()&amp;nbsp;&amp;nbsp; # Loop through every item in the list that was just generated for fc in fcs:&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # Break out the name, no path or extension, using the describe object. &amp;nbsp;&amp;nbsp;&amp;nbsp; desc = gp.describe(fc) &amp;nbsp;&amp;nbsp;&amp;nbsp; featureName = desc.name&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # Add a field to this shapefile, of type LONG &amp;nbsp;&amp;nbsp;&amp;nbsp; gp.AddField (fc, "NorthLat", "double", 6,6) &amp;nbsp;&amp;nbsp;&amp;nbsp; gp.AddField (fc, "SouthLat", "double", 6,6)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # Make temporary featureclasses &amp;nbsp;&amp;nbsp;&amp;nbsp; query = "\"%s\" = 'S'" % field.Name &amp;nbsp;&amp;nbsp;&amp;nbsp; gp.Select_analysis(fc,"output.shp",query)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # get latitude of north and south limits &amp;nbsp;&amp;nbsp;&amp;nbsp; ext = gp.describe("output.shp").extent &amp;nbsp;&amp;nbsp;&amp;nbsp; NorthLat = ext.YMax &amp;nbsp;&amp;nbsp;&amp;nbsp; SouthLat = ext.YMin&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; gp.CalculateField_management("output.shp", "NorthLat",str(NorthLat)) &amp;nbsp;&amp;nbsp;&amp;nbsp; gp.CalculateField_management("output.shp", "SouthLat",str(SouthLat))&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # copy temporary shapefile to fc &amp;nbsp;&amp;nbsp;&amp;nbsp; gp.Select_analysis("output.shp",fc) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # clear memory of temporary shapefile &amp;nbsp;&amp;nbsp;&amp;nbsp; gp.Delete("output.shp")&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.AddMessage(gp.GetMessages()) print gp.GetMessages()&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 15 May 2012 21:52:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/name-is-not-defined-for-query/m-p/353945#M27778</guid>
      <dc:creator>ElaineKuo</dc:creator>
      <dc:date>2012-05-15T21:52:06Z</dc:date>
    </item>
    <item>
      <title>Re: name is not defined for query</title>
      <link>https://community.esri.com/t5/python-questions/name-is-not-defined-for-query/m-p/353946#M27779</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Well, when you try to set the variable query to the string "\"%s\" = 'S'" % field.Name&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;the variable 'field' is not defined, and an undefined variable cannot have Name property.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I also notice you add the NorthLat and SouthLat fields as double, and then try to pass them a string in the calculation step.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Are you attempting to overwrite the fc with Select_analysis, or maybe append the data, or insert the update into the fc somehow? &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Whichever way, I really don't think that will work.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 16 May 2012 12:47:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/name-is-not-defined-for-query/m-p/353946#M27779</guid>
      <dc:creator>markdenil</dc:creator>
      <dc:date>2012-05-16T12:47:12Z</dc:date>
    </item>
    <item>
      <title>Re: name is not defined for query</title>
      <link>https://community.esri.com/t5/python-questions/name-is-not-defined-for-query/m-p/353947#M27780</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Well, when you try to set the variable query to the string "\"%s\" = 'S'" % field.Name&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;the variable 'field' is not defined, and an undefined variable cannot have Name property.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;=&amp;gt; they are C****, such as C5738 and C9875. That's why I put a query here.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I also notice you add the NorthLat and SouthLat fields as double, and then try to pass them a string in the calculation step.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;=&amp;gt; Please kindly explain why it is better to put it in the "double "way &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;"gp.CalculateField_management("output.shp", "NorthLat",NorthLat)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Are you attempting to overwrite the fc with Select_analysis, or maybe append the data, or insert the update into the fc somehow? &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;=&amp;gt; I used select_analysis because I wanted to make a temporary dataset to make the code below work.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt; ext = gp.describe("output.shp").extent&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Making an in-memory layer file instead of a temporary shapefile would be a better approach. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;=&amp;gt; in-memory layer cannot work in the gp.describe.extent in ArcGIS 9.3&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Please kindly advise any modification&amp;nbsp; and thanks again.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 17 May 2012 10:43:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/name-is-not-defined-for-query/m-p/353947#M27780</guid>
      <dc:creator>ElaineKuo</dc:creator>
      <dc:date>2012-05-17T10:43:29Z</dc:date>
    </item>
    <item>
      <title>Re: name is not defined for query</title>
      <link>https://community.esri.com/t5/python-questions/name-is-not-defined-for-query/m-p/353948#M27781</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;This line is:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;query = "\"%s\" = 'S'" % field.Name&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;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:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;query = "\"C5738\" = 'S'"&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;But there is a question about your logic as you give &lt;/SPAN&gt;&lt;STRONG&gt;TWO&lt;/STRONG&gt;&lt;SPAN&gt; 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:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;query = "\"C5738\" = 'S' OR \"C9875\" = 'S'"&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Duncan&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 17 May 2012 15:01:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/name-is-not-defined-for-query/m-p/353948#M27781</guid>
      <dc:creator>DuncanHornby</dc:creator>
      <dc:date>2012-05-17T15:01:01Z</dc:date>
    </item>
    <item>
      <title>Re: name is not defined for query</title>
      <link>https://community.esri.com/t5/python-questions/name-is-not-defined-for-query/m-p/353949#M27782</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for the response.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;However, there are 500 shapefiels.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Each of them has a name starting C.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The whole name is C****.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The corresponding field.name is C****, for example, C7839 (field name) for C7839 shapefile.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The purpose is to select the "S" of each field name of each shapefile.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;But there is a question about your logic as you give &lt;/SPAN&gt;&lt;STRONG&gt;TWO&lt;/STRONG&gt;&lt;SPAN&gt; 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:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;query = "\"C5738\" = 'S' OR \"C9875\" = 'S'"&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Duncan&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 17 May 2012 20:47:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/name-is-not-defined-for-query/m-p/353949#M27782</guid>
      <dc:creator>ElaineKuo</dc:creator>
      <dc:date>2012-05-17T20:47:11Z</dc:date>
    </item>
    <item>
      <title>Re: name is not defined for query</title>
      <link>https://community.esri.com/t5/python-questions/name-is-not-defined-for-query/m-p/353950#M27783</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;If you have a field that has the same name as the shapefile then you could alter the query to this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;desc = gp.describe(fc)&amp;nbsp; 
 query = "\"%s\" = 'S'" % desc.baseName&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So if your Shapefile is called C1234.shp then the query string will end up being &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;query = "C1234" = 'S'&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Duncan&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 16:35:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/name-is-not-defined-for-query/m-p/353950#M27783</guid>
      <dc:creator>DuncanHornby</dc:creator>
      <dc:date>2021-12-11T16:35:43Z</dc:date>
    </item>
    <item>
      <title>Re: name is not defined for query</title>
      <link>https://community.esri.com/t5/python-questions/name-is-not-defined-for-query/m-p/353951#M27784</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Duncan explains what I had tried to ask about above: that you do indeed know the field name, it is the file name.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;My second observation concerned the the field types of the added fields.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;You add the NorthLat field as double precision:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;gp.AddField (fc, "NorthLat", "double", 6,6)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;SPAN&gt;and you get a number to put in that field:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;NorthLat = ext.YMax&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;SPAN&gt;but when you calculate the field value, you change the input number into a string:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;gp.CalculateField_management("output.shp", "NorthLat",str(NorthLat))&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;SPAN&gt;str(NorthLat) makes a string out of your number, and you cannot put a string into a numeric field.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;My third observation, about Select_analysis, refered to your SECOND use of Select_analysis:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;# copy temporary shapefile to fc &amp;nbsp;&amp;nbsp;&amp;nbsp; gp.Select_analysis("output.shp",fc)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;SPAN&gt;in this case, the output fc already exists.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 18 May 2012 16:43:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/name-is-not-defined-for-query/m-p/353951#M27784</guid>
      <dc:creator>markdenil</dc:creator>
      <dc:date>2012-05-18T16:43:22Z</dc:date>
    </item>
  </channel>
</rss>

