Created
March 8, 2021 22:23
-
-
Save csaybar/6f3c350b9a636c6f3507a5ae3b5b8210 to your computer and use it in GitHub Desktop.
rgee v.1.0.9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Spatial R packages ------------------------------------------------------ | |
library(cptcity) | |
library(mapview) | |
library(leaflet) | |
library(rgee) | |
library(sf) | |
# Initialize Google Earth Engine (Just One time) | |
ee_Initialize() | |
# 1. world map ------------------------------------------------------------ | |
image <- ee$Image('CGIAR/SRTM90_V4') | |
Map$centerObject(image) | |
Map$addLayer( | |
eeObject = image, | |
visParams = list(min = 0, max = 5000, palette= cpt(pal = "mpl_inferno")), | |
name = 'SRTM90_V4' | |
) | |
# 2. Load Geometry -------------------------------------------------------- | |
nc <- st_read(system.file("shape/nc.shp", package="sf")) | |
nc_elv <- ee_extract(image, nc, sf = TRUE) | |
plot(nc_elv["elevation"]) |
Author
csaybar
commented
Mar 8, 2021
# Spatial R packages ------------------------------------------------------
library(cptcity)
library(raster)
library(rgee)
# Initialize Google Earth Engine (Just One time)
ee_Initialize()
# 1. Landsat 08 ----------------------------------------------------------
image <- ee$Image("LANDSAT/LC08/C01/T1/LC08_044034_20140318")
ndvi <- (image[["B5"]] - image[["B4"]])/(image[["B5"]] + image[["B4"]])
names(ndvi) <- "ndvi"
Map$centerObject(image)
Map$addLayer(
eeObject = ndvi,
visParams = list(min = 0, max = 0.7, palette = cpt("grass_ndvi")),
name = 'SRTM90_V4'
)
sf <- ee_as_sf(image$geometry())
sf_split <- sf::st_make_grid(sf, n = c(2,1))
# plot(sf)
# plot(sf_split, add = TRUE)
# 2. Download Image (on a batch way) ------------------------------------
ndvi_raster <- list()
ee_users <- c("aybar1994", "csaybar")
for (index in seq_along(sf_split)) {
ee_Initialize(ee_users[index])
ndvi_raster[[index]] <- ee_as_raster(
image = ndvi,
dsn = tempfile(),
region = sf_as_ee(sf_split[index]),
scale = 1000,
lazy = TRUE
)
}
ndvi_raster_split <- lapply(ndvi_raster, ee_utils_future_value)
# 3. Merge Split raster
ndvi_raster_f <- Reduce(function(...) mosaic(..., fun=mean), ndvi_raster_split)
plot(ndvi_raster_f)
dev.off()
# Spatial R packages ------------------------------------------------------
library(raster)
library(rgee)
# 1. Adds a band containing image date as years since 1991.
createTimeBand <-function(img) {
year <- ee$Date(img$get('system:time_start'))$get('year')$subtract(1991L)
ee$Image(year)$byte()$addBands(img)
}
# 2. Map the time band creation helper over the night-time lights collection.
collection <- ee$ImageCollection('NOAA/DMSP-OLS/NIGHTTIME_LIGHTS') %>%
ee$ImageCollection$select("stable_lights") %>%
ee$ImageCollection$map(createTimeBand)
# 3. Compute a linear fit over the series of values at each pixel
col_reduce <- collection$reduce(ee$Reducer$linearFit())
col_reduce <- col_reduce$addBands(col_reduce$select('scale'))
ee_print(col_reduce)
# 4. Create a interactive visualization!
Map$setCenter(9.08203, 47.39835, 3)
Map$addLayer(
eeObject = col_reduce,
visParams = list(
bands = c("scale", "offset", "scale"),
min = 0,
max = c(0.18, 20, -0.18)
),
name = "stable lights trend"
)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment