Error importing OSM map tiles into R in Windows - r

This code works fine on a Macbook, but fails on my Windows desktop with the error:
Error in raster(x) :
error in evaluating the argument 'x' in selecting a method for function 'raster': Error: 'merge' is not an exported object from 'namespace:raster'
Any ideas what's causing it?
Full code:
> require(OpenStreetMap)
Loading required package: OpenStreetMap
Loading required package: rJava
Loading required package: raster
Loading required package: sp
Loading required package: rgdal
rgdal: version: 0.8-11, (SVN revision 479M)
Geospatial Data Abstraction Library extensions to R successfully loaded
Loaded GDAL runtime: GDAL 1.9.2, released 2012/10/08
Path to GDAL shared files: C:/Users/Robin/Documents/R/win-library/3.0/rgdal/gdal
GDAL does not use iconv for recoding strings.
Loaded PROJ.4 runtime: Rel. 4.7.1, 23 September 2009, [PJ_VERSION: 470]
Path to PROJ.4 shared files: C:/Users/Robin/Documents/R/win-library/3.0/rgdal/proj
> lat <- c(51.7, 51.3); lon <- c(-0.53, 0.3)
> map <- openproj(openmap(c(lat[1],lon[1]),c(lat[2],lon[2]), 8, 'osm'))
Error in raster(x) :
error in evaluating the argument 'x' in selecting a method for function 'raster': Error: 'merge' is not an exported object from 'namespace:raster'

Its a bug in raster related to the method used to merge tiles - either upgrade or at a pinch use mergeTiles=FALSE.

Related

R package doesn't detect updated version of GDAL

I am trying to shift from raster to the new terra R package, which requires a recent version of GDAL to be installed on my machine (MacOS 10.14.6).
I updated GDAL following the instructions here: http://www.alexchubaty.com/post/2020-01-08-using-latest-gdal-macos-redux/ and it seemed to work. When I run gdalinfo --version in the terminal it displays:
GDAL 3.1.1, released 2020/06/22
But when I load the terra library in R it still seems to link to an old version of GDAL (2.4.2):
> library(terra)
This is terra version 0.7.11 (alpha-release)
NOTE: You are using GDAL version 2.4.2
For full functionality you need at least version 3.0.4
Interestingly, rgdal package seems to detect the updated version:
> library(rgdal)
Loading required package: sp
rgdal: version: 1.5-12, (SVN revision 1018)
Geospatial Data Abstraction Library extensions to R successfully loaded
Loaded GDAL runtime: GDAL 3.1.2, released 2020/07/07
Path to GDAL shared files: /usr/local/Cellar/gdal/3.1.2/share/gdal
GDAL binary built with GEOS: TRUE
Loaded PROJ runtime: Rel. 7.1.0, August 1st, 2020, [PJ_VERSION: 710]
Path to PROJ shared files: /Users/jamesmccarthy/Library/Application Support/proj:/usr/local/opt/proj/share/proj:/usr/local/Cellar/proj/7.1.0/share/proj
PROJ CDN enabled:FALSE
Linking to sp version:1.4-2
To mute warnings of possible GDAL/OSR exportToProj4() degradation,
use options("rgdal_show_exportToProj4_warnings"="none") before loading rgdal.
Attaching package: ‘rgdal’
The following object is masked from ‘package:terra’:
project
Does anyone know how to get terra to link to the most recent version of GDAL? I'm guessing the old version is still installed somewhere on the machine.
I am guessing that you installed rgdal from source, but the binary version of terra from CRAN. So after doing (what you already did) from the command line:
brew install pkg-config
brew install gdal
You should get what you are looking for with the following R command
install.packages("terra", type = "source")
Or, for the cutting edge
remotes::install_github("rspatial/terra")
Good luck with terra!

Connecting rgeos to maptools in R 3.6.0

