<?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: Generate a Function for Map Algebra Expression in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496426#M38986</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Your mention was just me (cheekily) alerting you that there was an opportunity to use numpy, but after I provided one such solution, it didn't seem necessary. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 11 Mar 2016 21:33:19 GMT</pubDate>
    <dc:creator>DarrenWiens2</dc:creator>
    <dc:date>2016-03-11T21:33:19Z</dc:date>
    <item>
      <title>Generate a Function for Map Algebra Expression</title>
      <link>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496422#M38982</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I've written a Python script that reclassifies a raster using a set of conditional statements into 4 classes:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;0 - 12&lt;/LI&gt;&lt;LI&gt;12 - 24&lt;/LI&gt;&lt;LI&gt;24 - 30&lt;/LI&gt;&lt;LI&gt;30 - 60&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code jive_text_macro _jivemacro_uid_14577213635601408" data-renderedposition="227_50_798_32" jivemacro_uid="_14577213635601408"&gt;&lt;P&gt;rec_ras = Con(costmin &amp;lt;= 12, 12, Con((costmin &amp;gt; 12) &amp;amp; (costmin &amp;lt;= 24), 24, Con((costmin &amp;gt; 24) &amp;amp; (costmin &amp;lt;= 30), 30, Con((costmin &amp;gt; 30) &amp;amp; (costmin &amp;lt;= 60), 60))))&lt;/P&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I then needed to change the classes into 7 classes:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;0 - 5&lt;/LI&gt;&lt;LI&gt;5 - 10&lt;/LI&gt;&lt;LI&gt;10 - 15&lt;/LI&gt;&lt;LI&gt;15 - 20&lt;/LI&gt;&lt;LI&gt;20 - 25&lt;/LI&gt;&lt;LI&gt;25 - 30&lt;/LI&gt;&lt;LI&gt;30 - 60&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code jive_text_macro _jivemacro_uid_14577215843762401" data-renderedposition="569_50_798_48" jivemacro_uid="_14577215843762401"&gt;&lt;P&gt;rec_ras = Con(costmin &amp;lt;= 5, 5, Con((costmin &amp;gt; 5) &amp;amp; (costmin &amp;lt;= 10), 10, Con((costmin &amp;gt; 10) &amp;amp; (costmin &amp;lt;= 15), 15, Con((costmin &amp;gt; 15) &amp;amp; (costmin &amp;lt;= 20), 20, Con((costmin &amp;gt; 20) &amp;amp; (costmin &amp;lt;= 25), 25, Con((costmin &amp;gt; 25) &amp;amp; (costmin &amp;lt;= 30), 30, Con((costmin &amp;gt; 30) &amp;amp; (costmin &amp;lt;= 60), 60)))))))&lt;/P&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Is ther a better way of creating a Python function that can create my conditional statement as and if when I need to change the classes before running it. The following is part of&amp;nbsp; a larger Python Class.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 11 Mar 2016 18:42:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496422#M38982</guid>
      <dc:creator>PeterWilson</dc:creator>
      <dc:date>2016-03-11T18:42:19Z</dc:date>
    </item>
    <item>
      <title>Re: Generate a Function for Map Algebra Expression</title>
      <link>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496423#M38983</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Have you considered flipping to Numpy, reclassifying, and then back again?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://pro.arcgis.com/en/pro-app/arcpy/functions/rastertonumpyarray-function.htm" title="http://pro.arcgis.com/en/pro-app/arcpy/functions/rastertonumpyarray-function.htm"&gt;RasterToNumPyArray—ArcPy Functions | ArcGIS for Desktop&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://pro.arcgis.com/en/pro-app/arcpy/functions/numpyarraytoraster-function.htm" title="http://pro.arcgis.com/en/pro-app/arcpy/functions/numpyarraytoraster-function.htm"&gt;NumPyArrayToRaster—ArcPy Functions | ArcGIS for Desktop&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;edit: I assume you've considered and passed on using the &lt;A href="http://pro.arcgis.com/en/pro-app/tool-reference/spatial-analyst/reclassify.htm"&gt;Reclassify&lt;/A&gt; geoprocessing tool.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 11 Mar 2016 18:47:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496423#M38983</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2016-03-11T18:47:36Z</dc:date>
    </item>
    <item>
      <title>Re: Generate a Function for Map Algebra Expression</title>
      <link>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496424#M38984</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Here is approximately how you can do this in numpy (need to do some other tedious stuff to position final raster correctly):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; import numpy&amp;nbsp;&amp;nbsp; 
