Rgooglemaps not plotting data over the map - r

I am having an issue with the R package Rgooglemaps. I can retrieve a map from google maps or open street map but the PlotOnStaticMap function does not overlap the data I want to plot to the map. Instead it opens a new graphical device where it plots the data. So I end up with:
an image saved in my working dir which contains only the retrieved google map
a R graphic device window with my data plotted over a white background.
I am on windows, using R version 2.13.1 and RGUI. The issue appears also when I run the code via command line. I remember I used Rgooglemaps a few months ago and it worked properly for me. Meanwhile I upgraded my R version, so it could be a version related issue. Can someone give it a try and see if you have the same issue (and a way to solve it)? Here an example to reproduce the issue (taken from http://www.r-bloggers.com/visualizing-gis-data-with-r-and-open-street-map/). The example is using Open Street Map instead of googlemaps but the behavior is the same.
The code:
require(RgoogleMaps)
lat_c<-51.47393
lon_c<-7.22667
bb<-qbbox(lat = c(lat_c[1]+0.01, lat_c[1]-0.01), lon = c(lon_c[1]+0.03, lon_c[1]-0.03))
OSM.map<-GetMap.OSM(lonR=bb$lonR, latR=bb$latR, scale = 20000, destfile="bochum.png")
image(OSM.map)
lat<- c(51.47393, 51.479021)
lon<- c(7.22667, 7.222526)
val <- c(0, 255)
lat_adj<-function(lat, map){(map$BBOX$ll[1]-lat)/(map$BBOX$ll[1]-map$BBOX$ur[1])}
lon_adj<-function(lon, map){(map$BBOX$ll[2]-lon)/(map$BBOX$ll[2]-map$BBOX$ur[2])}
PlotOnStaticMap(OSM.map, lat = lat_adj(lat, OSM.map), lon = lon_adj(lon, OSM.map),
col=rgb(255,0, val,90,maxColorValue=255),pch=16,cex=4)
dev.print(jpeg,"test.jpeg", width=1204, height=644, units="px")

I've only re-run your code, and not looked into the problems and get the error message:
dev.print(jpeg,"test.jpeg", width=1204, height=644, units="px")
windows
2
Error: REAL() can only be applied to a 'numeric', not a 'NULL'

I ran the above code on R2.14.0 and everything appears to work.

Related

Download GPM-IMERG satellite images in R

I've been trying (a lot) to download NASA's GPM-IMERG satellite images with precipitation data from R. I'm doing the question in this forum and not in the GIS forum because I read that most r users are here. (FYI: Im running Windows 10 and r 3.6.3 in rstudio). So far I've tried the following:
Created an account at PMM-NASA (see here). Everything worked well.
Installed the gpm package from devtools::install_github("csaybar/gpm"), followed by
gpm_getaxel() (see here). I tried running the following code.
gpm_download(path = RutaDownloads,
user = "myuser#email.com",
password = "myuser#email.com",
dates = c('2017-01-01','2017-02-28'),
band = 3,
lonMin = 70,
lonMax = 75,
latMin = 34,
latMax = 38,
product = 'finalrun',
quiet = F,
n = 1)
However, it did not worked. The error shown in rstudio is the following.
'gdal_translate' not found Error in gdaltranslate_exist() : GDAL
should be declared as a system variable if you are using Windows
I haven't had any problems running gdal when working with multiple rasters/vectors in R (so far). Does anyone know if I have to install gdal in my PC apart from installing rgdal in r? If so, how can I do it and 'synchronize' it with R to be used with gpm package? I know there is a lot of information in google, however, I'd rather take the advice from anyone that has done this before, because in the past I did not have such a good experience working, for example, with GDAL and Python, and that is the main reason I started working my GIS codes in R.
I also tried with another alternative installing the remotes package and remotes::install_github("bastianmanz/GPM_rain") (See here). However, this uses the rhdf5 package, which is not available for my R version, and thus I installed the BiocManager and then BiocManager::install("rhdf5") (following the instructions from here). With GPM_rain there are two possible ways to download the images: (1) with wget i.e. system_download, but its not straightforward because it needs a list of the files to download, and (2) with rcurl_download using RCurl. This seemed easy to use. Nevertheless, the first problem is that I cannot specify lat/long for the image extent and, as in point 2, it did not work. I tried running the following code:
rcurl_download(product = "nrt",
nrt_type = "late",
start = 20170101,
end = 20170131,
userpwd = "myuser#email.com:myuser#email.com")
But the error says that rcurl_downloadfunction was not found. How to fix any of the above errors or have any other solutions, I would be really grateful if you could share your experience, specially for option 2, where I can set lat/long to download the data. Thanks in advance,
Jorge.
--------------------------------------------------------------------------------------------------------
EDIT:
Package gpm is "retired" according to its developer (see here).

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)

