# Exploring vector data and raster data.

# Load packages
library(sp)
library(maptools)   # spatial vector data
## Checking rgeos availability: TRUE
library (rgdal)     # Geospatial Data Abstraction Library for spatial data 
## 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 (raster)    # raster and vector data read & write

# Exploring vector data.
setwd("E:/Clases R/Huancavelica/datos shape")
list.files(path=".")
##  [1] "25m"                 "26m"                 "cuencamantaro.dbf"  
##  [4] "cuencamantaro.prj"   "cuencamantaro.shp"   "cuencamantaro.shx"  
##  [7] "Datosgeoquimica.dbf" "Datosgeoquimica.prj" "Datosgeoquimica.shp"
## [10] "Datosgeoquimica.shx" "Departamento.dbf"    "Departamento.prj"   
## [13] "Departamento.shp"    "Departamento.shx"    "Distrito.dbf"       
## [16] "Distrito.prj"        "Distrito.shp"        "Distrito.shx"       
## [19] "EI.dbf"              "EI.prj"              "EI.shp"             
## [22] "EI.shx"              "municipio.dbf"       "municipio.mshp"     
## [25] "municipio.prj"       "municipio.shp"       "municipio.shx"      
## [28] "Provi.dbf"           "Provi.prj"           "Provi.shp"          
## [31] "Provi.shx"           "Puntosedu.dbf"       "Puntosedu.prj"      
## [34] "Puntosedu.shp"       "Puntosedu.shx"
point <- readOGR('Puntosedu.shp', layer='Puntosedu')
## OGR data source with driver: ESRI Shapefile 
## Source: "E:\Clases R\Huancavelica\datos shape\Puntosedu.shp", layer: "Puntosedu"
## with 766 features
## It has 10 fields
summary(point)
## Object of class SpatialPointsDataFrame
## Coordinates:
##               min     max
## coords.x1  442084  533835
## coords.x2 8549115 8631410
## Is projected: TRUE 
## proj4string :
## [+proj=utm +zone=18 +south +ellps=WGS84 +units=m +no_defs]
## Number of points: 766
## Data attributes:
##     COD_MOD           CODLOCAL                             DIR_CEN   
##  Min.   :   1506   Min.   :   1506   PLAZA PRINCIPAL S/N       :211  
##  1st Qu.: 430597   1st Qu.: 173228   PLAZA PRINCIPAL           :174  
##  Median :1059612   Median : 175620   PARQUE PLAZA PRINCIPAL S/N: 10  
##  Mean   :1315143   Mean   : 837476   CALLE REAL S/N            :  7  
##  3rd Qu.:1602893   3rd Qu.: 678999   AVENIDA HUANCAVELICA S/N  :  6  
##  Max.   :3895115   Max.   :3895115   JIRON HILDAURO CASTRO S/N :  6  
##                                      (Other)                   :352  
##      CODGEO         CODCP_MED               CEN_POB        N_ALUM       
##  Min.   : 90101   Min.   :110077   HUANCAVELICA : 41   Min.   :   0.00  
##  1st Qu.: 90103   1st Qu.:121198   ASCENSION    : 15   1st Qu.:   6.00  
##  Median : 90106   Median :132420   SANTA ANA    : 14   Median :  15.00  
##  Mean   : 90507   Mean   :308632   VISTA ALEGRE : 10   Mean   :  49.86  
##  3rd Qu.: 90117   3rd Qu.:540870   YANANACO     :  8   3rd Qu.:  47.75  
##  Max.   :151032   Max.   :683253   SAN CRISTOBAL:  7   Max.   :1413.00  
##                                    (Other)      :671                    
##    N_DOCENTES           X                Y          
##  Min.   : 0.000   Min.   :442084   Min.   :8549115  
##  1st Qu.: 1.000   1st Qu.:497277   1st Qu.:8587191  
##  Median : 1.500   Median :505086   Median :8601020  
##  Mean   : 3.786   Mean   :504276   Mean   :8599779  
##  3rd Qu.: 4.000   3rd Qu.:515095   3rd Qu.:8610324  
##  Max.   :85.000   Max.   :533835   Max.   :8631410  
## 
# Attribute analysis 

