使用 Jupyter Notebook 导入 GDAL 和 ogr 时遇到问题

Trouble importing GDAL and ogr using Jyupter Notebook

我正在做一个学校项目,对加利福尼亚州的县进行一些空间分析;使用 Jyupter Notebook 运行 Python(2.7) 脚本并创建可视化。

我用conda下载底图后,成功将底图导入我的notebook。我是 Python 的新手,不记得我是如何做到的。我相信我使用了 this website 中的说明。

但是现在,当我试图将我的 shapefile 强加到底图上时,我 运行 出现了这个错误:

ValueError: shapefile must have lat/lon vertices  - it looks like this one has vertices in map projection coordinates. You can convert the shapefile to geographic
coordinates using the shpproj utility from the shapelib tools
(http://shapelib.maptools.org/shapelib-tools.html)

在过去的一个小时里,我一直在寻找转换坐标的方法并学习了 GDAL 和一些命令 ogr2ogr。我已尝试按照 gdal.org 上发布的说明执行 运行 此命令:conda install -c conda-forge gdal。 conda 和 conda-forge 之间有区别吗?我已经尝试过此命令的许多变体,例如:conda install -c anaconda2 gdalinstall conda gdal。没有任何效果,因为当我回到我的笔记本并尝试 import gdalimport ogr 时,我收到如下错误:

ImportError: dlopen(/anaconda2/lib/python2.7/site-packages/osgeo/_gdal.so, 2): Library not loaded: @rpath/libgif.7.dylib
  Referenced from: /anaconda2/lib/libgdal.20.dylib
  Reason: image not found

我对 conda 的工作原理、conda 和 conda-forge 之间的区别以及我下载的所有这些东西的去向感到非常困惑。任何人都可以帮助我了解正在发生的事情以及我做错了什么吗?

首先,提问时请说明你的环境:OS、conda版本(conda info)、Python版本等

什么是频道

conda 是一个 Python 环境管理器和包管理器。要安装包,conda 必须从某处下载包。 conda获取包列表的地方叫做通道。

默认情况下,名为mainr 的两个频道是启用的,这两个频道都由公司自己的Anaconda 维护。 conda-force 是第三方社区的频道。

Quote from conda-force documentation

What is conda-forge?

conda-forge is a community effort that provides conda packages for a wide range of software.

在命令行中使用 -c channelname 指定频道,--channel 的快捷方式。

如何解决您的问题

您的问题是由一个包的混合渠道使用引起的。 gdal 被指定为从 conda-forge 安装,而不是其依赖项。由于未指定其依赖项的通道,因此将使用默认通道 mainr

要修复混合依赖使用,请启用 "strict" 通道优先级。

conda config --set channel_priority strict

这使得 --channel 指定的频道优先于其他频道。

channel_priority (ChannelPriority)

With strict channel priority, packages in lower priority channels are not considered if a package with the same name appears in a higher priority channel.

然后重新创建环境并安装gdal

conda create --name test4gdal python=2.7
conda activate test4gdal
conda install -c conda-forge gdal

这次,所有gdal相关的包都是从conda-forge安装的。

参考资料