Search code examples
rdata.tableopenstreetmaptidyrosmar

Divide efficiently an OSMAR Object into chunks in R


How could i divide efficiently an osmar object into several parts and combine them back again?

library(osmar)
src <- osmsource_api(url = "https://api.openstreetmap.org/api/0.6/")
bbox <- center_bbox(11.575278, 48.137222, 1000, 1000)
object <- get_osm(bbox, src)

accourding to class it is an "osmar" "list".

class(object)
[1] "osmar" "list" 

So I tried to divide it with the folowing approach:

 divide_a_vector_into_chunks <- function(x,n) split(x, cut(seq_along(x), n, labels = FALSE))
 div_osmar<-divide_a_vector_into_chunks(object,2)

It creates a list:

$`1`
$`1`$nodes
osmar$nodes object
1690 nodes, 1210 tags 

         lat     lon
min 48.13385 11.5675
max 48.14264 11.5800

$`1`$ways
osmar$ways object
239 ways, 1306 tags, 2144 refs 


$`2`
$`2`$relations
osmar$relations object
136 relations, 1327 tags, 958 node_refs, 8034 way_refs 

But i can not work with this list as i would with an osmar object. Furthermore osmar package supports combining osmar object with

o1 <- subset(object, node_ids = find(object, node(tags(v == "Marienplatz"))))
o2 <- subset(object, ids = find_down(object, way(c(96619179, 105071000))))
o1
o2
c(o1, o2)

so it should be possible to divide an osmar object and combine it again.


Solution

  • I found a solution but i dont know how efficient this solution is:

    node_ids_vector<-object$nodes$attrs$id
    
    node_ids_vector_devided<-divide_a_vector_into_chunks(node_ids_vector,2)
    o1<-subset(object,ids=find_up(object,node(node_ids_vector_devided$`1`)))
    o2<-subset(object,ids=find_up(object,node(node_ids_vector_devided$`2`)))
    
    c(o1, o2)