#install.packages("raster")
#install.packages("sp")
#install.packages("rgdal")
library(raster)
## Warning: package 'raster' was built under R version 4.0.5
## Loading required package: sp
## Warning: package 'sp' was built under R version 4.0.5
library(sp)
library(rgdal)
## Warning: package 'rgdal' was built under R version 4.0.5
## rgdal: version: 1.5-23, (SVN revision 1121)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.2.1, released 2020/12/29
## Path to GDAL shared files: C:/Users/jose_/OneDrive/Documentos/R/win-library/4.0/rgdal/gdal
## GDAL binary built with GEOS: TRUE 
## Loaded PROJ runtime: Rel. 7.2.1, January 1st, 2021, [PJ_VERSION: 721]
## Path to PROJ shared files: C:/Users/jose_/OneDrive/Documentos/R/win-library/4.0/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.4-5
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading rgdal.
## Overwritten PROJ_LIB was C:/Users/jose_/OneDrive/Documentos/R/win-library/4.0/rgdal/proj
#concatenar las bandas
bandas <-paste0("C:/Users/jose_/Desktop/2021-1/ELECTIVA_2/zipaquira/LC08_L1TP_008056_20210325_20210402_02_T1_B",1:7,".tif")
#bandas
landsat <- stack(bandas)
#landsat
landsat <- subset(landsat,1:7)
#landsat
#install.packages("readxl")
library(readxl)
## Warning: package 'readxl' was built under R version 4.0.5
#Llamamos el archivo de excel con puntos
puntos <- read_xlsx("C:/Users/jose_/Desktop/2021-1/ELECTIVA_2/zipaquira/zipaquira.xlsx")
puntos
## # A tibble: 12 x 4
##       id clases       x         y        
##    <dbl> <chr>        <chr>     <chr>    
##  1     1 vegetacion   612981.00 541832.81
##  2     2 agua         629732.96 549924.36
##  3     3 construccion 603704.33 541556.98
##  4     4 suelos       627836.99 558812.65
##  5     5 vegetacion   608173.76 561565.99
##  6     6 agua         625441.81 544964.80
##  7     7 construccion 617255.40 542613.51
##  8     8 suelos       622199.31 563905.23
##  9     9 vegetacion   596713.44 542083.22
## 10    10 agua         615312.83 569745.28
## 11    11 construccion 620499.14 548921.44
## 12    12 suelos       622312.07 551847.57
#install.packages("sf")
library(sf)
## Warning: package 'sf' was built under R version 4.0.5
## Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
#Covertimos el archivo de excel en BDE
puntos_geo  <- puntos %>%
  st_as_sf (coords = c("x","y"))
plot(puntos_geo)

