Error working with OpenStreetMap spatial Data

Hi there,
I'm pretty new to RStudio community and RStudio Cloud, so first of all...Congrats and many thanks for this platform is really awesome!

I'm running a basic spatial plot using the function openmap() from the OpenStreetMap package. When asking for downloading data with the aforementioned function, the following error appears:

Error in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl, : java.lang.NoClassDefFoundError: Could not initialize class sun.awt.X11GraphicsEnvironment

I've tried to follow the examples provided in the help menu of the openmap() function and the same messages appears.

The (example) code provided by the help menu of the openmap() function is:

## Not run: 
#show some of the maps available
nm <- c("osm", "maptoolkit-topo", "bing", "stamen-toner", 
		"stamen-watercolor", "esri", "esri-topo", 
		"nps", "apple-iphoto", "skobbler")
par(mfrow=c(3,4))
#Korea
for(i in 1:length(nm)){
	map <- openmap(c(43.46886761482925,119.94873046875),
			c(33.22949814144951,133.9892578125),
		minNumTiles=3,type=nm[i])
plot(map)
}

And my own code is:

Galapagos <- openmap(upperLeft = c(-0.18, -91) , lowerRight = c(-1.0, -89.8), 
zoom = 10, type = "bing")

I would like to know if the problem arises because of some rJava configuration in my laptop and/or in the RStudio Cloud....THANKS!

sessionInfo()
R version 3.6.0 (2019-04-26)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.5 LTS

Matrix products: default
BLAS: /usr/lib/atlas-base/atlas/libblas.so.3.0
LAPACK: /usr/lib/atlas-base/atlas/liblapack.so.3.0

locale:
[1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8
[4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8
[7] LC_PAPER=C.UTF-8 LC_NAME=C.UTF-8 LC_ADDRESS=C.UTF-8
[10] LC_TELEPHONE=C.UTF-8 LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C.UTF-8

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] maps_3.3.0 ggplot2_3.1.1 OpenStreetMap_0.3.3

loaded via a namespace (and not attached):
[1] Rcpp_1.0.1 rstudioapi_0.10 raster_2.8-19 magrittr_1.5
[5] tidyselect_0.2.5 munsell_0.5.0 colorspace_1.4-1 lattice_0.20-38
[9] R6_2.4.0 rlang_0.3.4 plyr_1.8.4 dplyr_0.8.0.1
[13] tools_3.6.0 rgdal_1.4-3 grid_3.6.0 gtable_0.3.0
[17] withr_2.1.2 digest_0.6.18 lazyeval_0.2.2 assertthat_0.2.1
[21] tibble_2.1.1 crayon_1.3.4 rJava_0.9-11 purrr_0.3.2
[25] codetools_0.2-16 glue_1.3.1 labeling_0.3 sp_1.3-1
[29] compiler_3.6.0 pillar_1.3.1 scales_1.0.0 pkgconfig_2.0.2

I don't actually know anything about the OpenStreetMap package, but that error indicates something was attempting to access a display, which doesn't exist, since rstudio.cloud is not running locally but instead on a server in the cloud.

What exactly you trying to get from OpenStreetMap?

The reason I am asking is that there are other OSM related packages, without the troublesome rJava dependency (which I found is best left alone).

I had good results with https://ropensci.github.io/osmdata package - in particular the osmdata::osmdata_sf() function - but I am uncertain whether this will work in your use case.

Thanks josh...any idea to handle it?

Hi jlacho,
The idea is to get a "satellite" view from Galapagos Islands and plot on it the sampling points of my research and paint with a continuous colour gradient values of species diversity (see attached). This work is already done (and finally published!..see here), so the idea is to give this as an example for my students.
I have been looking at the recommended package but I haven't found any good connection with ggplot2 (do you know if can be added to a classical ggplot() and further geom_(s) layers?).
Thanks!

Oki, now I understand. The approach I suggested originally (the osmdata package) is not really appropriate for downloading map backgrounds. It works the best for getting vector representations of points of interest, administrative regions and so on.

For background maps you might get better results with the ggmap package. It should support Open Street Map, but the functionality is unfortunately defunct at the moment.

If you are not dead set on Open Street Map tiles, and can work with other providers - I propose Stamen - then this code might be of use to you. I have verified it on a RStudio server installation, so I expect it to work in your use case as well.

library(ggmap)

galapagos <- get_stamenmap(bbox = c(left = - 91, 
                                    bottom = -1.0, 
                                    right = -89.8, 
                                    top = -0.18),
                           zoom = 10, 
                           maptype = "terrain-background")

points <- data.frame(x = c(-90, -90.5, -90.25), # some random points...
                     y = c(-0.5, -0.8, -0.3))

ggmap(galapagos) + # the base map
  geom_point(data = points, aes(x = x, y = y), 
             color = "red") # standard ggplot2 syntax

1 Like

Many thanks for your reply.
It was a worthwhile exercise, since I thought all the products from ggmap requeried the "(un-)famous" API Key. Now, solving the integration problem with RStudio Cloud ans with the API (problem) with Stamen, I keep plotting as usual!
Many thanks!

Glad to be of service!

I can work with the Google API key (it is not that hard to obtain) but I have serious issues with Google T&C. They in effect prohibit combining Google and non-Google content in a single map.

Stamen basemaps are a good way out, with a much more permissible license.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.