R Leaflet: lines missing when plotting polylines

I have a pretty simple spatial object composed of a bunch of lines. I can plot it in different ways with no problems: QGIS, mapshaper.org. Even the standard R plot() function:
But when I plot it with leaflet(), some segments mysteriously disappear, leaving disconnected lines behind:
A reproducible example follows. NOTE: I use a GeoJSON source file for simplicity here. I have also tried saving the lines as an ESRI shapefile, with the same effect: The data is plotted OK with QGIS, or plot(), etc but not with leaflet().
library(leaflet)
library(rgdal)
download.file("https://www.dropbox.com/s/nij2oa2rp7ijaaj/commuter_rail.geojson?dl=1",
method = "auto", mode = "wb", destfile = "commuter_rail.json")
commuterLines <- readOGR("commuter_rail.json",
"OGRGeoJSON")
# Straight R Plot - Looks good
plot(commuterLines)
# Plot using leaflet - Some lines are missing!
leaflet() %>% addPolylines(data = commuterLines)
UPDATE:
Here's the reproducible example running as a shiny app, hosted at shinyapps.io, and showing the weird leaflet behavior: https://havb.shinyapps.io/leaflet_example/
UPDATE: the problem seems to be a bug in an older version of the leaflet package available from CRAN. Installing the latest development version from Github resolves the issue.
I don't have enough rep to comment, but I tried your code and it worked for me:
Perhaps it has something to do with your local configuration? Have you tried reinstalling the leaflet package?

Output from 'choice' in R's kml

I'm having trouble getting 'choice' to create output. When the graphical interface launches, I am selecting a partition with the space bar. This creates a black circle around the partition, indicating it has been selected. When I click 'return', nothing happens.
I checked my working directory to look for the output files, but they are not there. I used getwd() to ensure that I have the correct setwd(). No dice.
There was a similar question posted: Exporting result from kml package in R; however, the answer does not work for me.
Any suggestions? I am using R 3.1.0 GUI Mavericks build(6734) and XQuartz 2.7.6. Thanks for any help getting this working.
Here is my code:
setwd("/Users/eightfrench")
mydata <- read.csv("hcris_long3.csv")
cldHCRIS <- clusterLongData(traj=mydata)
kml(cldHCRIS,nbClusters=2:4,nbRedrawing=2,toPlot="both")
X11(type="Xlib")
choice(cldHCRIS, typeGraph= "bmp")
I had the same problem when using RStudio and i solved it just opening the x11 device with no specification. Like this:
setwd("/Users/eightfrench")
mydata <- read.csv("hcris_long3.csv")
cldHCRIS <- clusterLongData(traj=mydata)
kml(cldHCRIS,nbClusters=2:4,nbRedrawing=2,toPlot="both")
X11()
choice(cldHCRIS, typeGraph= "bmp")
Hope this also works for you.
The new version of kml (2.3) fix that, it provide a key to export data with linux as well (not "return" since getGraphicsEvent does not accept "return" using linux, so I have to map another key : 'm')
Christophe

GoogleVis, Geomap Plot error

When I want to create the map using the gvisGeoMap() from googleVis, I get error:
## Using the google visualization API with R
library(googleVis)
input<- read.csv("data.csv")
select<- input[which(input$Subgroup=="Total 5-14"),]
select<- input[which(input$Subgroup=="Total 5-14 yr"),]
Map<- data.frame(select$Country.or.Area, select$Value)
names(Map)<- c("Country", "Percentage")
Geo=gvisGeoMap(Map, locationvar="Country", numvar="Percentage",
options=list(height=350, dataMode='regions'))
plot(Geo)
#starting httpd help server ... done
#Error in ifelse(interactive(), getOption("browser"), "false") :
#replacement has length zero
The above is the error in the "RGui". The error message in "RStudio" is differet:
#object of type 'closure' is not subsettable
and the browser does not fire at all. The HTTP server works fine since I can simply call help pages.( for example ?googleVis will fire up the browser and give the help page). The "Geo" object in the code above is fine and contains the html code only that the plot() does not do what it is supposed to do (I can manually run the html file in the temp folder and see the results). The example above is available here.
I would appreciate your clues.
Thank you
This is the result of correspondance with the the Authors of the packages. It seems that there was a bug that prevented the plot to work properly. The released a new version. You can find the link below.
TQ
.....
Yesterday evening I realised that with version 0.3.0 of googleVis I unfortunately introduced a bug in RStudio and R on Windows.
The bug has been fixed already and a new version (0.3.1) is available from our project site (here is the link), but not on CRAN yet.
I have put a note on my blog (here is the link) to inform others as well.
I hope this helps.
Best regards
Markus

Resources