#puntos_geo
df <- extract(landsat, puntos_geo)
df
##       LC08_L1TP_008056_20210325_20210402_02_T1_B1
##  [1,]                                       32600
##  [2,]                                       42556
##  [3,]                                       31439
##  [4,]                                       28101
##  [5,]                                       45897
##  [6,]                                       37805
##  [7,]                                       38986
##  [8,]                                       44435
##  [9,]                                       32482
## [10,]                                       31514
## [11,]                                       28463
## [12,]                                       31992
##       LC08_L1TP_008056_20210325_20210402_02_T1_B2
##  [1,]                                       32304
##  [2,]                                       42607
##  [3,]                                       31075
##  [4,]                                       27354
##  [5,]                                       46408
##  [6,]                                       37596
##  [7,]                                       38911
##  [8,]                                       44479
##  [9,]                                       32401
## [10,]                                       31181
## [11,]                                       28032
## [12,]                                       31430
##       LC08_L1TP_008056_20210325_20210402_02_T1_B3
##  [1,]                                       31396
##  [2,]                                       41177
##  [3,]                                       30033
##  [4,]                                       26621
##  [5,]                                       45700
##  [6,]                                       36352
##  [7,]                                       37823
##  [8,]                                       43875
##  [9,]                                       31662
## [10,]                                       30053
## [11,]                                       26576
## [12,]                                       29930
##       LC08_L1TP_008056_20210325_20210402_02_T1_B4
##  [1,]                                       31858
##  [2,]                                       42290
##  [3,]                                       30550
##  [4,]                                       26644
##  [5,]                                       47190
##  [6,]                                       37054
##  [7,]                                       38679
##  [8,]                                       45052
##  [9,]                                       32365
## [10,]                                       30535
## [11,]                                       26771
## [12,]                                       30523
##       LC08_L1TP_008056_20210325_20210402_02_T1_B5
##  [1,]                                       35201
##  [2,]                                       44280
##  [3,]                                       34413
##  [4,]                                       30611
##  [5,]                                       50330
##  [6,]                                       39006
##  [7,]                                       41657
##  [8,]                                       47763
##  [9,]                                       35759
## [10,]                                       32550
## [11,]                                       30093
## [12,]                                       34508
##       LC08_L1TP_008056_20210325_20210402_02_T1_B6
##  [1,]                                       26665
##  [2,]                                       28392
##  [3,]                                       26748
##  [4,]                                       25427
##  [5,]                                       35486
##  [6,]                                       28232
##  [7,]                                       30116
##  [8,]                                       30729
##  [9,]                                       29549
## [10,]                                       25735
## [11,]                                       24204
## [12,]                                       26528
##       LC08_L1TP_008056_20210325_20210402_02_T1_B7
##  [1,]                                       19782
##  [2,]                                       21067
##  [3,]                                       20358
##  [4,]                                       20745
##  [5,]                                       27729
##  [6,]                                       20331
##  [7,]                                       21753
##  [8,]                                       22378
##  [9,]                                       23515
## [10,]                                       19758
## [11,]                                       19290
## [12,]                                       20921
df <- aggregate(df, list(puntos_geo$clases), mean)
df
##        Group.1 LC08_L1TP_008056_20210325_20210402_02_T1_B1
## 1         agua                                    37291.67
## 2 construccion                                    32962.67
## 3       suelos                                    34842.67
## 4   vegetacion                                    36993.00
##   LC08_L1TP_008056_20210325_20210402_02_T1_B2
## 1                                    37128.00
## 2                                    32672.67
## 3                                    34421.00
## 4                                    37037.67
##   LC08_L1TP_008056_20210325_20210402_02_T1_B3
## 1                                    35860.67
## 2                                    31477.33
## 3                                    33475.33
## 4                                    36252.67
##   LC08_L1TP_008056_20210325_20210402_02_T1_B4
## 1                                    36626.33
## 2                                    32000.00
## 3                                    34073.00
## 4                                    37137.67
##   LC08_L1TP_008056_20210325_20210402_02_T1_B5
## 1                                    38612.00
## 2                                    35387.67
## 3                                    37627.33
## 4                                    40430.00
##   LC08_L1TP_008056_20210325_20210402_02_T1_B6
## 1                                    27453.00
## 2                                    27022.67
## 3                                    27561.33
## 4                                    30566.67
##   LC08_L1TP_008056_20210325_20210402_02_T1_B7
## 1                                    20385.33
## 2                                    20467.00
## 3                                    21348.00
## 4                                    23675.33
#Exportar datos a excell 
#install.packages("writer")
library(writexl)
## Warning: package 'writexl' was built under R version 4.0.5
write_xlsx(df, "C:/Users/jose_/Desktop/2021-1/ELECTIVA_2/zipaquira/datos_zipaquira.xlsx")
#install.packages("leaflet")
#install.packages("dplyr")
library(leaflet)
## Warning: package 'leaflet' was built under R version 4.0.5
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.0.5
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:raster':
## 
##     intersect, select, union
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
r <- raster("C:/Users/jose_/Desktop/2021-1/ELECTIVA_2/zipaquira/zipaquira.bil")

mapa <- leaflet() %>%
  #Agg mosaicos
  addTiles() %>%  
  #Establecemos coordenadas y el zoom
  setView(lng = -73.980634, lat = 5.024563, zoom = 10) %>%
  #Agg el area de trabajo
  addRasterImage(r, colors = "navajowhite", opacity = 0.6) %>%
  #Agg un marcador 
  addMarkers(lng = -73.980634, lat = 5.024563, popup = "Referencia")
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137
## +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null
## +wktext +no_defs +type=crs
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum World Geodetic System 1984 in Proj4 definition
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137
## +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null
## +wktext +no_defs +type=crs
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
## prefer_proj): Discarded datum World Geodetic System 1984 in Proj4 definition
mapa

datos