UNIVERSIDAD COOPEARTIVA DE COLOMBIA

Facultad de Ingenieria

Continuacion exploracion en sentinel

practica3 sentinel

Integrantes:

Andres Felipe Rivas Morales

Clase Electiva II

#instalamos paquetes nuevos para excel
#install.packages("sf")
#install.packages("readxl")
#install.packages("writexl")


#contunuacion de la practica2 con landsat2
library(raster)
## Loading required package: sp
library(rgdal)
## Please note that rgdal will be retired by the end of 2023,
## plan transition to sf/stars/terra functions using GDAL and PROJ
## at your earliest convenience.
## 
## rgdal: version: 1.5-25, (SVN revision 1143)
## 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/felip/Documents/R/win-library/4.1/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/felip/Documents/R/win-library/4.1/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 sp or rgdal.
## Overwritten PROJ_LIB was C:/Users/felip/Documents/R/win-library/4.1/rgdal/proj
library(sf)
## Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1
library(readxl)
library(writexl)


#Nuevas bandas:
    
band2<-raster("C:/Users/felip/Desktop/electiva2/practica2/T18NVH_20170921T152631_B02.jp2")
band3<-raster("C:/Users/felip/Desktop/electiva2/practica2/T18NVH_20170921T152631_B02.jp2")
band4<-raster("C:/Users/felip/Desktop/electiva2/practica2/T18NVH_20170921T152631_B04.jp2")
band8<-raster("C:/Users/felip/Desktop/electiva2/practica2/T18NVH_20170921T152631_B08.jp2")


#creacion del stack con las banas anteriores:
s <- stack(band8, band4, band3, band2)

#creamos un landsatRGB2 para el stack anterior:
sentinelrgb <- stack(band8, band4, band3, band2)

#plots
par(mfrow= c(2,3))
plot(band2, main="blue",col =gray (0:100/100))
plot(band3, main="Green",col =gray (0:100/100))
plot(band4, main="red",col =gray (0:100/100))
plot(band8, main="NIR",col =gray (0:100/100))



#extendemos para ver sus medidas:

extent(sentinelrgb)
## class      : Extent 
## xmin       : 399960 
## xmax       : 509760 
## ymin       : 190200 
## ymax       : 3e+05
#usamos comandos para los recortes y sus variables:
area_recorte <- extent (400000, 469000, 23800, 300000)

recorte <- crop(sentinelrgb, area_recorte)

sentinel_subset <- crop(sentinelrgb, area_recorte)

sentinel_subset
## class      : RasterBrick 
## dimensions : 10980, 6900, 75762000, 4  (nrow, ncol, ncell, nlayers)
## resolution : 10, 10  (x, y)
## extent     : 4e+05, 469000, 190200, 3e+05  (xmin, xmax, ymin, ymax)
## crs        : +proj=utm +zone=18 +datum=WGS84 +units=m +no_defs 
## source     : r_tmp_2021-09-24_145819_13324_18111.grd 
## names      : T18NVH_20170921T152631_B08, T18NVH_20170921T152631_B04, T18NVH_20170921T152631_B02.1, T18NVH_20170921T152631_B02.2 
## min values :                          1,                          1,                          154,                          154 
## max values :                      18743,                      18724,                        15367,                        15367
plot(recorte)

#vemos la banda8y la band2

compareRaster(band8, band2)
## [1] TRUE
#probamos el color verdadero

plotRGB(sentinelrgb, r=4, g=3, b=2, axes = TRUE, stretch = "hist", main = "color verdadero de sentinel")


#llamamos excel
names(sentinel_subset)<- c("blue", "green", "red", "NIR")

entrenamiento <- read_xlsx("C:/Users/felip/Desktop/electiva2/practica2/entrenamiento.xlsx")

entrenamiento <- entrenamiento %>%
st_as_sf(coords = c("x", "y"))


plot (entrenamiento$geometry)


