我想使用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 number)的属性
site_no <- collection$site_no如果我这样做了:
withMoreProperties = featcol$map(function(f) {
# Set a property.
f$set("site_no", site_no)
})它不起作用,它不是为每个元素添加一个站点编号,而是将所有站点编号添加到所有站点。
对如何解决这个问题有什么建议吗?也许使用循环?或者ee$列表?
发布于 2020-12-15 01:43:18
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)https://stackoverflow.com/questions/65279318
复制相似问题