2016-01-22 53 views
2

我需要在R中搜索特定坐标的细节。假设我的坐标是:25.34926,51.47819。我使用nominatim来解析关于特定坐标的细节。在R中解析XML输出,打开街道地图数据

query <- sprintf("http://nominatim.openstreetmap.org/reverse?format=xml& lat=%s&lon=%s",lat,lon) 

result <- GET(query) 
xml <- content(result, 'parsed') 
list_xml = xmlToList(xml) 

place_id = list_xml$result$.attrs["osm_id"] 
place_id = as.numeric(place_id) 
type = list_xml$result$.attrs["osm_type"] 
type = as.character(type) 

我得到了道路的类型和使用这些代码的唯一ID。现在,我传递这些参数来获取有关位置的详细信息。

query <- sprintf("http://www.openstreetmap.org/api/0.6/%s/%s",type,place_id) 

结果< - GET(查询) XML < - 内容(结果, '解析')

这是XML输出,

<?xml version="1.0" encoding="UTF-8"?> 
<osm version="0.6" generator="CGImap 0.4.0 (5500 thorn-02.openstreetmap.org)" copyright="OpenStreetMap and contributors" attribution="http://www.openstreetmap.org/copyright" license="http://opendatacommons.org/licenses/odbl/1-0/"> 

<way id="25935530" visible="true" version="8" changeset="25997075" timestamp="2014-10-11T06:26:45Z" user="Kostik" uid="384084"> 
<nd ref="2012765518"/> 
<nd ref="2012765515"/> 
<nd ref="1138152548"/> 
<nd ref="1138151957"/> 
<nd ref="1138152112"/> 
<nd ref="1138152995"/> 
<nd ref="1885503851"/> 
<nd ref="1138152065"/> 
<nd ref="282906579"/> 
<nd ref="282905674"/> 
<nd ref="282905676"/> 
<nd ref="282905677"/> 
<nd ref="282905678"/> 
<nd ref="282905679"/> 
<nd ref="1684400272"/> 
<nd ref="1684400191"/> 
<nd ref="282906298"/> 
<nd ref="1138152685"/> 
<nd ref="1138152719"/> 
<nd ref="1138151621"/> 
<tag k="highway" v="primary"/> 
<tag k="name" v="Al Khafaji Street"/> 
<tag k="oneway" v="yes"/> 
</way> 
</osm> 

我关心<tag>标签,我如何获取价值?我使用下面提到的代码,但是我很难解析它。我非常疯狂地搜查,但无济于事。

list_xml = xmlToList(xml) 
tr <- getNodeSet(xml, "//osm/way/tags") 
+1

https://github.com/hrbrmstr/nominatim – hrbrmstr

+0

它在R中的osmar包中起作用 –

回答

0

解决了!我在R中下载了'osmar'包,并使用下面的代码 - 我可以获取所需的值。

query <- sprintf("http://www.openstreetmap.org/api/0.6/%s/%s",type,place_id) 
    result <- GET(query) 
    xml <- content(result, 'parsed') 
    osm = as_osmar(xml) 

    if(type == "way") { 
    w <- way(osm) 
    t <- w$ways$tags 
    } else { 
    if(type == "node") { 
     n <- node(osm) 
     t <- n$nodes$tags 
    } else { 
     r <- relation(osm) 
     t <- r$relations$tags  
    } 
    } 

    for(j in 1:nrow(t)){ 
    #print(as.character(t[j,2])) 
    if(as.character(t[j,2]) %in% colnames(features)){ 
     colNumber = which(colnames(features) == as.character(t[j,2])) 
     if(as.character(t[j,3]) == '0'){ 
     features[i, colNumber] = 'Not Defined' 
     } 
     else{ 
     features[i, colNumber] = as.character(t[j,3]) 
     } 
     # 
    } 
    #else{ 
    # if(as.character(t[j,2]) != 'name:en' && as.character(t[j,2]) != 'name:ar' && as.character(t[j,2]) != 'cuisine' && as.character(t[j,2]) != 'ref' && as.character(t[j,2]) != 'source'){ 
    #  rest <- sprintf("lat=%s&lon=%s,%s",lat,lon,as.character(t[j,2])) 
      #print(rest)  
    # } 
    #} 
    } 
相关问题