Skip to content

Instantly share code, notes, and snippets.

@csaybar
Last active November 30, 2020 16:22
Show Gist options
  • Select an option

  • Save csaybar/3f2f95790bf645a2da3ad82879bf8c39 to your computer and use it in GitHub Desktop.

Select an option

Save csaybar/3f2f95790bf645a2da3ad82879bf8c39 to your computer and use it in GitHub Desktop.
rgee example #2.1
library(cptcity)
library(raster)
library(stars)
library(rgee)
library(sf)
ee_Initialize(drive = TRUE)
roi <- st_read(system.file("shape/nc.shp", package="sf")) %>%
st_bbox() %>%
st_as_sfc() %>%
sf_as_ee()
ee_search_dataset() %>%
ee_search_title("mod13") %>%
ee_search_title("1km") %>%
ee_search_display()
modis_ndvi <- ee$ImageCollection("MODIS/006/MOD13A2")
getQABits <- function(image, qa) {
# Convert decimal (character) to decimal (little endian)
qa <- sum(2^(which(rev(unlist(strsplit(as.character(qa), "")) == 1))-1))
# Return a single band image of the extracted QA bits, giving the qa value.
image$bitwiseAnd(qa)$lt(1)
}
mod13A2_clean <- function(img) {
# Extract the NDVI band
ndvi_values <- img$select("NDVI")
# Extract the quality band
ndvi_qa <- img$select("SummaryQA")
# Select only pixels with high confidence
quality_mask <- getQABits(ndvi_qa, "11")
# Mask pixels with value zero.
ndvi_values$updateMask(quality_mask)
}
ndvi_composite <- modis_ndvi$
filter(ee$Filter$date('2001-01-01', '2019-12-31'))$
filter(ee$Filter$calendarRange(1, field = "month"))$
map(mod13A2_clean)$
median()
scale <- 0.0001
Map$setCenter(lon = -79,lat = 35,zoom = 5)
Map$addLayer(
eeObject = ndvi_composite,
visParams = list(
min = 0.2 / scale,
max = 0.7 / scale,
palette = cpt("grass_ndvi", 10)
)
) + Map$addLayer(roi)
mod_ndvi <- ee_as_raster(
image = ndvi_composite,
region = roi,
dsn = "ndvi_data.tif",
scale = 1000,
via = 'drive'
)
@Joy-Giovanni
Copy link

HI Cesar,
I am running the last part of the script and getting this error:
Error in py_get_attr_impl(x, name, silent) :
AttributeError: 'Geometry' object has no attribute 'geometry'

any idea on how it can be solved?

@csaybar
Copy link
Author

csaybar commented Nov 30, 2020

Hi! @Joy-Giovanni,

Thanks for taking the time to point out this error. I made some small changes ... let me know if it still doesn't work!

@Joy-Giovanni
Copy link

Thank you so much it is working perfectly!
Giovanni.

@Joy-Giovanni
Copy link

Cesar, I would like to make project based on rgee;
I would appreciate If I can get your email so I can tell you more about it and see if you can share some guidance on how I can proceed.
Best,
Giovanni

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment