A continuación se muestran los procesos de creación de datos vectoriales utilizando la librería “sp”, la librería “rgdal” y “raster”, en este ejercicio se explica cómo crear datos vectoriales de tipo punto, línea y polígono , así mismo, en esta guía se explica como hacer el intento de presentar los datos en una aproximación de mapa aplicando la librería tmap, con el propósito de difundir el potencial de las librerías sp y tmap, en el programa de RStudio, del mismo modo, fomentar el desarrollo de las habilidades en el uso de los datos espaciales.

library(sp)
library(rgdal)
## rgdal: version: 1.4-8, (SVN revision 845)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 2.2.3, released 2017/11/20
##  Path to GDAL shared files: C:/Users/Usuario/Documents/R/win-library/3.6/rgdal/gdal
##  GDAL binary built with GEOS: TRUE 
##  Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
##  Path to PROJ.4 shared files: C:/Users/Usuario/Documents/R/win-library/3.6/rgdal/proj
##  Linking to sp version: 1.4-1
library(tmap)
## Warning: replacing previous import 'sf::st_make_valid' by
## 'lwgeom::st_make_valid' when loading 'tmap'
library(raster)
#Primera opcion

punt <- rbind(c(584694.64,527402.96),c(584166.54,526778.46),c(585413.33,527126.30), c(585254.07,526487.81), c(584746.83,525920.30))
punt.sp <- SpatialPoints(punt)
summary(punt.sp)
## Object of class SpatialPoints
## Coordinates:
##                min      max
## coords.x1 584166.5 585413.3
## coords.x2 525920.3 527403.0
## Is projected: NA 
## proj4string : [NA]
## Number of points: 5
plot(punt.sp)

#Adicionando sistemas de coordenadas 
is.projected(punt.sp)
## [1] NA
proj4string(punt.sp) <-CRS("+proj=utm +zone=18 +ellps=WGS84 +datum=WGS84 +units=m +no_defs")

summary(punt.sp)
## Object of class SpatialPoints
## Coordinates:
##                min      max
## coords.x1 584166.5 585413.3
## coords.x2 525920.3 527403.0
## Is projected: TRUE 
## proj4string :
## [+proj=utm +zone=18 +ellps=WGS84 +datum=WGS84 +units=m +no_defs
## +towgs84=0,0,0]
## Number of points: 5
#Agregando atributos 

atts <- data.frame(attr1 = c("P1", "P2", "P3", "P4", "P5"), attr2 = c(1:5), 
                   attr3 = c("Almacen", "reservorio", "caseta1", "vivienda", "caseta2"))


punt.df <- SpatialPointsDataFrame(punt.sp, atts)
punt.df
## class       : SpatialPointsDataFrame 
## features    : 5 
## extent      : 584166.5, 585413.3, 525920.3, 527403  (xmin, xmax, ymin, ymax)
## crs         : +proj=utm +zone=18 +ellps=WGS84 +datum=WGS84 +units=m +no_defs +towgs84=0,0,0 
## variables   : 3
## names       : attr1, attr2,    attr3 
## min values  :    P1,     1,  Almacen 
## max values  :    P5,     5, vivienda
#Segunda opcion 
# Primera Linea
est1<- c(585436.32,585388.48,585272.61,585124.14,585376.01,585625.35,585699.67,586069.90)
nort1<- c(527095.11,526978.94,526900.43,526528.08,526439.07,526181.24,526101.68,525720.83)

latlon1<- cbind(est1,nort1)
crdref <- CRS('+proj=utm +zone=18 +ellps=WGS84 +datum=WGS84 +units=m +no_defs')
via1 <- spLines(latlon1, crs=crdref)
via1
## class       : SpatialLines 
## features    : 1 
## extent      : 585124.1, 586069.9, 525720.8, 527095.1  (xmin, xmax, ymin, ymax)
## crs         : +proj=utm +zone=18 +ellps=WGS84 +datum=WGS84 +units=m +no_defs +towgs84=0,0,0
plot(via1)

atts1 <- data.frame(attr1 = c("P1", "P2", "P3", "P4", "P5","P6", "P7", "P8"))


red1 <- SpatialLinesDataFrame(via1, atts1)
red1
## class       : SpatialLinesDataFrame 
## features    : 1 
## extent      : 585124.1, 586069.9, 525720.8, 527095.1  (xmin, xmax, ymin, ymax)
## crs         : +proj=utm +zone=18 +ellps=WGS84 +datum=WGS84 +units=m +no_defs +towgs84=0,0,0 
## variables   : 1
## names       : attr1 
## value       :    P1
# Segunda linea
est2<- c(584707.52,584765.60, 585231.79,585374.39)
nort2<- c(525895.64,526035.05,526107.62,526434.13)

