简体   繁体   中英

Geocoding Data Locations With Google in R

I am trying to use very well written instructions from this blog: https://www.jessesadler.com/post/geocoding-with-r/ to geocode locational data in R including specific cites and cities in Hawaii. I am having issues pulling information from Google. When running mutate_geocode my data runs but no output is gathered. I bypassed this for the time being with manual entry of lat and lon for just one location of my dataset, attempting to trouble shoot. Now, when I use get_googlemap, I get the error message "Error in Download File"

I have tried using mutate_geocode as well as running a loop using geocode. I either do not get output or I get the OVER_QUERY_LIMIT error (which seems to be very classic). After checking my query limit I am nowhere near the limit.

Method 1:

BH <- rename(location, place = Location)
BH_df <- as.data.frame(BH)
location_df <- mutate_geocode(HB, Location)

Method 2:

origAddress <- read.csv("HSMBH.csv", stringsAsFactors = FALSE)
geocoded <- data.frame(stringsAsFactors = FALSE)
for(i in 1:nrow(origAddress))
{
  result <- geocode(HB$Location[i], output = "latlona", source = "google")
  HB$lon[i] <- as.character(result[1])
  HB$lat[i] <- as.character(result[2])
  HB$geoAddress[i] <- as.character(result[3])
}

Post Manual Entry of lon and lat points I run in to this error:

map <- get_googlemap(center = c(-158.114, 21.59), zoom = 4)

I am hoping to gather lat and lon points for my locations, and then be able to use get_googlemap to draft a map with which I can plot density points of occurrences (I have the code for the points already).

Alternatively, you can use a one-liner for rapid geocoding via tmaptools::geocode_OSM() :

Data

library(tmaptools)
addresses <- data.frame(address = c("New York", "Berlin", "Huangpu Qu", 
                                    "Vienna", "St. Petersburg"), 
                                    stringsAsFactors = FALSE)

Code

result <- lapply(addresses[, 1], geocode_OSM)

> result 
$address
           query      lat       lon  lat_min  lat_max   lon_min   lon_max
1       New York 40.73086 -73.98716 40.47740 40.91618 -74.25909 -73.70018
2         Berlin 52.51704  13.38886 52.35704 52.67704  13.22886  13.54886
3     Huangpu Qu 31.21823 121.48030 31.19020 31.24653 121.45220 121.50596
4         Vienna 48.20835  16.37250 48.04835 48.36835  16.21250  16.53250
5 St. Petersburg 27.77038 -82.66951 27.64364 27.91390 -82.76902 -82.54062

This way, you have both

  1. the centroids ( lon , lat ) that are important for Google Maps and
  2. boundary boxes ( lon_min , lat_min , lon_max , lat_max ) that mapping services like OSM or Stamen need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM