Python中安装GDAL

Posted 大作家佚名

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python中安装GDAL相关的知识,希望对你有一定的参考价值。

Python中安装GDAL

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,应该就好了。

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(最简单,最详细图文教程)

以上是关于Python中安装GDAL的主要内容,如果未能解决你的问题,请参考以下文章

pyrhon中安装django和在cmd中安装django有啥区别?

在windows中安装python

在 Python 中安装包

如何在python中安装mysqlclient?

linux中安装python

无法在python 3.x中安装wsgiref。