Skip to content

Commit

Permalink
fix scale for buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
kongdd committed May 25, 2024
1 parent f153a12 commit 30f5cbc
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: rgee2
Title: Extra functions for R Google Earth Engine
Version: 0.1.1
Version: 0.1.2
Authors@R:
person(given = "Dongdondg",
family = "Kong",
Expand Down
14 changes: 8 additions & 6 deletions R/ee_buffer.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@
#' @param scale in the unit of `m`
#' @param half_win half window of the buffer (in grids). window size equals to
#' `2*halfwin + 1`.
#'
#'
#' @examples
#' \dontrun{
#' st <- st_212[, .(site, lon, lat, IGBP)]
#' st_point_buffer_3by3(st, 500)
#' }
#' @export
st_point_buffer <- function(st, scale = 500, half_win = 1, cellsize = NULL) {
st_point_buffer <- function(
st, scale = 463.3127,
half_win = 1, cellsize = NULL) {
if (is.null(cellsize)) {
cellsize <- scale / 500 * 1 / 240
cellsize <- scale / 463.3127 * 1 / 240
}

win <- half_win * 2 + 1
lon <- seq(-half_win:half_win) * cellsize
lat <- seq(-half_win:half_win) * cellsize
lon <- c(-half_win:half_win) * cellsize
lat <- c(-half_win:half_win) * cellsize
adj_mat <- expand.grid(lon = lon, lat = lat)

grps <- 1:nrow(adj_mat) %>% set_names(., .)
Expand All @@ -37,7 +39,7 @@ st_point_buffer <- function(st, scale = 500, half_win = 1, cellsize = NULL) {
#' df2sf
#' @example R/examples/ex-df2sf.R
#' @keywords internal
#' @export
#' @export
df2sf <- function(d, coords = c("lon", "lat"), crs = 4326) {
sf::st_as_sf(d, coords = coords, crs = crs)
}
2 changes: 1 addition & 1 deletion man/st_point_buffer.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ library(rfluxnet)
library(sf)
library(sf2)
library(Ipaper)
ee_Initialize(drive = TRUE)
# ee_Initialize(drive = TRUE)
ee_init()
```

```{r}
Expand All @@ -21,10 +22,18 @@ proj = ee_get_proj(imgcol)
scale = proj$scale # scale should lte prj.scale
# scale = 463.3127
col = ee$ImageCollection("MODIS/061/MOD15A2H")$select(c("Lai_500m"))
# sp_2 = st_point_buffer(st, scale = scale, half_win = 1) # -half_win: half_win
# sp_5 = st_point_buffer(st, scale = scale, half_win = 2)
```

```{r}
# col <- ee$ImageCollection("MODIS/061/MCD12Q1")$select(0)
system.time({
df = ee_extract(col, sp, via = "getInfo", lazy = FALSE, scale = 450)
})
```

```{r}
# col <- ee$ImageCollection("MODIS/061/MCD12Q1")$select(0)
# df = ee_extract(col, sp, via = "getInfo", lazy = FALSE, scale = 500)
Expand Down
13 changes: 7 additions & 6 deletions scripts/Extractor/ee_extract_04_PMLV2_China_forcing.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
library(rgee)
library(sf)
library(dplyr)


library(tidymet)
library(sf2)

devtools::load_all()
# ee_Initialize(user = "kongdd.sysu", drive = TRUE)
ee_Initialize(user = "cuijian426", drive = TRUE)
# ee_Initialize(user = "cuijian426", drive = TRUE)
# ee_Initialize(user = "kjding93", drive = TRUE)
# ee_Initialize(drive = TRUE)
ee_Initialize(drive = TRUE)

## 1. read tested points
sp <- read_sf(path.mnt("C:/Users/kongdd/Desktop/学生研究/谢宇轩研究-planB/st_met2481.shp")) %>%
dplyr::select(site)
sp = st_met2481 |> df2sp() |> select(site)
# sp <- read_sf(path.mnt("C:/Users/kongdd/Desktop/学生研究/谢宇轩研究-planB/st_met2481.shp")) %>%
# dplyr::select(site)
# sp %<>% mutate(ID = 1:nrow(.)) %>% select(ID, IGBPcode)

## 2. clip ERA5L data by `rgee`
Expand Down

0 comments on commit 30f5cbc

Please sign in to comment.