rgee学习笔记
01_rgee_hello world
library(rgee)
ee_Initialize()
print("Hello World")
# [1] "Hello World"
#python style
ee$String("Hello World from Earth Engine!")$getInfo()
# [1] "Hello World from Earth Engine!"
ee$Image("LANDSAT/LC08/C01/T1/LC08_044034_20140318")$getInfo()
#pipes style
ee$String("Hello World frmo Earth Engine!") %>%
ee$String$getInfo()
# [1] "Hello World frmo Earth Engine!"
ee$Image("LANDSAT/LC08/C01/T1/LC08_044034_20140318") %>%
ee$Image$getInfo()
02_rgee_simple mapdisplay
library(rgee)
library(sf)
ee_Initialize()
image <- ee$Image("LANDSAT/LC08/C02/T1_TOA/LC08_121038_20220115")
#Display the image
Map$centerObject(image)
Map$addLayer(image, name = "HF Landsat 8 original image")
#Define visualization parameters in an object literal
visParams <- list(
bands = c("B5", "B4", "B3"),
min = 0, max = 0.8
)
hf <- sf::st_read("D:\\01learn\\rgee\\shp\\hefei.shp") %>%
st_transform(4326) %>%
sf_as_ee()
Map$addLayer(image, visParams, "Landsat 8 False color") +
Map$addLayer(
eeObject = hf,
opacity = 0.8,
name = "hf"
)
03_rgee_finding images
library(rgee)
ee_Initialize()
collection <- ee$ImageCollection("LANDSAT/LC08/C01/T1")
point <- ee$Geometry$Point(117.25, 31.83)
filterCollection <- ee$ImageCollection("LANDSAT/LC09/C02/T1_L2")$
filterBounds(point)$
filterDate('2021-12-01', '2022-04-11')$
sort("CLOUD_COVER", TRUE)
ee_get_date_ic(filterCollection)
first <- filterCollection$first()
visParams <- list(
bands = c("SR_B5", "SR_B4", "SR_B3"),
min = 7272, max = 18182
)
Map$addLayer(first, visParams, "hf l9 image")
hf <- st_read("D:\\01learn\\rgee\\shp\\hefei.shp")%>%
st_transform(4326)%>%
sf_as_ee()
hf$getInfo()
hf_filter <- hf$filter(ee$Filter$eq("name","肥西县"))
Map$addLayer(first, visParams, "hf l9 image")+
Map$addLayer(
eeObject = hf_filter,
visParams = list(palette="yellow"),
name = "肥西县"
)
04_rgee_band math
library(rgee)
ee_Initialize()
#计算NDVI
getNDVI <- function(image){
return(image$normalizedDifference(c("B5","B4")))
}
image1 <- ee$Image("LANDSAT/LC08/C02/T1_TOA/LC08_121038_20220115")
image2 <- ee$Image("LANDSAT/LC08/C02/T1_TOA/LC08_121038_20210605")
NDVI1 <- getNDVI(image1)
NDVI2 <- getNDVI(image2)
#计算ndvi差异
ndviDifference <- NDVI2$subtract(NDVI1)
ndviParams <- list(palette=c(
"#d73027", "#f46d43", "#fdae61", "#fee08b",
"#d9ef8b", "#a6d96a", "#66bd63", "#1a9850"
))
ndwiParams <- list(min=-0.5, max = 0.5, palette = c("FF0000", "FFFFFF", "0000FF"))
Map$centerObject(NDVI1)
Map$addLayer(NDVI1, ndviParams, "NDVI1")+
Map$addLayer(NDVI2, ndviParams, "NDVI2")+
Map$addLayer(ndviDifference, ndviParams, "NDVI Difference")
05_rgee_map function
library(rgee)
ee_Initialize()
addNDVI <- function(image){
return(image$addBands(image$normalizedDifference(c('B5', 'B4'))$rename("NDVI")))
}
#根据位置和时间添加Lansat8数据
col <- ee$ImageCollection("LANDSAT/LC08/C02/T1")$
filterBounds(ee$Geometry$Point(117.38, 31.83))$
filterDate("2022-01-01","2022-04-14")
ee_get_date_ic(col)
# 1 LANDSAT/LC08/C02/T1/LC08_121038_20220115 2022-01-15 02:43:46
# 2 LANDSAT/LC08/C02/T1/LC08_121038_20220131 2022-01-31 02:43:41
# 3 LANDSAT/LC08/C02/T1/LC08_121038_20220304 2022-03-04 02:43:33
ndviCol <- col$map(addNDVI)
first <- ndviCol$first()
print(first$getInfo()$properties$CLOUD_COVER)
# 0.11
bandNames <- first$bandNames()
print(bandNames$getInfo())
# [1] "B1" "B2" "B3" "B4" "B5" "B6" "B7" "B8"
# [9] "B9" "B10" "B11" "QA_PIXEL" "QA_RADSAT" "SAA" "SZA" "VAA"
# [17] "VZA" "NDVI"
06_rgee_reducing
library(rgee)
ee_Initialize()
#引接导数据集
col = ee$ImageCollection("LANDSAT/LC08/C02/T1")$
filterBounds(ee$Geometry$Point(117.25,31.83))$
filterDate("2021-01-01","2022-04-15")$
sort("CLOUD_COVER")
#计算前5景云最少影像的像素中值
median <- col$limit(5)$reduce(ee$Reducer$median())
# median$bandNames()$getInfo()
# [1] "B1_median" "B2_median" "B3_median" "B4_median" "B5_median"
# [6] "B6_median" "B7_median" "B8_median" "B9_median" "B10_median"
# [11] "B11_median" "QA_PIXEL_median" "QA_RADSAT_median" "SAA_median" "SZA_median"
# [16] "VAA_median" "VZA_median"
#定义可视化参数
vizParams <- list(
bands = c("B5_median", "B4_median", "B3_median"),
min = 5000, max = 15000, gamma = 1.3
)
Map$centerObject(ee$Geometry$Point(117.25,31.83),8)
Map$addLayer(
eeObject = median,
visParams = vizParams,
name = "median image"
)
07_rgee_image statistics
library(rgee)
ee_Initialize()
#加载并显示Landsat TOA影像
image <- ee$Image("LANDSAT/LC08/C02/T1_TOA/LC08_121038_20220115")
image$bandNames()$getInfo()
Map$addLayer(
eeObject = image,
visParams = list(bands=c("B5","B4","B3"), gamma = 1.3),
name = "Landsat 8"
)
#创建一个矩形区域并展示
region <- ee$Geometry$Rectangle(117.2500,31.8300,117.3700,31.9500)
Map$addLayer(
eeObject = image,
visParams = list(bands=c("B5","B4","B3"), gamma = 1.3),
name = "Landsat 8"
)+
Map$addLayer(
eeObject = region,
name = "Region")
mean <- image$reduceRegion(
reducer = ee$Reducer$mean(),
geometry = region,
scale = 30
)
print(mean$getInfo())
# $B1
# [1] 0.1907487
#
# $B10
# [1] 280.9586
#
# $B11
# [1] 281.0627
#
# $B2
# [1] 0.168557
#
# $B3
# [1] 0.134187
#
# $B4
# [1] 0.1248478
#
# $B5
# [1] 0.1553403
#
# $B6
# [1] 0.1325599
#
# $B7
# [1] 0.1015001
#
# $B8
# [1] 0.1306403
#
# $B9
# [1] 0.001265682
#
# $QA_PIXEL
# [1] 22936.31
#
# $QA_RADSAT
# [1] 0
#
# $SAA
# [1] 15329.37
#
# $SZA
# [1] 5782.864
#
# $VAA
# [1] 1246.485
#
# $VZA
# [1] 53.5197
08_rgee_masking
因为需要借助谷歌云存储,所以只用了简单的clip()函数,没有用updatemask()
library(rgee)
ee_Initialize(gcs = TRUE)
#计算Landsat 5的NDVI
getNDVI <- function(image){
return(image$normalizedDifference(c("B4","B3")))
}
COL <- ee$ImageCollection("LANDSAT/LT05/C01/T1_TOA")$
filterBounds(ee$Geometry$Point(117.25,31.83))$
filterDate("1990-01-01","2010-12-31")$
sort("CLOUD_COVER",TRUE)
ee_get_date_ic(COL)
# 1 LANDSAT/LT05/C01/T1_TOA/LT05_121038_19901209 1990-12-09 02:02:50
# 2 LANDSAT/LT05/C01/T1_TOA/LT05_121038_19911025 1991-10-25 02:07:39
# 3 LANDSAT/LT05/C01/T1_TOA/LT05_121038_19911110 1991-11-10 02:07:44
# 4 LANDSAT/LT05/C01/T1_TOA/LT05_121038_19911228 1991-12-28 02:07:52
# 5 LANDSAT/LT05/C01/T1_TOA/LT05_121038_19920605 1992-06-05 02:07:02
# 6 LANDSAT/LT05/C01/T1_TOA/LT05_121038_19921128 1992-11-28 02:04:38
# 7 LANDSAT/LT05/C01/T1_TOA/LT05_121038_19930421 1993-04-21 02:05:56
# 8 LANDSAT/LT05/C01/T1_TOA/LT05_121038_19931115 1993-11-15 02:05:29
# 9 LANDSAT/LT05/C01/T1_TOA/LT05_121038_19931217 1993-12-17 02:05:19
# 10 LANDSAT/LT05/C01/T1_TOA/LT05_121038_19940323 1994-03-23 02:04:03
# 11 LANDSAT/LT05/C01/T1_TOA/LT05_121038_19941204 1994-12-04 01:57:32
# 12 LANDSAT/LT05/C01/T1_TOA/LT05_121038_19950326 1995-03-26 01:53:19
# 13 LANDSAT/LT05/C01/T1_TOA/LT05_121038_19950902 1995-09-02 01:46:08
# 14 LANDSAT/LT05/C01/T1_TOA/LT05_121038_19951207 1995-12-07 01:43:59
# 15 LANDSAT/LT05/C01/T1_TOA/LT05_121038_19970110 1997-01-10 02:06:59
# 16 LANDSAT/LT05/C01/T1_TOA/LT05_121038_19970211 1997-02-11 02:08:25
# 17 LANDSAT/LT05/C01/T1_TOA/LT05_121038_19970331 1997-03-31 02:10:19
# 18 LANDSAT/LT05/C01/T1_TOA/LT05_121038_20000915 2000-09-15 02:21:46
##……
#计算两景间隔20年的Landsat 5影像
hf <- st_read("D:\\01learn\\rgee\\shp\\hefei.shp")%>%
sf_as_ee()
image1 <- ee$Image("LANDSAT/LT05/C01/T1_TOA/LT05_121038_19901209")$
clip(hf)
image2 <- ee$Image("LANDSAT/LT05/C01/T1_TOA/LT05_121038_20100319")$
clip(hf)
image1$getInfo()$properties$CLOUD_COVER
# 0
image2$getInfo()$properties$CLOUD_COVER
# 0
ndvi1 <- getNDVI(image1)
ndvi2 <- getNDVI(image2)
#计算两景NDVI的差异
ndviDifference <- ndvi2$subtract(ndvi1)
# MaskedDifference <- ndviDifference$updateMask(hf)
#结果可视化
vizParams <- list(
min = -0.5,
max = 1,
palette = c('FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901',
'66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01',
'012E01', '011D01', '011301')
)
vidParams <- list(
min = -0.5,
max = 0.5,
palette = c("FF0000", "FFFFFF", "0000FF")
)
Map$centerObject(ndvi1)
Map$addLayer(
eeObject = ndvi1,
visParams = vizParams,
name = "NDVI1"
)+
Map$addLayer(
eeObject = ndvi2,
visParams = vizParams,
name = "NDVI2"
)+
Map$addLayer(
eeObject = ndviDifference,
visParams = vidParams,
name = "NDVI Difference"
)
1990年NDVI,从图中可以看出巢湖里满是绿色,而2010年便没有了。究其原因主要是因为当时蓝藻太多,水体富营养化严重。