Skip to content

Instantly share code, notes, and snippets.

@csaybar
Created March 8, 2021 22:23
Show Gist options
  • Save csaybar/6f3c350b9a636c6f3507a5ae3b5b8210 to your computer and use it in GitHub Desktop.
Save csaybar/6f3c350b9a636c6f3507a5ae3b5b8210 to your computer and use it in GitHub Desktop.
rgee v.1.0.9
# 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"])
@csaybar
Copy link
Author

csaybar commented Mar 8, 2021

# 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