<?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: How to use Min/Max values of raster in Python script in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-to-use-min-max-values-of-raster-in-python/m-p/367763#M29007</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Can you explain this further? I was under the impression that once a raster was included in the expression, it is interpreted as map algebra. For example, similar to the original post:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;inRaster1 = arcpy.Raster(&lt;SPAN style="color: #000000; font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;"&gt;r&lt;/SPAN&gt;&lt;SPAN class="string" style="font-size: 12px; font-family: Consolas, 'Courier New', Courier, mono, serif; color: blue;"&gt;"C:\DATA\RASTER\DEM\example")&lt;/SPAN&gt;
&lt;SPAN style="color: #000000; font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;"&gt;elevMINResult = arcpy.GetRasterProperties_management(inRaster1, &lt;/SPAN&gt;&lt;SPAN class="string" style="font-size: 12px; font-family: Consolas, 'Courier New', Courier, mono, serif; color: blue;"&gt;"MINIMUM"&lt;/SPAN&gt;&lt;SPAN style="font-size: 12px; font-family: Consolas, 'Courier New', Courier, mono, serif; color: #000000;"&gt;) &lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;elevMin = float(elevMINResult.getOutput(&lt;SPAN class="number" style="font-weight: inherit; font-style: inherit; color: green; font-size: 9pt !important; background-color: inherit;"&gt;0&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt;))&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN style="color: #000000; font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;"&gt;outMinus = inRaster1 - elevMin&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;"&gt;I believe this should work, producing a new raster minus the constant, elevMin. However, I'm not clear on how Python actually decides to apply a map algebra expression on what may appear to be a normal expression.&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 17:04:11 GMT</pubDate>
    <dc:creator>DarrenWiens2</dc:creator>
    <dc:date>2021-12-11T17:04:11Z</dc:date>
    <item>
      <title>How to use Min/Max values of raster in Python script</title>
      <link>https://community.esri.com/t5/python-questions/how-to-use-min-max-values-of-raster-in-python/m-p/367760#M29004</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I would like to use Min/Max values of my raster data in Python script. However, I cannot find an information how to implement these values properly in script. My script is as follow:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;#Get the geoprocessing result object&lt;/P&gt;&lt;P&gt;elevMINResult = arcpy.GetRasterProperties_management("6", "MINIMUM")&lt;/P&gt;&lt;P&gt;elevMAXResult = arcpy.GetRasterProperties_management("6", "MAXIMUM")&lt;/P&gt;&lt;P&gt;#Get the elevation min/max value from geoprocessing result object&lt;/P&gt;&lt;P&gt;elevMin = elevMINResult.getOutput(0)&lt;/P&gt;&lt;P&gt;elevMax = elevMAXResult.getOutput(0)&lt;/P&gt;&lt;P&gt;# Set local variables&lt;/P&gt;&lt;P&gt;inRaster1 = Raster("6")&lt;/P&gt;&lt;P&gt;# Execute Minus&lt;/P&gt;&lt;P&gt;outMinus = ((inRaster1 - (elevMin)))/((elevMax) - (elevMin))&lt;/P&gt;&lt;P&gt;# Save the output&lt;/P&gt;&lt;P&gt;outMinus.save("C:/out")&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;After running the script, I have the following error:&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;ERROR 000732: Dataset -1.79666268825531 does not exist or is not supported. &lt;/SPAN&gt;-1.79666268825531 is elevMin in my data&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Does anyone know how to implement Min/Max values to Python script?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 27 Jul 2015 15:30:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-use-min-max-values-of-raster-in-python/m-p/367760#M29004</guid>
      <dc:creator>KarolinaKorzeniowska</dc:creator>
      <dc:date>2015-07-27T15:30:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to use Min/Max values of raster in Python script</title>
      <link>https://community.esri.com/t5/python-questions/how-to-use-min-max-values-of-raster-in-python/m-p/367761#M29005</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;In addition to Jake's answer (now deleted), here's why you need to cast to float.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;elevMin = float(elevMINResult.getOutput(0))&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;getOutput(0) returns either a record set, string, or layer. In your case it returns a string.&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; print type(elevMINResult.getOutput(0))
&amp;lt;type 'unicode'&amp;gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In turn, your map algebra expression interprets the value as a string, or raster name.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:04:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-use-min-max-values-of-raster-in-python/m-p/367761#M29005</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2021-12-11T17:04:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to use Min/Max values of raster in Python script</title>
      <link>https://community.esri.com/t5/python-questions/how-to-use-min-max-values-of-raster-in-python/m-p/367762#M29006</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Karolina,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;You will also need to call the Minus and Divide functions.&amp;nbsp; Ex:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
