<?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: arcpy CalculateField - combine field values and derived value in Python Snippets Questions</title>
    <link>https://community.esri.com/t5/python-snippets-questions/arcpy-calculatefield-combine-field-values-and/m-p/813700#M302</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I figured out how to get the results I wanted. After trying several different ways to format the Value field in the expression, I abandoned this approach and used map algebra instead, on someone's advice. This doesn't require adding and calculating a field, or using Lookup. The final code that seems to be working is:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;for inRaster in arcpy.ListFiles("*.tif"):&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; inRastName = os.path.splitext(inRaster)[0]&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outRastName = inRastName + "f.tif"&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; raster1 = arcpy.Raster(inRaster)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rasterMax = raster1.maximum&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outRaster = Int(((raster1 / rasterMax) *10) +0.5) &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outRaster.save("...path/SpeciesR07_Frag1/" + outRastName)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A couple of tips I learned in doing this: use int for values and Int for rasters (capitalization matters!) and... adding 0.5 to the raster rounds the float values to integers, instead of just lopping off the decimals.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 29 Apr 2020 04:43:19 GMT</pubDate>
    <dc:creator>LeeO_Brien</dc:creator>
    <dc:date>2020-04-29T04:43:19Z</dc:date>
    <item>
      <title>arcpy CalculateField - combine field values and derived value</title>
      <link>https://community.esri.com/t5/python-snippets-questions/arcpy-calculatefield-combine-field-values-and/m-p/813696#M298</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Newbie here. I've been trying to solve this for weeks and have searched and tried many suggestions from this site and still can't get this to work correctly.&amp;nbsp; I'm trying to use a derived value (max) from the Value field of a raster and use it in a calculation on each row of that field, and put the results in a new field (then use that field as a lookup to create a new integer raster). I can do this easily in desktop ArcGIS, but have several hundred rasters to process, so am trying to use an arcpy for loop.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It seems, if I specify the calculation expression outside of the For loop, I can get it to run a calculation on each row in the field, but can't get the derived value, using all the values in the field, into the calculation. If I define the expression inside the for loop (as below), it uses the constant value (max), but then won't iterate through each row in the Value field.&amp;nbsp; (This way the output rasters have only two values: 0 and 10.&amp;nbsp; The other way, they have the original raster values multiplied by whatever I set the maxValue variable to initially). &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Can someone tell me what I'm doing wrong?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;import arcpy&lt;BR /&gt;import os&lt;BR /&gt;from arcpy import env&lt;BR /&gt;from arcpy.sa import *&lt;BR /&gt;arcpy.CheckOutExtension("Spatial")&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;# Set environment settings&lt;BR /&gt;env.workspace = "...path/SpeciesR07_1"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;# Set local variables&lt;BR /&gt;fieldName = "Frag"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;# Processes: AddField, CalculateField, LookUp&lt;BR /&gt;for inRaster in arcpy.ListFiles("*.tif"):&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; inRastName = os.path.splitext(inRaster)[0]&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outRastName = inRastName + "f.tif"&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; maxValue = 0&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; values = [row[0] for row in arcpy.da.SearchCursor(inRaster, "Value")]&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; maxValue = max(values)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; expression = "(!Value! / {}".format(maxValue) + ") *10"&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management(inRaster, fieldName, "SHORT")&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(inRaster, fieldName, expression, "PYTHON")&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lookupRast = Lookup(inRaster, fieldName)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lookupRast.save("...path/SpeciesR07_Frag1/" + outRastName)&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 27 Apr 2020 16:46:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-snippets-questions/arcpy-calculatefield-combine-field-values-and/m-p/813696#M298</guid>
      <dc:creator>LeeO_Brien</dc:creator>
      <dc:date>2020-04-27T16:46:58Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy CalculateField - combine field values and derived value</title>
      <link>https://community.esri.com/t5/python-snippets-questions/arcpy-calculatefield-combine-field-values-and/m-p/813697#M299</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE class="language-none line-numbers"&gt;&lt;CODE&gt;expression = "(!Value! / {}".format(maxValue) + ") *10"&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;What are you trying to evaluate with this line?&amp;nbsp; I don't understand what&amp;nbsp;&lt;/P&gt;&lt;PRE class="language-none line-numbers"&gt;&lt;CODE&gt;+''&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;does for you.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 27 Apr 2020 18:04:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-snippets-questions/arcpy-calculatefield-combine-field-values-and/m-p/813697#M299</guid>
      <dc:creator>JoeBorgione</dc:creator>
      <dc:date>2020-04-27T18:04:31Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy CalculateField - combine field values and derived value</title>
      <link>https://community.esri.com/t5/python-snippets-questions/arcpy-calculatefield-combine-field-values-and/m-p/813698#M300</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Trying to build the expression string: ( !Value! / num ) * 10&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 27 Apr 2020 18:38:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-snippets-questions/arcpy-calculatefield-combine-field-values-and/m-p/813698#M300</guid>
      <dc:creator>LeeO_Brien</dc:creator>
      <dc:date>2020-04-27T18:38:21Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy CalculateField - combine field values and derived value</title>
      <link>https://community.esri.com/t5/python-snippets-questions/arcpy-calculatefield-combine-field-values-and/m-p/813699#M301</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;where num = the max value in the !Value! field.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 27 Apr 2020 18:41:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-snippets-questions/arcpy-calculatefield-combine-field-values-and/m-p/813699#M301</guid>
      <dc:creator>LeeO_Brien</dc:creator>
      <dc:date>2020-04-27T18:41:03Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy CalculateField - combine field values and derived value</title>
      <link>https://community.esri.com/t5/python-snippets-questions/arcpy-calculatefield-combine-field-values-and/m-p/813700#M302</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I figured out how to get the results I wanted. After trying several different ways to format the Value field in the expression, I abandoned this approach and used map algebra instead, on someone's advice. This doesn't require adding and calculating a field, or using Lookup. The final code that seems to be working is:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;for inRaster in arcpy.ListFiles("*.tif"):&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; inRastName = os.path.splitext(inRaster)[0]&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outRastName = inRastName + "f.tif"&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; raster1 = arcpy.Raster(inRaster)&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rasterMax = raster1.maximum&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outRaster = Int(((raster1 / rasterMax) *10) +0.5) &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outRaster.save("...path/SpeciesR07_Frag1/" + outRastName)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A couple of tips I learned in doing this: use int for values and Int for rasters (capitalization matters!) and... adding 0.5 to the raster rounds the float values to integers, instead of just lopping off the decimals.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 29 Apr 2020 04:43:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-snippets-questions/arcpy-calculatefield-combine-field-values-and/m-p/813700#M302</guid>
      <dc:creator>LeeO_Brien</dc:creator>
      <dc:date>2020-04-29T04:43:19Z</dc:date>
    </item>
  </channel>
</rss>

