<?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: Updating field based on location in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/updating-field-based-on-location/m-p/331710#M25789</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Emil,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When using the 'Select Layer by Attribute' or 'Select Layer by Location' functions, the input needs to a be a layer.&amp;nbsp; Use the MakeFeatureLayer_management function to do this.&amp;nbsp; Ex:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
arcpy.MakeFeatureLayer_management("CityGrids.shp", "cityGridsLyr")
arcpy.SelectLayerByLocation_management("cityGridsLyr", "INTERSECT", "thecity", "", "NEW_SELECTION")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 15:42:23 GMT</pubDate>
    <dc:creator>JakeSkinner</dc:creator>
    <dc:date>2021-12-11T15:42:23Z</dc:date>
    <item>
      <title>Updating field based on location</title>
      <link>https://community.esri.com/t5/python-questions/updating-field-based-on-location/m-p/331707#M25786</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have the following information:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;A table with a list of cities&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;A cities layer&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;A grid layer&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am trying to write a code that goes through the list of cities and searches for each grid that intersects each city in the list. So if I have a city of 'somewhereville' python will find all the grids that intersect the 'somewhereville' feature and then updates a field in the grid's attribute with the city name ('somewhereville'). I have tried a few ways but I can't quite figure it out. Any direction would be great.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 15 Nov 2013 16:36:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/updating-field-based-on-location/m-p/331707#M25786</guid>
      <dc:creator>Emilbrundage</dc:creator>
      <dc:date>2013-11-15T16:36:34Z</dc:date>
    </item>
    <item>
      <title>Re: Updating field based on location</title>
      <link>https://community.esri.com/t5/python-questions/updating-field-based-on-location/m-p/331708#M25787</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I have the following information:&lt;BR /&gt;&lt;BR /&gt;A table with a list of cities&lt;BR /&gt;A cities layer&lt;BR /&gt;A grid layer&lt;BR /&gt;&lt;BR /&gt;I am trying to write a code that goes through the list of cities and searches for each grid that intersects each city in the list. So if I have a city of 'somewhereville' python will find all the grids that intersect the 'somewhereville' feature and then updates a field in the grid's attribute with the city name ('somewhereville'). I have tried a few ways but I can't quite figure it out. Any direction would be great.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;You can definitely accomplish this with a combination of &lt;/SPAN&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//018w00000011000000"&gt;search cursors&lt;/A&gt;&lt;SPAN&gt;, careful use of &lt;/SPAN&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//00170000006p000000"&gt;make feature layer&lt;/A&gt;&lt;SPAN&gt;, selecting by &lt;/SPAN&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//018w00000011000000"&gt;location&lt;/A&gt;&lt;SPAN&gt;, and the &lt;/SPAN&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//00170000004m000000"&gt;field calculator.&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is a suggested workflow:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;1. Start with a Search Cursor and loop over your table of cities.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2. For every row in the table, make a feature layer out of your cities layer. For the where clause, set it so that your City Name field is equal to the current city in the table.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;3. Use select by location to select all of the grids that intersect your city.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;4. Use the field calculator to update the city name field in your grid to be equal to the current city (again this is for the current row in your table).&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;5. Clear the selection - This is very important. You do this using Select by Attribute with the option 'CLEAR_SELECTION'&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;6. Delete the feature layer using arcpy.Delete_management('name of layer')&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Essentially, steps 2-6 will take place inside of the for loop as you iterate over every row in your cities table.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hope this helps!&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;- Doug&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 15 Nov 2013 16:58:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/updating-field-based-on-location/m-p/331708#M25787</guid>
      <dc:creator>DouglasSands</dc:creator>
      <dc:date>2013-11-15T16:58:58Z</dc:date>
    </item>
    <item>
      <title>Re: Updating field based on location</title>
      <link>https://community.esri.com/t5/python-questions/updating-field-based-on-location/m-p/331709#M25788</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks Doug! Your advice has been very helpful. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Now I'm stuck on one line that doesn't want to work on me. My code breaks when I try to select layer by location. Here is the code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
