<?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: Map Algebra in v10 in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/map-algebra-in-v10/m-p/707976#M54862</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Right on...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I think I'm getting into the groove now. Thanks a million Phil!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 08 Mar 2012 16:24:32 GMT</pubDate>
    <dc:creator>ChrisSnyder</dc:creator>
    <dc:date>2012-03-08T16:24:32Z</dc:date>
    <item>
      <title>Map Algebra in v10</title>
      <link>https://community.esri.com/t5/python-questions/map-algebra-in-v10/m-p/707971#M54857</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Struggling with "best" Map Algebra syntax to use in scripts via PythonWin (NOT the Python window in ArcGIS, which seems to allow for different syntax - which is confussingm, but off topic)...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Anyway... Classic "make streams from flow accumulation" scenario...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In v9.3 I could do this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;flowAccGrd = "flow_acc" flowDirGrd = "flow_dir" somaExp = "streamlink(con(" + flowAccGrd + " &amp;gt; 500, 1, setnull(1)), " + flowDirGrd + ")" streamLinkGrd = "stream_link" gp.SingleOutputMapAlgeba_sa(somaExp, streamLinkGrd)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Solid...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Now as I try to rewrite some stuff in the new v10 syntax, the best I can com up with is this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;flowAccGrd = "flow_acc" flowDirGrd = "flow_dir" streamLinkTmp = arcpy.sa.StreamLink(arcpy.sa.Con(flowAccGrd, 1, arcpy.sa.SetNull(1,1), "VALUE &amp;gt;= 500")), flowDirGrd) streamLinkGrd = "stream_link" streamLinkTmp.save(streamLinkGrd)&lt;/PRE&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Is there a better/more compact way to write this expresion ini v10 sytax (again, writting for a "stand-alone script")?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Is that imbedded arcpy.sa.SetNull(1,1) neccessary - or is there a more elegant way to use the IsNull() and SetNull() expressions inside of a con() statement... Seemed easier in v9.3.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 06 Mar 2012 23:27:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/map-algebra-in-v10/m-p/707971#M54857</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2012-03-06T23:27:49Z</dc:date>
    </item>
    <item>
      <title>Re: Map Algebra in v10</title>
      <link>https://community.esri.com/t5/python-questions/map-algebra-in-v10/m-p/707972#M54858</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Off the top of my head, cast your inputs as Raster objects to make things a little more compact. To wit:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;flowAccGrd = arcpy.Raster("C:\\example\\flowaccgrd") ... streamLinkTmp = arcpy.sa.StreamLink(arcpy.sa.Con(flowAccGrd &amp;gt; 500, 1), flowDirGrd) ...&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;SPAN&gt;Seems like that should do it. I don't think you even need SetNull, since the output of the Con statement will necessarily be constrained to values &amp;gt; 500.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 07 Mar 2012 12:55:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/map-algebra-in-v10/m-p/707972#M54858</guid>
      <dc:creator>PhilMorefield</dc:creator>
      <dc:date>2012-03-07T12:55:58Z</dc:date>
    </item>
    <item>
      <title>Re: Map Algebra in v10</title>
      <link>https://community.esri.com/t5/python-questions/map-algebra-in-v10/m-p/707973#M54859</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks Phil... I was missing the whole "casting as a raster" thing... and the implied setnull - didn't know about that - very usefull!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Took me a while to realize that the temporary raster objects (for example "slopePctTmp" in the expresion: slopePctTmp = arcpy.sa.Slope(fillGrd, "PERCENT_RISE", "") can and should be used even after you ".save()" them, and low and behold, they now reference the updated .save() path! One you you use a variable to reference the .save() output path, that variable is no longer a "raster object", so just keep using the raster reference (slopePctTemp).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The big lightbulb though - and a **MAJOR ARCPY CRASH BUG ISSUE***:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What I had been doing: Seems that you CAN use the the output path variable (as opposed to the raster object variable) in SINGLE tool expreessions... for example:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;#Process: Run a Fill
fillTmp = arcpy.sa.Fill(clipDem, "")
fillGrd = areaIdFolderPath + "\\fill"
fillTmp.save(fillGrd)
#Process: FlowDir
flowDirTmp = arcpy.sa.FlowDirection(fillGrd, "", "") #NOTE: I am using the fillGrd variable not the the fillTmp variable: BOTH work for "single tool expressions" 
flowDirGrd = areaIdFolderPath + "\\flowdir"
flowDirTmp.save(flowDirGrd)&lt;/PRE&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;But if you try to use the output path variables (and not the raster object variables) in MULTI TOOL EXPRESSIONS Python will CRASH BOOM BLOWUP... for example:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is OK:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;#Process: FlowAcc
flowAccTmp = arcpy.sa.FlowAccumulation(flowDirGrd, "", "INTEGER"); showGpMessage()
flowAccGrd = areaIdFolderPath + "\\flowacc"
flowAccTmp.save(flowAccGrd)

