Search code examples
rgoogle-mapsgeocode

R: geocode is returning more results than my query


I have a data frame with almost 10k locations and want to get as many coordinates as geocode can find for me, but my code is not returning results as I expected them to be. I'll explain. I have this:

# Getting records for Promenaea genus for an exemple
library(dismo)
promena<-gbif("Promenaea")

#Once I get all records I keep only those that don't have coordinates
promena<-promena[(is.na(promena$lon)),]

#And then, try to get coordinates for them
b <- try( geocode(promena$cloc) )

My plan was to cbind my data frame of Promenaea species with the results of geocode, and then just remove those rows geocode couldn't find coordinates for (that woud have NA). But my data frame has 259 rows and geocode returns 318 rows as result... so something is not matching there.

I appreciate any tips


Solution

  • So I kept researching and found some options:

    The dismo::geocode function has an argument named oneRecord for this purpose:

    b <- try( geocode(promena$cloc, oneRecord=TRUE))
    

    But:

    oneRecord Logical. If TRUE a single record for each item in x is returned. If the API returned multiple records, the values of this record are computed by averaging the coordinates and taking the union of all bounding boxes (says the documentation)

    As I prefer to have the real measure instead of an average, I kept looking and found that ggmap also has a geocode function:

    #Nem attempt of getting coordinates. 
    b <- try( ggmap::geocode(promena$cloc))
    

    But I got only coordinates and I'd like to have the location associated to the coordinates so I can check if geocode returned a good result, so I used this argument:

    b <- try(ggmap::geocode(promena$cloc, output="more"))
    

    Finally, I've compared results from both methods and they are pretty much the same, so I guess it's really important to check the records, because the function may indeed return averages