0

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

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

在此处下载数据

#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?

4

1 回答 1

0
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)
于 2020-12-14T17:43:18.383 回答