Aplicaciones de Overlay

#Institucion: Unidades Tecnológicas de Santander
# Programa: Ingenieria en Topografía
# Autor: "Aponte Saravia Jhonathan"
library(raster)     
## Loading required package: 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(rgeos)
## rgeos version: 0.5-2, (SVN revision 621)
##  GEOS runtime version: 3.6.1-CAPI-1.10.1 
##  Linking to sp version: 1.3-2 
##  Polygon checking: TRUE
library(maptools)
## Checking rgeos availability: TRUE
library(sf)
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(tmap)
## Warning: replacing previous import 'sf::st_make_valid' by
## 'lwgeom::st_make_valid' when loading 'tmap'
setwd("E:/A_UTS/2021-II/Curso geografia y sistemas de informacion geografica II/Modulo 9 Analisis de datos vectoriales/Taller")
list.files(path=".")
##  [1] "25m-cotas.dbf"    "25m-cotas.mshp"   "25m-cotas.prj"    "25m-cotas.shp"   
##  [5] "25m-cotas.shx"    "25m-curvas.dbf"   "25m-curvas.mshp"  "25m-curvas.prj"  
##  [9] "25m-curvas.shp"   "25m-curvas.shx"   "25m-rios.dbf"     "25m-rios.prj"    
## [13] "25m-rios.shp"     "25m-rios.shp.xml" "25m-rios.shx"     "26m-cotas.dbf"   
## [17] "26m-cotas.mshp"   "26m-cotas.prj"    "26m-cotas.shp"    "26m-cotas.shx"   
## [21] "26m-curvas.dbf"   "26m-curvas.mshp"  "26m-curvas.prj"   "26m-curvas.shp"  
## [25] "26m-curvas.shx"   "26m-rios.dbf"     "26m-rios.mshp"    "26m-rios.prj"    
## [29] "26m-rios.shp"     "26m-rios.shx"     "geol25m_p.cpg"    "geol25m_p.dbf"   
## [33] "geol25m_p.mshp"   "geol25m_p.prj"    "geol25m_p.shp"    "geol25m_p.shx"   
## [37] "geol26m_p.cpg"    "geol26m_p.dbf"    "geol26m_p.mshp"   "geol26m_p.prj"   
## [41] "geol26m_p.shp"    "geol26m_p.shx"    "muni.cpg"         "muni.dbf"        
## [45] "muni.mshp"        "muni.prj"         "muni.shp"         "muni.shx"        
## [49] "Provi.dbf"        "Provi.mshp"       "Provi.prj"        "Provi.shp"       
## [53] "Provi.shx"
dataFolder<-"E://A_UTS//2021-II//Curso geografia y sistemas de informacion geografica II//Modulo 9 Analisis de datos vectoriales//Taller//"


distr<-shapefile(paste0(dataFolder,"muni.shp"))
distr
## class       : SpatialPolygonsDataFrame 
## features    : 19 
## extent      : -75.57961, -74.6838, -13.18564, -12.36028  (xmin, xmax, ymin, ymax)
## crs         : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
## variables   : 18
## names       : IDDIST,    DISTRITO, FUENTE,         Area, covid_IDDI, covid_sabe, covid_No.s, covid_Urba, covid_Rura, covid_Pobl, covid_fmas, covid_ffen, covid_tfal, covid_Imas, covid_Ifem, ... 
## min values  : 090101, ACOBAMBILLA,   INEI, 5232390.0324,      90101,        368,         83,          0,        524,        524,          0,          0,          0,          0,          0, ... 
## max values  : 090119,       YAULI,   INEI, 736862727.78,      90119,      33507,       4225,      36975,      14922,      40745,         29,          6,         35,       1356,       1567, ...
prov<-shapefile(paste0(dataFolder,"Provi.shp"))
prov
## class       : SpatialPolygonsDataFrame 
## features    : 1 
## extent      : -75.57961, -74.6838, -13.18564, -12.36028  (xmin, xmax, ymin, ymax)
## crs         : +proj=utm +zone=18 +south +ellps=WGS84 +units=m +no_defs 
## variables   : 1
## names       : id 
## value       : NA
# Presentando la grfica de ambos datos 
par(mfrow=c(1,2))
plot(prov, main="Provincia de Huancavelica")
plot(distr, main="Distritos de provincia de Huancavelica")

par(mfrow=c(1,1))

distr$DISTRITO
##  [1] "ACORIA"           "HUACHOCOLPA"      "ASCENSION"        "CONAYCA"         
##  [5] "IZCUCHACA"        "NUEVO OCCORO"     "YAULI"            "PALCA"           
##  [9] "ACOBAMBILLA"      "MANTA"            "LARIA"            "HUANDO"          
## [13] "VILCA"            "CUENCA"           "HUANCAVELICA"     "MARISCAL CACERES"
## [17] "MOYA"             "PILCHACA"         "HUAYLLAHUARA"
#extraemos poligonos independientes o en grupo.

# Limite provinicia 
acob <- subset(distr, DISTRITO=="ACOBAMBILLA")


var <-subset(distr,DISTRITO=="VILCA"| DISTRITO=="MOYA"| DISTRITO=="MANTA")

par(mfrow=c(1,2))
plot(acob, main="Distrito Acobambilla")
plot(var, main="Varios distritos")

