首页 > 解决方案 > R osmdata:获取 other_tags 作为列

问题描述

我想使用 osmdata 并将 other_tags 作为显式列。

示例代码:

library(osmextract)
library(osmdata)

itsleeds = oe_get("ITS Leeds")
oe_get_keys(itsleeds)

我有兴趣从 itsleeds 数据中提取 other_tags col。

谢谢你。

标签: ropenstreetmap

解决方案


我们可以使用 tidyverse 方法将“other_tags”列拆分并重塑为两列 -select感兴趣的列,separate_rows通过在 处拆分来扩展数据,,用于separate从“other_tags”创建两列“key”、“value”,通过在这些列中拆分=>并最终删除双引号str_remove_all

library(osmextract)
library(osmdata)
library(dplyr)
library(tidyr)
library(stringr)
itsleeds %>% 
  select(other_tags) %>%
  separate_rows(other_tags, sep=",") %>%
  separate(other_tags, into = c("key", "value"), sep="=>") %>% 
  mutate(across(c(key, value), str_remove_all, '"'))

-输出

Simple feature collection with 297 features and 2 fields
Geometry type: LINESTRING
Dimension:     XY
Bounding box:  xmin: -1.562458 ymin: 53.80471 xmax: -1.548076 ymax: 53.81105
Geodetic CRS:  WGS 84
# A tibble: 297 x 3
   key     value                                                                                                                                 geometry
 * <chr>   <chr>                                                                                                                         <LINESTRING [°]>
 1 <NA>    <NA>                                                 (-1.560083 53.80855, -1.560152 53.80865, -1.560228 53.80878, -1.560672 53.80962, -1.56...
 2 bicycle designated                                           (-1.559709 53.80815, -1.559756 53.80813, -1.559842 53.80812, -1.559919 53.80814, -1.55...
 3 foot    designated                                           (-1.559709 53.80815, -1.559756 53.80813, -1.559842 53.80812, -1.559919 53.80814, -1.55...
 4 website http://woodhousemooronline.com/the-cannon-destroyer/ (-1.5609 53.80851, -1.560663 53.80859, -1.560228 53.80878, -1.560025 53.80903, -1.5599...
 5 access  permissive                                           (-1.552587 53.80712, -1.552541 53.8072, -1.55251 53.80725, -1.552479 53.8073, -1.55241...
 6 lanes   1                                                    (-1.552587 53.80712, -1.552541 53.8072, -1.55251 53.80725, -1.552479 53.8073, -1.55241...
 7 oneway  yes                                                  (-1.552587 53.80712, -1.552541 53.8072, -1.55251 53.80725, -1.552479 53.8073, -1.55241...
 8 lanes   1                                                    (-1.551771 53.8073, -1.551929 53.80727, -1.552007 53.80727, -1.552181 53.80728, -1.552...
 9 oneway  yes                                                  (-1.551771 53.8073, -1.551929 53.80727, -1.552007 53.80727, -1.552181 53.80728, -1.552...
10 lanes   2                                                                  (-1.552894 53.809, -1.552663 53.8089, -1.55246 53.80881, -1.552261 53.8087)
# … with 287 more rows

推荐阅读