-
Notifications
You must be signed in to change notification settings - Fork 0
/
raster-math.rmd
229 lines (175 loc) · 3.84 KB
/
raster-math.rmd
1
2
3
4
5
6
7
8
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
---
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'>
```