from arcpy import env


#arcpy.AddMessage ("1")
arcpy.env.workspace = "C:\E1B8\Grid_Create_Update_11-2013"

#arcpy.AddMessage ("2")

# Create the search cursor
#

tablecur = arcpy.SearchCursor("SampleCityTable")
for row in tablecur:
&amp;nbsp;&amp;nbsp;&amp;nbsp; cityname = row.City
&amp;nbsp;&amp;nbsp;&amp;nbsp; sql = "\"'NAME10' = " + cityname + '"'
#&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage (sql)
#&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage (cityname)

# Make new feature for current city
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management("state_places.shp","thecity",sql)
#&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage ("3")

# Select grid that intersect with new feature
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management("CityGrids.shp", "INTERSECT", "thecity", "", "NEW_SELECTION")
#&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage ("4")

# Update field in grid layer with city name 
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management("CityGrids.shp", "CITY_GRID", cityname, "PYTHON", "")

# Remove selection from grid
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management("CityGrids.shp", "CLEAR_SELECTION", "")

# Delete newly made city layer
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management("thecity", "")
#&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage ("5")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Again, any help is very much appreciated!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Edit: To be more specific, the error I get is:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;ERROR 000368: Invalid input data.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;from the line:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.SelectLayerByLocation_management("CityGrids.shp", "INTERSECT", "thecity", "", "NEW_SELECTION")&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:42:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/updating-field-based-on-location/m-p/331709#M25788</guid>
      <dc:creator>Emilbrundage</dc:creator>
      <dc:date>2021-12-11T15:42:21Z</dc:date>
    </item>
    <item>
      <title>Re: Updating field based on location</title>
      <link>https://community.esri.com/t5/python-questions/updating-field-based-on-location/m-p/331710#M25789</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Emil,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When using the 'Select Layer by Attribute' or 'Select Layer by Location' functions, the input needs to a be a layer.&amp;nbsp; Use the MakeFeatureLayer_management function to do this.&amp;nbsp; Ex:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
arcpy.MakeFeatureLayer_management("CityGrids.shp", "cityGridsLyr")
arcpy.SelectLayerByLocation_management("cityGridsLyr", "INTERSECT", "thecity", "", "NEW_SELECTION")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:42:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/updating-field-based-on-location/m-p/331710#M25789</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2021-12-11T15:42:23Z</dc:date>
    </item>
    <item>
      <title>Re: Updating field based on location</title>
      <link>https://community.esri.com/t5/python-questions/updating-field-based-on-location/m-p/331711#M25790</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;What if one of your grid cells has multiple cities within it?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Nov 2013 16:52:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/updating-field-based-on-location/m-p/331711#M25790</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2013-11-19T16:52:07Z</dc:date>
    </item>
    <item>
      <title>Re: Updating field based on location</title>
      <link>https://community.esri.com/t5/python-questions/updating-field-based-on-location/m-p/331712#M25791</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;What if one of your grid cells has multiple cities within it?&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Then the first city in the list takes precedent. I was going to incorperate this aspect into my code (with an if statement) once I got everything else working.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Nov 2013 17:36:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/updating-field-based-on-location/m-p/331712#M25791</guid>
      <dc:creator>Emilbrundage</dc:creator>
      <dc:date>2013-11-19T17:36:16Z</dc:date>
    </item>
    <item>
      <title>Re: Updating field based on location</title>
      <link>https://community.esri.com/t5/python-questions/updating-field-based-on-location/m-p/331713#M25792</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;How about something like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