arcpy.CheckOutExtension("Spatial")
from arcpy.sa import *

inRaster1 = r"C:\DATA\RASTER\DEM\example"

elevMINResult = arcpy.GetRasterProperties_management(inRaster1, "MINIMUM")
elevMAXResult = arcpy.GetRasterProperties_management(inRaster1, "MAXIMUM")

elevMin = float(elevMINResult.getOutput(0))
elevMax = float(elevMAXResult.getOutput(0))

divideValue = (elevMax - elevMin)

outMinus = Minus(inRaster1, elevMin)
outDivide = Divide(outMinus, divideValue)

outDivide.save(r"C:\temp\output.tif")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:04:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-use-min-max-values-of-raster-in-python/m-p/367762#M29006</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2021-12-11T17:04:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to use Min/Max values of raster in Python script</title>
      <link>https://community.esri.com/t5/python-questions/how-to-use-min-max-values-of-raster-in-python/m-p/367763#M29007</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Can you explain this further? I was under the impression that once a raster was included in the expression, it is interpreted as map algebra. For example, similar to the original post:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;inRaster1 = arcpy.Raster(&lt;SPAN style="color: #000000; font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;"&gt;r&lt;/SPAN&gt;&lt;SPAN class="string" style="font-size: 12px; font-family: Consolas, 'Courier New', Courier, mono, serif; color: blue;"&gt;"C:\DATA\RASTER\DEM\example")&lt;/SPAN&gt;
