Python中安装GDAL

4 篇文章 1 订阅

1. 安装步骤

方法1更简单,但是也可能出现较多问题,方法2更直观一次成功。

1.1 方法1

步骤1,终端/命令行中输入:

pip install gdal

如果出现错误:

Traceback (most recent call last):
    File "/tmp/pip-install-h_n_kokk/gdal_0c5c253514974d76a20d2183c5b4798d/setup.py", line 188, in get_gdal_config
      return fetch_config(option, gdal_config=self.gdal_config)
    File "/tmp/pip-install-h_n_kokk/gdal_0c5c253514974d76a20d2183c5b4798d/setup.py", line 90, in fetch_config
      raise gdal_config_error(e)
  __main__.gdal_config_error: [Errno 2] No such file or directory: 'gdal-config': 'gdal-config'
  
  During handling of the above exception, another exception occurred:

步骤2,安装 libgdal-dev:

 sudo apt install libgdal-dev

重复步骤1,如果出现错误:

extensions/gdal_wrap.cpp:48261:97: error: ‘GVOT_MIN_TARGET_HEIGHT_FROM_DEM’ was not declared in this scope
       SWIG_Python_SetConstant(d, "GVOT_MIN_TARGET_HEIGHT_FROM_DEM",SWIG_From_int(static_cast< int >(GVOT_MIN_TARGET_HEIGHT_FROM_DEM)));
                                                                                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    extensions/gdal_wrap.cpp:48262:100: error: ‘GVOT_MIN_TARGET_HEIGHT_FROM_GROUND’ was not declared in this scope
       SWIG_Python_SetConstant(d, "GVOT_MIN_TARGET_HEIGHT_FROM_GROUND",SWIG_From_int(static_cast< int >(GVOT_MIN_TARGET_HEIGHT_FROM_GROUND)));
                                                                                                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    extensions/gdal_wrap.cpp: At global scope:
    extensions/gdal_wrap.cpp:5383:15: warning: ‘MDArrayReadWriteCheckArguments’ defined but not used [-Wunused-variable]
     static CPLErr MDArrayReadWriteCheckArguments(GDALMDArrayHS* array,
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    extensions/gdal_wrap.cpp:5360:13: warning: ‘CheckNumericDataType’ defined but not used [-Wunused-variable]
     static bool CheckNumericDataType(GDALExtendedDataTypeHS* dt)
                 ^~~~~~~~~~~~~~~~~~~~
    error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /home/zwshi/PycharmProjects/LaserAltimetry/venv/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-rw1cca2p/gdal_71f0a1fbeb04448fa91e2007642cbb8d/setup.py'"'"'; __file__='"'"'/tmp/pip-install-rw1cca2p/gdal_71f0a1fbeb04448fa91e2007642cbb8d/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-mcpr3nb_/install-record.txt --single-version-externally-managed --compile --install-headers /home/zwshi/PycharmProjects/LaserAltimetry/venv/include/site/python3.6/gdal Check the logs for full command output.

步骤3,安装gdal的依赖库:

sudo apt-get install gdal-bin libgdal-dev python3-gdal

然后,重复步骤1,应该就好了。

1.2 方法2

GDAL官网下载,下载地址:https://www.lfd.uci.edu/~gohlke/pythonlibs/#gdal
目前官网提供以下版本的GDAL库,win32代表32位系统,amd64代表64位操作系统,cp36代表你装的Python是3.6版本的,依次类推。
在这里插入图片描述

如果Python解释器已经安装,然后Windows在cmd中,cd到GDAL-3.4.3-cp38-cp38-win32.whl所在文件夹,输入命令:

pip install GDAL-3.4.3-cp38-cp38-win32.whl

我安装了Anaconda,在启动程序里,选择Anaconda prompt(myenv),然后cd到GDAL-3.4.3-cp38-cp38-win32.whl所在文件夹,然后输入命令:

pip install GDAL-3.4.3-cp38-cp38-win32.whl

下面命令是不行的

conda install GDAL-3.4.3-cp38-cp38-win32.whl

2. 测试

测试:

from __future__ import print_function
from osgeo import gdal
print("GDAL's version is:" + gdal.__version__)
print(gdal)

输出:

GDAL's version is:2.2.3
<module 'osgeo.gdal' from '/usr/lib/python3/dist-packages/osgeo/gdal.py'>

3. 参考

[1] ChatGPT. www.openai.com
[2] GDAL for python 教程
[3] 在Python中安装GDAL(最简单,最详细图文教程)
[4] Python配置和安装gdal库(Windows下)

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值