latlon1<- cbind(est2,nort2)
crdref <- CRS('+proj=utm +zone=18 +ellps=WGS84 +datum=WGS84 +units=m +no_defs')
via2 <- spLines(latlon1, crs=crdref)
via2
## class       : SpatialLines 
## features    : 1 
## extent      : 584707.5, 585374.4, 525895.6, 526434.1  (xmin, xmax, ymin, ymax)
## crs         : +proj=utm +zone=18 +ellps=WGS84 +datum=WGS84 +units=m +no_defs +towgs84=0,0,0
plot(via2)

atts2 <- data.frame(attr2 = c("P1", "P2", "P3", "P4"))


red2 <- SpatialLinesDataFrame(via2, atts2)
red2
## class       : SpatialLinesDataFrame 
## features    : 1 
## extent      : 584707.5, 585374.4, 525895.6, 526434.1  (xmin, xmax, ymin, ymax)
## crs         : +proj=utm +zone=18 +ellps=WGS84 +datum=WGS84 +units=m +no_defs +towgs84=0,0,0 
## variables   : 1
## names       : attr2 
## value       :    P1
# Tercera linea
est3<- c(585879.31,585495.23,585701.46)
nort3<- c(525227.34,525571.18,526094.24)

latlon1<- cbind(est3,nort3)
crdref <- CRS('+proj=utm +zone=18 +ellps=WGS84 +datum=WGS84 +units=m +no_defs')
via3 <- spLines(latlon1, crs=crdref)
via3
## class       : SpatialLines 
## features    : 1 
## extent      : 585495.2, 585879.3, 525227.3, 526094.2  (xmin, xmax, ymin, ymax)
## crs         : +proj=utm +zone=18 +ellps=WGS84 +datum=WGS84 +units=m +no_defs +towgs84=0,0,0
plot(via3)

atts3 <- data.frame(attr3 = c("P1", "P2", "P3"))

red3 <- SpatialLinesDataFrame(via3, atts3)
red3
## class       : SpatialLinesDataFrame 
## features    : 1 
## extent      : 585495.2, 585879.3, 525227.3, 526094.2  (xmin, xmax, ymin, ymax)
## crs         : +proj=utm +zone=18 +ellps=WGS84 +datum=WGS84 +units=m +no_defs +towgs84=0,0,0 
## variables   : 1
## names       : attr3 
## value       :    P1
# Cuarta linea
est4<- c(585496.10,585452.88,585769.90,585724.77,586402.83)
nort4<- c(527254.79,527133.19,527007.82,526865.42,526581.52)

latlon1<- cbind(est4,nort4)
crdref <- CRS('+proj=utm +zone=18 +ellps=WGS84 +datum=WGS84 +units=m +no_defs')
via4 <- spLines(latlon1, crs=crdref)
via4
## class       : SpatialLines 
## features    : 1 
## extent      : 585452.9, 586402.8, 526581.5, 527254.8  (xmin, xmax, ymin, ymax)
## crs         : +proj=utm +zone=18 +ellps=WGS84 +datum=WGS84 +units=m +no_defs +towgs84=0,0,0
plot(via4)

atts4 <- data.frame(attr4 = c("P1", "P2","P3", "P4","P5"))

red4 <- SpatialLinesDataFrame(via4, atts4)
red4
## class       : SpatialLinesDataFrame 
## features    : 1 
## extent      : 585452.9, 586402.8, 526581.5, 527254.8  (xmin, xmax, ymin, ymax)
## crs         : +proj=utm +zone=18 +ellps=WGS84 +datum=WGS84 +units=m +no_defs +towgs84=0,0,0 
## variables   : 1
## names       : attr4 
## value       :    P1
subs_union <- union(red1, red2)
unir <-  union(subs_union, red3)
unir1 <-  union(unir, red4)

plot(unir1)

#para crear poligonos 
#Primer poligono.
este<- c(585361.65, 584425.21,584233.73,584442.30,584805.81,585079.26,585130.25,585361.65)
norte<- c(526430.86,526764.45,526327.28,526114.78,525653.62,525922.02,525877.93,526430.86)
poly1 <- sp::Polygon(cbind(este,norte))
poli <- sp::Polygons(list(poly1), ID = "A")
polig <- sp::SpatialPolygons(list(poli))
polig
## class       : SpatialPolygons 
## features    : 1 
## extent      : 584233.7, 585361.7, 525653.6, 526764.4  (xmin, xmax, ymin, ymax)
## crs         : NA
plot(polig)

