反向地理编码以提取地址组件 [英] reverse geocoding to extract address components
问题描述
我试图用R来反转地理编码。我第一次使用ggmap,但无法让它与我的API密钥一起工作。现在我试着用googleway。
newframe [,c(Front.lat,Front.long)]
Front.lat Front.long
1 -37.82681 144.9592
2 -37.82681 145.9592
newframe $ address< - apply(newframe,1,function(x){
google_reverse_geocode(location = as.numeric(c(x [Front.lat],
x [Front.long])),
key =xxxx)
})
这将列表中的变量提取出来,但我无法弄清楚结构。
我正在努力弄清楚如何提取下面列出的地址组件作为newframe中的变量。
postal_code
, administrative_area_level_1
, administrative_area_level_2
,地点
,路线
, street_number
我更喜欢每个地址组件作为一个单独的变量。
反向地理编码到newframe $地址后,地址组件可以进一步提取如下:
#创建有效(OK状态)响应的布尔数组(其他状态可能为NO_RESULTS,REQUEST_DENIED等)。
sel< - sapply(c(1:nrow(newframe)),function(x){
newframe $ address [[x]] $ status =='OK'
})
#获取每个地理编码坐标返回的第一个结果(即最佳匹配)的address_components。
address.components< - sapply(c(1:nrow(newframe [sel,])),function(x){
newframe $ address [[x]] $ results [1,] $ address_components
})
#获取所有可能的组件类型。
all.types< - unique(unlist(sapply(c(1:length(address.components)),function(x){
unlist(lapply(address.components [[x]] $类型,函数(l)l [[1]]))
})))
#获取每个存在类型的address_components的long_name值(另一个选项是short_name )。
all.values< - lapply(c(1:length(address.components)),function(x){
types< - unlist(lapply(address.components [[x]] $类型,函数(l)l [[1]]))
匹配< - match(all.types,types)
values< - address.components [[x]] $ long_name [matches ]
})
#将结果绑定到数据框中。
all.values< - do.call(rbind,all.values)
all.values< - as.data.frame(all.values)
名称(全部。值)< - all.types
#添加列并更新原始数据框。
newframe [,all.types]< - NA
newframe [sel,] [,all.types]< - all.values
请注意,我只保留每个组件给出的第一个类型,实际上跳过了政治类型,因为它出现在多个组件中,并且可能是多余的,例如administrative_area_level_1,政治。
I'm trying to reverse geocode with R. I first used ggmap but couldn't get it to work with my API key. Now i'm trying it with googleway.
newframe[,c("Front.lat","Front.long")]
Front.lat Front.long
1 -37.82681 144.9592
2 -37.82681 145.9592
newframe$address <- apply(newframe, 1, function(x){
google_reverse_geocode(location = as.numeric(c(x["Front.lat"],
x["Front.long"])),
key = "xxxx")
})
This extracts the variables as a list but I can't figure out the structure.
I'm struggling to figure out how to extract the address components listed below as variables in newframe
postal_code
, administrative_area_level_1
, administrative_area_level_2
, locality
, route
, street_number
I would prefer each address component as a separate variable.
After reverse geocoding into newframe$address the address components could be extracted further as follows:
# Make a boolean array of the valid ("OK" status) responses (other statuses may be "NO_RESULTS", "REQUEST_DENIED" etc).
sel <- sapply(c(1: nrow(newframe)), function(x){
newframe$address[[x]]$status == 'OK'
})
# Get the address_components of the first result (i.e. best match) returned per geocoded coordinate.
address.components <- sapply(c(1: nrow(newframe[sel,])), function(x){
newframe$address[[x]]$results[1,]$address_components
})
# Get all possible component types.
all.types <- unique(unlist(sapply(c(1: length(address.components)), function(x){
unlist(lapply(address.components[[x]]$types, function(l) l[[1]]))
})))
# Get "long_name" values of the address_components for each type present (the other option is "short_name").
all.values <- lapply(c(1: length(address.components)), function(x){
types <- unlist(lapply(address.components[[x]]$types, function(l) l[[1]]))
matches <- match(all.types, types)
values <- address.components[[x]]$long_name[matches]
})
# Bind results into a dataframe.
all.values <- do.call("rbind", all.values)
all.values <- as.data.frame(all.values)
names(all.values) <- all.types
# Add columns and update original data frame.
newframe[, all.types] <- NA
newframe[sel,][, all.types] <- all.values
Note that I've only kept the first type given per component, effectively skipping the "political" type as it appears in multiple components and is likely superfluous e.g. "administrative_area_level_1, political".
这篇关于反向地理编码以提取地址组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!