#Process: Create a streamlink
acreThreshold = 1
acreThresholdInPixels = int(43560 * acreThreshold / cellSize ** 2)
flowAccGrd = arcpy.Raster(flowAccGrd)
streamLinkTmp = arcpy.sa.StreamLink(arcpy.sa.Con(flowAccTmp &amp;gt; acreThresholdInPixels, 1), flowDirGrd)
streamLinkGrd = areaIdFolderPath + "\\strmlnk"
streamLinkTmp.save(streamLinkGrd)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This VERY bad and cause Python "Application Error" explosion:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;#Process: FlowAcc
flowAccTmp = arcpy.sa.FlowAccumulation(flowDirGrd, "", "INTEGER"); showGpMessage()
flowAccGrd = areaIdFolderPath + "\\flowacc"
flowAccTmp.save(flowAccGrd)

#Process: Create a streamlink
acreThreshold = 1
acreThresholdInPixels = int(43560 * acreThreshold / cellSize ** 2)
flowAccGrd = arcpy.Raster(flowAccGrd)
streamLinkTmp = arcpy.sa.StreamLink(arcpy.sa.Con(flowAccGrd &amp;gt; acreThresholdInPixels, 1), flowDirGrd)
streamLinkGrd = areaIdFolderPath + "\\strmlnk"
streamLinkTmp.save(streamLinkGrd)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So the best solution is to ***ALWAYS*** use the raster objects andnot the path variables:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;#Process: FlowAcc
flowAccTmp = arcpy.sa.FlowAccumulation(flowDirGrd, "", "INTEGER"); showGpMessage()
flowAccGrd = areaIdFolderPath + "\\flowacc"
flowAccTmp.save(flowAccGrd)

#Process: Create a streamlink
acreThreshold = 1
acreThresholdInPixels = int(43560 * acreThreshold / cellSize ** 2)
flowAccGrd = arcpy.Raster(flowAccGrd)
streamLinkTmp = arcpy.sa.StreamLink(arcpy.sa.Con(flowAccTmp &amp;gt; acreThresholdInPixels, 1), flowDirTmp)
streamLinkGrd = areaIdFolderPath + "\\strmlnk"
streamLinkTmp.save(streamLinkGrd)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So, moral of the story... Don't use the .save(pathvariable) path variables in raster expresions!!!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 16:49:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/map-algebra-in-v10/m-p/707973#M54859</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2021-12-12T16:49:58Z</dc:date>
    </item>
    <item>
      <title>Re: Map Algebra in v10</title>
      <link>https://community.esri.com/t5/python-questions/map-algebra-in-v10/m-p/707974#M54860</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;New strugle:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I had written some code years ago that would create a 1 cell outline of the DATA portion of a raster (see attached image):&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;somaExp = "con(" + flowDirGrd + " &amp;gt; 0 &amp;amp; isnull(Focalmax(" + flowDirGrd + ", 'RECTANGLE', 3, 3, 'NODATA')), 1, setnull(" + flowDirGrd + "))"&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;....where 'flowDirGrd' is just some raster (doesn't neccesarily have to be a flow direction raster).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So basically just the outer DATA cells get coded as a 1 and everything else is NoData.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I came up with this for v10 sytax:&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;demRingTmp = arcpy.sa.Con(flowDirTmp &amp;gt; 0 &amp;amp; arcpy.sa.IsNull(arcpy.sa.FocalStatistics(flowDirTmp, arcpy.sa.NbrRectangle(3, 3, "CELL"), "MAXIMUM", "NODATA")), 1)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;...which runs but produces the wrong results for some reason.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Anyone wanna take a stab at writting this ugly thing in v10 syntax (and get the correct result)?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;[ATTACH=CONFIG]12500[/ATTACH]&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 07 Mar 2012 23:47:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/map-algebra-in-v10/m-p/707974#M54860</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2012-03-07T23:47:40Z</dc:date>
    </item>
    <item>
      <title>Re: Map Algebra in v10</title>
      <link>https://community.esri.com/t5/python-questions/map-algebra-in-v10/m-p/707975#M54861</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Add two sets of parentheses, one around each of your conditions. So:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# your way; gave me a raster full of 1s
demRingTmp = arcpy.sa.Con(flowDirTmp &amp;gt; 0 &amp;amp; arcpy.sa.IsNull(arcpy.sa.FocalStatistics(flowDirTmp, arcpy.sa.NbrRectangle(3, 3, "CELL"), "MAXIMUM", "NODATA")), 1)

# my way; gave me a solid 'ring' around the data
from arcpy import sa
demRingTmp = sa.Con((flowDirTemp &amp;gt; 0) &amp;amp; (sa.IsNull(sa.FocalStatistics(flowDirTemp, sa.NbrRectangle(3, 3, 'CELL'), 'MAXIMUM', 'NODATA'))), 1)&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;In my experience, compound conditional statements in numpy (i.e., using 'numpy.where') require the same treatment.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hope that helps.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 05:44:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/map-algebra-in-v10/m-p/707975#M54861</guid>
      <dc:creator>PhilMorefield</dc:creator>
      <dc:date>2021-12-12T05:44:22Z</dc:date>
    </item>
    <item>
      <title>Re: Map Algebra in v10</title>
      <link>https://community.esri.com/t5/python-questions/map-algebra-in-v10/m-p/707976#M54862</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Right on...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I think I'm getting into the groove now. Thanks a million Phil!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 08 Mar 2012 16:24:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/map-algebra-in-v10/m-p/707976#M54862</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2012-03-08T16:24:32Z</dc:date>
    </item>
  </channel>
</rss>

