The rgdal package is a library that provides bindings to GDAL (Geospatial Data Abstraction Library). It can be used to read many different raster and vector file types, including KML. In this post I’ll show how to install GDAL and rgdal on a *nix system such as Ubuntu or CentOS, and how to use rgdal to read a KML file.
Installing with package manager
GDAL can be installed through a package manager like apt-get or yum, but you likely won’t be able to install the latest version. At the time of this writing, the version available in the repositories is 1.11.3 in apt-get and 1.9.2 in yum, but the latest version is 2.1.1. I have run into some issues with 1.11.3 that were not present in 2.1.1, such as there being inconsistency in what to supply for a layer name when reading KML files.
To install GDAL using a package manager, you first need the correct repository configured on your system, which you should already have if you installed R through a package manager. If using Ubuntu, you can find that documentation here.
To install GDAL using using apt-get (if using a Debian-based distribution such as Ubuntu):
sudo apt-get install libgdal-dev gdal-bin libproj-dev
If you’re using an RPM-based distribution such as CentOS or Red Hat, then this may be slightly more involved. You can try the following and see if it works:
sudo yum install gdal
If that doesn’t work, then you may need to follow this guide.
Then we can install the R packages from the R terminal:
> install.packages('sp') > install.packages('rgdal')
Compiling from source
If you need to install a newer version of GDAL, you may need to compile it manually from source.
Before we compile GDAL, we need to install the Expat XML library, otherwise reading KML files will not work.
sudo apt-get install libexpat1-dev
Or, using yum
sudo yum install expat-devel.x86_64
Then we can download, compile, and install GDAL:
wget http://download.osgeo.org/gdal/2.1.1/gdal-2.1.1.tar.gz tar xvf gdal-2.1.1.tar.gz cd gdal-2.1.1 ./configure
It’s important that in the output, we see that “Expat support” is shown as “yes”, otherwise GDAL will not properly load KML files, even though it may compile successfully:
Xerces-C support: no NAS support: no Expat support: yes libxml2 support: no Google libkml support: no
sudo make sudo make install
You can verify the installed version with:
We’ll also need to install PROJ.4. We can use the same approach of compiling from source, since the version in the package manager may be out of date:
wget http://download.osgeo.org/proj/proj-4.9.3.tar.gz tar xvf proj-4.9.3.tar.gz cd proj-4.9.3 ./configure sudo make sudo make install
Now we can install the R packages. We first need to install the “sp” dependency if it is not installed already:
We can attempt to install “rgdal” now, but it’s possible an error will be thrown:
> install.packages('rgdal') ** testing if installed package can be loaded Error in dyn.load(file, DLLpath = DLLpath, ...) : unable to load shared object '/usr/local/lib/R/site-library/rgdal/libs/rgdal.so': libgdal.so.20: cannot open shared object file: No such file or directory Error: loading failed Execution halted ERROR: loading failed
To fix this, we have to find where rgdal.so is located. On my system, it was in /usr/local/lib, so to fix this we can create a file such as /etc/ld.so.conf.d/my.gdal.conf with the contents: /usr/local/lib
Then run this to update the bindings:
Now installing “rgdal” should work.
Verifying that it works
If you need a KML file to test with, you can download a sample file as: https://developers.google.com/kml/documentation/KML_Samples.kml
In order to read the KML file with rgdal, we need to know the name of a layer in the file. Typically the layer names are determined by the contents of the XML file. However, in some instances (depending on the KML file and the version of GDAL installed), the layer name may be the actual name of the file. You can view the layers available in the file by running:
Note: If you get an error with this command saying that it cannot load the file with the available drivers, then it’s possible that you compiled from source and did not have the Expat library installed first.
You should see output similar to this (if using the sample file linked above):
Had to open data source read-only. INFO: Open of `KML_Samples.kml' using driver `KML' successful. 1: Placemarks (3D Point) 2: Highlighted Icon (3D Point) 3: Paths (3D Line String) 4: Google Campus (3D Polygon) 5: Extruded Polygon (3D Polygon) 6: Absolute and Relative (3D Polygon)
Then in R, we load the ‘rgdal’ library:
> library(rgdal) Loading required package: sp rgdal: version: 1.1-10, (SVN revision 622) Geospatial Data Abstraction Library extensions to R successfully loaded Loaded GDAL runtime: GDAL 2.1.1, released 2016/07/07 Path to GDAL shared files: /usr/local/share/gdal 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-3
We can then attempt to load the KML file:
> paths = readOGR(dsn = 'KML_Samples.kml', layer='Paths')
You should get this output:
OGR data source with driver: KML Source: 'KML_Samples.kml', layer: 'Paths' with 6 features It has 2 fields Warning message: In readOGR(dsn = 'KML_Samples.kml', layer = 'Paths') : Z-dimension discarded
Documentation on the rgdal API can be found here.
Click here to view my post on Amazon Redshift.
This is awesome!
LikeLiked by 1 person
Thank you so much bro!
[…] is 1.11.3, this causes installation of rGDAL to fail. Here is the solution that worked for me (from this […]