... reclass_map = [[10,999],[20,1000]] # reclass list: [[old1,new1],[old2,new2],...]
... arr = arcpy.RasterToNumPyArray('idw.tif',nodata_to_value=0) # raster to&amp;nbsp; numpy
... for i in reclass_map: # loop through class list
...&amp;nbsp;&amp;nbsp;&amp;nbsp; temp = numpy.less(arr,i[0]) # if less than value
...&amp;nbsp;&amp;nbsp;&amp;nbsp; numpy.putmask(arr,temp,i[1]) # set to new value
... new_ras = arcpy.NumPyArrayToRaster(arr) # flip back to raster&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 21:49:44 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496424#M38984</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2021-12-11T21:49:44Z</dc:date>
    </item>
    <item>
      <title>Re: Generate a Function for Map Algebra Expression</title>
      <link>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496425#M38985</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;It seems the original post got moved or something... and my mention deleted, so here it is again&lt;/P&gt;&lt;P&gt;Conceptually and overtly just convert to array first, then zz back to raster&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import numpy as np
a = np.arange(60).reshape((6,10))
zz = np.zeros_like(a)
bins = [0,5,10,15,20,25,30,60,100]
newclass = [1, 2, 3, 4, 5, 6, 7, 8]
noob = zip(bins[:-1],bins[1:],newclass)
qs =[]
for rc in noob:
&amp;nbsp;&amp;nbsp;&amp;nbsp; q1 = (a &amp;gt;= rc[0])
&amp;nbsp;&amp;nbsp;&amp;nbsp; q2 = (a &amp;lt; rc[1])
&amp;nbsp;&amp;nbsp;&amp;nbsp; z = np.where(q1 &amp;amp; q2, rc[2],0)
&amp;nbsp;&amp;nbsp;&amp;nbsp; zz = zz + z
print("old\n{}\nnew\n{}".format(a,zz))