cityFC = r"C:\temp\city_polygons.shp"
indexFC = r"C:\temp\index_polygons.shp"
arcpy.MakeFeatureLayer_management(indexFC, "index_fl")
searchRows = arcpy.da.SearchCursor(cityFC, ["SHAPE@","CITY_NAME"])
for searchRow in searchRows:
&amp;nbsp;&amp;nbsp; geomObj, cityName = searchRow
&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management("index_fl", "INTERSECT", geomObj, "", "NEW_SELECTION")
&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management("index_fl", "CITY_GRID", "'" + cityName + "'", "PYTHON", "")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Note that if you want the "first" city to trump subsequent ones (in the case of overlap), you would want to sort the cityFC search cursor by OID in decending order (by default the sort order is by OID in ascending order).&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:42:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/updating-field-based-on-location/m-p/331713#M25792</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2021-12-11T15:42:26Z</dc:date>
    </item>
    <item>
      <title>Re: Updating field based on location</title>
      <link>https://community.esri.com/t5/python-questions/updating-field-based-on-location/m-p/331714#M25793</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;How about something like this:&lt;BR /&gt;&lt;BR /&gt;Note that if you want the "first" city to trump subsequent ones (in the case of overlap), you would want to sort the cityFC search cursor by OID in decending order (by default the sort order is by OID in ascending order).&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks CS, I shall study this code to see if I get it, if I can't get my current code working.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am getting the current error:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;ExecuteError: ERROR 999999: Error executing function.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;An invalid SQL statement was used.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;An invalid SQL statement was used.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Apparently for this line:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management("cityGridsLyr", "INTERSECT", "thecity", "", "NEW_SELECTION")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Which doesn't make much sense to me since that line does not contain SQL. Am I missing something?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
from arcpy import env

arcpy.env.workspace = "C:\E1B8\Grid_Create_Update_11-2013"

# Create the search cursor
#

tablecur = arcpy.SearchCursor("SampleCityTable.dbf")
for row in tablecur:
&amp;nbsp;&amp;nbsp;&amp;nbsp; cityname = row.City
&amp;nbsp;&amp;nbsp;&amp;nbsp; clause = "\"'NAME10' = " + cityname + '"'
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage (clause)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage (cityname)

# Make new feature for current city
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management("state_places.shp","thecity",clause)

# Create layer for selection
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management("CityGrids.shp", "cityGridsLyr")

# Select grid that intersect with new feature
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management("cityGridsLyr", "INTERSECT", "thecity", "", "NEW_SELECTION")

# Update field in grid layer with city name 
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management("cityGridsLyr", "CITY_GRID", cityname, "PYTHON", "")

# Remove selection from grid
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management("cityGridsLyr", "CLEAR_SELECTION", "")

# Delete newly made city layer
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management("thecity", "")
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management("cityGridsLyr", "")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:42:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/updating-field-based-on-location/m-p/331714#M25793</guid>
      <dc:creator>Emilbrundage</dc:creator>
      <dc:date>2021-12-11T15:42:29Z</dc:date>
    </item>
    <item>
      <title>Re: Updating field based on location</title>
      <link>https://community.esri.com/t5/python-questions/updating-field-based-on-location/m-p/331715#M25794</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;An invalid SQL statement was used.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Probably not for the SelectByLocation, probably for the MakeFeatureLayer... Instead of:&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;clause = "\"'NAME10' = " + cityname + '"'&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;try:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;clause = "NAME10 = " + "'" + cityname + "'"&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Nov 2013 22:23:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/updating-field-based-on-location/m-p/331715#M25794</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2013-11-19T22:23:19Z</dc:date>
    </item>
    <item>
      <title>Re: Updating field based on location</title>
      <link>https://community.esri.com/t5/python-questions/updating-field-based-on-location/m-p/331716#M25795</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&lt;BR /&gt;Note that if you want the "first" city to trump subsequent ones (in the case of overlap), you would want to sort the cityFC search cursor by OID in decending order (by default the sort order is by OID in ascending order).&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//006z00000009000000"&gt;OID&lt;/A&gt;&lt;SPAN&gt; is largely arbitrary, though. If there is a way you want to define 'first', for example alphabetically first, you'd need to sort on the name field in descending order.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 20 Nov 2013 14:28:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/updating-field-based-on-location/m-p/331716#M25795</guid>
      <dc:creator>DouglasSands</dc:creator>
      <dc:date>2013-11-20T14:28:11Z</dc:date>
    </item>
  </channel>
</rss>

