Sometimes it can be a challenge to find the right ArcGIS Pro functions and not everything you're familiar with the ArcGIS interface is readily available in Python. However, there are a huge number of functions available. Sometimes you just have to do things differently than you might first expect. For example:
Below are some of the functions I use with ArcGIS Pro. Reference Esri's online information for more details and examples.
You've already seen that many of the raster functions return a "Raster Object" that you can store in a variable. You can also just load a raster into a new raster object and then access the raster's properties as below.
TheRaster = Raster("c:/Temp/Tiny.img") print(TheRaster.extent) print(TheRaster.bandCount)
print(TheRaster.height)
print(TheRaster.width)
Then, you can perform mathematric operations on rasters. The result will be a new raster where the opeation is applied to each matching pixel in the two rasters, just as with Raster Calculator in ArcMap.
# Mathematical operators Result=Raster1 * Raster2 # mulitplication Result=Raster1 / Raster2 # division Result=Raster1 + Raster2 # addtion Result=Raster1 - Raster2 # subtraction # Boolean operations. These operations produce pixels with a 1 (true) or 0 (false) value Result=Raster1 | Raster2 # Boolean Or Result=Raster1 & Raster2 # Boolean And # comparison operators. These operations produce pixels with a 1 (true) or 0 (false) value Result=Raster1 == Raster2 # Equal to Result=Raster1 != Raster2 # Not equal Result=Raster1 > Raster2 # Greater than Result=Raster1 >= Raster2 # Greater than or equal to Result=Raster1 < Raster2 # Less than Result=Raster1 <= Raster2 # Less than or equal to
Note: This was discovered by Kris in 318 in the Spring 2020 class
After you are done with the calculations, you can save the raster.
Raster1.save("C:/Temp/Output.img")
The functions below perform "math" on rasters. In other words, if you multiple two rasters together, each pixel will be the result of multiplying the pixel from the first raster that is in the same location as the resulting pixel with a pixel in the second rasters that is also in the same spatial location.
You can pass in a file path, name of a raster in the workspace (after workspace is set), or a current raster for the input raster in these functions. The output raster can then be saved to a file as:
OutRaster=arcpy.sa.Plus("C:/Temp/FirstRaster.img","C:/Temp/SecondRaster.img") OutRaster.save("C:/Temp/Result.img")
Name | Description |
---|---|
Arithmetic | |
arcpy.sa.Plus(InRasterOrConstant,InRasterOrContant) | Add the two rasters together |
arcpy.sa.Minus(InRasterOrConstant,InRasterOrContant) | Subtract the second raster from the first |
arcpy.sa.Times(InRasterOrConstant,InRasterOrConstant) | Multiply the first raster by the second |
arcpy.sa.Divide(InRasterOrConstant,InRasterOrConstant) | Divide the first raster by the second |
arcpy.sa.FloatDivide(InRasterOrConstant,InRasterOrContant) | Divides the first raster by the second (not sure when this is needed, for floating-point rasters?) |
These functions return a boolean raster based on a comparison between two other rasters.
Name | Description |
---|---|
arcpy.sa.EqualTo(InRasterOrContsant,InRasterOrConstant) | Returns a raster with pixels set to 1 |
arcpy.sa.GreaterThan(InRasterOrContsant,InRasterOrConstant) | |
arcpy.sa.GreaterThanEqual(InRasterOrContsant,InRasterOrConstant) | |
arcpy.sa.LessThan(InRasterOrContsant,InRasterOrConstant) | |
arcpy.sa.LessThanEqual(InRasterOrContsant,InRasterOrConstant) |
These operations work with rasters that have pixels set to 0 ("false") or 1 ("true"). These are also known as "bit masks" or "boolean rasters" and can be used to manipulate areas within a raster.
Name | Description |
---|---|
arcpy.sa.BooleanAnd(RasterOrConstant,RasterOrConstant) | |
arcpy.sa.BooleanNot(InRaster) | |
arcpy.sa.BooleanOr(RasterOrConstant,RasterOrConstant) | Performs a boolean "OR" on the two rasters |
arcpy.sa.Con(ConditiontalRaster,InTrueRaster,InFalseRaster) | Performance a conditional operation. The resulting raster will have values from the "true" raster where the pixels in the ConditionalRaster are true and values from the "false" raster where pixels in the Conditional raster are false. |
© Copyright 2018 HSU - All rights reserved.