forked from charlottegermain/RasterPkgDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRasterPkgPresCharlotte.Rpres
96 lines (77 loc) · 1.99 KB
/
RasterPkgPresCharlotte.Rpres
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
Raster Package
========================================================
author: Charlotte
date: April 17th, 2015
Working with raster files
========================================================
The Raster package allows you to import, manipulate and project maps and their data. There are TONS of functions in raster!!!
- Importing a raster file
- plotting the map
- cell statistics
- Cropping the map to the desired area
- raster to points, distance from points
- stacking
- layers statistics
- extracting values
- exporting
Importing a map
========================================================
```{r}
library(raster)
##alt <- raster(url("https://raw.githubusercontent.com/charlottegermain/RasterPkgDemo/master/data/alt.asc"))
alt <- raster("data/alt.asc")
alt
LATLON<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0")
projection(alt) <- LATLON
alt
```
stats and plots
========================================================
```{r}
plot(alt)
plot(alt, col=c("blue", "red", "green"))
min <- cellStats(alt, min)
min
max <- cellStats(alt, max)
max
breakpoints <- c(-50, 50, 100, 330)
colors <- c("purple", "tomato", "yellow")
plot(alt, breaks=breakpoints, col=colors)
```
Cropping a map
========================================================
```{r}
library(maptools)
Florida <- readShapePoly("data/FLstate2.shp")
projection(Florida)<- LATLON
Map <- crop(alt, Florida)
plot(Map)
Map1 <- mask (Map, Florida)
plot(Map1)
writeRaster(Map1, "Map1.asc", format="ascii", overwrite=TRUE)
```
Stacking Maps
========================================================
```{r}
Map2 <- raster("data/bio2.asc")
Map2
Map1
projection(Map2)<- LATLON
stack <- stack(Map1, Map2)
stack
plot(stack)
Average <- calc(stack, mean)
plot(Average)
fun <- function(x) {x*10}
Times <- calc(stack, fun)
plot(Times)
```
Extracting values
========================================================
```{r}
spLat <- c(26, 28)
spLong <- c(-81, -82)
xy <- cbind(spLong, spLat)
values <- extract(stack, xy, method="simple")
values
```