Manejo de datos espaciales en R
Universidad de Córdoba (España)
Se calculan aproximaciones usando modelos de la Tierra (Datum)
Conformes, equidistantes, equivalentes y afilácticas
terra
Los más sencillos de crear son los puntos
class : SpatVector
geometry : points
dimensions : 10, 0 (geometries, attributes)
extent : -120.8, -110.7, 35.7, 45.3 (xmin, xmax, ymin, ymax)
coord. ref. :
Warning
Fijaros que el apartado “coord. ref.” está vacío
[1] "GEOGCRS[\"unknown\",\n DATUM[\"World Geodetic System 1984\",\n ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n LENGTHUNIT[\"metre\",1]],\n ID[\"EPSG\",6326]],\n PRIMEM[\"Greenwich\",0,\n ANGLEUNIT[\"degree\",0.0174532925199433],\n ID[\"EPSG\",8901]],\n CS[ellipsoidal,2],\n AXIS[\"longitude\",east,\n ORDER[1],\n ANGLEUNIT[\"degree\",0.0174532925199433,\n ID[\"EPSG\",9122]]],\n AXIS[\"latitude\",north,\n ORDER[2],\n ANGLEUNIT[\"degree\",0.0174532925199433,\n ID[\"EPSG\",9122]]]]"
Voy a simular valores de precipitación al azar (sample
), un valor para cada punto
class : SpatVector
geometry : points
dimensions : 10, 2 (geometries, attributes)
extent : -120.8, -110.7, 35.7, 45.3 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs
names : ID precip
type : <int> <int>
values : 1 81
2 61
3 26
id part lon2 lat2
[1,] 1 1 -116.8 41.3
[2,] 1 1 -114.2 42.9
[3,] 1 1 -112.9 42.4
[4,] 1 1 -111.9 39.8
[5,] 1 1 -114.2 37.6
[6,] 1 1 -115.4 38.3
[7,] 1 1 -117.7 37.6
class : SpatVector
geometry : lines
dimensions : 1, 0 (geometries, attributes)
extent : -117.7, -111.9, 37.6, 42.9 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs
class : SpatVector
geometry : polygons
dimensions : 1, 0 (geometries, attributes)
extent : -117.7, -111.9, 37.6, 42.9 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs
Descargar datos de Internet (v.gr. GADM) y cargarlos desde el disco duro
Hay paquetes de R que dan acceso directo a datos vectoriales (v.gr. geodata
)
class : SpatVector
geometry : polygons
dimensions : 18, 11 (geometries, attributes)
extent : -18.16153, 4.328195, 27.63736, 43.79153 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (EPSG:4326)
names : GID_1 GID_0 COUNTRY NAME_1 VARNAME_1 NL_NAME_1
type : <chr> <chr> <chr> <chr> <chr> <chr>
values : ESP.1_1 ESP Spain Andalucía Andalousie|And~ NA
ESP.2_1 ESP Spain Aragón Aragão|Aragó|A~ NA
ESP.3_1 ESP Spain Cantabria Cantàbria|Cant~ NA
TYPE_1 ENGTYPE_1 CC_1 HASC_1 ISO_1
<chr> <chr> <chr> <chr> <chr>
Comunidad Autó~ Autonomous Com~ 01 ES.AN NA
Comunidad Autó~ Autonomous Com~ 15 ES.AR NA
Comunidad Autó~ Autonomous Com~ 06 ES.CB ES-CB
[1] "GEOGCRS[\"WGS 84\",\n DATUM[\"World Geodetic System 1984\",\n ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n LENGTHUNIT[\"metre\",1]]],\n PRIMEM[\"Greenwich\",0,\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n CS[ellipsoidal,2],\n AXIS[\"geodetic latitude (Lat)\",north,\n ORDER[1],\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n AXIS[\"geodetic longitude (Lon)\",east,\n ORDER[2],\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n ID[\"EPSG\",4326]]"
[1] "PROJCRS[\"unknown\",\n BASEGEOGCRS[\"unknown\",\n DATUM[\"World Geodetic System 1984\",\n ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n LENGTHUNIT[\"metre\",1]],\n ID[\"EPSG\",6326]],\n PRIMEM[\"Greenwich\",0,\n ANGLEUNIT[\"degree\",0.0174532925199433],\n ID[\"EPSG\",8901]]],\n CONVERSION[\"UTM zone 19S\",\n METHOD[\"Transverse Mercator\",\n ID[\"EPSG\",9807]],\n PARAMETER[\"Latitude of natural origin\",0,\n ANGLEUNIT[\"degree\",0.0174532925199433],\n ID[\"EPSG\",8801]],\n PARAMETER[\"Longitude of natural origin\",-69,\n ANGLEUNIT[\"degree\",0.0174532925199433],\n ID[\"EPSG\",8802]],\n PARAMETER[\"Scale factor at natural origin\",0.9996,\n SCALEUNIT[\"unity\",1],\n ID[\"EPSG\",8805]],\n PARAMETER[\"False easting\",500000,\n LENGTHUNIT[\"metre\",1],\n ID[\"EPSG\",8806]],\n PARAMETER[\"False northing\",10000000,\n LENGTHUNIT[\"metre\",1],\n ID[\"EPSG\",8807]],\n ID[\"EPSG\",17019]],\n CS[Cartesian,2],\n AXIS[\"(E)\",east,\n ORDER[1],\n LENGTHUNIT[\"metre\",1,\n ID[\"EPSG\",9001]]],\n AXIS[\"(N)\",north,\n ORDER[2],\n LENGTHUNIT[\"metre\",1,\n ID[\"EPSG\",9001]]]]"
[1] "PROJCRS[\"ETRS89 / UTM zone 30N (N-E)\",\n BASEGEOGCRS[\"ETRS89\",\n ENSEMBLE[\"European Terrestrial Reference System 1989 ensemble\",\n MEMBER[\"European Terrestrial Reference Frame 1989\"],\n MEMBER[\"European Terrestrial Reference Frame 1990\"],\n MEMBER[\"European Terrestrial Reference Frame 1991\"],\n MEMBER[\"European Terrestrial Reference Frame 1992\"],\n MEMBER[\"European Terrestrial Reference Frame 1993\"],\n MEMBER[\"European Terrestrial Reference Frame 1994\"],\n MEMBER[\"European Terrestrial Reference Frame 1996\"],\n MEMBER[\"European Terrestrial Reference Frame 1997\"],\n MEMBER[\"European Terrestrial Reference Frame 2000\"],\n MEMBER[\"European Terrestrial Reference Frame 2005\"],\n MEMBER[\"European Terrestrial Reference Frame 2014\"],\n ELLIPSOID[\"GRS 1980\",6378137,298.257222101,\n LENGTHUNIT[\"metre\",1]],\n ENSEMBLEACCURACY[0.1]],\n PRIMEM[\"Greenwich\",0,\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n ID[\"EPSG\",4258]],\n CONVERSION[\"UTM zone 30N\",\n METHOD[\"Transverse Mercator\",\n ID[\"EPSG\",9807]],\n PARAMETER[\"Latitude of natural origin\",0,\n ANGLEUNIT[\"degree\",0.0174532925199433],\n ID[\"EPSG\",8801]],\n PARAMETER[\"Longitude of natural origin\",-3,\n ANGLEUNIT[\"degree\",0.0174532925199433],\n ID[\"EPSG\",8802]],\n PARAMETER[\"Scale factor at natural origin\",0.9996,\n SCALEUNIT[\"unity\",1],\n ID[\"EPSG\",8805]],\n PARAMETER[\"False easting\",500000,\n LENGTHUNIT[\"metre\",1],\n ID[\"EPSG\",8806]],\n PARAMETER[\"False northing\",0,\n LENGTHUNIT[\"metre\",1],\n ID[\"EPSG\",8807]]],\n CS[Cartesian,2],\n AXIS[\"northing (N)\",north,\n ORDER[1],\n LENGTHUNIT[\"metre\",1]],\n AXIS[\"easting (E)\",east,\n ORDER[2],\n LENGTHUNIT[\"metre\",1]],\n USAGE[\n SCOPE[\"Pan-European medium scale conformal mapping.\"],\n AREA[\"Europe between 6°W and 0°W: Faroe Islands offshore; Ireland - offshore; Jan Mayen - offshore; Norway including Svalbard - offshore; Spain - onshore and offshore.\"],\n BBOX[35.26,-6,80.49,0.01]],\n ID[\"EPSG\",3042]]"
Note
Cambia la representación gráfica porque las unidades cambian entre CRS (grados o metros). Mientras los metros se representan igual en el eje horizontal y vertical, los grados geográficos no.
Ejercicios de clase
Parte 1: Trabajando con datos vectoriales
class : SpatRaster
size : 10, 10, 1 (nrow, ncol, nlyr)
resolution : 7, 4 (x, y)
extent : -150, -80, 20, 60 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84)
source(s) : memory
name : lyr.1
min value : 0.01535991
max value : 0.99922492
Important
Al operar con datos ráster, las operaciones se aplican sobre cada píxel individualmente. Por ello, sólo se pueden realizar operaciones con ráster que tienen el mismo área y el mismo tamaño de píxel. El resultado, por tanto, es un nuevo objeto ráster con las mismas características geográficas, pero cuyos píxeles tienen los valores resultantes de aplicar las funciones matemáticas.
class : SpatRaster
size : 10, 10, 3 (nrow, ncol, nlyr)
resolution : 7, 4 (x, y)
extent : -150, -80, 20, 60 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84)
source(s) : memory
names : lyr.1, lyr.1, lyr.1
min values : 0.01535991, 0.0002359268, 0.1239351
max values : 0.99922492, 0.9984504488, 0.9996124
Para ello podemos descargar datos de distintas fuentes (v.gr. WorldClim)
class : SpatRaster
size : 1980, 2760, 12 (nrow, ncol, nlyr)
resolution : 0.008333333, 0.008333333 (x, y)
extent : -18.5, 4.5, 27.5, 44 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (EPSG:4326)
source : tavg_esp.tif
names : tavg_esp_1, tavg_esp_2, tavg_esp_3, tavg_esp_4, tavg_esp_5, tavg_esp_6, ...
min values : -9.8, -9.4, -7.7, -6, -1.4, 3.2, ...
max values : 19.2, 18.9, 21.6, 26, 30.4, 36.0, ...
… o podemos usar el paquete geodata
para acceder directamente a esa información
|---------|---------|---------|---------|
=========================================
class : SpatRaster
size : 2393, 2805, 12 (nrow, ncol, nlyr)
resolution : 814.2352, 814.2352 (x, y)
extent : -1041769, 1242161, 3041688, 4990152 (xmin, xmax, ymin, ymax)
coord. ref. : ETRS89 / UTM zone 30N (N-E) (EPSG:3042)
source : spat_6abc64f9a9_27324_o8up7yuDdJ6XHBX.tif
names : tavg_esp_1, tavg_esp_2, tavg_esp_3, tavg_esp_4, tavg_esp_5, tavg_esp_6, ...
min values : -9.649184, -9.150451, -7.476511, -5.675769, -1.142849, 3.455518, ...
max values : 19.200001, 18.900000, 21.600000, 26.000000, 30.400000, 36.000000, ...
Warning
No hay una correspondencia exacta de píxeles. Lo veréis más claro si revisáis las diapositivas anteriores (39 y 43).
class : SpatRaster
size : 2436, 2855, 12 (nrow, ncol, nlyr)
resolution : 800, 800 (x, y)
extent : -1041769, 1242231, 3041688, 4990488 (xmin, xmax, ymin, ymax)
coord. ref. : ETRS89 / UTM zone 30N (N-E) (EPSG:3042)
|---------|---------|---------|---------|
=========================================
class : SpatRaster
size : 2436, 2855, 12 (nrow, ncol, nlyr)
resolution : 800, 800 (x, y)
extent : -1041769, 1242231, 3041688, 4990488 (xmin, xmax, ymin, ymax)
coord. ref. : ETRS89 / UTM zone 30N (N-E) (EPSG:3042)
source : spat_6abc5cd96cab_27324_zxzxx9jyyx6UnnI.tif
names : tavg_esp_1, tavg_esp_2, tavg_esp_3, tavg_esp_4, tavg_esp_5, tavg_esp_6, ...
min values : -9.669218, -9.190617, -7.505003, -5.726402, -1.185399, 3.409383, ...
max values : 19.200001, 18.900000, 21.600000, 26.000000, 30.400000, 36.000000, ...
Ejercicios de clase
Parte 2. Manejo de datos ráster
GID_1 GID_0 COUNTRY NAME_1 VARNAME_1
1 ESP.1_1 ESP Spain Andalucía Andalousie|Andaluc¡a|Andalusien|
2 ESP.2_1 ESP Spain Aragón Aragão|Aragó|Aragón|Aragona|Arag
3 ESP.3_1 ESP Spain Cantabria Cantàbria|Cantábria|Cantabrie|Ka
4 ESP.4_1 ESP Spain Castilla-La Mancha Castela-La Mancha|Castela-Mancha
5 ESP.5_1 ESP Spain Castilla y León Castile and Leon|Castela e Leão|
6 ESP.6_1 ESP Spain Cataluña Catalogna|Catalogne|Catalonia|Ca
NL_NAME_1 TYPE_1 ENGTYPE_1 CC_1 HASC_1 ISO_1
1 <NA> Comunidad Autónoma Autonomous Community 01 ES.AN <NA>
2 <NA> Comunidad Autónoma Autonomous Community 15 ES.AR <NA>
3 <NA> Comunidad Autónoma Autonomous Community 06 ES.CB ES-CB
4 <NA> Comunidad Autónoma Autonomous Community 08 ES.CM ES-CM
5 <NA> Comunidad Autónoma Autonomous Community 07 ES.CL <NA>
6 <NA> Comunidad Autónoma Autonomous Community 09 ES.CT <NA>
[1] "Andalucía" "Aragón"
[3] "Cantabria" "Castilla-La Mancha"
[5] "Castilla y León" "Cataluña"
[7] "Ceuta y Melilla" "Comunidad de Madrid"
[9] "Comunidad Foral de Navarra" "Comunidad Valenciana"
[11] "Extremadura" "Galicia"
[13] "Islas Baleares" "Islas Canarias"
[15] "La Rioja" "País Vasco"
[17] "Principado de Asturias" "Región de Murcia"
class : SpatVector
geometry : polygons
dimensions : 18, 1 (geometries, attributes)
extent : -18.16153, 4.328195, 27.63736, 43.79153 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (EPSG:4326)
names : NAME_1
type : <chr>
values : Andalucía
Aragón
Cantabria
Warning
Notad que en este caso, las dos formas de extraer información no se comportan exactamente igual
[1] 2589087.46 1440885.84 846934.43 2244558.59 2663559.30 1544730.70
[7] 70829.05 728170.59 729188.20 1504057.15 1326491.81 2511306.43
[13] 1426141.05 1811869.11 553539.59 953048.57 962220.30 778427.60
Note
Me descargué datos del censo poblacional del país
comunidad total hombres mujeres
1 Andalucía 8631862 4249355 4382507
2 Aragón 1351591 668275 683316
3 Principado de Asturias 1009599 481478 528121
4 Islas Baleares 1231768 613910 617858
5 Islas Canarias 2238754 1105037 1133717
6 Cantabria 590851 286322 304529
7 Castilla y León 2391682 1177504 1214178
8 Castilla-La Mancha 2104433 1055223 1049210
9 Cataluña 8012231 3943698 4068533
10 Comunidad Valenciana 5319285 2615852 2703433
11 Extremadura 1054681 521677 533004
12 Galicia 2705833 1301744 1404089
13 Comunidad de Madrid 7009268 3356163 3653105
14 Región de Murcia 1568492 786038 782454
15 Comunidad Foral de Navarra 678333 335919 342414
16 País Vasco 2227684 1083488 1144196
17 La Rioja 324184 159979 164205
Para vincularla necesitamos un campo en común
class : SpatVector
geometry : polygons
dimensions : 17, 15 (geometries, attributes)
extent : -18.16153, 4.328195, 27.63736, 43.79153 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (EPSG:4326)
names : NAME_1 GID_1 GID_0 COUNTRY VARNAME_1 NL_NAME_1
type : <chr> <chr> <chr> <chr> <chr> <chr>
values : Andalucía ESP.1_1 ESP Spain Andalousie|And~ NA
Aragón ESP.2_1 ESP Spain Aragão|Aragó|A~ NA
Cantabria ESP.3_1 ESP Spain Cantàbria|Cant~ NA
TYPE_1 ENGTYPE_1 CC_1 HASC_1 (and 5 more)
<chr> <chr> <chr> <chr>
Comunidad Autó~ Autonomous Com~ 01 ES.AN
Comunidad Autó~ Autonomous Com~ 15 ES.AR
Comunidad Autó~ Autonomous Com~ 06 ES.CB
Note
La agregación aquí trabaja a nivel de píxel, por lo que se usan para cambiar la resolución de los datos.
class : SpatVector
geometry : polygons
dimensions : 4, 1 (geometries, attributes)
extent : -6, -4, 37, 41 (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (CRS84) (OGC:CRS84)
names : Zona
type : <int>
values : 1
2
3
Para ello creamos una capa de puntos cualquiera
id.y COUNTRY mean_perimetro mean_total mean_hombres mean_mujeres NAME_1 GID_1
1 1 Spain 1447895 2850031 1396568 1453463 NA NA
2 2 Spain 1447895 2850031 1396568 1453463 NA NA
3 3 Spain 1447895 2850031 1396568 1453463 NA NA
4 4 Spain 1447895 2850031 1396568 1453463 NA NA
5 5 Spain 1447895 2850031 1396568 1453463 NA NA
6 6 Spain 1447895 2850031 1396568 1453463 NA NA
GID_0 VARNAME_1 NL_NAME_1 TYPE_1 ENGTYPE_1 CC_1 HASC_1
1 ESP NA <NA> Comunidad Autónoma Autonomous Community NA NA
2 ESP NA <NA> Comunidad Autónoma Autonomous Community NA NA
3 ESP NA <NA> Comunidad Autónoma Autonomous Community NA NA
4 ESP NA <NA> Comunidad Autónoma Autonomous Community NA NA
5 ESP NA <NA> Comunidad Autónoma Autonomous Community NA NA
6 ESP NA <NA> Comunidad Autónoma Autonomous Community NA NA
ISO_1 agg_n
1 NA 17
2 NA 17
3 NA 17
4 NA 17
5 NA 17
6 NA 17
ID tavg_esp_1 tavg_esp_2 tavg_esp_3 tavg_esp_4 tavg_esp_5 tavg_esp_6
1 1 3.6 5.6 7.8 9.7 13.4 17.9
2 2 11.0 12.1 13.9 15.4 18.4 22.2
3 3 3.7 4.9 7.4 9.5 13.7 19.0
4 4 2.2 3.6 6.1 7.5 10.9 15.5
5 5 3.5 5.1 7.3 9.0 12.8 17.5
6 6 5.4 6.3 7.9 9.1 12.5 15.3
tavg_esp_7 tavg_esp_8 tavg_esp_9 tavg_esp_10 tavg_esp_11 tavg_esp_12
1 21.3 20.9 17.6 12.3 7.6 4.7
2 25.4 25.2 23.0 18.8 14.8 12.1
3 23.2 22.8 18.4 12.6 7.5 4.6
4 18.7 18.4 15.4 10.3 5.9 3.3
5 21.3 20.9 17.5 11.9 7.3 4.5
6 17.8 18.3 16.5 12.9 8.7 6.5
Warning
Fijaros que extrae un valor de cada capa del ráster stack
min
, max
, mean
, prod
, sum
, median
, cv
, range
, any
y all
crop
, trim
, merge
, disagg
, resample
, classify
o cover
El paquete gdistance
ofrece análisis de distancias complejos (least cost path, etcétera)
Ejercicios de clase
Parte 3. Manipulación de datos ráster y vectoriales