<?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: Update Cursor for Rasters? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/update-cursor-for-rasters/m-p/295728#M22854</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;My 2 cents:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Raster attribute tables are very tricky, as they are implemented differently for different image types. For example, with raw data formats like .bil, they are stored in an .aux/.aux.xml file. With .tiffs, the raster table is an RGB colormap table stored in the image header. ERDAS .img files look like more advanced tables, but I'm not sure you can successfully modify an .img's table because it is stored in the image itself. With grids, you may have better luck, as the table is a good old INFO table stored in the info folder parallel to the grid.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In general, I've found it best practice to only use the VALUE and COUNT fields and handle any other table information by joining to a separate lookup table. This generally makes more sense because most raster tools drop all variables from the output except VALUE and COUNT.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Your approach looks like it could be much more efficiently done&amp;nbsp; (and perhaps with more success with non-grid formats, but I don't know...)&amp;nbsp; by creating a lookup table in the in_memory workspace, populating it with a cursor, and then doing a Add Field / Add join / Calculate Field, or Join Field to copy the data over. But IMHO the separate ancillary data table is the most reliable path.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 25 Feb 2012 00:20:11 GMT</pubDate>
    <dc:creator>curtvprice</dc:creator>
    <dc:date>2012-02-25T00:20:11Z</dc:date>
    <item>
      <title>Update Cursor for Rasters?</title>
      <link>https://community.esri.com/t5/python-questions/update-cursor-for-rasters/m-p/295725#M22851</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm trying to use python to add a field and add some text for an attribute class. Funny thing is (actually not so funny) update cursor returns an error and destroys my raster!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy
#import os
from arcpy.sa import *
from arcpy import env
arcpy.CheckOutExtension("Spatial")
arcpy.env.overwriteOutput = True

WScom4_rc = "X:\\DATA\\watershed_100mbuf_lc_8class.img"

