Loop for Reverse Geocoding in R

后端 未结 2 1952
执笔经年
执笔经年 2020-12-16 19:00

I am trying to reverse geocode a large data-set (around 100k). I used the revgeocode function from the package ggmap. I got the result for 1 entry<

相关标签:
2条回答
  • 2020-12-16 19:05

    I have had a similar problem for integrating the API key. Basically it is a matter of integrating the API key in the URL that R calls. If this doesn't help you, you need to change the core code (look it up on Github) to allow for an argument calling a key.

    0 讨论(0)
  • 2020-12-16 19:17

    Based on this answer, you could create new variables in your data.frame

    We use mapply() to process your coordinates and return the results in a list res.

    res <- mapply(FUN = function(lon, lat) { 
      revgeocode(c(lon, lat), output = "more") 
      }, 
      df$lon, df$lat
      )
    

    Then, we use rbindlist() from data.table to convert the list into a data.frame (with fill = TRUE since not all elements of res have the same lenghts i.e. some results do not return a street_number and a postal_code) and cbind() it to the original data

    cbind(df, data.table::rbindlist(res, fill = TRUE))
    

    Update

    Following up on your comment, if you want to process more than 2500 queries, you could subscribe to Google Maps APIs Premium Plan to unlock higher quotas. Then you can pass on your credentials to revgeocode() using the signature and client parameter.

    As per mentionned in the documentation:

    Upon purchasing your Google Maps APIs Premium Plan license, you will receive a welcome email from Google that contains your client ID and your private cryptographic key.

    Your client ID is used to access the special features of Google Maps APIs Premium Plan. All client IDs begin with a gme- prefix. Pass your client ID as the value of the client parameter. A unique digital signature is generated using your private cryptographic key. Pass this signature as the value of the signature parameter.

    You can see how it works under the hood by examining the revgeocode() source and see how the URL is constructed:

    sensor4url <- paste("&sensor=", sensor, sep = "")
    client4url <- paste("&client=", client, sep = "")
    signature4url <- paste("&signature=", signature, sep = "")
    url_string <- paste("http://maps.googleapis.com/maps/api/geocode/json?latlng=", 
            loc4url, sensor4url, sep = "")
        if (userType == "business") {
            url_string <- paste(url_string, client4url, signature4url, 
                sep = "")
        }
    

    Data

    df <- structure(list(lat = c(32.31, 32.19, 34.75, 35.09, 35.35, 34.74 ), lon = 
    c(119.827, 119.637, 119.381, 119.364, 119.534, 119.421 )), .Names = 
    c("lat", "lon"), row.names = c(21L, 32L, 37L, 48L, 50L, 89L), class = "data.frame") 
    
    0 讨论(0)
提交回复
热议问题