在OSX / MAC上绑定GDAL和R - 无法打开/浏览R中的h4文件

时间:2017-08-09 19:04:11

标签: r macos gis gdal

我以某种方式搞砸了我的macbook上的gdal安装。以前的代码可以在R中打开和操作H4文件(.hdf)。相同的代码在我的桌面上运行。但是现在,代码可以在我的桌面上运行,而不是我的笔记本电脑。相同的代码。相同的数据。

我怀疑这与尝试通过gdal安装conda以获得python支持有关。

现在如果我在Bash的命令行上运行hdf文件上的gdalinfo - 它就可以正常读取文件了。但是,如果我在R中运行此代码 - 使用GDALutils函数get_subdatasets():

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

它返回错误:

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 

```

同样使用另一个包 - rgdal我运行函数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

我意识到这是两个不同的包和相关的功能。

我找到了这篇文章:

但是代码gdal_chooseInstallation(hasDrivers=c("HDF4","HDF5"))

返回

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

注意:有趣的是,我桌面上的相同代码(能够读取R中的文件)也会返回错误,即使它可以找到驱动程序)。

当然我知道驱动程序存在于我的计算机上,因为我能够在R之外的命令行中探索h4文件。我知道我的代码很好,因为它可以在我的桌面上使用相同的数据运行目录结构。

我有什么想法可以清除我的mac上的gdal slate以消除潜在的冲突或将R指向正确的gdal版本(我不确定那个版本是什么!)?这是将gdal绑定到R的具体问题,我不确定如何处理。

更新: 在R中运行:

system("gdalinfo --version")

返回:

GDAL 1.11.5, released 2016/07/01

然而在命令行

gdalinfo --version

返回

GDAL 2.1.0, released 2016/04/25

感谢您的任何线索...... 利亚

更新: 我终于想出了如何链接和取消链接gdal 2 vs 2,我已经想出如何使用brew安装hdf4但是此时,我无法重新安装gdal2

我在这里使用教程:

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

这在昨天工作但后来我需要安装h4库我删除了gdal2

brew remove gdal2
# install h4 libraries
brew install hdf4 

然后尝试按照教程中的说明重新安装。

这是我现在得到的

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

输出:

==> 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

```

1 个答案:

答案 0 :(得分:1)

好的......对于那里的MAC用户......我终于解决了这个问题。 如果你转到brew公式代码,你会注意到当你通过brew安装gdal时不支持hdf4。

这就是我在代码中遇到的情况。我认为类似的conda构建也可能类似,osgeo / 4mac也是如此。

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

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

安装gdal时 - R会查找最近安装的版本并默认使用它。但是,您可以在.bash_profile文档中指定所需的构建。此外还有King Chaos发行版 - 它有gdal 2.1支持h4。所以我做了什么:

  1. 前往KyngChaos并获得GDAL 2.1完整发布。陶醉于与本网站相关的前沿网站设计:)
  2. 在Mac上安装gdal complete 2.1
  3. 接下来,通过shell打开你的.bash_profile(它是一个隐藏文件)
  4. ```

     # 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 
    

    ```

    最后 - 如果你正在使用gdalUtils和rgdal,请确保在R中清理。删除它们然后重新安装。重新启动R.

    现在一切都应该有效。希望这可以帮助其他人解决同样的问题。