2014-02-11 64 views
0

作爲a question I posted yesterday,的跟進,是否有R中的包/功能可以給我國(和洲),其中由經度和緯度定義的點位於?沿着什麼樣的線東西做here in MatLab.從R經度和緯度點獲取國家(和大陸)

數據框看起來像這樣...

Point_Name    Longitude Latitude 
University of Arkansas 36.067832 -94.173655 
Lehigh University  40.601458 -75.360063 
Harvard University  42.379393 -71.115897 

而且我想與國家和大洲列添加到它的輸出上面的數據幀。 作爲的額外獎勵,美國專欄針對美國(而「其他」針對美國以外的專欄)?

+1

看一看[**這個問題**](http://stackoverflow.com/questions/14334970/convert-latitude-and-longitude-coordinates-to-country-name-in-r ?rq = 1),從本頁右邊空白處的「相關」列表中挑選出來。 – Henrik

+0

[這個答案](http://stackoverflow.com/a/8751965/980833)會讓你的狀態。 –

+0

謝謝,我不知道我是如何錯過的(我曾看過相當多的數字,但沒有找到那些) - 只要我根據舊的答案得到腳本,我將刪除我的Q. – Ell

回答

1

因此,這是在Google上使用reverse geocoding API的替代方案。該代碼部分基於this reference

呼喚你的數據幀以上df

reverseGeoCode <- function(latlng) { 
    require("XML") 
    require("httr") 
    latlng <- as.numeric(latlng) 
    latlngStr <- gsub(' ','%20', paste(round(latlng,2), collapse=",")) 
    url <- "http://maps.google.com" 
    path <- "/maps/api/geocode/xml" 
    query <- list(sensor="false",latlng=latlngStr) 
    response <- GET(url, path=path, query=query) 
    if (response$status !=200) { 
    print(paste("HTTP Error:",response$status),quote=F) 
    return(c(NA,NA)) 
    } 
    xml <- xmlInternalTreeParse(content(response,type="text")) 
    status <- xmlValue(getNodeSet(xml,"//status")[[1]]) 
    if (status != "OK"){ 
    print(paste("Query Failed:",status),quote=F) 
    return(c(NA,NA)) 
    } 
    xPath <- '//result[1]/address_component[type="country"]/long_name[1]' 
    country <- xmlValue(getNodeSet(xml,xPath)[[1]]) 
    xPath <- '//result[1]/address_component[type="administrative_area_level_1"]/long_name[1]' 
    state <- xmlValue(getNodeSet(xml,xPath)[[1]]) 
    return(c(state=state,country=country)) 
} 
st.cntry <- t(apply(df,1,function(x)reverseGeoCode(x[2:3]))) 
result <- cbind(df,st.cntry) 
result 
#    Point_Name Longitude Latitude   state  country 
# 1 University of Arkansas 36.06783 -94.17365  Arkansas United States 
# 2  Lehigh University 40.60146 -75.36006 Pennsylvania United States 
# 3  Harvard University 42.37939 -71.11590 Massachusetts United States 

在API定義中, 「administrative_area_level_1」 低於國家的最高行政區域。在美國,這些是國家。在其他國家,定義各不相同(例如,可能是省份)。

順便說一句,我相當肯定你有經度和緯度逆轉。

4

要得到大陸,您可以修改使用rworldmap從這個answer創建coords2continent功能,如下圖所示的coords2country函數的最後一行。選擇是否需要6或7洲模式。我會考慮將此代碼放入rworldmap

library(sp) 
library(rworldmap) 

# The single argument to this function, points, is a data.frame in which: 
# - column 1 contains the longitude in degrees 
# - column 2 contains the latitude in degrees 
coords2continent = function(points) 
{ 
    countriesSP <- getMap(resolution='low') 
    #countriesSP <- getMap(resolution='high') #you could use high res map from rworldxtra if you were concerned about detail 

    # converting points to a SpatialPoints object 
    # setting CRS directly to that from rworldmap 
    pointsSP = SpatialPoints(points, proj4string=CRS(proj4string(countriesSP))) 


    # use 'over' to get indices of the Polygons object containing each point 
    indices = over(pointsSP, countriesSP) 

    #indices$continent # returns the continent (6 continent model) 
    indices$REGION # returns the continent (7 continent model) 
    #indices$ADMIN #returns country name 
    #indices$ISO3 # returns the ISO3 code 
} 

這是一個測試。

points = data.frame(lon=c(0, 90, -45, -100, 130), lat=c(52, 40, -10, 45, -30)) 

coords2continent(points) 
#[1] Europe  Asia   South America North America Australia 
coords2country(points) 
#[1] United Kingdom China Brazil United States of America Australia