<?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: Advanced Con Statement in Raster Calculations in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/advanced-con-statement-in-raster-calculations/m-p/356482#M28029</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;It took some getting used to, but the new map algebra can make things pretty clean. My two cents (with Python style guide enforced!):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy as ap
from arcpy import sa

ap.CheckOutExtention('Spatial')
ap.env.overWriteOutput = True

raster1 = ap.Raster("D:\\ACC\\version01_lc2.img")
outraster4 = "D:\\ACC\\version01_lc_res04.img"

var1 = [311, 401]
var2 = 23
var3 = 27

tempRas = sa.Con(raster1 &amp;lt; var1[0] | raster1 &amp;gt; var1[1], var2, var3)
tempRas.save(outraster4)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 16:40:43 GMT</pubDate>
    <dc:creator>PhilMorefield</dc:creator>
    <dc:date>2021-12-11T16:40:43Z</dc:date>
    <item>
      <title>Advanced Con Statement in Raster Calculations</title>
      <link>https://community.esri.com/t5/python-questions/advanced-con-statement-in-raster-calculations/m-p/356477#M28024</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi there,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;i am trying to convert Python Code from 9.2 to 10.x&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The problem i am facing: in ArcGIS 9.2 it was possible, to create a Map Algebra Expression with a variable number of input rasters by defining it as a string Variable. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In ArcGis 10 it is not anymore possible to use String Variables in a Raster Expression for building complex statements in a Con command. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So if i have an input list with 4 values, i want to create the expression like this, &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;(Raster(inRaster1) &amp;lt; var1[0]) &amp;amp; (Raster(inRaster2) &amp;gt; var1[1]) &amp;amp; (Raster(inRaster3) &amp;lt;&amp;gt; var1[2]) &amp;amp; (Raster(inRaster4) == var1[3]) ,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;if i have only 2 input rasterfiles, only the first part of inRaster1 and inRaster2 should be used, if i have 16 or more files it should also work.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;A conversion from String to Raster would be maybe the best solution, but the Con Command doesn't accept this.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any suggestions, how to deal in ArcGis 10 with a variable number of input rasters?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;With best regards&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 16 Jan 2012 11:45:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/advanced-con-statement-in-raster-calculations/m-p/356477#M28024</guid>
      <dc:creator>franzsuppan</dc:creator>
      <dc:date>2012-01-16T11:45:17Z</dc:date>
    </item>
    <item>
      <title>Re: Advanced Con Statement in Raster Calculations</title>
      <link>https://community.esri.com/t5/python-questions/advanced-con-statement-in-raster-calculations/m-p/356478#M28025</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Some suggestions:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;1. v10.0 map algebra syntax is pretty hokey I think, but it is doable. This post (my own v10.0 flailings) may help: &lt;/SPAN&gt;&lt;A href="http://forums.arcgis.com/threads/47540-Lookup-function-in-v10.0-Map-Algebra-implementing-dot-notation-in-v10.0"&gt;http://forums.arcgis.com/threads/47540-Lookup-function-in-v10.0-Map-Algebra-implementing-dot-notation-in-v10.0&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;2. Continue to use the pre-v10 raster algebra syntax in v10.0... it's still there just hidden: arcpy.gp.SingleOutputMapAlgebra_sa(blah, blah)&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 16 Jan 2012 16:25:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/advanced-con-statement-in-raster-calculations/m-p/356478#M28025</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2012-01-16T16:25:05Z</dc:date>
    </item>
    <item>
      <title>Re: Advanced Con Statement in Raster Calculations</title>
      <link>https://community.esri.com/t5/python-questions/advanced-con-statement-in-raster-calculations/m-p/356479#M28026</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Perhaps try the &lt;/SPAN&gt;&lt;A href="http://docs.python.org/library/functions.html#eval" rel="nofollow noopener noreferrer" target="_blank"&gt;eval&lt;/A&gt;&lt;SPAN&gt; function (I haven't tested this):&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;expression = '(Raster(inRaster1) &amp;lt; var1[0]) &amp;amp; (Raster(inRaster2) &amp;gt; var1[1]) &amp;amp; (Raster(inRaster3) &amp;lt;&amp;gt; var1[2]) &amp;amp; (Raster(inRaster4) == var1[3])'
