rlistopenstreetmaposmar

Remove empty osmar objects from the list of osmar objects


i have the folowing list nodes_found with 2 elements in this example but the number of elemets will vary in the future:

library(osmar)

>nodes_found

$`1`
osmar object
0 nodes, 0 ways, 0 relations 

$`2`
osmar object
1 nodes, 0 ways, 0 relations 

> class(nodes_found)
[1] "list"

i would like to remove empty osmar objects (objects with 0 nodes in this case the object at [1]) from the list. Maybe filter based on the atributes of a node? if i basicaly request the atributes of an osmar object that contains 0 nodes it will produce a data structure with 0 rows. So maybe i could use that. But i dont think its efficient.

here is the nodes_found list:

nodes_found<-list(`1` = structure(list(nodes = structure(list(attrs = structure(list(
    id = numeric(0), visible = character(0), timestamp = structure(list(
        sec = numeric(0), min = integer(0), hour = integer(0), 
        mday = integer(0), mon = integer(0), year = integer(0), 
        wday = integer(0), yday = integer(0), isdst = integer(0), 
        zone = character(0), gmtoff = integer(0)), class = c("POSIXlt", 
    "POSIXt")), version = numeric(0), changeset = numeric(0), 
    user = structure(integer(0), .Label = character(0), class = "factor"), 
    uid = structure(integer(0), .Label = character(0), class = "factor"), 
    lat = numeric(0), lon = numeric(0)), row.names = integer(0), class = "data.frame"), 
    tags = structure(list(id = numeric(0), k = structure(integer(0), .Label = character(0), class = "factor"), 
        v = structure(integer(0), .Label = character(0), class = "factor")), row.names = integer(0), class = "data.frame")), class = c("nodes", 
"osmar_element", "list")), ways = structure(list(attrs = structure(list(
    id = numeric(0), visible = character(0), timestamp = structure(list(
        sec = numeric(0), min = integer(0), hour = integer(0), 
        mday = integer(0), mon = integer(0), year = integer(0), 
        wday = integer(0), yday = integer(0), isdst = integer(0), 
        zone = character(0), gmtoff = integer(0)), class = c("POSIXlt", 
    "POSIXt")), version = numeric(0), changeset = numeric(0), 
    user = structure(integer(0), .Label = character(0), class = "factor"), 
    uid = structure(integer(0), .Label = character(0), class = "factor")), row.names = integer(0), class = "data.frame"), 
    tags = structure(list(id = numeric(0), k = structure(integer(0), .Label = character(0), class = "factor"), 
        v = structure(integer(0), .Label = character(0), class = "factor")), row.names = integer(0), class = "data.frame"), 
    refs = structure(list(id = numeric(0), ref = numeric(0)), row.names = integer(0), class = "data.frame")), class = c("ways", 
"osmar_element", "list")), relations = structure(list(attrs = structure(list(
    id = numeric(0), visible = character(0), timestamp = structure(list(
        sec = numeric(0), min = integer(0), hour = integer(0), 
        mday = integer(0), mon = integer(0), year = integer(0), 
        wday = integer(0), yday = integer(0), isdst = integer(0), 
        zone = character(0), gmtoff = integer(0)), class = c("POSIXlt", 
    "POSIXt")), version = numeric(0), changeset = numeric(0), 
    user = structure(integer(0), .Label = character(0), class = "factor"), 
    uid = structure(integer(0), .Label = character(0), class = "factor")), row.names = integer(0), class = "data.frame"), 
    tags = structure(list(id = numeric(0), k = structure(integer(0), .Label = character(0), class = "factor"), 
        v = structure(integer(0), .Label = character(0), class = "factor")), row.names = integer(0), class = "data.frame"), 
    refs = structure(list(id = numeric(0), type = structure(integer(0), .Label = character(0), class = "factor"), 
        ref = numeric(0), role = structure(integer(0), .Label = character(0), class = "factor")), row.names = integer(0), class = "data.frame")), class = c("relations", 
"osmar_element", "list"))), class = c("osmar", "list")), `2` = structure(list(
    nodes = structure(list(attrs = structure(list(id = 7018492265, 
        visible = NA_character_, timestamp = structure(list(sec = 42, 
            min = 7L, hour = 17L, mday = 5L, mon = 2L, year = 121L, 
            wday = 5L, yday = 63L, isdst = 0L, zone = "UTC", 
            gmtoff = NA_integer_), class = c("POSIXlt", "POSIXt"
        )), version = 1, changeset = NA_real_, user = structure(NA_integer_, .Label = character(0), class = "factor"), 
        uid = structure(NA_integer_, .Label = character(0), class = "factor"), 
        lat = 48.1102703, lon = 11.8303853), row.names = 4281593L, class = "data.frame"), 
        tags = structure(list(id = numeric(0), k = structure(integer(0), .Label = character(0), class = "factor"), 
            v = structure(integer(0), .Label = character(0), class = "factor")), row.names = integer(0), class = "data.frame")), class = c("nodes", 
    "osmar_element", "list")), ways = structure(list(attrs = structure(list(
        id = numeric(0), visible = character(0), timestamp = structure(list(
            sec = numeric(0), min = integer(0), hour = integer(0), 
            mday = integer(0), mon = integer(0), year = integer(0), 
            wday = integer(0), yday = integer(0), isdst = integer(0), 
            zone = character(0), gmtoff = integer(0)), class = c("POSIXlt", 
        "POSIXt")), version = numeric(0), changeset = numeric(0), 
        user = structure(integer(0), .Label = character(0), class = "factor"), 
        uid = structure(integer(0), .Label = character(0), class = "factor")), row.names = integer(0), class = "data.frame"), 
        tags = structure(list(id = numeric(0), k = structure(integer(0), .Label = character(0), class = "factor"), 
            v = structure(integer(0), .Label = character(0), class = "factor")), row.names = integer(0), class = "data.frame"), 
        refs = structure(list(id = numeric(0), ref = numeric(0)), row.names = integer(0), class = "data.frame")), class = c("ways", 
    "osmar_element", "list")), relations = structure(list(attrs = structure(list(
        id = numeric(0), visible = character(0), timestamp = structure(list(
            sec = numeric(0), min = integer(0), hour = integer(0), 
            mday = integer(0), mon = integer(0), year = integer(0), 
            wday = integer(0), yday = integer(0), isdst = integer(0), 
            zone = character(0), gmtoff = integer(0)), class = c("POSIXlt", 
        "POSIXt")), version = numeric(0), changeset = numeric(0), 
        user = structure(integer(0), .Label = character(0), class = "factor"), 
        uid = structure(integer(0), .Label = character(0), class = "factor")), row.names = integer(0), class = "data.frame"), 
        tags = structure(list(id = numeric(0), k = structure(integer(0), .Label = character(0), class = "factor"), 
            v = structure(integer(0), .Label = character(0), class = "factor")), row.names = integer(0), class = "data.frame"), 
        refs = structure(list(id = numeric(0), type = structure(integer(0), .Label = character(0), class = "factor"), 
            ref = numeric(0), role = structure(integer(0), .Label = character(0), class = "factor")), row.names = integer(0), class = "data.frame")), class = c("relations", 
    "osmar_element", "list"))), class = c("osmar", "list")))

Solution

  • You can use Filter to keep only those objects that have more than 0 rows in attrs.

    Filter(function(x) nrow(x$nodes$attrs) > 0, nodes_found)
    
    #$`2`
    #osmar object
    #1 nodes, 0 ways, 0 relations