地理编码口语地名:零结果,但可以获得手动结果(R ggmap)
Gecoding Colloquial Place Names: Zero Results, but Can Get Manual Results (R ggmap)
我想知道印度尼西亚Java岛上的民政事务处的经纬度。区是行政区域,就像美国的州一样。我的大多数地理编码查询 return 都是不准确的结果:纬度和经度是针对整个学区的,而不是针对学区办公室的。然而,如果我手动将查询输入 Google 地图,我会找到我想要的。
library("ggmap")
# list of district names
dists <- read.csv("../javaDistNames.csv")
# vector of queries for Google maps
queries <- paste("Kantor Bupati ", dists$distName, ", ", dists$distName,
", ", dists$provinceName, ", Indonesia", sep="")
# impute latitude and longitude
dists[c("lon", "lat")] <- geocode(queries)
表达式"Kantor Bupati"在印度尼西亚语中的意思是地区办公室。
例如,如果我在 google 地图中输入 "Kantor Bupati BOGOR, BOGOR, JAWA BARAT, Indonesia",我会找到 district office: lat=-6.479745, lon=106.824742. But geocode returns:lat=-6.597147,lon=106.806。那是 20 公里远:对于我的目的来说不够精确。
我已经解决了这个问题:我使用 Google Places API as SymbolixAU suggested. The vectorized function below takes as arguments the colloquial place names we want to geocode and a second vector of non-colloquial place names that can be geocoded using ggmap
's geocode
. It returns latitude, longitude, and the name of the place. Get an API key here。
library("ggmap") # regular geocode function
library("RJSONIO") # read JSON
# API Key for Google Places
key <- # your key here
geoCodeColloquial <- function(queries, bases) {
# need coordinates of base to focus search
print("Getting coordinates of bases...")
baseCoords <- geocode(bases, source="google")
# request to Google Places
print("Requesting coordinates of queries...")
requests <- paste("https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=",
baseCoords$lat, ",", baseCoords$lon,
"&radius=50000&keyword=",
queries,
"&key=",
key,
sep="")
# results from Google Places; take only top result for each query
info <- lapply(requests,
function(request)
fromJSON(request)$results[[1]])
# lat and lon
coords <- lapply(info, function(i) i$geometry$location)
# name of top result
geoCodeNames <- lapply(info, function(i) i$name)
geoCodeNamesDf <- data.frame(matrix(unlist(geoCodeNames),
nrow=length(geoCodeNames), byrow=T))
# add lat, lon, and discovered names to dataframe
outDf <- data.frame(matrix(unlist(coords),
nrow=length(coords), byrow=T))
names(outDf) <- c("lat", "lon")
outDf["geoCodeName"] <- geoCodeNamesDf
return(outDf)
}
我想知道印度尼西亚Java岛上的民政事务处的经纬度。区是行政区域,就像美国的州一样。我的大多数地理编码查询 return 都是不准确的结果:纬度和经度是针对整个学区的,而不是针对学区办公室的。然而,如果我手动将查询输入 Google 地图,我会找到我想要的。
library("ggmap")
# list of district names
dists <- read.csv("../javaDistNames.csv")
# vector of queries for Google maps
queries <- paste("Kantor Bupati ", dists$distName, ", ", dists$distName,
", ", dists$provinceName, ", Indonesia", sep="")
# impute latitude and longitude
dists[c("lon", "lat")] <- geocode(queries)
表达式"Kantor Bupati"在印度尼西亚语中的意思是地区办公室。
例如,如果我在 google 地图中输入 "Kantor Bupati BOGOR, BOGOR, JAWA BARAT, Indonesia",我会找到 district office: lat=-6.479745, lon=106.824742. But geocode returns:lat=-6.597147,lon=106.806。那是 20 公里远:对于我的目的来说不够精确。
我已经解决了这个问题:我使用 Google Places API as SymbolixAU suggested. The vectorized function below takes as arguments the colloquial place names we want to geocode and a second vector of non-colloquial place names that can be geocoded using ggmap
's geocode
. It returns latitude, longitude, and the name of the place. Get an API key here。
library("ggmap") # regular geocode function
library("RJSONIO") # read JSON
# API Key for Google Places
key <- # your key here
geoCodeColloquial <- function(queries, bases) {
# need coordinates of base to focus search
print("Getting coordinates of bases...")
baseCoords <- geocode(bases, source="google")
# request to Google Places
print("Requesting coordinates of queries...")
requests <- paste("https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=",
baseCoords$lat, ",", baseCoords$lon,
"&radius=50000&keyword=",
queries,
"&key=",
key,
sep="")
# results from Google Places; take only top result for each query
info <- lapply(requests,
function(request)
fromJSON(request)$results[[1]])
# lat and lon
coords <- lapply(info, function(i) i$geometry$location)
# name of top result
geoCodeNames <- lapply(info, function(i) i$name)
geoCodeNamesDf <- data.frame(matrix(unlist(geoCodeNames),
nrow=length(geoCodeNames), byrow=T))
# add lat, lon, and discovered names to dataframe
outDf <- data.frame(matrix(unlist(coords),
nrow=length(coords), byrow=T))
names(outDf) <- c("lat", "lon")
outDf["geoCodeName"] <- geoCodeNamesDf
return(outDf)
}