Professional Documents
Culture Documents
library(terra)
Cargamos un archivo tipo raster
find("rast")
## [1] "package:terra"
RasDEM
## class : SpatRaster
## dimensions : 1650, 978, 1 (nrow, ncol, nlyr)
## resolution : 15.03529, 15.03529 (x, y)
## extent : 295896.6, 310601.1, 2006739, 2031547 (xmin, xmax, ymin, ymax)
## coord. ref. : WGS 84 / UTM zone 15N (EPSG:32615)
## source : RasterDEM.tif
## name : RasterDEM
2030000
1600
1400
2025000 1200
1000
2020000
800
2015000
600
400
2010000
200
RasDEM
## class : SpatRaster
## dimensions : 1650, 978, 1 (nrow, ncol, nlyr)
## resolution : 15.03529, 15.03529 (x, y)
## extent : 295896.6, 310601.1, 2006739, 2031547 (xmin, xmax, ymin, ymax)
## coord. ref. : WGS 84 / UTM zone 15N (EPSG:32615)
## source : RasterDEM.tif
## name : RasterDEM
RasDEM[1]
## RasterDEM
## 1 917
RasDEM[[1]]
## class : SpatRaster
## dimensions : 1650, 978, 1 (nrow, ncol, nlyr)
## resolution : 15.03529, 15.03529 (x, y)
## extent : 295896.6, 310601.1, 2006739, 2031547 (xmin, xmax, ymin, ymax)
## coord. ref. : WGS 84 / UTM zone 15N (EPSG:32615)
## source : RasterDEM.tif
## name : RasterDEM
Atributos
names(RasDEM)
## [1] "RasterDEM"
head(RasDEM$RasterDEM,12)
## RasterDEM
## 1 917
## 2 919
## 3 919
## 4 919
## 5 919
## 6 920
## 7 921
## 8 924
## 9 927
## 10 931
## 11 935
## 12 940
RasDEM$RasterDEM[]
## RasterDEM
## [1,] 917
## [2,] 919
## [3,] 919
## [4,] 919
## [5,] 919
## [6,] 920
## [7,] 921
## [8,] 924
## [9,] 927
Estadística
Podemos ver la altura mínima del mapa, con min()
min(RasDEM[[1]][])
## [1] 39
## [1] 1682
o de manera alternativa. . .
quantile(RasDEM$RasterDEM[], probs = c(0.25,0.5, 0.75), type=7,names = FALSE)
Histogram of RasDEM$RasterDEM[]
250000
200000
150000
Frequency
100000
50000
0
RasDEM$RasterDEM[]
Extent
ext(RasDEM)
ext(RasDEM)[2] - ext(RasDEM)[1]
ext(RasDEM)[4] - ext(RasDEM)[3]
## xmax
## 14704.51
## ymax
## 24808.23
class(values(RasDEM))
## RasterDEM
## 1 917
## 2 919
## 3 919
## 4 919
## 5 919
## 6 920
Otros comandos
dim(RasDEM)
res(RasDEM)
ncell(RasDEM)
## [1] 1613700
ncol(RasDEM)
## [1] 978
nrow(RasDEM)
## [1] 1650
crs(RasDEM)
SPolInfluencia
## class : SpatVector
## geometry : polygons
## dimensions : 1, 0 (geometries, attributes)
## extent : 299882.7, 303746.1, 2011527, 2024702 (xmin, xmax, ymin, ymax)
## coord. ref. :
plot(SPolInfluencia,col='red')
2024000
2022000
2020000
2018000
2016000
2014000
2012000
3e+05
Graficamos
plot(RasDEM,main='Río Soteapan')
plot(SPolInfluencia,col='red',add=TRUE)
lines(SVL,col='blue',lwd=1)
points(SVP,col='black',pch=19,cex=1.0)
Río Soteapan
2030000
1600
1400
2025000
1200
1000
2020000
800
2015000
600
400
2010000
200
## ID RasterDEM
## 1 1 847
## 2 1 831
## 3 1 784
## 4 1 674
## 5 1 596
## 6 1 399
## 7 1 199
## 8 1 174
head(extract(RasDEM,SPolInfluencia),12)
## ID RasterDEM
## 1 1 851
## 2 1 850
## 3 1 850
## 4 1 850
## 5 1 858
## 6 1 854
## 7 1 852
## 8 1 850
## 9 1 850
## 10 1 850
## 11 1 849
## 12 1 850
dim(extract(RasDEM,SPolInfluencia))
## [1] 18397 2
Gráfica
z<-extract(RasDEM,SPolInfluencia)
class(z[2][,])
## [1] "numeric"
plot(z[2][,],main="Alturas",type="l")
Alturas
800
600
z[2][, ]
400
200
Index
Crearemos un raster desde cero
Vamos a crear un raster. Hay que especificar el número de columnas y filas, así como su extensión Podemos pensar
en un raster como una matriz, es decir una malla cuadriculada pero con una extensión.
RasPrueba1 <- rast(ncol=10, nrow=10, xmin=-100, xmax=100, ymin=-200, ymax=200)
RasPrueba1
## class : SpatRaster
## dimensions : 10, 10, 1 (nrow, ncol, nlyr)
## resolution : 20, 40 (x, y)
## extent : -100, 100, -200, 200 (xmin, xmax, ymin, ymax)
## coord. ref. :
## lyr.1
## [1,] NaN
## [2,] NaN
## [3,] NaN
## [4,] NaN
## [5,] NaN
## [6,] NaN
Sigue
## class : SpatRaster
## dimensions : 10, 10, 1 (nrow, ncol, nlyr)
## resolution : 20, 40 (x, y)
## extent : -100, 100, -200, 200 (xmin, xmax, ymin, ymax)
## coord. ref. :
Asignamos valores
Asignamos valores
values(RasPrueba1) <- 1:ncell(RasPrueba1)
head(RasPrueba1[],20)
## lyr.1
## [1,] 1
## [2,] 2
## [3,] 3
## [4,] 4
## [5,] 5
## [6,] 6
## [7,] 7
## [8,] 8
## [9,] 9
## [10,] 10
## [11,] 11
## [12,] 12
## [13,] 13
## [14,] 14
## [15,] 15
## [16,] 16
## [17,] 17
## [18,] 18
## [19,] 19
## [20,] 20
Capa Raster
200
100
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
80
100
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
60
41 42 43 44 45 46 47 48 49 50
0
51 52 53 54 55 56 57 58 59 60
40
61 62 63 64 65 66 67 68 69 70
−100
71 72 73 74 75 76 77 78 79 80
20
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
−200
−100 0 100
## class : SpatRaster
## dimensions : 10, 10, 1 (nrow, ncol, nlyr)
## resolution : 20, 40 (x, y)
## extent : -100, 100, -200, 200 (xmin, xmax, ymin, ymax)
## coord. ref. :
## source(s) : memory
## name : lyr.1
## min value : 1
## max value : 100
RasPrueba1[73]
## lyr.1
## 1 73
RasPrueba1[7,5]
## lyr.1
## 1 65
Más complejo
RasPrueba1[4,]
## lyr.1
## 1 31
## 2 32
## 3 33
## 4 34
## 5 35
## 6 36
## 7 37
## 8 38
## 9 39
## 10 40
RasPrueba1[,6]
## lyr.1
## 1 6
## 2 16
## 3 26
## 4 36
## 5 46
## 6 56
## 7 66
## 8 76
## 9 86
## 10 96
Otro
RasPrueba1[c(5,3,1),(5:8)]
## lyr.1
## 1 45
## 2 46
## 3 47
## 4 48
## 5 25
## 6 26
## 7 27
## 8 28
## 9 5
## 10 6
## 11 7
## 12 8
## [1] "lyr.1"
RasPrueba1$lyr.1
## class : SpatRaster
## dimensions : 10, 10, 1 (nrow, ncol, nlyr)
## resolution : 20, 40 (x, y)
## extent : -100, 100, -200, 200 (xmin, xmax, ymin, ymax)
## coord. ref. :
## source(s) : memory
## name : lyr.1
## min value : 1
## max value : 100
RasPrueba1
## class : SpatRaster
## dimensions : 10, 10, 1 (nrow, ncol, nlyr)
## resolution : 20, 40 (x, y)
## extent : -100, 100, -200, 200 (xmin, xmax, ymin, ymax)
## coord. ref. :
## source(s) : memory
## name : Atributo
Multicapa
Se pueden agregar más capas de atributos y hacer operaciones algebraicas entre ellas
names(RasMultiCapa)
## [1] "Atributo"
names(RasMultiCapa)
Atributo1 Atributo2
200
200
100 10000
80 8000
100
100
60 6000
0
0
40 4000
−100
−100
20 2000
−200
−200
−100 0 100 −100 0 100
Atributo3 Atributo4
200
200
20000
100
100
100.5 15000
100.0 10000
0
0
−100
−100
99.5 5000
−200
−200
200
10000
1 4 9 16 25 36 49 64 81 100
121 144 169 196 225 256 289 324 361 400
8000
100
441 484 529 576 625 676 729 784 841 900
961 1024 1089 1156 1225 1296 1369 1444 1521 1600
6000
1681 1764 1849 1936 2025 2116 2209 2304 2401 2500
0
2601 2704 2809 2916 3025 3136 3249 3364 3481 3600
4000
3721 3844 3969 4096 4225 4356 4489 4624 4761 4900
−100
5041 5184 5329 5476 5625 5776 5929 6084 6241 6400
2000
6561 6724 6889 7056 7225 7396 7569 7744 7921 8100
8281 8464 8649 8836 9025 9216 9409 9604 9801 10000
−200
−100 0 100
Raster con Valor constante
RasMultiCapa$Atributo5 <- 20
plot(RasMultiCapa$Atributo5,xaxt="n",yaxt="n")
200
20
100
0
−100
−200
−100 0 100
Raster con NA
RasMultiCapa$Atributo6 <- NA
Echemos otra vez un vistazo general a la estructura del archivo tipo RasterLayer
RasPrueba1
## class : SpatRaster
## dimensions : 10, 10, 1 (nrow, ncol, nlyr)
## resolution : 20, 40 (x, y)
## extent : -100, 100, -200, 200 (xmin, xmax, ymin, ymax)
## coord. ref. :
## source(s) : memory
## name : Atributo
## min value : 1
## max value : 100
## class : SpatRaster
## dimensions : 10, 10, 6 (nrow, ncol, nlyr)
## resolution : 20, 40 (x, y)
## extent : -100, 100, -200, 200 (xmin, xmax, ymin, ymax)
## coord. ref. :
## source(s) : memory
## names : Atributo1, Atributo2, Atributo3, Atributo4, Atributo5, Atributo6
## min values : 1, 1, 99.00004, 102.5403, 20, NaN
## max values : 100, 10000, 100.99984, 20100.8623, 20, NaN
Indización con doble corchete [[]]
Se puede acceder a las capas utilizando [[ ]] ya sea por su número de aparición, o por su nombre
## class : SpatRaster
## dimensions : 10, 10, 1 (nrow, ncol, nlyr)
## resolution : 20, 40 (x, y)
## extent : -100, 100, -200, 200 (xmin, xmax, ymin, ymax)
## coord. ref. :
## source(s) : memory
## name : Atributo3
## min value : 99.00004
## max value : 100.99984
## class : SpatRaster
## dimensions : 10, 10, 1 (nrow, ncol, nlyr)
## resolution : 20, 40 (x, y)
## extent : -100, 100, -200, 200 (xmin, xmax, ymin, ymax)
## coord. ref. :
## source(s) : memory
## name : Atributo2
## min value : 1
## max value : 10000
Más indización
Accedemos a las capas 2 y 3
RasMultiCapa[[c(2,3)]]
## class : SpatRaster
## dimensions : 10, 10, 2 (nrow, ncol, nlyr)
## resolution : 20, 40 (x, y)
## extent : -100, 100, -200, 200 (xmin, xmax, ymin, ymax)
## coord. ref. :
## source(s) : memory
## names : Atributo2, Atributo3
## min values : 1, 99.00004
## max values : 10000, 100.99984
## Atributo2
## 1 25
## Atributo1
## 1 11
## 2 12
## 3 13
## 4 14
## 5 15
Todavía más indización
Accedemos a los datos de la capa llamada Atributo1
RasMultiCapa[['Atributo1']][,]
## Atributo1
## [1,] 1
## [2,] 2
## [3,] 3
## [4,] 4
## [5,] 5
## [6,] 6
## [7,] 7
## [8,] 8
## [9,] 9
## [10,] 10
## [11,] 11
## [12,] 12
## [13,] 13
## [14,] 14
## [15,] 15
## [16,] 16
## [17,] 17
## [18,] 18
## [19,] 19
## [20,] 20
## [21,] 21
## [22,] 22
## [23,] 23
## [24,] 24
## [25,] 25
## [26,] 26
## [27,] 27
## [28,] 28
## [29,] 29
## [30,] 30
Más . . .
## Atributo1 Atributo3
## 1 9 99.08887
## 2 10 99.16093
## 3 11 100.00443
## 4 12 100.84385
## 5 13 100.90745
## 6 14 100.13674
## 7 15 99.24031
## 8 16 99.04234
## 9 17 99.72484
## 10 18 100.66032
## 11 19 100.98870
## 12 20 100.40808
## 13 21 99.45227
class(RasMultiCapa[[c(1,3)]][9:21])
## [1] "data.frame"
Indización con un corchete []
Si utilizamos nada más un corchete accedemos directamente a los valores
RasPrueba1[]
## Atributo
## [1,] 1
## [2,] 2
## [3,] 3
## [4,] 4
## [5,] 5
## [6,] 6
## [7,] 7
## [8,] 8
## [9,] 9
## [10,] 10
## [11,] 11
## [12,] 12
## [13,] 13
## [14,] 14
## [15,] 15
## [16,] 16
## [17,] 17
## [18,] 18
## [19,] 19
## [20,] 20
## [21,] 21
## [22,] 22
## [23,] 23
## [24,] 24
## [25,] 25
## [26,] 26
## [27,] 27
## [28,] 28
## [29,] 29
## [30,] 30
Sigue
RasMultiCapa[1]
class(RasMultiCapa[1])
## [1] "data.frame"
class(RasMultiCapa[50])
## [1] "data.frame"
Más
class(RasMultiCapa[1,])
## [1] "data.frame"
Más de indización
Llamamos dos filas
RasMultiCapa[c(4,2),]
class(RasMultiCapa[c(4,2),])
## [1] "data.frame"
Otro
Llamamos la sexta columna
RasMultiCapa[,6]
class(RasMultiCapa[,6])
## [1] "data.frame"
## Atributo2
## 1 36
## 2 256
## 3 676
## 4 1296
Más complejos
RasMultiCapa[c(1,3,4),c(1,3,7)][c(1,2)]
## Atributo1 Atributo2
## 1 1 1
## 2 3 9
## 3 7 49
## 4 21 441
## 5 23 529
## 6 27 729
## 7 31 961
## 8 33 1089
## 9 37 1369
class(RasMultiCapa[c(1,3,4),c(1,3,7)][c(1,2)])
## [1] "data.frame"
Condicionales
RasPrueba2 <- RasPrueba1
## Atributo
## [1,] FALSE
## [2,] FALSE
## [3,] FALSE
## [4,] FALSE
## [5,] FALSE
## [6,] FALSE
## [7,] FALSE
## [8,] FALSE
## [9,] FALSE
## [10,] FALSE
## [11,] FALSE
## [12,] FALSE
## [13,] FALSE
## [14,] FALSE
## [15,] FALSE
## [16,] FALSE
## [17,] FALSE
## [18,] FALSE
## [19,] FALSE
## [20,] FALSE
## [21,] FALSE
## [22,] FALSE
## [23,] FALSE
## [24,] FALSE
which()
Con which podemos identificar la posición de la celda que cumpla con cierta condición
indices <- which(103 <RasMultiCapa[[2]][,] & RasMultiCapa[[2]][,]<500)
indices
## [1] 11 12 13 14 15 16 17 18 19 20 21 22
La variable indices nos muestra las posiciones de los elementos que cumplieron la condición. La sustituimos en el
raster para mostrar los valores.
RasMultiCapa[[2]][indices]
## Atributo2
## 1 121
## 2 144
## 3 169
## 4 196
## 5 225
## 6 256
## 7 289
## 8 324
## 9 361
## 10 400
## 11 441
## 12 484
## Atributo2
## [1,] FALSE
## [2,] FALSE
## [3,] FALSE
## [4,] FALSE
## [5,] FALSE
## [6,] FALSE
## [7,] FALSE
## [8,] FALSE
## [9,] FALSE
## [10,] FALSE
## [11,] TRUE
## [12,] TRUE
## [13,] TRUE
## [14,] TRUE
## [15,] TRUE
## [16,] TRUE
## [17,] TRUE
## [18,] TRUE
## [19,] TRUE
## [20,] TRUE
## [21,] TRUE
## [22,] TRUE
## [23,] FALSE
## [24,] FALSE
## [25,] FALSE
## [26,] FALSE
## [27,] FALSE
## [28,] FALSE
## [29,] FALSE
which.min() y which.max()
Los índices de las posiciones de los valores mínimos
which.min(RasMultiCapa$Atributo4)
## class : SpatRaster
## dimensions : 10, 10, 1 (nrow, ncol, nlyr)
## resolution : 20, 40 (x, y)
## extent : -100, 100, -200, 200 (xmin, xmax, ymin, ymax)
## coord. ref. :
## source(s) : memory
## name : which.min
## min value : 1
## max value : 1
## class : SpatRaster
## dimensions : 10, 10, 1 (nrow, ncol, nlyr)
## resolution : 20, 40 (x, y)
## extent : -100, 100, -200, 200 (xmin, xmax, ymin, ymax)
## coord. ref. :
## source(s) : memory
## name : which.max
## min value : 1
## max value : 1
Gráfica. . .
Explique la siguiente gráfica
RasMultiCapa[20:30] <- NA
plot(RasMultiCapa)
200
200
100 10000
80 8000
100
100
100
100.5
60 6000
100.0
0
0
40 4000
−100
−100
−100
99.5
20 2000
−200
−200
−200
−100 −50 0 50 100 −100 −50 0 50 100 −100 −50 0 50 100
Atributo4 Atributo5
200
200
20000
20
100
100
15000
10000
0
0
−100
−100
5000
−200
−200
200
101 100 99 99 100 101 101 100 99 99
100
100.5
100.0
0
−100 0 100
Normalizar
RasDEM$Normalizado <- RasDEM$RasterDEM/max(RasDEM$RasterDEM[])
head(RasDEM$Normalizado[,],20)
## Normalizado
## [1,] 0.5451843
## [2,] 0.5463734
## [3,] 0.5463734
## [4,] 0.5463734
## [5,] 0.5463734
## [6,] 0.5469679
## [7,] 0.5475624
## [8,] 0.5493460
## [9,] 0.5511296
## [10,] 0.5535077
## [11,] 0.5558859
## [12,] 0.5588585
## [13,] 0.5618312
## [14,] 0.5642093
## [15,] 0.5671819
## [16,] 0.5695600
## [17,] 0.5713436
## [18,] 0.5725327
## [19,] 0.5743163
## [20,] 0.5760999
0.80
Ejercicio:
3e+05
2e+05 Filtro
Frequency
1e+05
0e+00
v
Respuesta 5
2030000
1600
1400
2025000
1200
2020000
1000
800
2015000
600
2010000
400