old
[[ 0&amp;nbsp; 1&amp;nbsp; 2&amp;nbsp; 3&amp;nbsp; 4&amp;nbsp; 5&amp;nbsp; 6&amp;nbsp; 7&amp;nbsp; 8&amp;nbsp; 9]
[10 11 12 13 14 15 16 17 18 19]
[20 21 22 23 24 25 26 27 28 29]
[30 31 32 33 34 35 36 37 38 39]
[40 41 42 43 44 45 46 47 48 49]
[50 51 52 53 54 55 56 57 58 59]]
new
[[1 1 1 1 1 2 2 2 2 2]
[3 3 3 3 3 4 4 4 4 4]
[5 5 5 5 5 6 6 6 6 6]
[7 7 7 7 7 7 7 7 7 7]
[7 7 7 7 7 7 7 7 7 7]
[7 7 7 7 7 7 7 7 7 7]]
old
[[ 0&amp;nbsp; 1&amp;nbsp; 2&amp;nbsp; 3&amp;nbsp; 4&amp;nbsp; 5&amp;nbsp; 6&amp;nbsp; 7&amp;nbsp; 8&amp;nbsp; 9]
[10 11 12 13 14 15 16 17 18 19]
[20 21 22 23 24 25 26 27 28 29]
[30 31 32 33 34 35 36 37 38 39]
[40 41 42 43 44 45 46 47 48 49]
[50 51 52 53 54 55 56 57 58 59]]
new
[[1 1 1 1 1 2 2 2 2 2]
[3 3 3 3 3 4 4 4 4 4]
[5 5 5 5 5 6 6 6 6 6]
[7 7 7 7 7 7 7 7 7 7]
[7 7 7 7 7 7 7 7 7 7]
[7 7 7 7 7 7 7 7 7 7]]&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 21:49:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496425#M38985</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2021-12-11T21:49:46Z</dc:date>
    </item>
    <item>
      <title>Re: Generate a Function for Map Algebra Expression</title>
      <link>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496426#M38986</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Your mention was just me (cheekily) alerting you that there was an opportunity to use numpy, but after I provided one such solution, it didn't seem necessary. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 11 Mar 2016 21:33:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496426#M38986</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2016-03-11T21:33:19Z</dc:date>
    </item>
    <item>
      <title>Re: Generate a Function for Map Algebra Expression</title>
      <link>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496427#M38987</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;naw... it just said "mention removed" on the mail link I had to post it twice, since you were probably editing yours when I posted and My original didn't keep&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 11 Mar 2016 21:38:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496427#M38987</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2016-03-11T21:38:38Z</dc:date>
    </item>
    <item>
      <title>Re: Generate a Function for Map Algebra Expression</title>
      <link>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496428#M38988</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Dan&lt;/P&gt;&lt;P&gt;The following looks to be what I was looking for. I've been swamped the last few days. Will come back to the following over the weekend and post my code using numpy.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 17 Mar 2016 08:11:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496428#M38988</guid>
      <dc:creator>PeterWilson</dc:creator>
      <dc:date>2016-03-17T08:11:38Z</dc:date>
    </item>
    <item>
      <title>Re: Generate a Function for Map Algebra Expression</title>
      <link>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496429#M38989</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Dan&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Your sample code worked perfectly, with some minor changes. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;'''
Created on Mar 19, 2016