To implement a choropleth, I looked back to some prompt code which I found on the Internet and read how to use the 'maptools' library to fortify a SpatialPolygonsDataFrame object.
The premise that I have to make is that I installed recently R 3.6.0, also because the new version of 'sp' (necessary to make 'maptools' running correctly) requires it.
From the exploration I did, it turns out that the library 'gpclib' is not anymore available for R, and got substituted by 'rgeos'.
Now, I installed the most recent versions of 'maptools', 'rgeos' and 'rgdal', and in my code I am calling them in the sequence (for some security reasons related to my company, I had to specify a local path for the library):
library(sp)
library(rgdal)
library(ggplot2)
library(rgeos)
library(maptools)
The output for the rgeos library load looks like:
"rgeos version: 0.4-3, (SVN revision 595)
GEOS runtime version: 3.6.1-CAPI-1.10.1
Linking to sp version: 1.3-1
Polygon checking: TRUE "
However when I call maptools, it says:
"Checking rgeos availability: FALSE
Note: when rgeos is not available, polygon geometry computations in maptools depend on gpclib, which has a restricted licence. It is disabled by default;
to enable gpclib, type gpclibPermit()"
So looks like maptools is not connected to rgeos. Consequently, when I try to fortify a SpatialPolygonDataFrame, I would get the error:
"Error in maptools::unionSpatialPolygons(cp, attr[, region]) :
isTRUE(gpclibPermitStatus()) is not TRUE"
Then I am not understanding, neither finding related resources, how to solve this issue.

Error in readOGR(dsn, layer) : could not find function "readOGR"

I am very new to R and so what does this mean? Does this tell me that I need to install some package named "ReadOGR"
I am following along using code written by someone else in an effort to generate a heat map. here is the current section to give some context.
setwd("C:/Users/Someone/Documents/R")
dsn <- "cb_2017_us_state_20m.shp"
layer <- "cb_2017_us_state_20m"
cb5 = readOGR(dsn, layer)
Installed rgdal to no success. Tried library(rgdal)
library(rgdal)
Loading required package: sp
rgdal: version: 1.3-9, (SVN revision 794)
Geospatial Data Abstraction Library extensions to R successfully loaded
Loaded GDAL runtime: GDAL 2.2.3, released 2017/11/20
Path to GDAL shared files: C:/Users/trwilcox/Documents/R/win-
library/3.5/rgdal/gdal
GDAL binary built with GEOS: TRUE
Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
Path to PROJ.4 shared files: C:/Users/someone/Documents/R/win-
library/3.5/rgdal/proj
Linking to sp version: 1.3-1
Warning messages:
1: package ‘rgdal’ was built under R version 3.5.2
2: package ‘sp’ was built under R version 3.5.2
> cb5 = readOGR(dsn, layer)
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv =
use_iconv, :
Cannot open data source
Try:
setwd("C:/Users/Someone/Documents/R")
dsn <- "cb_2017_us_state_20m.shp"
cb5 = readOGR(dsn)
Also, you can find the original files here: https://www.census.gov/geo/maps-data/data/cbf/cbf_state.html. Maybe there are some extra info there you want to use.
You have to use the one with .shp not the shp.xlm If you do not have it, download from the link above!!!
It should work now, if not, happy to help you!

Cannot load shapefile with readOGR - ogrinfo: all features NULL

