解析R中的XML输出,打开街道地图数据

解析R中的XML输出,打开街道地图数据,xml,r,openstreetmap,nominatim,Xml,R,Openstreetmap,Nominatim,我需要在R中搜索特定坐标的详细信息。假设我的坐标是:25.34926,51.47819。我使用namitm来解析特定坐标的细节 query <- sprintf("http://nominatim.openstreetmap.org/reverse?format=xml& lat=%s&lon=%s",lat,lon) result <- GET(query) xml <- content(result, 'parsed') list_xml = xmlT

我需要在R中搜索特定坐标的详细信息。假设我的坐标是:25.34926,51.47819。我使用namitm来解析特定坐标的细节

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)

query解决了它!我在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)     
    #  }
    #}
  }

query它与R中的osmar包一起工作
<?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>
list_xml = xmlToList(xml)
tr <- getNodeSet(xml, "//osm/way/tags")
  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)     
    #  }
    #}
  }