arcpy.AddField_management(WScom4_rc, "Class_Name", "TEXT")
rows = arcpy.UpdateCursor(WScom4_rc)
for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.Value == 1:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.Class_Name = "Forest"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.Value == 2:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.Class_Name = "Developed"&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.Value == 3:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.Class_Name = "Fields/Pastures"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.Value == 4:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.Class_Name = "Wetlands"&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.Value == 5:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.Class_Name = "Water"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.Value == 6:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.Class_Name = "Barren/Soil"&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.Value == 7:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.Class_Name = "Open Space"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.Value == 8:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.Class_Name = "Deforested"&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)
&amp;nbsp;&amp;nbsp;&amp;nbsp; if row.Value == 9:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.Class_Name = "Ice/Snow/Shadow (unclassified)"&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows.updateRow(row)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is the error code: &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Traceback (most recent call last):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; File "X:\DATA\FloodRisk2_Canada\02_Source_Data\2_Working_Source_Data\Goulds_working\FloodVul_process_script1_goulds.py", line 74, in &amp;lt;module&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.Class_Name = "Forest"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\arcobjects\_base.py", line 35, in __setattr__&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return setattr(self._arc_object, attr, ao)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;RuntimeError: ERROR 999999: Error executing function.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The input raster has a normal raster attribute table: Rowid, value, count.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The output is an empty raster where the attribute table contains Rowid, value, count, and Class, but no rows (no data). &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Can update cursor be used for editing raster attributes? If so, How?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Rich&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 24 Feb 2012 16:01:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-cursor-for-rasters/m-p/295725#M22851</guid>
      <dc:creator>RichardThurau</dc:creator>
      <dc:date>2012-02-24T16:01:51Z</dc:date>
    </item>
    <item>
      <title>Re: Update Cursor for Rasters?</title>
      <link>https://community.esri.com/t5/python-questions/update-cursor-for-rasters/m-p/295726#M22852</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Rich,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It looks like you are specifying the incorrect field name.&amp;nbsp; You created the new field called 'Class', but you are trying to update a field called 'Class_Name'.&amp;nbsp; Change all 'row.Class_Name' objects to 'row.Class' and your code should work.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 24 Feb 2012 19:09:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-cursor-for-rasters/m-p/295726#M22852</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2012-02-24T19:09:13Z</dc:date>
    </item>
    <item>
      <title>Re: Update Cursor for Rasters?</title>
      <link>https://community.esri.com/t5/python-questions/update-cursor-for-rasters/m-p/295727#M22853</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Jake,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for taking a look.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;While that is the source of the error, it is not the source of the problem. Changing the field name was something I was messing with to try and resolve.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I edited the code in my original post.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The main issue still remains:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;When I push the above code to Update Cursor, I get an empty raster output. The code runs, with no errors, but an empty dataset is returned.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It is very bad when stuff does that to data.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Anyone out there successfully use Update Cursor to modify Raster attributes?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;How about Calculate Field (which I have not tried)?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;RT&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 24 Feb 2012 20:09:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-cursor-for-rasters/m-p/295727#M22853</guid>
      <dc:creator>RichardThurau</dc:creator>
      <dc:date>2012-02-24T20:09:01Z</dc:date>
    </item>
    <item>
      <title>Re: Update Cursor for Rasters?</title>
      <link>https://community.esri.com/t5/python-questions/update-cursor-for-rasters/m-p/295728#M22854</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;My 2 cents:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Raster attribute tables are very tricky, as they are implemented differently for different image types. For example, with raw data formats like .bil, they are stored in an .aux/.aux.xml file. With .tiffs, the raster table is an RGB colormap table stored in the image header. ERDAS .img files look like more advanced tables, but I'm not sure you can successfully modify an .img's table because it is stored in the image itself. With grids, you may have better luck, as the table is a good old INFO table stored in the info folder parallel to the grid.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In general, I've found it best practice to only use the VALUE and COUNT fields and handle any other table information by joining to a separate lookup table. This generally makes more sense because most raster tools drop all variables from the output except VALUE and COUNT.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Your approach looks like it could be much more efficiently done&amp;nbsp; (and perhaps with more success with non-grid formats, but I don't know...)&amp;nbsp; by creating a lookup table in the in_memory workspace, populating it with a cursor, and then doing a Add Field / Add join / Calculate Field, or Join Field to copy the data over. But IMHO the separate ancillary data table is the most reliable path.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 25 Feb 2012 00:20:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-cursor-for-rasters/m-p/295728#M22854</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2012-02-25T00:20:11Z</dc:date>
    </item>
    <item>
      <title>Re: Update Cursor for Rasters?</title>
      <link>https://community.esri.com/t5/python-questions/update-cursor-for-rasters/m-p/295729#M22855</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;This could be something specific with the raster you are using.&amp;nbsp; I tested with a .IMG using ArcGIS 10 SP3 and everything worked as it should.&amp;nbsp; I've attached the .IMG file.&amp;nbsp; See if you are able to reproduce the same issue with the attached IMG.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 27 Feb 2012 09:29:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-cursor-for-rasters/m-p/295729#M22855</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2012-02-27T09:29:31Z</dc:date>
    </item>
    <item>
      <title>Re: Update Cursor for Rasters?</title>
      <link>https://community.esri.com/t5/python-questions/update-cursor-for-rasters/m-p/295730#M22856</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks Curtis and Jake for your input. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;First of all, it is very important for this work to remain in raster format: raster processing is a lot faster, and raster is required for further analysis steps.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Second, IMG is over geodatabase grid because it is interchangeable between ESRI and Erdas.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Having said that: &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Jake, I ran my script on the img you sent, and it did the same thing: erased the data, spit out an empty attribute table. Did you run the same code I posted? &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Can you post your code? It would be awesome to get this working for IMGs. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I am also running 10, sp3. It's on a 64-bit win7 machine but that shouldn't matter.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I ran the code on a geodatabase grid, and it worked the way it's supposed to. I don't like being tied to the geodatabase but for now I think it will do.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks again for your input.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Rich&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 27 Feb 2012 14:52:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-cursor-for-rasters/m-p/295730#M22856</guid>
      <dc:creator>RichardThurau</dc:creator>
      <dc:date>2012-02-27T14:52:52Z</dc:date>
    </item>
    <item>
      <title>Re: Update Cursor for Rasters?</title>
      <link>https://community.esri.com/t5/python-questions/update-cursor-for-rasters/m-p/295731#M22857</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Rich,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I ran the same code that you posted.&amp;nbsp; The only difference that I can see is that my raster is on my local disk.&amp;nbsp; Try copying the IMG to your local C:\ drive and see if you can reproduce the same issue.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 27 Feb 2012 15:10:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-cursor-for-rasters/m-p/295731#M22857</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2012-02-27T15:10:31Z</dc:date>
    </item>
  </channel>
</rss>

