R osmdata: get other_tags as columns

284 Views Asked by At

I would like to use osmdata and get the other_tags as explicit columns.

Example code:

library(osmextract)
library(osmdata)

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

I am interested in extracting the other_tags col from itsleeds data.

Thank you.

1

There are 1 best solutions below

0
On BEST ANSWER

We can use tidyverse methods to split and reshape the 'other_tags' column into two columns - select the column of interest, with separate_rows expand the data by splitting at the ,, use separate to create two columns 'key', 'value' from the 'other_tags', by splitting at the => and finally remove the double quotes with str_remove_all in those columns

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, '"'))

-output

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