Está en la página 1de 47

Manejo de Datos Vectoriales

SpatialPolygons

library(sp)
library(raster)
library(terra)
Comunas Medellin
Comunas <- '../../Archivos/Medellin/Comunas/Limite_Comuna_Corregimiento.shp'

SPolComunas <- vect(Comunas)

SPolComunas

## class : SpatVector
## geometry : polygons
## dimensions : 23, 9 (geometries, attributes)
## extent : -75.71931, -75.47185, 6.162904, 6.374872 (xmin, xmax, ymin, ymax)
## source : Limite_Comuna_Corregimiento.shp
## coord. ref. : lon/lat WGS 84 (EPSG:4326)
## names : OBJECTID CODIGO NOMBRE IDENTIFICA LIMITEMUNI SUBTIPO_CO
## type : <int> <chr> <chr> <chr> <chr> <int>
## values : 321 01 Popular Comuna 1 001 1
## 322 02 Santa Cruz Comuna 2 001 1
## 323 03 Manrique Comuna 3 001 1
## LINK_DOCUM SHAPEAREA SHAPELEN
## <chr> <num> <num>
## NA 3.098e+06 9605
## NA 2.196e+06 8598
## NA 5.097e+06 1.208e+04

names(SPolComunas)

## [1] "OBJECTID" "CODIGO" "NOMBRE" "IDENTIFICA" "LIMITEMUNI"


## [6] "SUBTIPO_CO" "LINK_DOCUM" "SHAPEAREA" "SHAPELEN"
Graficamos 1

plot(SPolComunas,col=rainbow(length(SPolComunas)),main='Comunas')
text(SPolComunas,SPolComunas$CODIGO)

Comunas

6.35

50
6.3

06 02
05 01
60

07 04 03
SN01
13 12
6.25

11 10 08

90
09
70 16
80 15
14
6.2

SN02

−75.7 −75.65 −75.6 −75.55 −75.5


Graficamos 2

