-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr_raster_basics.r
48 lines (36 loc) · 1.32 KB
/
r_raster_basics.r
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
# This is how you can get going with raster data handling in R
# The Swiss Army knife for spatial infromation
require(rgdal)
# Large raster handling and analysis
require(raster)
# You can even read and write NetCDF files if you really want to
require(ncdf)
# Work with projections like pro
require(proj4)
# Open
r <- raster("C:\\temp\\eu_dir_15s_sample.tif")
# you can check various properties like
# projection, resolution, dimensions, etc.
print(r)
# or access individual properties
projection(r)
res(r)
nrow(r)
ncol(r)
dim(r)
bbox(r)
xmax(r)
filename(r)
plot(r)
vals <- getValues(r, 100:110, 150:180)
# create a new raster from a matrix
m<-raster(matrix(runif(100000), 1000, 1000))
# defining projection is a breeze with proj4 and EPSG codes
# WGS84 is 4326, British National Grid is 27700,... (see http://epsg.io)
projection(m)<-CRS("+init=EPSG:27700")
# rgdal allows you to save in variety of formats, just a basic geotiff example:
rtf <- writeRaster(m, filename="c:\\temp\\my.tif", format="GTiff", overwrite=TRUE)
# if you have netcdf library and ncdf package, you can go wild and save it as NetCDF
rnc <- writeRaster(m, filename="c:\\temp\\yours.nc", format="CDF", overwrite=TRUE)
# You can do various calculations and analyses with rasters too.
# See way more at http://cran.r-project.org/web/packages/raster/vignettes/Raster.pdf