&lt;SPAN style="color: #000000; font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;"&gt;elevMINResult = arcpy.GetRasterProperties_management(inRaster1, &lt;/SPAN&gt;&lt;SPAN class="string" style="font-size: 12px; font-family: Consolas, 'Courier New', Courier, mono, serif; color: blue;"&gt;"MINIMUM"&lt;/SPAN&gt;&lt;SPAN style="font-size: 12px; font-family: Consolas, 'Courier New', Courier, mono, serif; color: #000000;"&gt;) &lt;/SPAN&gt;
&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;elevMin = float(elevMINResult.getOutput(&lt;SPAN class="number" style="font-weight: inherit; font-style: inherit; color: green; font-size: 9pt !important; background-color: inherit;"&gt;0&lt;/SPAN&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; font-size: 9pt !important; background-color: inherit;"&gt;))&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;SPAN style="color: #000000; font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;"&gt;outMinus = inRaster1 - elevMin&lt;/SPAN&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #000000; font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px;"&gt;I believe this should work, producing a new raster minus the constant, elevMin. However, I'm not clear on how Python actually decides to apply a map algebra expression on what may appear to be a normal expression.&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:04:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-use-min-max-values-of-raster-in-python/m-p/367763#M29007</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2021-12-11T17:04:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to use Min/Max values of raster in Python script</title>
      <link>https://community.esri.com/t5/python-questions/how-to-use-min-max-values-of-raster-in-python/m-p/367764#M29008</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Suppose you have a raster called "DEM" (in your TOC), you could use the maximum and minimum properties directly like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
dem = Raster("DEM")
result = (dem - dem.minimum) / (dem.maximum - dem.minimum)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This will generate a raster with values ranging from 0 (minimum) to 1 (maximum).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What does the "6" stand for in your code? Is this the name of a raster in your TOC or a value that you want to use as constant?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:04:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-use-min-max-values-of-raster-in-python/m-p/367764#M29008</guid>
      <dc:creator>XanderBakker</dc:creator>
      <dc:date>2021-12-11T17:04:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to use Min/Max values of raster in Python script</title>
      <link>https://community.esri.com/t5/python-questions/how-to-use-min-max-values-of-raster-in-python/m-p/367765#M29009</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You can include a raster in an expression, but you need to specify the raster first, as &lt;A href="https://community.esri.com/migrated-users/3100" target="_blank"&gt;Xander Bakker&lt;/A&gt; mentions, using a &lt;A href="http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-classes/raster-object.htm" rel="nofollow noopener noreferrer" target="_blank"&gt;raster object&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For example, the following will produce the error '&lt;STRONG&gt;TypeError: unsupported operand type(s) for -: 'str' and 'float'&lt;/STRONG&gt;':&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;inRaster1 = ("int_example1")

elevMINResult = arcpy.GetRasterProperties_management(r"C:\DATA\RASTER\DEM\int_example1", "MINIMUM")
elevMAXResult = arcpy.GetRasterProperties_management(r"C:\DATA\RASTER\DEM\int_example1", "MAXIMUM")

elevMin = float(elevMINResult.getOutput(0))
elevMax = float(elevMAXResult.getOutput(0))

outMinus = ((inRaster1 - (elevMin)))/((elevMax) - (elevMin))
outMinus.save(r"C:\temp\python\output.tif")&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However, the following will work:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;inRaster1 = arcpy.Raster("int_example1")

elevMINResult = arcpy.GetRasterProperties_management(r"C:\DATA\RASTER\DEM\int_example1", "MINIMUM")
elevMAXResult = arcpy.GetRasterProperties_management(r"C:\DATA\RASTER\DEM\int_example1", "MAXIMUM")

elevMin = float(elevMINResult.getOutput(0))
elevMax = float(elevMAXResult.getOutput(0))

outMinus = ((inRaster1 - (elevMin)))/((elevMax) - (elevMin))
outMinus.save(r"C:\temp\python\output.tif")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:04:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-use-min-max-values-of-raster-in-python/m-p/367765#M29009</guid>
      <dc:creator>JakeSkinner</dc:creator>
      <dc:date>2021-12-11T17:04:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to use Min/Max values of raster in Python script</title>
      <link>https://community.esri.com/t5/python-questions/how-to-use-min-max-values-of-raster-in-python/m-p/367766#M29010</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you for all the answers. I tried to implement both solution, and both works fine.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;"6" in my code is the result map, like 1, 2, 3, 4, 5. I am trying to implement saving the intermediate results in the memory, but sometimes I have errors, so for now it is 1, 2, 3, 4...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have one more question, related to Jake's code. Once he is writing subtraction using minus and once using comma. What is more correct. It depend on the input data, or something else?&lt;/P&gt;&lt;OL class="dp-py" start="1" style="font-size: 12px; font-family: Consolas, 'Courier New', Courier, mono, serif; color: #5c5c5c; margin-bottom: 1px !important; margin-left: 45px !important;"&gt;&lt;LI&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;divideValue = (elevMax - elevMin)&amp;nbsp; &lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN style="font-weight: inherit; font-style: inherit; color: black; font-size: 9pt !important; background-color: inherit;"&gt;outMinus = Minus(inRaster1, elevMin)&lt;/SPAN&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;And also one more question regarding the results. At first I built my script using Model Builder. In model builder by taking min and max value of raster I have never received 0 (zero) as the smallest value but something like 2.5569548399738e-015 or similar. Using Jake's and your code for Python the smallest value is 0 (zero). Is there something wrong with model Builder?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 28 Jul 2015 06:18:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-use-min-max-values-of-raster-in-python/m-p/367766#M29010</guid>
      <dc:creator>KarolinaKorzeniowska</dc:creator>
      <dc:date>2015-07-28T06:18:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to use Min/Max values of raster in Python script</title>
      <link>https://community.esri.com/t5/python-questions/how-to-use-min-max-values-of-raster-in-python/m-p/367767#M29011</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Karolina, up-thread this was discussed some. Esri has implemented their raster tools so they can work easily in Python syntax, so what the "-" does depends on the input. For example this python code will set result to the integer value 1, and result2 to a float (float - integer =&amp;gt; integer). So the result depends on the inputs.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;x = 1
y = 2
result = y - x
y = 2.0
result2 = y - x&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Similarly if you subtract a raster object from an integer, you'll get a raster out.&lt;/P&gt;&lt;P&gt;The map algebra syntax has the advantage that it is easy to read, write, and debug, and you can create complex expressions that can nest your processing very efficiently -- instead of running a bunch of tools one at a time.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;from arcpy.sa import *
x = 1
y = Raster("y.tif") # convert a text path to a raster object
# the following three lines are entirely equivalent (they do the same thing)
result3 = y - x 
result3 = Subtract(y, x)
result3 = Subtract("y.tif", x)
result3.save("z.tif")&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Hopefully my little discussion above will help you make more sense of the &lt;A href="http://resources.arcgis.com/en/help/main/10.2/index.html#//00p600000002000000" rel="nofollow noopener noreferrer" target="_blank"&gt;help pages on arcpy map algebra.&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:04:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-use-min-max-values-of-raster-in-python/m-p/367767#M29011</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-11T17:04:19Z</dc:date>
    </item>
  </channel>
</rss>