sd(point$N_ALUM) #calculation of standard deviation of a column 
## [1] 120.0881
mean(point$N_ALUM) #calculation of the average data set
## [1] 49.86423
length(point$N_ALUM) # amount of data in sample
## [1] 766
summary(point$N_ALUM) #calculation of descriptive statistics. 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    0.00    6.00   15.00   49.86   47.75 1413.00
# Spatial exploration 
point  #full exploration
## class       : SpatialPointsDataFrame 
## features    : 766 
## extent      : 442084, 533835, 8549115, 8631410  (xmin, xmax, ymin, ymax)
## crs         : +proj=utm +zone=18 +south +ellps=WGS84 +units=m +no_defs 
## variables   : 10
## names       : COD_MOD, CODLOCAL,   DIR_CEN, CODGEO, CODCP_MED,   CEN_POB, N_ALUM, N_DOCENTES,      X,       Y 
## min values  :    1506,     1506, ACCOCCASA,  90101,    110077, 3 DE MAYO,      0,          0, 442084, 8549115 
## max values  : 3895115,  3895115, ZUNIPAMPA, 151032,    683253, ZUNIPAMPA,   1413,         85, 533835, 8631410
class(point) # What type of vector is a point, line or polygon.
## [1] "SpatialPointsDataFrame"
## attr(,"package")
## [1] "sp"
extent(point) # Extreme values
## class      : Extent 
## xmin       : 442084 
## xmax       : 533835 
## ymin       : 8549115 
## ymax       : 8631410
crs(point) # reference system
## CRS arguments:
##  +proj=utm +zone=18 +south +ellps=WGS84 +units=m +no_defs
names(point) # Attribute name.
##  [1] "COD_MOD"    "CODLOCAL"   "DIR_CEN"    "CODGEO"     "CODCP_MED" 
##  [6] "CEN_POB"    "N_ALUM"     "N_DOCENTES" "X"          "Y"
# Map point shape file
x11()
plot(point,
     main= "Number of schools Huancavelica",
     pch=10,   # symbol type
     cex=0.8,  # symbol size
      axes=TRUE)  # symbol size

hist(point$N_DOCENTES,
     main= "Number of teachers by school Huancavelica")

# Exploring  raster data.


setwd("E:/Datos dem/Colombia/Datos Alos palsar")

files <- list.files(pattern='.tif')
files
## [1] "AP_20846_FBS_F0120_RT2.dem.tif"
dem <- raster("E:/Datos dem/Colombia/Datos Alos palsar/AP_20846_FBS_F0120_RT2.dem.tif")

dem
## class      : RasterLayer 
## dimensions : 553, 485, 268205  (nrow, ncol, ncell)
## resolution : 30, 30  (x, y)
## extent     : 1097115, 1111665, 1269285, 1285875  (xmin, xmax, ymin, ymax)
## crs        : +proj=tmerc +lat_0=4.596200416666666 +lon_0=-74.07750791666666 +k=1 +x_0=1000000 +y_0=1000000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs 
## source     : E:/Datos dem/Colombia/Datos Alos palsar/AP_20846_FBS_F0120_RT2.dem.tif 
## names      : AP_20846_FBS_F0120_RT2.dem 
## values     : 603.6502, 1775.037  (min, max)
summary(dem)
## Warning in .local(object, ...): summary is an estimate based on a sample of 1e+05 cells (37.28% of all cells)
##         AP_20846_FBS_F0120_RT2.dem
## Min.                      600.0993
## 1st Qu.                   810.8240
## Median                    931.4439
## 3rd Qu.                  1082.3910
## Max.                     1784.5298
## NA's                        0.0000
hist(dem, 
     main="Frecuency data", 
     xlab="altitude elevation", 
     border="blue", 
     col="green",
     xlim=c(500,2000),
     las=1, 
     breaks=5)
## Warning in .hist1(x, maxpixels = maxpixels, main = main, plot = plot, ...): 37%
## of the raster cells were used. 100000 values used.

x11()
plot(dem, main= "DEM Alos Palsar ")