---
title: "Raster math with arcpy"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Raster Math}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---

The `arcpy` package, via
[`reticulate`](https://cran.r-project.org/package=reticulate),
supports Raster math operations. This vignette demonstrates basic
usage of arcpy Raster Algebra from R. First, we'll set up a
workspace and create some temporary rasters.


```r
library(arcpy)
```


```r
arcpy$env$workspace = tempdir()
arcpy$env$scratchWorkspace = tempdir()

cellSize = 2
outExtent = arcpy$Extent(0, 0, 250, 250)

# Check out the ArcGIS Spatial Analyst extension license
arcpy$CheckOutExtension("Spatial")
```

```
## [1] "CheckedOut"
```

```r
# Execute CreateConstantRaster
raster1 = arcpy$sa$CreateConstantRaster(12, "FLOAT",
  cellSize, outExtent)

raster2 = arcpy$sa$CreateConstantRaster(6, "FLOAT",
  cellSize, outExtent)
```

## Math operations

All of the basic mathematical operations are supported. Usage
follows the 
[R operator symbology](https://stat.ethz.ch/R-manual/R-devel/library/base/html/Arithmetic.html), 
rather than the 
[Python operator symbology](https://docs.python.org/3.4/library/operator.html#mapping-operators-to-functions)
or [arcpy operator symbology](http://pro.arcgis.com/en/pro-app/arcpy/spatial-analyst/an-overview-of-the-map-algebra-operators.htm)
(e.g., Modulus is achieved using `%%`, not `%`, and exponentiation is achieved
using `^` rather than `pow()` or `**`).


```r
raster3 = raster1 + raster2
arcpy$management$GetRasterProperties(raster3, "MAXIMUM")
```

```
## <Result '18'>
```

```r
raster3 = raster1 - raster2
arcpy$management$GetRasterProperties(raster3, "MAXIMUM")
```

```
## <Result '6'>
```

```r
raster3 = raster1 * raster2
arcpy$management$GetRasterProperties(raster3, "MAXIMUM")
```

```
## <Result '72'>
```

```r
raster3 = raster1 / raster2
arcpy$management$GetRasterProperties(raster3, "MAXIMUM")
```

```
## <Result '2'>
```

```r
raster3 = raster1 ^ raster2
arcpy$management$GetRasterProperties(raster3, "MAXIMUM")
```

```
## <Result '2985984'>
```

```r
raster3 = raster1 %/% raster2
arcpy$management$GetRasterProperties(raster3, "MAXIMUM")
```

```
## <Result '2'>
```

```r
raster3 = -raster1
arcpy$management$GetRasterProperties(raster3, "MAXIMUM")
```

```
## <Result '-12'>
```

```r
# compound statements work too
raster3 = (raster1 / raster2) * (raster2 - 3.0)
arcpy$management$GetRasterProperties(raster3, "MAXIMUM")
```

```
## <Result '6'>
```

## Logical operators

Logical operators, including compound operations, work on rasters too.


```r
raster3 = raster1 > raster2
arcpy$management$GetRasterProperties(raster3, "MAXIMUM")
```

```
## <Result '1'>
```

```r
raster3 = raster1 < raster2
arcpy$management$GetRasterProperties(raster3, "MAXIMUM")
```

```
## <Result '0'>
```

```r
raster3 = raster1 != raster2
arcpy$management$GetRasterProperties(raster3, "MAXIMUM")
```

```
## <Result '1'>
```

```r
raster3 = raster1 == raster2
arcpy$management$GetRasterProperties(raster3, "MAXIMUM")
```

```
## <Result '0'>
```

```r
raster3 = raster1 >= 12
arcpy$management$GetRasterProperties(raster3, "MAXIMUM")
```

```
## <Result '1'>
```

```r
raster3 = raster2 <= 6
arcpy$management$GetRasterProperties(raster3, "MAXIMUM")
```

```
## <Result '1'>
```

```r
# compound statements work as expected too
raster3 = (raster1 > 10) & (raster2 > 10)
arcpy$management$GetRasterProperties(raster3, "MAXIMUM")
```

```
## <Result '0'>
```

```r
raster3 = (raster1 > 10) | (raster2 > 10)
arcpy$management$GetRasterProperties(raster3, "MAXIMUM")
```

```
## <Result '1'>
```

```r
# The not operator works in R too
raster3 = !(raster2 > 10)
arcpy$management$GetRasterProperties(raster3, "MAXIMUM")
```

```
## <Result '1'>
```

```r
# or you can do
raster3 = arcpy$sa$BooleanNot(raster2 > 10)
arcpy$management$GetRasterProperties(raster3, "MAXIMUM")
```

```
## <Result '1'>
```