NewRaster=eval(expression)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 16:40:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/advanced-con-statement-in-raster-calculations/m-p/356479#M28026</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2021-12-11T16:40:38Z</dc:date>
    </item>
    <item>
      <title>Re: Advanced Con Statement in Raster Calculations</title>
      <link>https://community.esri.com/t5/python-questions/advanced-con-statement-in-raster-calculations/m-p/356480#M28027</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks a lot Chris and Luke for your quick and perfect response!!!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I tried the eval Function (to be honest - i never used it before - and it works!!!)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Below is a working sample code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN style="font-size:1;"&gt;import arcpy&lt;BR /&gt;import sys, string, os, time, math&lt;BR /&gt;from arcpy.sa import *&lt;/SPAN&gt;&lt;SPAN style="font-size:1;"&gt;&lt;BR /&gt;&lt;BR /&gt;arcpy.CheckOutExtension("Spatial")&lt;BR /&gt;arcpy.env.overwriteOutput = True&lt;BR /&gt;&lt;BR /&gt;raster1 = "D:/ACC/version01_lc2.img"&lt;BR /&gt;outraster4 = "D:/ACC/version01_lc_res04.img"&lt;BR /&gt;&lt;BR /&gt;# suppose &lt;BR /&gt;var1 = [311, 401]&lt;BR /&gt;var2 = 23&lt;BR /&gt;var3 = 27&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN style="font-size:2;"&gt;&lt;STRONG&gt;exp04 = "Con((Raster(raster1) &amp;lt; var1[0]) | (Raster(raster1) &amp;gt; var1[1]), var2, var3)"&lt;BR /&gt;res04 = eval(exp04)&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;BR /&gt;res04.save(outraster4)&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 17 Jan 2012 15:12:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/advanced-con-statement-in-raster-calculations/m-p/356480#M28027</guid>
      <dc:creator>franzsuppan</dc:creator>
      <dc:date>2012-01-17T15:12:25Z</dc:date>
    </item>
    <item>
      <title>Re: Advanced Con Statement in Raster Calculations</title>
      <link>https://community.esri.com/t5/python-questions/advanced-con-statement-in-raster-calculations/m-p/356481#M28028</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;IMHO, using substitution syntax makes for more readable code, whether you're using the old or new tools. Note the %s substitution converts numbers to strings for you if that is required.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
expression = "(Raster(%s) &amp;lt; %s) &amp;amp; (Raster(%s) &amp;gt; %s) &amp;amp; "
&amp;nbsp;&amp;nbsp;&amp;nbsp; "(Raster(%s) &amp;lt;&amp;gt; %s) &amp;amp; (Raster(%s) == %s)"
expression = expression % \
&amp;nbsp;&amp;nbsp;&amp;nbsp; (inRaster1,var1[0],inRaster2,var1[1],inRaster3,var1[2],inraster4,var1[3])
NewRaster=eval(expression).
NewRaster.save("outRaster")
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 16:40:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/advanced-con-statement-in-raster-calculations/m-p/356481#M28028</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-11T16:40:41Z</dc:date>
    </item>
    <item>
      <title>Re: Advanced Con Statement in Raster Calculations</title>
      <link>https://community.esri.com/t5/python-questions/advanced-con-statement-in-raster-calculations/m-p/356482#M28029</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;It took some getting used to, but the new map algebra can make things pretty clean. My two cents (with Python style guide enforced!):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy as ap
from arcpy import sa

ap.CheckOutExtention('Spatial')
ap.env.overWriteOutput = True

raster1 = ap.Raster("D:\\ACC\\version01_lc2.img")
outraster4 = "D:\\ACC\\version01_lc_res04.img"

var1 = [311, 401]
var2 = 23
var3 = 27

tempRas = sa.Con(raster1 &amp;lt; var1[0] | raster1 &amp;gt; var1[1], var2, var3)
tempRas.save(outraster4)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 16:40:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/advanced-con-statement-in-raster-calculations/m-p/356482#M28029</guid>
      <dc:creator>PhilMorefield</dc:creator>
      <dc:date>2021-12-11T16:40:43Z</dc:date>
    </item>
  </channel>
</rss>

