为 rgee 中的 Feature Collection 的每个特征添加属性

adding properties to each feature of Feature Collection in rgee

我想使用 rgee 添加 属性 到功能集合的每个元素。我拥有的这个要素集合只是一个多边形列表,我想为每个几何图形添加一个 ID(不同的)。到目前为止,我有:

library(rgee)
library(dplyr)
library(readr)

download data here

#Read polygons 
collection <- read_rds("polygons.rds")
    # convert collection to feature collection using sf_as_ee 
featcol <- sf_as_ee(collection$geometry)

所以对于这个集合的每个元素,我想添加一个名为 site_no(站点编号)

的 属性
site_no <- collection$site_no

如果我这样做:

withMoreProperties = featcol$map(function(f) {
  # Set a property.
  f$set("site_no", site_no)
})

它不起作用,它不是向每个元素添加一个站点编号,而是将所有站点编号添加到所有站点。

关于如何解决这个问题有什么建议吗?也许使用循环?或 ee$List?

library(rgee)
library(dplyr)
library(readr)

collection <- read_rds("polygons.rds")
# convert collection to feature collection using sf_as_ee

# Simple solution
collection_with_prop <- collection[c("site_no", "geometry")] %>%
  st_as_sf() %>% 
  sf_as_ee()
ee_as_sf(collection_with_prop)

# Add properties in the server-side (using ee$List$zip)
geom_with_prop <- sf_as_ee(collection$geometry)
prop_to_add <- collection$site_no %>% ee$List()
collection_with_prop <- geom_with_prop %>% 
  ee$FeatureCollection$toList(nrow(collection)) %>% 
  ee$List$zip(prop_to_add) %>% # Pairs the elements of two lists to create a list of two-element lists
  ee$List$map(
    ee_utils_pyfunc(function(l){
      lpair <- ee$List(l)
      ee$Feature(lpair$get(0))$set('site_no', lpair$get(1))    
    })    
  ) %>% 
  ee$FeatureCollection()
ee_as_sf(collection_with_prop)