par(mfrow=c(1,1))

# Uniendo poligonos 

distnorte <-union(acob,var)

plot(distnorte)

# conviertiendo en un solo poligono (Disolver poligono) 
lps <- getSpPPolygonsLabptSlots(distnorte)
## Warning: use coordinates method
IDOneBin <- cut(lps[,1], range(lps[,1]), include.lowest=TRUE)
ddisolv<- unionSpatialPolygons(distnorte,IDOneBin)


plot(ddisolv, main="Municipios de provincia de Huancavelica")

ddisolv
## class       : SpatialPolygons 
## features    : 1 
## extent      : -75.57961, -75.08354, -12.8285, -12.3785  (xmin, xmax, ymin, ymax)
## crs         : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
# definimos el dato en otro  sistema de refrencia espacial.

cnor = CRS("+proj=utm +zone=18 +south +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0")
municnorte<- spTransform(ddisolv,cnor)
municnorte
## class       : SpatialPolygons 
## features    : 1 
## extent      : 437056, 490921.1, 8581801, 8631589  (xmin, xmax, ymin, ymax)
## crs         : +proj=utm +zone=18 +south +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
plot(municnorte,axes = TRUE, main = 'Cuenca cono norte')

# Generamos area de influencia del poligono.
# para generar areas de influencia
cuenca<-gBuffer(municnorte, width=500)
plot(cuenca, main= "Influencia de territorio")

cuenca
## class       : SpatialPolygons 
## features    : 1 
## extent      : 436559.2, 491421.1, 8581301, 8632085  (xmin, xmax, ymin, ymax)
## crs         : +proj=utm +zone=18 +south +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
tm_shape(cuenca)+ tm_polygons()+ tm_borders() + tm_shape(municnorte)+ tm_polygons()+ tm_borders()
## Warning: One tm layer group has duplicated layer types, which are omitted. To
## draw multiple layers of the same type, use multiple layer groups (i.e. specify
## tm_shape prior to each of them).

## Warning: One tm layer group has duplicated layer types, which are omitted. To
## draw multiple layers of the same type, use multiple layer groups (i.e. specify
## tm_shape prior to each of them).

# ahora integramos otros datos para continuar con otras aplicacaiones 

rio26<-shapefile(paste0(dataFolder,"26m-rios.shp"))
rio26
## class       : SpatialLinesDataFrame 
## features    : 1490 
## extent      : 445672, 5e+05, 8562813, 8618156  (xmin, xmax, ymin, ymax)
## crs         : +proj=utm +zone=18 +south +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
## variables   : 4
## names       :      NOMBRE, TIPO, ESTADO, CODIGO 
## min values  : _AHUINCOCHA,    Q,      1,     NA 
## max values  :  YURAJRANRA,    R,      2,     NA
tm_shape(cuenca)+ tm_polygons()+ tm_borders()+ tm_shape(rio26)+ tm_lines()
## Warning: One tm layer group has duplicated layer types, which are omitted. To
## draw multiple layers of the same type, use multiple layer groups (i.e. specify
## tm_shape prior to each of them).

rio25<-shapefile(paste0(dataFolder,"25m-rios.shp"))
rio25
## class       : SpatialLinesDataFrame 
## features    : 1130 
## extent      : 445571.6, 500001, 8618110, 8673446  (xmin, xmax, ymin, ymax)
## crs         : +proj=utm +zone=18 +south +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
## variables   : 4
## names       :    NOMBRE, TIPO, ESTADO, CODIGO 
## min values  : Abrachica,    Q,      1,   1846 
## max values  :     Yunga,    R,      2,   1846
hidro <-union(rio26,rio25)

plot(hidro)

#generamos el proceso de recorte de recorte con el borde del territorio

clip_02 <- intersect(hidro, cuenca)
clip_02
## class       : SpatialLinesDataFrame 
## features    : 630 
## extent      : 445719, 490382.9, 8581687, 8632061  (xmin, xmax, ymin, ymax)
## crs         : +proj=utm +zone=18 +south +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0 
## variables   : 4
## names       :      NOMBRE, TIPO, ESTADO, CODIGO 
## min values  :       ACCHI,    Q,      1,   1846 
## max values  : YURAJOCOCHA,    R,      2,   1846
 plot(clip_02)

 tm_shape(cuenca)+ tm_polygons()+ tm_borders()+ tm_shape(clip_02)+ tm_lines()
## Warning: One tm layer group has duplicated layer types, which are omitted. To
## draw multiple layers of the same type, use multiple layer groups (i.e. specify
## tm_shape prior to each of them).

# generando el area de influencia revereña de la cueca.
 Zonarev<-gBuffer(clip_02, width=200)
 
 #Presentando la regiones que cooresponden a las zonas de proteccion 
 tm_shape(cuenca)+ tm_polygons()+ tm_borders() + tm_shape(Zonarev)+ tm_polygons()+ tm_borders()+tm_grid()
## Warning: One tm layer group has duplicated layer types, which are omitted. To
## draw multiple layers of the same type, use multiple layer groups (i.e. specify
## tm_shape prior to each of them).

## Warning: One tm layer group has duplicated layer types, which are omitted. To
## draw multiple layers of the same type, use multiple layer groups (i.e. specify
## tm_shape prior to each of them).