Skip to content

Instantly share code, notes, and snippets.

@csaybar
Last active June 8, 2020 07:00
Show Gist options
  • Select an option

  • Save csaybar/9412f1fc5705833d948b40efb49d2cb7 to your computer and use it in GitHub Desktop.

Select an option

Save csaybar/9412f1fc5705833d948b40efb49d2cb7 to your computer and use it in GitHub Desktop.
rgee example #1
library(mapedit) # (OPTIONAL) Interactive editing of vector data
library(raster) # Manipulate raster data
library(scales) # Scale functions for visualization
library(cptcity) # cptcity color gradients!
library(tmap) # Thematic Map Visualization <3
library(rgee) # Bindings for Earth Engine
ee_Initialize()
## 1. Wrangling data
ee_search_dataset() %>%
ee_search_tags("mod11a1") %>%
ee_search_display()
dataset <- ee$ImageCollection("MODIS/006/MOD11A1")$
filter(ee$Filter$date('2020-01-01', '2020-01-31'))$
mean()
landSurfaceTemperature <- dataset$
select("LST_Day_1km")$
multiply(0.02)$
subtract(273.15)
# landSurfaceTemperature <- dataset %>%
# ee$Image$select("LST_Day_1km") %>%
# ee$Image$multiply(0.02) %>%
# ee$Image$subtract(273.15)
# ee_help(ee$ImageCollection$select)
## 2. Dynamic map
landSurfaceTemperatureVis <- list(
min = -50,
max = 50,
palette = cpt("grass_bcyr")
)
Map$addLayer(
eeObject = landSurfaceTemperature,
visParams = landSurfaceTemperatureVis,
name = 'Land Surface Temperature'
)
Map$addLayer(
eeObject = landSurfaceTemperature,
visParams = landSurfaceTemperatureVis,
name = 'Land Surface Temperature'
) %>% editMap() -> my_polygon
## 3. Static map
geometry <- ee$Geometry$Rectangle(
coords = c(-180,-90,180,90),
proj = "EPSG:4326",
geodesic = FALSE
)
world_temp <- ee_as_thumbnail(
image = landSurfaceTemperature, # Image to export
region = geometry, # Region of interest
dimensions = 1024, # output dimension
raster = TRUE, # If FALSE returns a stars object. FALSE by default
vizparams = landSurfaceTemperatureVis[-3] # Delete the palette element
)
min_lst <- landSurfaceTemperatureVis$min
max_lst <- landSurfaceTemperatureVis$max
world_temp[] <- scales::rescale(
getValues(world_temp), c(min_lst, max_lst)
)
data("World") # Load world data (available after load tmap)
world_temp_robin <- projectRaster(
from = world_temp,
crs = crs(World)
) %>% mask(World)
tm_shape(shp = world_temp_robin) +
tm_raster(
title = "LST (°C)",
palette = cpt("grass_bcyr", n = 100),
stretch.palette = FALSE,
style = "cont"
) +
tm_shape(shp = World) +
tm_borders(col = "black", lwd = 0.7) +
tm_graticules(alpha=0.8, lwd = 0.5, labels.size = 0.5) +
tm_layout() +
tmap_style(style = "natural") +
tm_layout(
scale = .8,
bg.color = "gray90",
frame = FALSE,
frame.lwd = NA,
panel.label.bg.color = NA,
attr.outside = TRUE,
main.title.size = 0.8,
main.title = "Global monthly mean LST from MODIS: January 2020",
main.title.fontface = 2,
main.title.position = 0.1,
legend.title.size = 1,
legend.title.fontface = 2,
legend.text.size = 0.7,
legend.frame = FALSE,
legend.outside = TRUE,
legend.position = c(0.10, 0.38),
legend.bg.color = "white",
legend.bg.alpha = 1
) +
tm_credits(
text = "Source: MOD11A1 - Terra Land Surface Temperature and Emissivity Daily Global 1km",
size = 0.8,
position = c(0.1,0)
) -> lst_tmap
tmap_save(lst_tmap, "lst_tmap.svg", width = 1865,height = 1165)