首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向rgee中要素集合的每个要素添加属性

向rgee中要素集合的每个要素添加属性
EN

Stack Overflow用户
提问于 2020-12-14 02:44:32
回答 1查看 107关注 0票数 0

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

代码语言:javascript
复制
library(rgee)
library(dplyr)
library(readr)

download data here

代码语言:javascript
复制
#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)的属性

代码语言:javascript
复制
site_no <- collection$site_no

如果我这样做了:

代码语言:javascript
复制
withMoreProperties = featcol$map(function(f) {
  # Set a property.
  f$set("site_no", site_no)
})

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

对如何解决这个问题有什么建议吗?也许使用循环?或者ee$列表?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-15 01:43:18

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65279318

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档