Using Bing option with taRifx.geo in R - r

I'm attempting to find the coordinates for several cities in Ghana. I've used the geocode function in taRifx.geo with the "google" option and had success, but Google doesn't know where all my cities are.
I'd like to use the Bing service to fill in the coordinates that were missing from my Google list and I'm starting with just one city ("Awutu Breku") to make sure the code works. I run this code, though, and get an error:
> Bkey <- "______________(my key would be here)____"
> Loc5 <- geocode("Awutu Breku", service="bing", BingMapsKey = Bkey,
+ returntype="coordinates")
Error in geocode.default("Awutu Breku", service = "bing", BingMapsKey = Bkey, :
To use Bing, you must save your Bing Maps API key (obtain at http://msdn.microsoft.com/en-us/library/ff428642.aspx) using options(BingMapsKey='mykey').
This error is very confusing since I do include the BingMapsKey argument but the function doesn't seem to be recognizing it.
I've searched quite a bit for an answer to this one and haven't come across anything. I even looked on the github page but couldn't figure out what might be wrong with my code. I feel like I'm missing something simple. Any help would be appreciated!

I needed to do a devtools::install_github("gsk3/taRifx.geo") to be able to use the geocode function and the instructions clearly state that you need to do a:
options(BingMapsKey='ridiculously_long_string')
before calling using the bing option:
geocode("1600 Pennsylvania Ave NW, Washington, DC 20500", service="bing")
## [1] 38.89719 -77.03655

Related

Reverse geocode with open streetmaps

I found this question about converting a longitude and latitude into an address. The question uses ggmap's revgeocode which uses the google API. If I understood correctly, this is very costly.
I noticed that the following link about Accessing OpenStreetMap data with R, also uses ggmap. I was wondering if it is possible to use open street maps for revgeocode instead of the google API, or whether there is some other way to use OpenStreetMap to reverse geocode.
Data used for question:
humandate lat lon
09/10/2014 13:41 41.83174254 -75.87998774
09/10/2014 13:53 41.83189873 -75.87994957
Answer provided:
library(ggmap)
revgeocode(c(df$lon[1], df$lat[1]))
"27-37 Beech Street, Montrose, PA 18801, USA"
I found an answer for the API here, and I found this package is available in R: tmaptools.
I tried to do the following, but to no avail:
address <- tmaptools::rev_geocode_OSM(41.83174254 , -75.87998774)
Error in names(search_result_id) <- c("place_id", "osm_type", "osm_id", :
attempt to set an attribute on NULL

openmap NullPointerException Error in osmtile could not obtain tile

I am trying to plot a small rectangle of a map:
library(OpenStreetMap)
upper_left <- c(47.413, 8.551);
lower_right <- c(47.417, 8.556);
map_osm <- openmap(upper_left, lower_right, type = 'osm' );
plot(map_osm );
When I run that, the openmap function gives me the error Error in osmtile(x%%nX, y, zoom, type) : could not obtain tile: 540 298 10.
The documentation of OpenStreetMap seems to indicate that I need to add an API key. However, I am not sure how exactly I would do that (because I use type='osm', not type = url) and I am also unclear where I'd get such an API key from.
The java.lang.NullPointerException and the following R-error (Error in osmtile(...)) seem to come from an older version of OpenStreetMap.
By updating OpenStreetMap to the latest version (0.3.4 currently), the error disappears and the example code of OP should work as it is, without needing an API key.
The accepted answer is not adequate as the error can occur even with the most recent package version.
Sometimes if a particular area is not available in a specific style, you get an error similar to the one mentioned above independent of the package version. The solution would be to try the function with a different style. This is mentioned in the following blog post
As an example, the following modification may solve the issue:
library(OpenStreetMap)
upper_left <- c(47.413, 8.551);
lower_right <- c(47.417, 8.556);
map_osm <- openmap(upper_left, lower_right, type = 'opencyclemap');
plot(map_osm)

Unable to fetch maps with dismo gmap()

I am trying to follow a tutorial found here:
https://data.cdrc.ac.uk/tutorial/aa5491c9-cbac-4026-97c9-f9168462f4ac/4b026153-2953-4173-ab44-b24b2fb559fd
Note: the tutorial requires a free login to access
The step I am stuck at in the tutorial is this code:
library(raster)
library(dismo)
google.map <- gmap("Camden, London", type = "satellite")
I am having trouble getting dismo::gmap() to work. Every time I run it, I get
REQUEST_DENIED:Camden,London
Error in dismo::gmap("Camden, London", type = "satellite") : location not found
The same error pops up when I point it at any other location. Can anyone help?
Try reviewing the following:
Google now requires an API key.
See ?register_google for details.
?register_google

R: Error in get_map()/get_googlemap() from ggmap

I am trying to use GGmap to create a plot of vehicle car crashes by state. The map will have dots which are sized based on the number of car crashes in the state.
In particular I am trying to recreate the usa-plot shown in the visualizing clusters section of this blog post.
However, whenever I try to create the map I get this error.
Error in aperm.default(map, c(2, 1, 3)) :
invalid first argument, must be an array
I have setup the Google API and see that it is recieving hits. I have also enabled it and have the key.
In addition I have installed GGmap from the github account using this command:
devtools::install_github("dkahle/ggmap", ref = "tidyup", force=TRUE)
since the CRAN one isn't updated.
I have restarted and quit R several times as well but the error persists.
Even if I just simply run:
get_map()
it still results in the error:
Error in aperm.default(map, c(2, 1, 3)) :
invalid first argument, must be an array
Below is my code, it is similar to the code in the blog post:
mydata$State <- as.character(mydata$State)
mydata$MV.Number = as.numeric(mydata$MV.Number)
mydata = mydata[mydata$State != "Alaska", ]
mydata = mydata[mydata$State != "Hawaii", ]
devtools::install_github("dkahle/ggmap", ref = "tidyup", force=TRUE)
library(ggmap)
ggmap::register_google(key = "...") #my key is here
for (i in 1:nrow(mydata)) {
latlon = geocode(mydata[i,1])
mydata$lon[i] = as.numeric(latlon[1])
mydata$lat[i] = as.numeric(latlon[2])
}
mv_num_collisions = data.frame(mydata$MV.Number, mydata$lon, mydata$lat)
colnames(mv_num_collisions) = c('collisions','lon','lat')
usa_center = as.numeric(geocode("United States"))
USAMap = ggmap(get_googlemap(center=usa_center, scale=2, zoom=4),
extent="normal")
USAMap +
geom_point(aes(x=lon, y=lat), data=mv_num_collisions, col="orange",
alpha=0.4, size=mv_num_collisions$collisions*circle_scale_amt) +
scale_size_continuous(range=range(mv_num_collisions$collisions))
I expect the map to output like this
But I cannot seem to get passed this error.
If anyone can help that would be great.
Please let me know if you need any more information.
Thank you.
This error is due to the google key not having the appropriate API activity enabled for that key.
Go into the google API console and enable the API "Maps Static API" and it should work for you.
EDIT: Jan 2020 - I was doing some similar work and found that a similar API was failing because billing information had to be added to the project in the Google Cloud console before it would work.
Make sure to enable billing. You don't have to restrict api, but make sure all the api's you need are enabled. if you want to search location names, you'll need geocoding api in addition to static maps. ggmap from CRAN is OK now (don't need github version).
it´s necessary confirm your credit card in Google API, with this, your API key is activated and you can use ggmap normally

Valid client and signature using ggmap package in R

I am using ggmap package in R software in order to geocode a list of addresses. The counting is over 2500 - Googles diary quota for free users.
I have been searched about it and I found out that it is possible to geocode 100 thousand addresses per day if you have an account. I decided to open an account and I received a message that I have 60 trial days and they gave a 300 dollars credit. Despite this, I didnt discover where I can find the "client" and "signature" options that ggmap requires.
If someone can tell me where I can find it, please let me know!
My aim is to make Google searches by the address. If address is not recognized, then I make it searches for the local using the zip code. Here is what I am doing:
library(RJSONIO)
library(RCurl)
getGeoData <- function(location, api_key){
location <- gsub(' ','+',location)
geo_data <- getURL(paste("https://maps.googleapis.com/maps/api/geocode/json?address=",location,sprintf("&key=%s",api_key), sep=""))
geo_data <- fromJSON(geo_data)
if(geo_data$status=="ZERO_RESULTS"){
return(NA)
} else return(geo_data$results[[1]]$geometry$location)
}
New error:
getGeoData(basefinal$cep[6793], "MY KEY")[[2]]
Error in getGeoData(basefinal$cep[6793], "MY KEY")[[2]] :
subscript out of bounds

Resources