r-在rgee中为feature Collection的每个功能添加属性



我想使用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?

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)

最新更新