【问题标题】:How can I transform a SF object into a Spatial Points Data Frame?如何将 SF 对象转换为空间点数据框?
【发布时间】:2020-01-11 15:19:17
【问题描述】:

我目前正在尝试将北卡罗来纳州的所有邮政编码映射到传单地图上。最终,我想用另一个数据集的值对它们进行颜色编码。

虽然我已经设法使用这些值在地图上放置标记,但以彩色方式使用邮政编码这样做确实会为我的项目增加价值。

我在 R 中下载了 USAboundaries 库,因此我认为我有足够的数据在传单中创建多边形,但数据不是 SpatialPointsDataFrame 格式。

USAboundaries 数据集的示例输出如下所示:

USAboundaries::us_zipcodes()

Simple feature collection with 33144 features and 6 fields
geometry type:  POINT
dimension: XY
bbox:  
 xmin: -176.6316 
 ymin: -14.22075 
 xmax: 145.7536 
 ymax: 71.2738

epsg (SRID):    4326

proj4string:    +proj=longlat +datum=WGS84 +no_defs

First 10 features:
  zipcode zcta5ce10     affgeoid10 geoid10   aland10 awater10                   geometry
    21914     21914 8600000US21914   21914   1924479   477175 POINT (-75.98187 39.57303)
    01001     01001 8600000US01001   01001  29731610  2118827 POINT (-72.62585 42.06258)
    34736     34736 8600000US34736   34736 322808220 78588518 POINT (-81.89624 28.55458)
    46151     46151 8600000US46151   46151 530632048  9804480  POINT (-86.4305 39.44806)
    48039     48039 8600000US48039   48039  59592687  4845242 POINT (-82.55054 42.69666)

在我看来,这应该足以转换为 SpatialPointsDataFrame,但我不知道如何在 R 中做到这一点!任何提示将不胜感激!

【问题讨论】:

    标签: r leaflet sf spatial-data-frame


    【解决方案1】:

    Leaflet 支持 sf 对象,因此您不应该尝试任何问题。 不过,您可以使用as(YourObject, "Spatial")sf 对象转换为sp(例如空间点数据框)。要从 sp 更改为 sf,请使用 as_as_sf

    library(sf)
    library(sp)
    data(meuse)
    coordinates(meuse) = ~x+y
    m.sf = st_as_sf(meuse) 
    x = st_sfc(st_point(c(5,5)), st_point(c(6,9)), crs = 4326)
    as(x, "Spatial")
    

    this

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-15
      • 2018-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-26
      • 2020-08-17
      • 2020-08-03
      相关资源
      最近更新 更多