Cannot load shapefile with readOGR - ogrinfo: all features NULL - r

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"

Related

Does installing the package rgdal also install GDAL on your computer?

When I look at the documentation of the package rgdal it says
"The R contributed package rgdal is available from CRAN as a source package for installation on platforms with the full build train and external dependencies."
GDAL is one of the external dependencies, so I though this meant that it would download off CRAN and be installed along with the package.
When I load the package rgdal into my R workspace, I get this message:
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/andersonch/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/andersonch/Documents/R/win-library/3.5/rgdal/proj
Linking to sp version: 1.4-1
Again, I would have thought that means that there is a valid version of GDAL that the package is connecting to.
But when I try to use functions from the gdalUtils package, I get this warning message:
Warning messages:
1: In gdal_setInstallation(ignore.full_scan = ignore.full_scan, verbose = verbose) :
No GDAL installation found. Please install 'gdal' before continuing:
- www.gdal.org (no HDF4 support!)
- trac.osgeo.org/osgeo4w/ (with HDF4 support RECOMMENDED)
- www.fwtools.maptools.org (with HDF4 support)
2: In gdal_setInstallation(ignore.full_scan = ignore.full_scan, verbose = verbose) :
If you think GDAL is installed, please run:
gdal_setInstallation(ignore.full_scan=FALSE)"
Does this mean that I need to install gdal separately?

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!

Missing projection file in R's rgdal package for geospatial analysis

I'm having a problem I can't solve with rgdal in which the projection file seems to be
missing. This is the error I get:
proj4string(d) <- CRS("+init=epsg:28992")
Error in CRS("+init=epsg:28992") : no system list, errno: 2
It's confusing because when I load the package, it seems to successfully
autodetect the PROJ.4 files:
> library(rgdal)
rgdal: version: 0.9-2, (SVN revision 526)
Geospatial Data Abstraction Library extensions to R successfully loaded
Loaded GDAL runtime: GDAL 1.11.2, released 2015/02/10
Path to GDAL shared files:
Loaded PROJ.4 runtime: Rel. 4.9.1, 04 March 2015, [PJ_VERSION: 491]
Path to PROJ.4 shared files: (autodetected)
But when I do this, it returns zero:
.Call("PROJcopyEPSG", tempfile(), PACKAGE = "rgdal")
The only answer I've found from googling this and searching SO is that epsg has to be lowercase, which it is in my code.
I'm on a Mac running Yosemite, with R 3.2.0, and working inside RStudio
(version 0.99.441). Other packages and versions from my session info:
rgeos_0.3-8 ggmap_2.4 ggplot2_1.0.1 rgdal_0.9-2
leaflet_0.0.15 tidyr_0.2.0 dplyr_0.4.1
UScensus2010blk_1.00 UScensus2010_0.11 foreign_0.8-63
maptools_0.8-36 sp_1.1-0
I had a quick Google and came across this post which suggests this was an OS X error where the packages were built incorrectly.
On my Mac system, I wasn't able to reproduce your error, so for what it's worth I installed my versions of rgdal and rgeos from http://www.kyngchaos.com/software:frameworks Maybe you can reinstall your versions of these packages and see if this resolves the issue? I would also update sp at the same time.
As a workaround, you might be able to specify the projection manually. The proj string for your projection can be obtained from: http://spatialreference.org/ref/epsg/amersfoort-rd-new/
So, for example, instead of:
proj4string(d) <- CRS("+init=epsg:28992")
you could specify:
proj4string(d) <- CRS("+proj=sterea +lat_0=52.15616055555555 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +units=m +no_defs ")
Not an answer, maybe, but worth a try? Good luck.

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)

Error importing OSM map tiles into R in Windows

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.

Resources