如何将包含多种方式的osmar对象(OSM关系)转换为闭合多边形

时间:2014-12-10 14:13:08

标签: r openstreetmap osmar

我想知道如何通过合并其“外部”方式将osmar对象(OSM关系)转换为闭合多边形。我想要获取的关系如下:http://www.openstreetmap.org/relation/416271

library(osmar)
wv <- get_osm(relation(416271), full=T)
wv
osmar object
4337 nodes, 138 ways, 9 relations

str(wv)
List of 3
 $ nodes    :List of 2
  ..$ attrs:'data.frame':   4337 obs. of  9 variables:
  .. ..$ id       : num [1:4337] 22290863 22290866 22290869 22290872 22290875 ...
  .. ..$ visible  : Factor w/ 1 level "true": 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ timestamp: POSIXlt[1:4337], format: "2006-12-30 15:22:45" "2006-12-30 15:22:45" "2006-12-30 15:22:45" ...
  .. ..$ version  : num [1:4337] 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ changeset: num [1:4337] 175959 175959 175959 175959 175959 ...
  .. ..$ user     : Factor w/ 69 levels "3dShapes","AtonX",..: 63 63 63 63 63 63 63 63 63 63 ...
  .. ..$ uid      : Factor w/ 69 levels "10613","107257",..: 45 45 45 45 45 45 45 45 45 45 ...
  .. ..$ lat      : num [1:4337] 51.4 51.4 51.4 51.4 51.4 ...
  .. ..$ lon      : num [1:4337] 3.35 3.35 3.34 3.34 3.34 ...
  ..$ tags :'data.frame':   575 obs. of  3 variables:
  .. ..$ id: num [1:575] 22290863 22290863 22290866 22290866 22290869 ...
  .. ..$ k : Factor w/ 43 levels "addr:postcode",..: 2 39 2 39 2 39 2 39 2 39 ...
  .. ..$ v : Factor w/ 40 levels "1","117639","1819",..: 9 32 9 32 9 32 9 32 9 32 ...
  ..- attr(*, "class")= chr [1:3] "nodes" "osmar_element" "list"
 $ ways     :List of 3
  ..$ attrs:'data.frame':   138 obs. of  7 variables:
  .. ..$ id       : num [1:138] 29158308 31175543 45749525 51104371 60186141 ...
  .. ..$ visible  : Factor w/ 1 level "true": 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ timestamp: POSIXlt[1:138], format: "2014-10-13 12:16:16" "2014-05-18 20:22:47" "2014-10-13 12:17:01" ...
  .. ..$ version  : num [1:138] 17 4 15 8 5 4 8 3 10 27 ...
  .. ..$ changeset: num [1:138] 26046342 22413405 26046342 20050162 22413405 ...
  .. ..$ user     : Factor w/ 8 levels "bibi6","escada",..: 7 7 7 8 7 8 8 8 1 7 ...
  .. ..$ uid      : Factor w/ 8 levels "145231","2449622",..: 7 7 7 1 7 1 1 1 5 7 ...
  ..$ tags :'data.frame':   855 obs. of  3 variables:
  .. ..$ id: num [1:855] 29158308 29158308 29158308 29158308 29158308 ...
  .. ..$ k : Factor w/ 19 levels "admin_level",..: 1 4 5 8 9 10 11 12 13 14 ...
  .. ..$ v : Factor w/ 41 levels "2","2009-10-20 00:00:02",..: 1 20 7 15 17 16 14 18 13 41 ...
  ..$ refs :'data.frame':   4473 obs. of  2 variables:
  .. ..$ id : num [1:4473] 29158308 29158308 29158308 29158308 29158308 ...
  .. ..$ ref: num [1:4473] 3.21e+08 3.21e+08 3.21e+08 3.21e+08 3.21e+08 ...
  ..- attr(*, "class")= chr [1:3] "ways" "osmar_element" "list"
 $ relations:List of 3
  ..$ attrs:'data.frame':   9 obs. of  7 variables:
  .. ..$ id       : num [1:9] 416271 2239171 2239750 2239123 2239751 ...
  .. ..$ visible  : Factor w/ 1 level "true": 1 1 1 1 1 1 1 1 1
  .. ..$ timestamp: POSIXlt[1:9], format: "2014-12-01 08:23:50" "2014-10-06 16:01:00" "2014-10-06 16:01:02" ...
  .. ..$ version  : num [1:9] 164 13 12 9 9 24 16 14 14
  .. ..$ changeset: num [1:9] 27148003 25898929 25898929 25898929 25898929 ...
  .. ..$ user     : Factor w/ 3 levels "Bas de Wit","QuercE",..: 1 2 2 2 2 3 2 2 3
  .. ..$ uid      : Factor w/ 3 levels "201359","2434393",..: 2 3 3 3 3 1 3 3 1
  ..$ tags :'data.frame':   121 obs. of  3 variables:
  .. ..$ id: num [1:121] 416271 416271 416271 416271 416271 ...
  .. ..$ k : Factor w/ 33 levels "admin_level",..: 1 2 3 5 6 8 9 11 18 28 ...
  .. ..$ v : Factor w/ 73 levels "30000","31000",..: 10 12 61 21 65 66 64 39 65 58 ...
  ..$ refs :'data.frame':   600 obs. of  4 variables:
  .. ..$ id  : num [1:600] 416271 416271 416271 416271 416271 ...
  .. ..$ type: Factor w/ 3 levels "node","relation",..: 1 1 3 3 3 3 3 3 3 3 ...
  .. ..$ ref : num [1:600] 2.70e+09 1.65e+09 4.11e+06 1.26e+08 1.69e+08 ...
  .. ..$ role: Factor w/ 4 levels "admin_centre",..: 2 1 3 3 3 3 3 3 3 3 ...
  ..- attr(*, "class")= chr [1:3] "relations" "osmar_element" "list"
 - attr(*, "class")= chr [1:2] "osmar" "list"

我看到的第一个问题是,这些方式的顺序与OSM网站上显示的顺序不同。在OSM网站上,似乎正确地订购了方式。在osmar对象中,顺序似乎是随机的。知道如何将这些方法合并成一个封闭的多边形吗?

2 个答案:

答案 0 :(得分:4)

我想我会发布我现在使用的解决方案:

library(osmar)
library(rgeos)

wv <- get_osm(relation(416271), full=T)
wv_lines <- as_sp(wv, 'lines')
wv_lines_closed <- gLineMerge(wv_lines)

list_of_Lines <- slot(wv_lines_closed, "lines")
wv_polygon <- SpatialPolygons(lapply(list_of_Lines, function(x) {
  Polygons(list(Polygon(slot(slot(x, "Lines")[[1]], "coords"))),
           ID=slot(x, "ID"))
}),
proj4string=CRS("+proj=longlat +ellps=WGS84"))
plot(wv_polygon, col='red')

答案 1 :(得分:3)

Relation个元素无需正确排序。事实上,甚至不可能为某些类型的关系找到合适的排序。

要订购关系元素,请查看elements本身。 way始终以特定的node ID开头和结尾。连续方式始终在开头或结尾共享相同的节点ID。对于排序,您只需确定哪些方式在开始和结束时共享相同的节点ID。这只有在关系没有被破坏时才会起作用,不幸的是,这可能不时发生。

This related question也可以为您提供帮助。