2

Sometimes when I want to convert a subset of ways from an osm file loaded with the osmar package, I run into an error complaining about duplicate row names. The following code run on this file in attach throws this error:

Error in `row.names<-.data.frame`(`*tmp*`, value = c(4067932, 4296467,  : 
  duplicate 'row.names' are not allowed
In addition: Warning message:
non-unique values when setting 'row.names': ‘123074181’, ‘154043350’, ‘171826984’, ‘176273732’, ‘196520271’, ‘202157002’, ‘24240315’, ‘24913519’, ‘284493291’, ‘304517627’, ‘35556702’, ‘38210407’, ‘4399491’, ‘4840974’, ‘4840977’, ‘60333308’, ‘61583424’, ‘67627350’, ‘67627353’, ‘68711084’, ‘72157174’ 

This is the code:

  osm_data <- get_osm(complete_file(), source = osmsource_file('extract.osm'))

  hw_ids <- find(osm_data, way(tags(k == "highway")))
  hw_ids <- find_down(osm_data, way(hw_ids))
  ways <- subset(osm_data, ids = hw_ids)
  ways <- as_sp(ways, "lines")

I suppose this is because of SpatialLines objects with the same osm_id ? How do I avoid this problem ?

1 Answer 1

2

Not sure it is what you are looking for, but here is a start with this workaround (based on this answer):

library(osmar)
osm_data <- get_osm(complete_file(), source = osmsource_file('extract.osm'))

hw_ids <- find(osm_data, way(tags(k == "highway")))
hw_ids <- find_down(osm_data, way(hw_ids))
ways <- subset(osm_data, ids = hw_ids)

way_ids <- unique(ways$ways$refs$id)
way_lns <- vector("list", length(way_ids))
for (i in 1:length(way_lns)) {
  way_lns[[i]] <- Lines(osmar:::ways_nodes2Line(way_ids[i], ways$ways, ways$nodes), way_ids[i])
}

way_lns <- osmar:::remove_emptyLines(way_lns)
splns <- SpatialLines(way_lns, proj4string = osm_crs())

dat <- cbind(ways$ways$attrs, type = as.factor("way"))
daf <- data.frame(id = unique(dat$id))
rownames(daf) <- daf$id
# used to be data$id

ret <- SpatialLinesDataFrame(splns, daf)

> class(ret)
[1] "SpatialLinesDataFrame"
attr(,"package")
[1] "sp"

plot(ret)

enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.