Compiling the gdal-grass plugin for GRASS 7

The idea of the GDAL-GRASS plugin is to directly access GRASS raster and vector data from outside. Any GDAL enabled software (QGIS, R, …) can read and write through the plugin from the GRASS database. The different steps are explained on the grass wiki. Mostly as a note to self, I am copying those steps below (for GRASS 7), with some small changes that made this work for me.

Compilation and Installation of Prerequisites

The install order should be kept due to inter-dependencies between the packages. Please keep the order or install all in a single step:

  • PROJ4 with Datum grids file proj-datumgrid-1.X.zip (expand in nad/ before compiling)
  • Optional: GEOS
  • Optional: PostgreSQL, mySQL, unixODBC, SQLite (SQLite is needed for QGIS)
  • GDAL/OGR compiled without GRASS support (because we use the plugin!)
  • GRASS GIS (to state the obvious)

Note that the plugin needs to find the GRASS GIS libraries. The best way is to add a text file ‘grass7.conf’ into the directory /etc/ld.so.conf.d/ (edit as root) which contains the path to the GRASS libraries (e.g., /usr/local/grass7/grass-7.1.svn/lib); then run ‘ldconfig’. You can do this using your favourite text editor or on the command line (see below).

sudo sh -c "echo /usr/local/grass7/grass-7.1.svn/lib > /etc/ld.so.conf.d/grass7.conf"
cd /etc/ld.so.conf.d/
sudo ldconfig

In the README file it reads that you may need to provide the full path to the library location of proj (often /usr/local/lib), geos-config and gdal-config as well. If so, you can do this in a similar way as we just did for GRASS.

Get the GDAL/OGR-GRASS plugin

You can get the plugin from http://download.osgeo.org/gdal/. Get the latest plugin that fits with your GDAL install. If you have GDAL 2.0, you should download gdal-grass-2.0.0.tar.gz.

Compile the plugin

Now you can compile the addon. Note that if you have compiled GDAL (and GRASS) with PostgreSQL support, you need to provide the path here as well (as in the example below). Below I assume you have unzipped the file with the source code in your home directory. The paths to GDAL, GRASS and Postgres may differ on your computer.

cd gdal-grass-2.0.0/
./configure \
--with-gdal=/usr/local/gdal/bin/gdal-config \
--with-grass=/usr/local/grass7/grass-7.1.svn \
--with-postgres-includes=/usr/include/postgresql
make
sudo make install

Testing the plugin

To test if all is installed correctly, you can for example run:

gdalinfo grassdata/nc_basic_spm_grass7/PERMANENT/vector/boundary_region/head

where grassdata represents the path to your GRASS GIS database with the North Carolina sample database. Or use the path to a vector layers in your own GRASS GIS database.

You should get something like:

Warning 1: GRASS warning: GISBASE environment variable was not set, using:
/usr/local/grass7/grass-7.1.svn
INFO: Open of `/home/paulo/Data/GRASSdb/nc_basic_spm_grass7/PERMANENT/vector/boundary_region/head'
using driver `OGR_GRASS' successful.
1: 0 (Line String)
2: 1 (Polygon)

To test if GDAL can read a GRASS raster layer, run something like:

gdalinfo grassdata/nc_basic_spm_grass7/PERMANENT/cellhd/elevation

This should give you something like:

Warning 1: GRASS warning: GISBASE environment variable was not set, using:
/usr/local/grass7/grass-7.1.svn
Driver: GRASS/GRASS Rasters (5.7+)
Files: /home/paulo/Data/GRASSdb/nc_basic_spm_grass7/PERMANENT/cellhd/elevation
Size is 1500, 1350
Coordinate System is:

Trouble shooting

After running ‘sudo make install’ I got the following error message:

[install -d /usr/local/gdal/lib/gdalplugins
cp gdal_GRASS.so /usr/local/gdal/lib/gdalplugins
cp ogr_GRASS.so /usr/local/gdal/lib/gdalplugins
test -d /usr/local/share/gdal/grass || mkdir /usr/local/share/gdal/grass
mkdir: cannot create directory ‘/usr/local/share/gdal/grass’: No such file or directory
make: *** [install] Error 1

This is something that has come up before on the GRASS mailing list; apparently the issue is that the /usr/local/gdal/ directory didn’t exist yet, and “mkdir without -p can’t create two directories at once”. To solve this, create the directory /usr/local/gdal before running make install:

sudo mkdir -p /usr/local/share/gdal

One thought on “Compiling the gdal-grass plugin for GRASS 7

  1. Pingback: Compiling the gdal-grass plugin for GRASS 7 | GeoNe.ws

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s