r - 如何将 SF 对象转换为空间点数据框?

标签 r leaflet sf spatial-data-frame

我目前正在尝试将北卡罗来纳州的所有邮政编码映射到传单 map 上。最终,我想用来自另一个数据集的值对这些进行颜色编码。
虽然我已经设法用这些值将标记放在 map 上,但以带有邮政编码的彩色方式这样做确实会为我的项目增加值(value)。
我在 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 中做到这一点!任何提示将非常感谢!

最佳答案

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

关于r - 如何将 SF 对象转换为空间点数据框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57861088/

相关文章:

javascript - R 中带有 addCircleMarkers 的传单 map 图例中的圆圈 - 没有 Shiny

R Auto Zoom 在 Shiny 中更新了 leafletProxy Map 对象

javascript - LeafletJS : Load Tile Layer based on bounding box

r - 我实际上应该怎么做来回应这个警告? "Please note that rgdal will be retired by the end of 2023"

r - 在 R 中加入不同的 SpatialPolygonsDataFrame 对象时出现问题

r - ggplot2:强制为空的二级类别留出空间

r - 如何在最接近的日期合并 2 个 Zoo/data.frame 对象?

r - 如何在R中将表列表转换为数据框

r - 收集四列,其中两个键中包含值

r - 是否可以排除使用 sf 和 tmap 制作的 map 区域?