@author: PeterW
'''
# import site-packages and modules
import os
import numpy as np
import arcpy
from arcpy.sa import *


# set arguments
input_raster = arcpy.Raster(r"E:\Python\Testing\Numpy_Reclassify\SAA_Raster\tbr")
output_folder = r"E:\Python\Testing\Numpy_Reclassify\SAA_Reclassified"


# set environment settings
arcpy.env.overwriteOutput = True
arcpy.env.outputCoordinateSystem = input_raster


# check out extensions
arcpy.CheckOutExtension("Spatial")




def reclass_raster(input_raster, output_folder):
&amp;nbsp;&amp;nbsp;&amp;nbsp; lower_left = arcpy.Point(input_raster.extent.XMin,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; input_raster.extent.YMin)
&amp;nbsp;&amp;nbsp;&amp;nbsp; cell_size = input_raster.meanCellWidth
&amp;nbsp;&amp;nbsp;&amp;nbsp; saa_array = arcpy.RasterToNumPyArray(input_raster)
&amp;nbsp;&amp;nbsp;&amp;nbsp; reclass_array = np.zeros_like(saa_array)
&amp;nbsp;&amp;nbsp;&amp;nbsp; bins = [0, 5, 10, 15, 20, 25, 30, 60]
&amp;nbsp;&amp;nbsp;&amp;nbsp; new_bins = [5, 10, 15, 20, 25, 30, 60]
&amp;nbsp;&amp;nbsp;&amp;nbsp; new_classes = zip(bins[:-1], bins[1:], new_bins)
&amp;nbsp;&amp;nbsp;&amp;nbsp; for rc in new_classes:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; q1 = (saa_array &amp;gt;= rc[0])
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; q2 = (saa_array &amp;lt; rc[1])
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; z = np.where(q1 &amp;amp; q2, rc[2], 0)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; reclass_array = reclass_array + z
&amp;nbsp;&amp;nbsp;&amp;nbsp; reclass_raster = arcpy.NumPyArrayToRaster(reclass_array,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lower_left,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x_cell_size=cell_size,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; value_to_nodata=0)
&amp;nbsp;&amp;nbsp;&amp;nbsp; Int(reclass_raster).save(os.path.join(output_folder, "rec"))


reclass_raster(input_raster, output_folder)


# check in extensions
arcpy.CheckInExtension("Spatial")&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 21:49:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496429#M38989</guid>
      <dc:creator>PeterWilson</dc:creator>
      <dc:date>2021-12-11T21:49:49Z</dc:date>
    </item>
    <item>
      <title>Re: Generate a Function for Map Algebra Expression</title>
      <link>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496430#M38990</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;looks good Peter&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 28 Mar 2016 21:24:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496430#M38990</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2016-03-28T21:24:39Z</dc:date>
    </item>
    <item>
      <title>Re: Generate a Function for Map Algebra Expression</title>
      <link>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496431#M38991</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;A href="https://community.esri.com/migrated-users/4495"&gt;Peter Wilson&lt;/A&gt;​ why did you not consider any of Spatial Analyst's &lt;A href="http://desktop.arcgis.com/en/arcmap/latest/tools/spatial-analyst-toolbox/an-overview-of-the-reclass-tools.htm"&gt;reclass &lt;/A&gt;tools? They now support muti-core processing&lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;-Steve&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 28 Mar 2016 22:20:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496431#M38991</guid>
      <dc:creator>SteveLynch</dc:creator>
      <dc:date>2016-03-28T22:20:49Z</dc:date>
    </item>
    <item>
      <title>Re: Generate a Function for Map Algebra Expression</title>
      <link>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496432#M38992</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Steve... I venture is was because of the speed and pure beauty of numpy&amp;nbsp;&amp;nbsp; &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/wink.png" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 28 Mar 2016 22:26:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496432#M38992</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2016-03-28T22:26:34Z</dc:date>
    </item>
    <item>
      <title>Re: Generate a Function for Map Algebra Expression</title>
      <link>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496433#M38993</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Steve &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt;&lt;/P&gt;&lt;P&gt;I was originally using map algebra Conditional Statement to reclassify my output raster and was looking for a way to build a function for the new classes that I wanted the raster to be reclassified to. Darren and Dan suggested that Numpy would allow me to generate a function to define the classes more cleanly. I must admit I had not considered the reclassify tool, RemapRange would have worked just just as well.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 29 Mar 2016 08:24:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496433#M38993</guid>
      <dc:creator>PeterWilson</dc:creator>
      <dc:date>2016-03-29T08:24:31Z</dc:date>
    </item>
    <item>
      <title>Re: Generate a Function for Map Algebra Expression</title>
      <link>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496434#M38994</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Dan&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I found that the new script that I wrote using Numpy has a further advantage in that it supports reclassifying an integer raster to float raster.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;'''
Created on Mar 19, 2016

