Leah Wasser
Leah Wasser

Reputation: 717

Binding GDAL with R on OSX / MAC -- can't open / explore h4 files in R

I've somehow messed up a gdal install on my macbook. Previously code worked to open and manipulate H4 files (.hdf) in R. And the same code works on my desktop. However NOW, the code works on my desktop and not my laptop. Same code. Same data.

I suspect this has something to do with trying to install gdal via conda for python support.

Now if I run gdalinfo on the hdf file at the command line in Bash - it reads the file just fine. However, if I run this code in R - using the GDALutils function get_subdatasets():

get_subdatasets("MOD09GA.A2016199.h09v05.006.2016201065406.hdf")

it returns the error:

ERROR 4: `data/week6/modis/reflectance/17_july_2016/MOD09GA.A2016199.h09v05.006.2016201065406.hdf' not recognised as a supported file format.

gdalinfo failed - unable to open 'data/week6/modis/reflectance/17_july_2016/MOD09GA.A2016199.h09v05.006.2016201065406.hdf'.
Error in split1[[1]] : subscript out of bounds
In addition: Warning message:
running command '"/usr/local/Cellar/gdal/1.11.5_3/bin/gdalinfo" "data/week6/modis/reflectance/17_july_2016/MOD09GA.A2016199.h09v05.006.2016201065406.hdf"' had status 1 

```

similarly using another package - rgdal I run the function, gdalinfo()

Warning message: running command '"/usr/local/Cellar/gdal/1.11.5_3/bin/gdalinfo" "data/week6/modis/reflectance/17_july_2016/MOD09GA.A2016199.h09v05.006.2016201065406.hdf"' had status 1

I realize these are two different packages and associated functions.

I found this post:

but the code gdal_chooseInstallation(hasDrivers=c("HDF4","HDF5"))

returns

Error in gdal_chooseInstallation(hasDrivers = c("HDF4", "HDF5")) : No installations match.

NOTE: Interestingly this same code on my desktop (that is able to read the files in R) also returns an error even though it can find the drivers).

Naturally I know that the drivers exist on my computer because I am able to explore h4 files at the command line outside of R. And I know my code is just fine because it works on my desktop on the same data with the same directory structure.

Any ideas how I can clean my gdal slate on my mac to remove potential conflicts OR to direct R to the correct gdal version (i am not sure what that version is however!)? This is something specific associated with binding gdal to R that i'm not sure how to tackle.

UPDATE: Running this in R:

system("gdalinfo --version")

Returns:

GDAL 1.11.5, released 2016/07/01

Yet at the command line

gdalinfo --version

Returns

GDAL 2.1.0, released 2016/04/25

Thank you for any leads... Leah

UPDATES: i have finally figured out how to link and unlink gdal 2 vs 2 and i have figured out how to use brew to install hdf4 however at this point, i can't reinstall gdal2

I am using the tutorial here:

https://www.karambelkar.info/2016/10/gdal-2-on-mac-with-homebrew/

This worked yesterday but then i needed to install the h4 libraries to i removed gdal2

brew remove gdal2
# install h4 libraries
brew install hdf4 

And then tried to reinstall following the directions in the tutorial.

Here is what i get at this point

$ brew install gdal2 --with-armadillo --with-complete --with-libkml --with-unsupported

OUTPUT:

