我是R和Python的新手,正在尝试从事有趣的项目。
我目前有一个包含6k地址的电子表格。是否可以使用任何脚本或可以对所有代码进行地理编码并为我提供lat_long坐标的应用程序,而不会超时或不受API限制?
我尝试过使用Openstreetmap和Googlemap API,但始终存在会话超时或限制。
library(RODBC)
library(jsonlite)
library(OpenStreetMap)
library(plyr)
ScriptPause <- function(x)
{
p1 <- proc.time()
Sys.sleep(x)
proc.time() - p1
}
nominatim_osm <- function(address = NULL)
{
if(suppressWarnings(is.null(address)))
return(data.frame("NA"))
tryCatch(
d <- jsonlite::fromJSON(
gsub('\\@addr\\@', gsub('\\s+', '\\%20', addresses[i,]$Address),
'http://nominatim.openstreetmap.org/search/@addr@?format=json&addressdetails=0&limit=1')
), error = function(c) return(data.frame("NA"))
)
if(length(d) == 0) return(data.frame("NA"))
return(data.frame(lon = as.numeric(d$lon), lat = as.numeric(d$lat)))
}
myConnection <- odbcConnect("Warehouse") # Connect to ODBC DSN (configured in control panel)
addresses <- sqlQuery(myConnection, "
SELECT LocationKey
,StreetAddressLine1
,City
,ZipCode
,StateAbbreviation
,REPLACE(REPLACE(CASE WHEN StreetAddressLine1 LIKE '%,%'
THEN LEFT(StreetAddressLine1,CHARINDEX(',',StreetAddressLine1)-1)
ELSE StreetAddressLine1
END,'#',''),'\','') + ', ' +
CASE WHEN City = 'Unknown' THEN ' ' ELSE City + ', ' END +
StateAbbreviation + ' ' +
CASE WHEN ZipCode = 'Unknown' THEN '' ELSE ZipCode END AS [Address]
FROM Dim.[Location]
WHERE AttemptGeocode = 0
AND Latitude IS NULL
AND IsRating = 1
AND LocationKey <> 0
AND LocationKey NOT IN (74221,175560) -- Having issues with this one.
")
close(myConnection)
rm(myConnection)
addresses$Address <- as.character(addresses$Address)
StgDimLocation <- read.csv("sample2.csv")
attach(sample2)
addresses <- addresses[which(!(addresses$LocationKey %in% StgDimLocation$LocationKey)),]
latlon <- setNames(data.frame(matrix(ncol = 5, nrow = 0)),c('LocationKey','Address','lat','lon','X.NA.'))
for (i in 1:nrow(addresses)) {
api_output <- suppressWarnings(nominatim_osm(addresses[i,]$Address))
result <- cbind(addresses[i,],api_output)
latlon <- rbind.fill(latlon,result, fill = NULL)
ScriptPause(1)
print(i)}
latlon <- rbind(latlon,StgDimLocation)
#Output results to file. Evaluate using Tableau.
FileName <-"StgDimLocation.csv"
file.exists(FileName) && file.remove(FileName)
write.csv(latlon, FileName, na = "", row.names = FALSE)
#-----------------------------------------------