r-如何在shapefile的Simple feature集合中添加字段



如果我有这个形状文件:

myurl <- "http://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_050_00_500k.json"
geo <- readLines(myurl)
geo <- paste0(geo, collapse = "")
library(geojsonsf)
system.time({ sf <- geojson_sf(geo)})
library(sf) 
sf

我想在sf中添加一个字段"ID",为sf 中列名中的每个名称提供一个唯一的数字(从一开始(

请在下面找到一个可能的解决方案

Reprex

  • 代码
library(geojsonsf)
library(sf)
sf$NAME <- paste0(sf$NAME,"_", 1:nrow(sf))
  • 输出
sf
#> Simple feature collection with 3221 features and 6 fields
#> Geometry type: GEOMETRY
#> Dimension:     XY
#> Bounding box:  xmin: -179.1473 ymin: 17.88481 xmax: 179.7785 ymax: 71.35256
#> Geodetic CRS:  WGS 84
#> First 10 features:
#>    CENSUSAREA   LSAD         GEO_ID        NAME COUNTY STATE
#> 1     560.100 County 0500000US01029  Cleburne_1    029    01
#> 2     678.972 County 0500000US01031    Coffee_2    031    01
#> 3     650.926 County 0500000US01037     Coosa_3    037    01
#> 4    1030.456 County 0500000US01039 Covington_4    039    01
#> 5     608.840 County 0500000US01041  Crenshaw_5    041    01
#> 6     561.150 County 0500000US01045      Dale_6    045    01
#> 7     777.093 County 0500000US01049    DeKalb_7    049    01
#> 8     945.080 County 0500000US01053  Escambia_8    053    01
#> 9     627.660 County 0500000US01057   Fayette_9    057    01
#> 10    574.408 County 0500000US01061   Geneva_10    061    01
#>                          geometry
#> 1  POLYGON ((-85.38872 33.9130...
#> 2  POLYGON ((-86.03044 31.6189...
#> 3  POLYGON ((-86.00928 33.1016...
#> 4  POLYGON ((-86.34851 30.9943...
#> 5  POLYGON ((-86.14699 31.6804...
#> 6  POLYGON ((-85.79043 31.3202...
#> 7  POLYGON ((-85.57593 34.8237...
#> 8  POLYGON ((-87.16308 30.9990...
#> 9  POLYGON ((-87.63593 33.8787...
#> 10 POLYGON ((-85.77267 30.9946...

创建于2022-02-28由reprex包(v2.0.1(

最新更新