df <- extract (sentinelrgb, entrenamiento); df
##       T18NVH_20170921T152631_B08 T18NVH_20170921T152631_B04
##  [1,]                        815                        711
##  [2,]                        570                        655
##  [3,]                       1886                       1559
##  [4,]                       2631                       1722
##  [5,]                        877                        701
##  [6,]                        819                        669
##  [7,]                       1814                        959
##  [8,]                       2940                        830
##  [9,]                        774                        896
## [10,]                        506                        698
## [11,]                        971                        776
## [12,]                       1268                        898
## [13,]                       2819                       1122
## [14,]                        852                        650
## [15,]                       1208                        879
## [16,]                       2414                       1573
## [17,]                       2352                       1463
## [18,]                       2265                       1104
## [19,]                       1817                        941
## [20,]                       2019                       1140
## [21,]                       1760                       1142
## [22,]                       3135                        915
## [23,]                       2545                        999
## [24,]                       2521                       1226
## [25,]                       3480                       2581
## [26,]                       3038                       2047
## [27,]                       1771                       1191
## [28,]                       2298                       1570
## [29,]                       2059                       1010
## [30,]                        686                        776
## [31,]                       3311                        641
## [32,]                       2587                        724
## [33,]                       2906                        898
## [34,]                       2489                        545
## [35,]                       2914                        518
## [36,]                       2790                        616
## [37,]                       2370                       1089
## [38,]                       3301                        687
## [39,]                       2285                        575
## [40,]                       2828                        619
## [41,]                       2072                        458
## [42,]                       2504                        474
## [43,]                       3620                        913
## [44,]                       2974                        648
## [45,]                       3229                        535
## [46,]                       2269                       1250
## [47,]                       1995                       1821
## [48,]                       2579                       1076
## [49,]                       2059                       1387
## [50,]                       2445                       1529
## [51,]                       2357                       1215
## [52,]                       2142                       1371
## [53,]                       2256                       1624
## [54,]                       2064                       1450
## [55,]                       1869                       1594
## [56,]                       2353                       1704
## [57,]                       2320                       1532
## [58,]                       2820                        967
## [59,]                       3361                        925
## [60,]                       2277                       1649
##       T18NVH_20170921T152631_B02.1 T18NVH_20170921T152631_B02.2
##  [1,]                         1125                         1125
##  [2,]                         1119                         1119
##  [3,]                         1530                         1530
##  [4,]                         1515                         1515
##  [5,]                         1123                         1123
##  [6,]                         1104                         1104
##  [7,]                         1206                         1206
##  [8,]                         1160                         1160
##  [9,]                         1182                         1182
## [10,]                         1168                         1168
## [11,]                         1169                         1169
## [12,]                         1193                         1193
## [13,]                         1225                         1225
## [14,]                         1034                         1034
## [15,]                         1219                         1219
## [16,]                         1426                         1426
## [17,]                         1409                         1409
## [18,]                         1208                         1208
## [19,]                         1210                         1210
## [20,]                         1238                         1238
## [21,]                         1243                         1243
## [22,]                         1140                         1140
## [23,]                         1171                         1171
## [24,]                         1198                         1198
## [25,]                         1828                         1828
## [26,]                         1554                         1554
## [27,]                         1267                         1267
## [28,]                         1403                         1403
## [29,]                         1148                         1148
## [30,]                         1191                         1191
## [31,]                         1049                         1049
## [32,]                         1089                         1089
## [33,]                         1111                         1111
## [34,]                          902                          902
## [35,]                          883                          883
## [36,]                          944                          944
## [37,]                         1196                         1196
## [38,]                         1091                         1091
## [39,]                          875                          875
## [40,]                          951                          951
## [41,]                          823                          823
## [42,]                          844                          844
## [43,]                         1287                         1287
## [44,]                         1072                         1072
## [45,]                          840                          840
## [46,]                         1279                         1279
## [47,]                         1634                         1634
## [48,]                         1218                         1218
## [49,]                         1333                         1333
## [50,]                         1527                         1527
## [51,]                         1389                         1389
## [52,]                         1449                         1449
## [53,]                         1618                         1618
## [54,]                         1425                         1425
## [55,]                         1733                         1733
## [56,]                         1653                         1653
## [57,]                         1533                         1533
## [58,]                         1323                         1323
## [59,]                         1194                         1194
## [60,]                         1530                         1530
ms <- aggregate(df, list(entrenamiento$clase), mean)

#write_xlsx(ms,"C:/Users/felip/Desktop/electiva2/practica2/file_name.xlsx")