Documentos de Académico
Documentos de Profesional
Documentos de Cultura
SpatialPolygons
library(sp)
library(raster)
library(terra)
Comunas Medellin
Comunas <- '../../Archivos/Medellin/Comunas/Limite_Comuna_Corregimiento.shp'
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)
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
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
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)
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)
o de forma equivalente
SPolComunas <- SPolComunas[c('CODIGO','NOMBRE','ZONA','SHAPEAREA')]
names(SPolComunas)
length(SPolComunas)
## [1] 16
nrow(SPolComunas)
## [1] 16
ncol(SPolComunas)
## [1] 4
Atributo Nombre
SPolComunas$NOMBRE
SPolComunas$NOMBRE[c(1,5,9)]
head(values(SPolComunas),6)
values(SPolComunas[5,])
x <- geom(SPolComunas[14,])
head(x,10)
Polígonos
6.23
6.22
6.21
y1
6.20
6.19
6.18
x1
Geometría Completo
x <- geom(SPolComunas)
head(x,8)
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
x1
Extraer Atributos
DF <- data.frame(SPolComunas)
head(DF)
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
head(values(SPolComunas),5)
names(DF2[1])
## [1] "CODIGO"
names(DF2[1]) <- "CODIGO2"
head(values(SPolComunas2),5)
perim(SPolComunas)
k <- order(perim(SPolComunas))
SPolComunas$NOMBRE[k]
expanse(SPolComunas)/10000
k <- order(expanse(SPolComunas))
SPolComunas$NOMBRE[k]
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%")
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)
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)
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
length(SPolUnion)
## [1] 12
head(SPolUnion)
UNION
6.3
6.28
6.26
6.24
6.22
6.2
6.18
−75.6 −75.55
Suma
length(SPolSuma)
## [1] 12
head(SPolSuma)
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
## 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