@author: PeterW
'''
# import site-packages and modules
import os
import numpy as np
import arcpy
from arcpy.sa import *

# set arguments
input_raster = arcpy.Raster(r"E:\Projects\2016\G112030\SAA\Rasters\euc_road_clip")
output_folder = r"E:\Projects\2016\G112030\SAA\Rasters"

# set environment settings
arcpy.env.overwriteOutput = True
arcpy.env.outputCoordinateSystem = input_raster

# check out extensions
arcpy.CheckOutExtension("Spatial")


def reclass_raster(input_raster, output_folder):
&amp;nbsp;&amp;nbsp;&amp;nbsp; lower_left = arcpy.Point(input_raster.extent.XMin,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; input_raster.extent.YMin)
&amp;nbsp;&amp;nbsp;&amp;nbsp; cell_size = input_raster.meanCellWidth
&amp;nbsp;&amp;nbsp;&amp;nbsp; saa_array = arcpy.RasterToNumPyArray(input_raster)
&amp;nbsp;&amp;nbsp;&amp;nbsp; reclass_array = np.zeros_like(saa_array)
&amp;nbsp;&amp;nbsp;&amp;nbsp; bins = [0, 10, 25, 50, 100, 13069.5234375]
&amp;nbsp;&amp;nbsp;&amp;nbsp; new_bins = [1, 1.25, 1.5, 2, 5]
&amp;nbsp;&amp;nbsp;&amp;nbsp; new_classes = zip(bins[:-1], bins[1:], new_bins)
&amp;nbsp;&amp;nbsp;&amp;nbsp; print(new_classes)
&amp;nbsp;&amp;nbsp;&amp;nbsp; for rc in new_classes:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; q1 = (saa_array &amp;gt;= rc[0])
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; q2 = (saa_array &amp;lt; rc[1])
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; z = np.where(q1 &amp;amp; q2, rc[2], 0)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; reclass_array = reclass_array + z
&amp;nbsp;&amp;nbsp;&amp;nbsp; reclass_raster = arcpy.NumPyArrayToRaster(reclass_array,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lower_left,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x_cell_size=cell_size,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; value_to_nodata=0)
&amp;nbsp;&amp;nbsp;&amp;nbsp; reclass_raster.save(os.path.join(output_folder, "cost_road"))

reclass_raster(input_raster, output_folder)

# check in extensions
arcpy.CheckInExtension("Spatial")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 21:49:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496434#M38994</guid>
      <dc:creator>PeterWilson</dc:creator>
      <dc:date>2021-12-11T21:49:52Z</dc:date>
    </item>
    <item>
      <title>Re: Generate a Function for Map Algebra Expression</title>
      <link>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496435#M38995</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Nice...&amp;nbsp; further reducing steps &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/wink.png" /&gt; &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/laugh.png" /&gt;&amp;nbsp; and showing how arcpy and numpy get along well.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 30 Mar 2016 11:26:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496435#M38995</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2016-03-30T11:26:49Z</dc:date>
    </item>
    <item>
      <title>Re: Generate a Function for Map Algebra Expression</title>
      <link>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496436#M38996</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You numpy fanboys. I mean really.&amp;nbsp; &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/silly.png" /&gt;&lt;/P&gt;&lt;P&gt;Another thing about spatial analyst tools is they are somewhat more likely to work when you throw a 5GB raster at them. As I have been discovering recently processing big chunks of 10m elevation data!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-spatial-analyst/an-overview-of-transformation-classes.htm" title="http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-spatial-analyst/an-overview-of-transformation-classes.htm" rel="nofollow noopener noreferrer" target="_blank"&gt;RemapRange—Spatial Analyst module | ArcGIS for Desktop&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Four lines, my friends! (OK, Dan, six lines with the imports)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;gt;&amp;gt;&amp;gt; import arcpy
&amp;gt;&amp;gt;&amp;gt; from arcpy.sa import *
&amp;gt;&amp;gt;&amp;gt; bins = [0, 10, 25, 50, 100, 13069.5234375] 
&amp;gt;&amp;gt;&amp;gt; new_bins = [1, 1.25, 1.5, 2, 5] 
&amp;gt;&amp;gt;&amp;gt; remap = [[bins&lt;K&gt;, bins[k+1], int(new_bins&lt;K&gt; * 100)] for k in range(len(bins) - 1)]&lt;/K&gt;&lt;/K&gt;
&amp;gt;&amp;gt;&amp;gt; for kk in remap: print(kk)
...
[0, 10, 100]
[10, 25, 125]
[25, 50, 150]
[50, 100, 200]
[100, 13069.5234375, 500]
&amp;gt;&amp;gt;&amp;gt; out_raster =&amp;nbsp; Reclassify("in_raster", "VALUE", RemapRange(remap)) * 0.01&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 21:49:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496436#M38996</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-11T21:49:55Z</dc:date>
    </item>
    <item>
      <title>Re: Generate a Function for Map Algebra Expression</title>
      <link>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496437#M38997</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/laugh.png" /&gt; missing the import lines of course&lt;/P&gt;&lt;P&gt;rows and columns of raster?&lt;/P&gt;&lt;P&gt;assuming it is float of course...&lt;/P&gt;&lt;P&gt;just curious &lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/wink.png" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 09 Apr 2016 05:33:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496437#M38997</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2016-04-09T05:33:15Z</dc:date>
    </item>
    <item>
      <title>Re: Generate a Function for Map Algebra Expression</title>
      <link>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496438#M38998</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Okay, I added the import lines for you. This is Python map algebra, I don't need to worry about the raster dimensions!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I really liked Peter's nifty construction of the remap table below, but I needed to scale the data for the float output he was looking for, so I had to go with my somewhat clunkier (but still so pythonesque) list comprehension.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;new_classes = zip(bins[:-1], bins[1:], new_bins)&amp;nbsp; # cool&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 09 Apr 2016 05:39:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496438#M38998</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2016-04-09T05:39:57Z</dc:date>
    </item>
    <item>
      <title>Re: Generate a Function for Map Algebra Expression</title>
      <link>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496439#M38999</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Oh... it just changes the 'view' of the raster, it doesn't actually produce a new reclassed one (if that is correct)&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 09 Apr 2016 05:47:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496439#M38999</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2016-04-09T05:47:29Z</dc:date>
    </item>
    <item>
      <title>Re: Generate a Function for Map Algebra Expression</title>
      <link>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496440#M39000</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;It's not a view but a "temporary raster" in arcpy map algebra... out_raster will be written to a real raster as soon as it's needed in a later map algebra expression. Including if you save it with&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'courier new', courier;"&gt;out_raster.save("outraster.tif")&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In ArcMap at the python prompt, however, it gets calculated and written right away to a temp grid in the scratch folder so it can be added to the map (if addOutputsToMap is set).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The dimensions of the output are determined by the currently active environment settings.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 09 Apr 2016 05:55:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496440#M39000</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2016-04-09T05:55:05Z</dc:date>
    </item>
    <item>
      <title>Re: Generate a Function for Map Algebra Expression</title>
      <link>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496441#M39001</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;yes, one of several ways to generate it using LCs, but be cautious of zip&lt;/P&gt;&lt;P&gt;When people start using python 3.x + make sure to use&amp;nbsp; &lt;STRONG&gt;list(zip(....))&lt;/STRONG&gt;&amp;nbsp; or you will get a zip object... which is useful, or not depending upon what you need to do.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;bins&amp;nbsp; = [0, 10, 25, 50, 100, 13079]&lt;/P&gt;&lt;P&gt;new_bins = [1, 1.25, 1.5, 2, 5]&lt;/P&gt;&lt;P&gt;nc = [ (bins[i-1], bins&lt;I&gt;, new_bins[i-1]) for i in range(1,len(bins)) ]&amp;nbsp; # safe in python 2.x and 3.x&lt;/I&gt;&lt;/P&gt;&lt;P&gt;or&lt;/P&gt;&lt;P&gt;zip(bins[:-1],bins[1:],new_bins)&amp;nbsp; # python 2.x only&lt;/P&gt;&lt;P&gt;or&lt;/P&gt;&lt;P&gt;list(zip(blah)) # in python 3.x &lt;/P&gt;&lt;P&gt;yields&lt;/P&gt;&lt;P&gt;[(0, 10, 1), (10, 25, 1.25), (25, 50, 1.5), (50, 100, 2), (100, 13079, 5)]&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 09 Apr 2016 06:44:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/generate-a-function-for-map-algebra-expression/m-p/496441#M39001</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2016-04-09T06:44:43Z</dc:date>
    </item>
  </channel>
</rss>