plot(SPolComunas,"NOMBRE",col=rainbow(length(SPolComunas)),
Comunas
Aranjuez
Belén
Buenos Aires
Castilla
6.35

Corregimiento de Altavista
Corregimiento de San Antonio de Prado
Corregimiento de San Cristóbal
Corregimiento de San Sebastián de Palmitas
Corregimiento de Santa Elena
Doce de Octubre
El Poblado
Guayabal
La América
6.3

La Candelaria
Laureles Estadio
Manrique
Popular
Robledo
San Javier
Santa Cruz
Villa Hermosa
6.25
6.2

−75.7 −75.65 −75.6 −75.55 −75.5


Subconjunto

SPolComunas <- SPolComunas[c(1:15,length(SPolComunas)),]

SPolComunas

## class : SpatVector
## geometry : polygons
## dimensions : 16, 9 (geometries, attributes)
## extent : -75.63243, -75.52478, 6.175719, 6.312849 (xmin, xmax, ymin, ymax)
## coord. ref. : lon/lat WGS 84 (EPSG:4326)
## names : OBJECTID CODIGO NOMBRE IDENTIFICA LIMITEMUNI SUBTIPO_CO
## type : <int> <chr> <chr> <chr> <chr> <int>
## values : 321 01 Popular Comuna 1 001 1
## 322 02 Santa Cruz Comuna 2 001 1
## 323 03 Manrique Comuna 3 001 1
## LINK_DOCUM SHAPEAREA SHAPELEN
## <chr> <num> <num>
## NA 3.098e+06 9605
## NA 2.196e+06 8598
## NA 5.097e+06 1.208e+04

names(SPolComunas)

## [1] "OBJECTID" "CODIGO" "NOMBRE" "IDENTIFICA" "LIMITEMUNI"


## [6] "SUBTIPO_CO" "LINK_DOCUM" "SHAPEAREA" "SHAPELEN"
Graficamos

plot(SPolComunas,"NOMBRE",col=rainbow(length(SPolComunas)),main='Comunas')
text(SPolComunas,SPolComunas$CODIGO)

Comunas
Aranjuez
Belén
Buenos Aires

6.3
06 Castilla
02 Doce de Octubre
05 01 El Poblado
Guayabal
6.28 La América
La Candelaria
07 04 Laureles Estadio
03 Manrique
Popular
Robledo
San Javier
6.26

Santa Cruz
13
12 Villa Hermosa
11
10 08
6.24

09
16
6.22

15

14
6.2
6.18

−75.6 −75.55
Atributos

Asignamos atributos
SPolComunas$ZONA <- NA
SPolComunas$ZONA[1:4] <- "ZONA1"; SPolComunas$ZONA[5:7] <- "ZONA2"
SPolComunas$ZONA[8:10] <- "ZONA3"; SPolComunas$ZONA[11:13] <- "ZONA4"
SPolComunas$ZONA[14:16] <- "ZONA5"

names(SPolComunas)

## [1] "OBJECTID" "CODIGO" "NOMBRE" "IDENTIFICA" "LIMITEMUNI"


## [6] "SUBTIPO_CO" "LINK_DOCUM" "SHAPEAREA" "SHAPELEN" "ZONA"
Eliminamos atributos

SPolComunas$OBJECTID <- NULL; SPolComunas$IDENTIFICA <- NULL


SPolComunas$LIMITEMUNI <- NULL; SPolComunas$SUBTIPO_CO <- NULL
SPolComunas$LINK_DOCUM <- NULL; SPolComunas$SHAPELEN <- NULL

o de forma equivalente
SPolComunas <- SPolComunas[c('CODIGO','NOMBRE','ZONA','SHAPEAREA')]

names(SPolComunas)

## [1] "CODIGO" "NOMBRE" "SHAPEAREA" "ZONA"


Dimensiones

length(SPolComunas)

## [1] 16

nrow(SPolComunas)

## [1] 16

ncol(SPolComunas)

## [1] 4
Atributo Nombre

SPolComunas$NOMBRE

## [1] "Popular" "Santa Cruz" "Manrique" "Aranjuez"


## [5] "Castilla" "Doce de Octubre" "Robledo" "Villa Hermosa"
## [9] "Buenos Aires" "La Candelaria" "Laureles Estadio" "La América"
## [13] "San Javier" "El Poblado" "Belén" "Guayabal"
Subconjunto Nombre

SPolComunas$NOMBRE[c(1,5,9)]

## [1] "Popular" "Castilla" "Buenos Aires"

head(values(SPolComunas),6)

## CODIGO NOMBRE SHAPEAREA ZONA


## 1 01 Popular 3098290 ZONA1
## 2 02 Santa Cruz 2195875 ZONA1
## 3 03 Manrique 5096746 ZONA1
## 4 04 Aranjuez 4883731 ZONA1
## 5 05 Castilla 6064367 ZONA2
## 6 06 Doce de Octubre 3850036 ZONA2

values(SPolComunas[5,])

## CODIGO NOMBRE SHAPEAREA ZONA


## 1 05 Castilla 6064367 ZONA2
Geometria

x <- geom(SPolComunas[14,])

head(x,10)

## geom part x y hole


## [1,] 1 1 -75.56919 6.226584 0
## [2,] 1 1 -75.56919 6.226490 0
## [3,] 1 1 -75.56852 6.226496 0
## [4,] 1 1 -75.56849 6.226358 0
## [5,] 1 1 -75.56845 6.226174 0
## [6,] 1 1 -75.56841 6.225926 0
## [7,] 1 1 -75.56835 6.225755 0
## [8,] 1 1 -75.56828 6.225621 0
## [9,] 1 1 -75.56814 6.225682 0
## [10,] 1 1 -75.56803 6.225699 0

x1 <- x[,3]; y1 <- x[,4]


xy1 <- cbind(x1,y1)
Graficamos
plot(xy1,col='blue',main='Polígonos')
lines(xy1)

Polígonos
6.23
6.22
6.21
y1

6.20
6.19
6.18

−75.58 −75.57 −75.56 −75.55

x1
Geometría Completo

x <- geom(SPolComunas)
head(x,8)

## geom part x y hole


## [1,] 1 1 -75.54803 6.304356 0
## [2,] 1 1 -75.54795 6.304309 0
## [3,] 1 1 -75.54791 6.304197 0
## [4,] 1 1 -75.54787 6.304044 0
## [5,] 1 1 -75.54775 6.303918 0
## [6,] 1 1 -75.54759 6.303774 0
## [7,] 1 1 -75.54756 6.303759 0
## [8,] 1 1 -75.54739 6.303667 0

x1 <- x[,3]
y1 <- x[,4]
xy1 <- cbind(x1,y1)
Graficamos
plot(xy1,col='blue',main='Polígonos')
lines(xy1)

Polígonos
6.30
6.28
6.26
y1

6.24
6.22
6.20
6.18

−75.62 −75.60 −75.58 −75.56 −75.54

x1
Extraer Atributos

DF <- data.frame(SPolComunas)
head(DF)

## CODIGO NOMBRE SHAPEAREA ZONA


## 1 01 Popular 3098290 ZONA1
## 2 02 Santa Cruz 2195875 ZONA1
## 3 03 Manrique 5096746 ZONA1
## 4 04 Aranjuez 4883731 ZONA1
## 5 05 Castilla 6064367 ZONA2
## 6 06 Doce de Octubre 3850036 ZONA2
Subconjunto
SPol1 <- SPolComunas[1,]
SPol5 <- SPolComunas[5,]
SPol9 <- SPolComunas[9,]

plot(SPolComunas,main='Comunas')
plot(SPol1,col='red',add=TRUE)
plot(SPol5,col='green',add=TRUE)
plot(SPol9,col='blue',add=TRUE)

Comunas

6.3
6.28
6.26
6.24
6.22
6.2
6.18

−75.6 −75.55
Which
i <- which(SPolComunas$ZONA=="ZONA3")
i

## [1] 8 9 10

SPolZona3 <-SPolComunas[i,]

plot(SPolComunas)
plot(SPolZona3,col='red',add=TRUE)
box(lty='1373',col='purple',lwd=3)

6.3
6.28
6.26
6.24
6.22
6.2
6.18

−75.6 −75.55
Generamos data.frame con mismos atributos

DF2 = data.frame(CODIGO=SPolComunas$CODIGO,NOMBRE=SPolComunas$NOMBRE,ZONA=SPolComunas$ZONA,letra=letters[1

DF2

## CODIGO NOMBRE ZONA letra num


## 1 01 Popular ZONA1 a 1
## 2 02 Santa Cruz ZONA1 b 2
## 3 03 Manrique ZONA1 c 3
## 4 04 Aranjuez ZONA1 d 4
## 5 05 Castilla ZONA2 e 5
## 6 06 Doce de Octubre ZONA2 f 6
## 7 07 Robledo ZONA2 g 7
## 8 08 Villa Hermosa ZONA3 h 8
## 9 09 Buenos Aires ZONA3 i 9
## 10 10 La Candelaria ZONA3 j 10
## 11 11 Laureles Estadio ZONA4 k 11
## 12 12 La América ZONA4 l 12
## 13 13 San Javier ZONA4 m 13
## 14 14 El Poblado ZONA5 n 14
## 15 16 Belén ZONA5 o 15
## 16 15 Guayabal ZONA5 p 16
Merge

Le agregamos atributos al SPatialPolygon.


SPolComunas2 <- merge(SPolComunas,DF2)

head(values(SPolComunas),5)

## CODIGO NOMBRE SHAPEAREA ZONA


## 1 01 Popular 3098290 ZONA1
## 2 02 Santa Cruz 2195875 ZONA1
## 3 03 Manrique 5096746 ZONA1
## 4 04 Aranjuez 4883731 ZONA1
## 5 05 Castilla 6064367 ZONA2
head(values(SPolComunas2),5)

## CODIGO NOMBRE ZONA SHAPEAREA letra num


## 1 01 Popular ZONA1 3098290 a 1
## 2 02 Santa Cruz ZONA1 2195875 b 2
## 3 03 Manrique ZONA1 5096746 c 3
## 4 04 Aranjuez ZONA1 4883731 d 4
## 5 05 Castilla ZONA2 6064367 e 5
Cambiamos el nombre de un atributo

names(DF2[1])

## [1] "CODIGO"
names(DF2[1]) <- "CODIGO2"

SPolComunas2 <- merge(SPolComunas,DF2)

head(values(SPolComunas2),5)

## CODIGO NOMBRE ZONA SHAPEAREA letra num


## 1 01 Popular ZONA1 3098290 a 1
## 2 02 Santa Cruz ZONA1 2195875 b 2
## 3 03 Manrique ZONA1 5096746 c 3
## 4 04 Aranjuez ZONA1 4883731 d 4
## 5 05 Castilla ZONA2 6064367 e 5
Perímetros

perim(SPolComunas)

## [1] 9602.718 8595.685 12075.379 9640.808 16094.812 10707.623 23260.113


## [8] 16389.924 18361.489 13629.991 16361.577 15421.972 26377.999 19961.728
## [15] 25689.198 13276.378

k <- order(perim(SPolComunas))

SPolComunas$NOMBRE[k]

## [1] "Santa Cruz" "Popular" "Aranjuez" "Doce de Octubre"


## [5] "Manrique" "Guayabal" "La Candelaria" "La América"
## [9] "Castilla" "Laureles Estadio" "Villa Hermosa" "Buenos Aires"
## [13] "El Poblado" "Robledo" "Belén" "San Javier"
Áreas

expanse(SPolComunas)/10000

## [1] 309.6826 219.4838 509.4334 488.1421 606.1502 384.8218 945.7887


## [8] 571.4898 605.0391 735.7873 739.9445 397.1491 484.6970 1440.8576
## [15] 885.2257 728.5292

k <- order(expanse(SPolComunas))

SPolComunas$NOMBRE[k]

## [1] "Santa Cruz" "Popular" "Doce de Octubre" "La América"


## [5] "San Javier" "Aranjuez" "Manrique" "Villa Hermosa"
## [9] "Buenos Aires" "Castilla" "Guayabal" "La Candelaria"
## [13] "Laureles Estadio" "Belén" "Robledo" "El Poblado"
Raster a Polígonos

Ras<- rast(SPolComunas, nrow=3, ncol=3, vals=1:9)

Ras

## class : SpatRaster
## dimensions : 3, 3, 1 (nrow, ncol, nlyr)
## resolution : 0.03588224, 0.04570998 (x, y)
## extent : -75.63243, -75.52478, 6.175719, 6.312849 (xmin, xmax, ymin, ymax)
## coord. ref. : lon/lat WGS 84 (EPSG:4326)
## source(s) : memory
## name : lyr.1
## min value : 1
## max value : 9
Nombres

names(Ras)

## [1] "lyr.1"

Ras$lyr.1

## class : SpatRaster
## dimensions : 3, 3, 1 (nrow, ncol, nlyr)
## resolution : 0.03588224, 0.04570998 (x, y)
## extent : -75.63243, -75.52478, 6.175719, 6.312849 (xmin, xmax, ymin, ymax)
## coord. ref. : lon/lat WGS 84 (EPSG:4326)
## source(s) : memory
## name : lyr.1
## min value : 1
## max value : 9

names(Ras)<-'Region'
Graficamos Raster

plot(Ras)
text(Ras$Region)

9.0

6.3
8.0

1 2 3

6.28
7.0

6.0
6.26

4 5 6 5.0
6.24

4.0
6.22

3.0
6.2

7 8 9
2.0
6.18

1.0
−75.6 −75.55
SPatialPolygonsDataFrame

find("as.polygons")

## [1] "package:terra"

#v<-as(Ras,'SpatialPolygonsDataFrame')
v <- as.polygons(Ras)

names(v)

## [1] "Region"

values(v)

## Region
## 1 1
## 2 2
## 3 3
## 4 4
## 5 5
## 6 6
## 7 7
## 8 8
## 9 9
Graficar
plot(Ras)
plot(v,add=TRUE,lwd=2)
text(Ras$Region)

9.0

6.3
8.0

1 2 3
6.28

7.0

6.0
6.26

4 5 6 5.0
6.24

4.0
6.22

3.0
6.2

7 8 9
2.0
6.18

1.0
−75.6 −75.55
Subconjunto
v2 <- v[1,]

plot(Ras,out.width="80%")

## Warning in plot.window(...): "out.width" is not a graphical parameter

## Warning in plot.xy(xy, type, ...): "out.width" is not a graphical parameter

## Warning in title(...): "out.width" is not a graphical parameter


plot(SPolComunas,add=TRUE,border='red',density=c(30,0,0,0,0))
plot(v, add=TRUE, border='purple', lwd=2, density=5, col='red')
plot(v2,add=TRUE,col='green')

9.0
6.3

8.0
6.28

7.0

6.0
6.26

5.0
6.24
Bind
Añadimos objetos espaciales del mismo tipo
SPolBind<-rbind(SPolComunas,v2)

SPolBind

## class : SpatVector
## geometry : polygons
## dimensions : 17, 5 (geometries, attributes)
## extent : -75.63243, -75.52478, 6.175719, 6.312849 (xmin, xmax, ymin, ymax)
## coord. ref. : lon/lat WGS 84 (EPSG:4326)
## names : CODIGO NOMBRE SHAPEAREA ZONA Region
## type : <chr> <chr> <num> <chr> <int>
## values : 01 Popular 3.098e+06 ZONA1 NA
## 02 Santa Cruz 2.196e+06 ZONA1 NA
## 03 Manrique 5.097e+06 ZONA1 NA

head(values(SPolBind),6)

## CODIGO NOMBRE SHAPEAREA ZONA Region


## 1 01 Popular 3098290 ZONA1 NA
## 2 02 Santa Cruz 2195875 ZONA1 NA
## 3 03 Manrique 5096746 ZONA1 NA
## 4 04 Aranjuez 4883731 ZONA1 NA
## 5 05 Castilla 6064367 ZONA2 NA
## 6 06 Doce de Octubre 3850036 ZONA2 NA

Observamos que las regiones tienen NAs


Graficamos Bind
plot(SPolBind,lwd=6)
plot(SPolBind[c(1,2,5,6),],col=c('red','green','blue','purple'),add=TRUE)
plot(SPolBind[length(SPolBind),],border='pink',density=10,add=TRUE,lwd=2)

6.3
6.28
6.26
6.24
6.22
6.2
6.18

−75.6 −75.55
Aggregate
Agregamos por Zonas
SPolZonas <- aggregate(SPolComunas,by='ZONA')

length(SPolZonas)

## [1] 5

plot(Ras)
plot(v, col='light gray', border='light gray', lwd=5,add=TRUE)
plot(SPolZonas, add=TRUE, col=terrain.colors(length(SPolZonas)), lwd=3, border='white')

9.0
6.3

8.0
6.28

7.0

6.0
6.26

5.0
6.24

4.0
6.22

3.0
6.2

2.0
6.18

1.0
−75.6 −75.55
Agregamos todo el vector
SPolBindAgregada <- aggregate(SPolBind)

plot(SPolBindAgregada)

6.3
6.28
6.26
6.24
6.22
6.2
6.18

−75.6 −75.55
Agregar sin disolver
vag <- aggregate(SPolBind,dissolve=FALSE)
length(vag)

## [1] 1

names(vag)

## character(0)

vag$atributo <- 1:length(vag)

plot(vag,col=rainbow(length(vag)))
text(vag,vag$atributo)
6.3
6.28
6.26

1
6.24
6.22
6.2
6.18

−75.6 −75.55
Erase
e1 <- erase(SPolBind,v[c(2,5),])

plot(e1,col=rainbow(length(e1)))

6.3
6.28
6.26
6.24
6.22
6.2
6.18

−75.6 −75.55
Erase
e2 <- erase(e1,SPolComunas[7:10,])

length(e2)

## [1] 12

plot(e2,col=rainbow(length(e2)))
text(e2,e2$CODIGO)

05
6.3

02
01
6.28

04
03
6.26

13
12

11
6.24

16
6.22

15
6.2

14
6.18
Restar
resta <- SPolBind-v[7,]-SPolComunas[8:11,]

length(resta)

## [1] 13

plot(resta,col=rainbow(length(resta)))
text(resta,resta$CODIGO)

6.3

06 02
05 01
6.28

07 04
03
6.26

13
12
6.24

16
6.22

15

14
6.2
6.18
Interseccion
interseccion <- intersect(SPolComunas,v[6,])

length(interseccion)

## [1] 5

plot(interseccion,col=rainbow(length(interseccion)))
text(interseccion,interseccion$CODIGO)

04 03
6.26

10
6.25

08
6.24

09
6.23
Interseccion como producto
interseccion2<- SPolComunas*v[4,]

length(interseccion2)

## [1] 4

plot(SPolComunas,main='Interseccion')
plot(interseccion2,col=rainbow(length(interseccion2)),add=TRUE)
text(interseccion2,interseccion2$CODIGO)

Interseccion
6.3
6.28
6.26

13 12

11
6.24

16
6.22
6.2
8
UNION

SPolUnion <- union(v[1:2,],SPolComunas[10:14,])

length(SPolUnion)

## [1] 12

head(SPolUnion)

## Region CODIGO NOMBRE SHAPEAREA ZONA


## 1 1 <NA> <NA> NaN <NA>
## 2 2 <NA> <NA> NaN <NA>
## 3 NA 10 La Candelaria 7361364 ZONA3
## 4 NA 11 Laureles Estadio 7402954 ZONA4
## 5 NA 12 La América 3973374 ZONA4
## 6 NA 13 San Javier 4849268 ZONA4

Cuántos polígonos podemos observar?

Qué atributos aparecen?


Gráfica
plot(SPolBindAgregada,border='black',main='UNION')
plot(SPolUnion,col=terrain.colors(length(SPolUnion)),add=TRUE)

UNION

6.3
6.28
6.26
6.24
6.22
6.2
6.18

−75.6 −75.55
Suma

SPolSuma <- v[1:2,] + SPolComunas[10:14,]

length(SPolSuma)

## [1] 12

head(SPolSuma)

## Region CODIGO NOMBRE SHAPEAREA ZONA


## 1 1 <NA> <NA> NaN <NA>
## 2 2 <NA> <NA> NaN <NA>
## 3 NA 10 La Candelaria 7361364 ZONA3
## 4 NA 11 Laureles Estadio 7402954 ZONA4
## 5 NA 12 La América 3973374 ZONA4
## 6 NA 13 San Javier 4849268 ZONA4
Graficamos

plot(SPolBindAgregada,border='black',main='UNION')
plot(SPolSuma,col=rainbow(length(SPolUnion)),add=TRUE)

UNION

6.3
6.28
6.26
6.24
6.22
6.2
6.18

−75.6 −75.55
CROP

La extensión de un vector se puede ver con @bbox


ext(SPolComunas)

## SpatExtent : -75.6324264439868, -75.5247797206341, 6.17571857485516, 6.3128485283003 (xmin, xmax, ymin,

extension <- ext(-75.60,-75.56,6.23,6.28)


extension

## SpatExtent : -75.6, -75.56, 6.23, 6.28 (xmin, xmax, ymin, ymax)


SPolCrop <- crop(SPolComunas,extension)
SPolCrop

## class : SpatVector
## geometry : polygons
## dimensions : 12, 4 (geometries, attributes)
## extent : -75.6, -75.56, 6.23, 6.28 (xmin, xmax, ymin, ymax)
## coord. ref. : lon/lat WGS 84 (EPSG:4326)
## names : CODIGO NOMBRE SHAPEAREA ZONA
## type : <chr> <chr> <num> <chr>
## values : 04 Aranjuez 4.884e+06 ZONA1
## 05 Castilla 6.064e+06 ZONA2
## 07 Robledo 9.462e+06 ZONA2
Graficamos
plot(SPolComunas)
plot(SPolCrop,col='lightblue',add=TRUE)
plot(extension, lwd=3, col='red',add=TRUE)
text(SPolCrop,SPolCrop$CODIGO)

6.3
6.28

13 05 04
07

12
6.26

08
11
10
6.24

16 09
15 14
6.22
6.2
6.18

También podría gustarte