==> Installing gdal2 from osgeo/osgeo4mac
Warning: Use cfitsio instead of deprecated homebrew/science/cfitsio
Warning: Use cfitsio instead of deprecated homebrew/science/cfitsio
Warning: Use cfitsio instead of deprecated homebrew/science/cfitsio
Warning: Use cfitsio instead of deprecated homebrew/science/cfitsio
Warning: Use cfitsio instead of deprecated homebrew/science/cfitsio
Warning: Use cfitsio instead of deprecated homebrew/science/cfitsio
Warning: Use cfitsio instead of deprecated homebrew/science/cfitsio
Warning: Use cfitsio instead of deprecated homebrew/science/cfitsio
Warning: Use cfitsio instead of deprecated homebrew/science/cfitsio
Warning: Use cfitsio instead of deprecated homebrew/science/cfitsio
Warning: Use cfitsio instead of deprecated homebrew/science/cfitsio
Warning: Use cfitsio instead of deprecated homebrew/science/cfitsio
Warning: Use cfitsio instead of deprecated homebrew/science/cfitsio
Warning: Use cfitsio instead of deprecated homebrew/science/cfitsio
Warning: Use cfitsio instead of deprecated homebrew/science/cfitsio
Warning: Use cfitsio instead of deprecated homebrew/science/cfitsio
Warning: Use cfitsio instead of deprecated homebrew/science/cfitsio
Warning: Use cfitsio instead of deprecated homebrew/science/cfitsio
Warning: Use cfitsio instead of deprecated homebrew/science/cfitsio
Warning: Use cfitsio instead of deprecated homebrew/science/cfitsio
Warning: Use cfitsio instead of deprecated homebrew/science/cfitsio
Warning: Use cfitsio instead of deprecated homebrew/science/cfitsio
Warning: Use cfitsio instead of deprecated homebrew/science/cfitsio
Warning: Use cfitsio instead of deprecated homebrew/science/cfitsio
Warning: Use cfitsio instead of deprecated homebrew/science/cfitsio
Warning: Use cfitsio instead of deprecated homebrew/science/cfitsio
Warning: Use cfitsio instead of deprecated homebrew/science/cfitsio
Warning: Use cfitsio instead of deprecated homebrew/science/cfitsio
Warning: Use cfitsio instead of deprecated homebrew/science/cfitsio
Warning: Use cfitsio instead of deprecated homebrew/science/cfitsio
Warning: Use cfitsio instead of deprecated homebrew/science/cfitsio
Warning: Use cfitsio instead of deprecated homebrew/science/cfitsio
Warning: Use cfitsio instead of deprecated homebrew/science/cfitsio
==> Using Homebrew-provided fortran compiler.
This may be changed by setting the FC environment variable.
==> Downloading http://download.osgeo.org/gdal/2.2.1/gdal-2.2.1.tar.gz
Already downloaded: /Users/leah-su/Library/Caches/Homebrew/gdal2-2.2.1.tar.gz
==> ./configure --prefix=/usr/local/Cellar/gdal2/2.2.1 --mandir=/usr/local/Cella
Last 15 lines from /Users/leah-su/Library/Logs/Homebrew/gdal2/01.configure:
checking for SDget_maxopenfiles in -lmfhdf... yes
checking for H5Fopen in -lhdf5... yes
checking for kea-config... no
checking for kea... no
checking for nc-config... /usr/local/bin/nc-config
checking libnetcdf compiler and linker flags with nc-config... 
 got version="netCDF 4.4.1.1", prefix="/usr/local/Cellar/netcdf/4.4.1.1_6",
 libs="-L/usr/local/Cellar/netcdf/4.4.1.1_6/lib -lnetcdf -lhdf5_hl -lhdf5 -lsz -lz -ldl -lm -lcurl", includedir="/usr/local/Cellar/netcdf/4.4.1.1_6/include"
checking for nc_open in -lnetcdf... yes
checking for netcdf-4 (and HDF5) support in libnetcdf... yes
checking for HDF4 support in libnetcdf... no
configure: using JasPer library from /usr/local.
checking for jp2_encode_uuid in -ljasper... no
configure: hacked JasPer version not found, GeoJP2 disabled.
configure: error: openjpeg.h not found in /usr/local/include/openjpeg-2.0 or /usr/local/include/openjpeg-2.1

If reporting this issue please do so at (not Homebrew/brew or Homebrew/core):
https://github.com/osgeo/homebrew-osgeo4mac/issues

These open issues may also help:
brew install osgeo/osgeo4mac/gdal2-mrsid fails https://github.com/OSGeo/homebrew-osgeo4mac/issues/250
Failed to build gdal2 with common flags https://github.com/OSGeo/homebrew-osgeo4mac/issues/245
Cannot upgrade gdal2 on OS X 10.12.6 https://github.com/OSGeo/homebrew-osgeo4mac/issues/252
Please add --without-pdfium option to gdal2 formula https://github.com/OSGeo/homebrew-osgeo4mac/issues/220
Using osgeo4mac's gdal2 with homebrew core's postgis https://github.com/OSGeo/homebrew-osgeo4mac/issues/185
gdal1-mrsid and gdal2-mrsid fails to build https://github.com/OSGeo/homebrew-osgeo4mac/issues/213

```

Upvotes: 2

Views: 1069

Answers (1)

Leah Wasser
Leah Wasser

Reputation: 717

Ok... for the MAC users out there... I have finally resolved this issue. If you go to the brew formula code you will notice that hdf4 is NOT supported when you install gdal via brew.

This is what I was encountering in my code. I think similarly the conda build may also be similar and the osgeo/4mac too.

https://github.com/Homebrew/homebrew-core/blob/master/Formula/gdal.rb

https://github.com/OSGeo/homebrew-osgeo4mac

When you install gdal -- R looks for the most RECENTLY installed version and uses that by default. However, you can specify which build you want in the .bash_profile document. Also the King Chaos distribution - which has gdal 2.1 DOES support h4. So what i did:

  1. Head over to KyngChaos and get the GDAL 2.1 complete distribution. revel in the cutting edge website design associated with this site :)
  2. Install gdal complete 2.1 on your mac
  3. Next, open your .bash_profile via shell (it's a hidden file)

```

 # go to your home directory in bash
    cd  
    # open .bash_profile using your favorite text editor and whatever shortcut you have setup
    vim .bash_profile
    # add path to gdal to your profile (copy and paste or do it via shell)
    export PATH=/Library/Frameworks/GDAL.framework/Programs:$PATH
    # save and close 

```

Finally -- clean things up in R. if you're using gdalUtils and rgdal be sure to REMOVE BOTH. delete them then reinstall. Restart R.

Everything should work now. Hope this helps someone else with the same issue.

Upvotes: 2

Related Questions