I'm trying to plot some maps in Ubuntu 18.04 . I've downloaded some shapefiles but cannot read them with "readOGR" function.
I've installed "libdgal" and "libproj-dev".
sudo apt-get install libudunits2-dev libgdal-dev libgeos-dev libproj-dev
But when using:
library(rgdal) # R wrapper around GDAL/OGR
library(ggplot2) # for general plotting
library(ggmap) # for fortifying shapefiles
# First read in the shapefile, using the path to the shapefile and the shapefile name minus the
# extension as arguments
shapefile <- readOGR("/home/ogonzales/Escritorio/maps_in_r/", "BAS_LIM_DEPARTAMENTO")
I get this error:
Error in readOGR("/home/ogonzales/Escritorio/maps_in_r/", "BAS_LIM_DEPARTAMENTO") :
no features found
Además: Warning message:
In ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, :
ogrInfo: all features NULL
This is the message I get after loading "rgdal" package:
library(rgdal) # R wrapper around GDAL/OGR
Loading required package: sp
rgdal: version: 1.3-2, (SVN revision 755)
Geospatial Data Abstraction Library extensions to R successfully loaded
Loaded GDAL runtime: GDAL 2.2.3, released 2017/11/20
Path to GDAL shared files: /usr/share/gdal/2.2
GDAL binary built with GEOS: TRUE
Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
Path to PROJ.4 shared files: (autodetected)
Linking to sp version: 1.2-7
Path of file:
The file's path is correct:
list.files('/home/ogonzales/Escritorio/maps_in_r/', pattern='\\.shp$') #"BAS_LIM_DEPARTAMENTO.shp"
file.exists('/home/ogonzales/Escritorio/maps_in_r/BAS_LIM_DEPARTAMENTO.shp') #TRUE
UPDATE 1:
Doing:
library(sf)
shapefile <- sf::st_read("/home/ogonzales/Escritorio/maps_in_r/BAS_LIM_DEPARTAMENTO.shp")
Returns:
Cannot open data source /home/ogonzales/Escritorio/maps_in_r/BAS_LIM_DEPARTAMENTO.shp
Error in CPL_read_ogr(dsn, layer, as.character(options), quiet, type, :
Open failed.
UPDATE 2:
rgdal::ogrListLayers("/home/ogonzales/Escritorio/maps_in_r/BAS_LIM_DEPARTAMENTO")
Returns:
Error in rgdal::ogrListLayers("/home/ogonzales/Escritorio/maps_in_r/BAS_LIM_DEPARTAMENTO") :
Cannot open data source
And:
sf::st_layers("/home/ogonzales/Escritorio/maps_in_r/BAS_LIM_DEPARTAMENTO")
Cannot open data source /home/ogonzales/Escritorio/maps_in_r/BAS_LIM_DEPARTAMENTO
Error in CPL_get_layers(dsn, options, do_count) : Open failed.
UPDATE 2:
list.files("/home/ogonzales/Escritorio/maps_in_r/")
[1] "BAS_LIM_DEPARTAMENTO.dbf" "BAS_LIM_DEPARTAMENTO.prj" "BAS_LIM_DEPARTAMENTO.sbn"
[4] "BAS_LIM_DEPARTAMENTO.sbx" "BAS_LIM_DEPARTAMENTO.shp" "BAS_LIM_DEPARTAMENTO.shx"
[7] "maps_in_r.Rproj"

Can't open plot window for OpenStreetMap on Mac OS : unable to create quartz() device target

I am using the latest OpenStreetMap library on Mac OS. I am able to download a map, but I execute the plot(map) command, I get an error saying "unable to create quartz() device target, given type may not be supported". Can anyone help?
> library(OpenStreetMap)
Loading required package: rJava
Loading required package: raster
Loading required package: sp
Loading required package: rgdal
rgdal: version: 0.8-16, (SVN revision 498)
Geospatial Data Abstraction Library extensions to R successfully loaded
Loaded GDAL runtime: GDAL 1.10.1, released 2013/08/26
Path to GDAL shared files: /opt/local/share/gdal
Loaded PROJ.4 runtime: Rel. 4.8.0, 6 March 2012, [PJ_VERSION: 480]
Path to PROJ.4 shared files: (autodetected)
> upperLeft <- c(lat= 37.950, lon= -122.553)
> lowerRight <- c(lat= 37.650, lon= -122.250)
> mapMercator = openmap(upperLeft, lowerRight, minNumTiles=9,type="esri")
> map <- openproj(mapMercator)
> plot(map)
Error in (function (title, width, height, pointsize, family, antialias, :
unable to create quartz() device target, given type may not be supported
Interestingly, if I add a single plot(0,0) before the calling openmap(), the map will be displayed but with a warning "Process manager already initialized -- can't fully enable headless mode."
> library(OpenStreetMap)
Loading required package: rJava
Loading required package: raster
Loading required package: sp
Loading required package: rgdal
rgdal: version: 0.8-16, (SVN revision 498)
Geospatial Data Abstraction Library extensions to R successfully loaded
Loaded GDAL runtime: GDAL 1.10.1, released 2013/08/26
Path to GDAL shared files: /opt/local/share/gdal
Loaded PROJ.4 runtime: Rel. 4.8.0, 6 March 2012, [PJ_VERSION: 480]
Path to PROJ.4 shared files: (autodetected)
> plot(0,0)
> upperLeft <- c(lat= 37.950, lon= -122.553)
> lowerRight <- c(lat= 37.650, lon= -122.250)
> mapMercator = openmap(upperLeft, lowerRight, minNumTiles=9,type="esri")
2014-02-20 12:45:46.592 R[2539:607] Process manager already initialized -- can't fully enable headless mode.
> map <- openproj(mapMercator)
> plot(map)

Resources