2017-05-31 4 views
1

Ich habe 32K Zeilen von Adressen, für die ich Long/Latitude-Werte finden muss.R-Geocoding mit Adresse

Ich verwende den Code gefunden here. Ich bin so sehr dankbar für diese Person zu erstellen, aber ich habe eine Frage:

Ich möchte es bearbeiten, so dass, wenn die Schleife läuft ein Problem mit der aktuellen Zeile der Adresse, es einfach sagt NA in der Lat/Long Felder und bewegt sich zum nächsten. Weiß jemand, wie das erreicht werden kann? Der Code ist unten:

# Geocoding a csv column of "addresses" in R 

#load ggmap 
library(ggmap) 

# Select the file from the file chooser 
fileToLoad <- file.choose(new = TRUE) 

# Read in the CSV data and store it in a variable 
origAddress <- read.csv(fileToLoad, stringsAsFactors = FALSE) 

# Initialize the data frame 
geocoded <- data.frame(stringsAsFactors = FALSE) 

# Loop through the addresses to get the latitude and longitude of each address and add it to the 
# origAddress data frame in new columns lat and lon 
for(i in 1:nrow(origAddress)) 
{ 
    # Print("Working...") 
    result <- geocode(origAddress$addresses[i], output = "latlona", source = "google") 
    origAddress$lon[i] <- as.numeric(result[1]) 
    origAddress$lat[i] <- as.numeric(result[2]) 
    origAddress$geoAddress[i] <- as.character(result[3]) 
} 
# Write a CSV file containing origAddress to the working directory 
write.csv(origAddress, "geocoded.csv", row.names=FALSE) 

Antwort

5

Sie tryCatch() verwenden können die Geocodierung Warnung und geben einen data.frame mit der gleichen Struktur (lon, lat, Adresse) als geocode() zurückkehren würde zu isolieren.

Ihr Code wäre dann

# Geocoding a csv column of "addresses" in R 

# load ggmap 
library(ggmap) 

# Select the file from the file chooser 
fileToLoad <- file.choose(new = TRUE) 

# Read in the CSV data and store it in a variable 
origAddress <- read.csv(fileToLoad, stringsAsFactors = FALSE) 

# Loop through the addresses to get the latitude and longitude of each address and add it to the 
# origAddress data frame in new columns lat and lon 
for(i in 1:nrow(origAddress)) { 
    result <- tryCatch(geocode(origAddress$addresses[i], output = "latlona", source = "google"), 
        warning = function(w) data.frame(lon = NA, lat = NA, address = NA)) 
    origAddress$lon[i] <- as.numeric(result[1]) 
    origAddress$lat[i] <- as.numeric(result[2]) 
    origAddress$geoAddress[i] <- as.character(result[3]) 
} 
# Write a CSV file containing origAddress to the working directory 
write.csv(origAddress, "geocoded.csv", row.names=FALSE) 

Alternativ können Sie dieses schneller tun können und sauber ohne die Schleife und Fehlerprüfung. Ohne ein reproduzierbares Beispiel Ihrer Daten gibt es keine Möglichkeit zu wissen, ob dadurch alle benötigten Informationen erhalten bleiben.

# Substituted for for loop 
result <- geocode(origAddress$addresses, output = "latlona", source = "google") 
origAddress <- cbind(origAddress$addresses, result) 
+1

Das hat funktioniert! Vielen Dank @Ben Fasoli !! – Walker

+0

Ich bin auch auf dieses Problem gestoßen ... hat jemand irgendwelche Vorschläge? 'Abfrage maximal überschritten, siehe? Geocode. current total = 2500 ' – Walker

+0

Suchen Sie in der 'geocode()' -Dokumentation nach 'override_limit = TRUE' –