proj4string(polig) = CRS('+proj=utm +zone=18 +ellps=WGS84 +datum=WGS84 +units=m +no_defs')
summary(polig)
## Object of class SpatialPolygons
## Coordinates:
##        min      max
## x 584233.7 585361.7
## y 525653.6 526764.4
## Is projected: TRUE 
## proj4string :
## [+proj=utm +zone=18 +ellps=WGS84 +datum=WGS84 +units=m +no_defs
## +towgs84=0,0,0]
#Segundo poligono.
este1<- c(585130.25,585440.63,585690.41,585377.74,585130.25)
norte1<- c(525877.93,525509.54,526095.40,526426.29,525877.93)
poly1 <- sp::Polygon(cbind(este1,norte1))
poli1 <- sp::Polygons(list(poly1), ID = "A")
polig1 <- sp::SpatialPolygons(list(poli1))
polig1
## class       : SpatialPolygons 
## features    : 1 
## extent      : 585130.2, 585690.4, 525509.5, 526426.3  (xmin, xmax, ymin, ymax)
## crs         : NA
plot(polig1)

proj4string(polig1) = CRS('+proj=utm +zone=18 +ellps=WGS84 +datum=WGS84 +units=m +no_defs')
summary(polig1)
## Object of class SpatialPolygons
## Coordinates:
##        min      max
## x 585130.2 585690.4
## y 525509.5 526426.3
## Is projected: TRUE 
## proj4string :
## [+proj=utm +zone=18 +ellps=WGS84 +datum=WGS84 +units=m +no_defs
## +towgs84=0,0,0]
#Tercer poligono.
este1<- c(585440.63,585695.17,585769.90,585980.42,586034.50,586065.84,585703.98,585440.63)
norte1<- c(525509.54,525215.52,525020.69,525231.17,525422.34,525715.01,526089.42,525509.54)
poly1 <- sp::Polygon(cbind(este1,norte1))
poli2 <- sp::Polygons(list(poly1), ID = "A")
polig2 <- sp::SpatialPolygons(list(poli2))
polig2
## class       : SpatialPolygons 
## features    : 1 
## extent      : 585440.6, 586065.8, 525020.7, 526089.4  (xmin, xmax, ymin, ymax)
## crs         : NA
plot(polig2)

proj4string(polig2) = CRS('+proj=utm +zone=18 +ellps=WGS84 +datum=WGS84 +units=m +no_defs')
summary(polig2)
## Object of class SpatialPolygons
## Coordinates:
##        min      max
## x 585440.6 586065.8
## y 525020.7 526089.4
## Is projected: TRUE 
## proj4string :
## [+proj=utm +zone=18 +ellps=WGS84 +datum=WGS84 +units=m +no_defs
## +towgs84=0,0,0]
#cuarto poligono.
este1<- c(585378.85,585608.26,585449.46,585389.45,585269.54,585128.15,585378.85)
norte1<- c(526441.90,527069.42,527132.39,526972.99,526889.88,526528.43,526441.90)
poly1 <- sp::Polygon(cbind(este1,norte1))
poli3 <- sp::Polygons(list(poly1), ID = "A")
polig3 <- sp::SpatialPolygons(list(poli3))
polig3
## class       : SpatialPolygons 
## features    : 1 
## extent      : 585128.2, 585608.3, 526441.9, 527132.4  (xmin, xmax, ymin, ymax)
## crs         : NA
plot(polig3)

proj4string(polig3) = CRS('+proj=utm +zone=18 +ellps=WGS84 +datum=WGS84 +units=m +no_defs')
summary(polig3)
## Object of class SpatialPolygons
## Coordinates:
##        min      max
## x 585128.2 585608.3
## y 526441.9 527132.4
## Is projected: TRUE 
## proj4string :
## [+proj=utm +zone=18 +ellps=WGS84 +datum=WGS84 +units=m +no_defs
## +towgs84=0,0,0]
poliunion <- union(polig, polig1)
vinc <-  union(poliunion, polig2)
poligono <-union(vinc,polig3)
plot(poligono, axe=TRUE)

poligono
## class       : SpatialPolygons 
## features    : 4 
## extent      : 584233.7, 586065.8, 525020.7, 527132.4  (xmin, xmax, ymin, ymax)
## crs         : +proj=utm +zone=18 +ellps=WGS84 +datum=WGS84 +units=m +no_defs +towgs84=0,0,0
spplot(poligono,axe=TRUE)

#calculo de area
library(geosphere)
poligono$area_sqkm <- area(poligono)/10000 
poligono$area_sqkm
## [1] 68.75784 26.36017 37.39102 15.49646
# Presentando en el mapa 

tm_shape(poligono)+ tm_polygons()+ tm_grid(n.x = 5, n.y = 5,labels.inside.frame = FALSE)+
tm_compass(position = c("right", "top"))+ tm_layout(title = "Mapa de predios")+
tm_scale_bar(position=c("left", "bottom"))+
tm_shape(unir1)+tm_lines(col= "red", lwd= 1.5)+
tm_shape(punt.sp)+tm_dots(size = 0.2,col= "blue")