安装地理空间库

地理空间库

GeoDjango 使用和/或提供以下开源地理空间库的接口:

程序

描述

必须的

支持的版本

GEOS

几何引擎开源

3.14, 3.13, 3.12, 3.11, 3.10, 3.9, 3.8

PROJ

制图投影库

是(仅 PostgreSQL 和 SQLite)

9.x, 8.x, 7.x, 6.x

GDAL

地理空间数据抽象库

3.11, 3.10, 3.9, 3.8, 3.7, 3.6, 3.5, 3.4, 3.3, 3.2, 3.1

GeoIP

基于 IP 的地理定位库

2

PostGIS

PostgreSQL 的空间扩展

是(仅 PostgreSQL)

3.5, 3.4, 3.3, 3.2, 3.1

SpatiaLite

SQLite 的空间扩展

是(仅 SQLite)

5.1, 5.0, 4.3

请注意,这些库的老版本或最新版本可能也能与 GeoDjango 完全兼容。你的里程数可能会有所不同。

Note

The GeoDjango interfaces to GEOS, GDAL, and GeoIP may be used independently of Django. In other words, no database or settings file required -- import them as normal from django.contrib.gis.

在 Debian/Ubuntu 上,我们建议您安装以下软件包,它们将直接或通过依赖关系安装所需的地理空间库:

$ sudo apt-get install binutils libproj-dev gdal-bin

如果你使用的是 macOSWindows,也请参考特定平台的说明。

从源码构建

When installing from source on UNIX and GNU/Linux systems, please follow the installation instructions carefully, and install the libraries in the given order. If using MySQL or Oracle as the spatial database, only GEOS is required.

Note

在 Linux 平台上,在安装每个库之后可能需要运行 ldconfig 命令。例如:

$ sudo make install
$ sudo ldconfig

Note

macOS 用户必须安装 Xcode 才能从源码编译软件。

GEOS

GEOS is a C++ library for performing geometric operations, and is the default internal geometry representation used by GeoDjango (it's behind the "lazy" geometries). Specifically, the C API library is called (e.g., libgeos_c.so) directly from Python using ctypes.

首先,从 GEOS 网站下载 GEOS,并解压源代码存档:

$ wget https://download.osgeo.org/geos/geos-X.Y.Z.tar.bz2
$ tar xjf geos-X.Y.Z.tar.bz2

然后进入 GEOS 目录,创建一个名为 build 的文件夹,并进入其中:

$ cd geos-X.Y.Z
$ mkdir build
$ cd build

然后构建并安装该软件包:

$ cmake -DCMAKE_BUILD_TYPE=Release ..
$ cmake --build .
$ sudo cmake --build . --target install

错误调试

找不到 GEOS 库

当 GeoDjango 找不到 GEOS 时,就会出现这个错误:

ImportError: Could not find the GEOS library (tried "geos_c"). Try setting GEOS_LIBRARY_PATH in your settings.

最常见的解决方案是正确配置你的 库环境配置 在你的配置中设置 GEOS_LIBRARY_PATH

如果使用 GEOS 的二进制包(例如,在 Ubuntu 上),你可能需要 安装 binutils

GEOS_LIBRARY_PATH

If your GEOS library is in a non-standard location, or you don't want to modify the system's library path then the GEOS_LIBRARY_PATH setting may be added to your Django settings file with the full path to the GEOS C library. For example:

GEOS_LIBRARY_PATH = '/home/bob/local/lib/libgeos_c.so'

Note

这个配置必须是 C 共享库的 完整 路径;换句话说,你要使用 libgeos_c.so,而不是 libgeos.so

另见 我的日志中充满了与 GEOS 有关的错误

PROJ

PROJ 是一个用于将地理空间数据转换为不同坐标参考系统的库。

首先,下载 PROJ 源代码:

$ wget https://download.osgeo.org/proj/proj-X.Y.Z.tar.gz

...和数据转换文件(如果使用 PROJ < 7.x,请下载 proj-datumgrid-X.Y.tar.gz)[#]_:

$ wget https://download.osgeo.org/proj/proj-data-X.Y.tar.gz

接下来,解压源代码归档文件,并提取 data 子目录中的基准转换文件。这必须在配置 之前 完成:

$ tar xzf proj-X.Y.Z.tar.gz
$ cd proj-X.Y.Z/data
$ tar xzf ../../proj-data-X.Y.tar.gz
$ cd ../..

对于 PROJ 9.x 及更高版本,发布版仅支持使用 CMake 进行构建(参见 PROJ RFC-7)。

要使用 CMake 进行构建,请确保您的系统满足 构建要求。然后在 PROJ 目录中创建一个名为 build 的文件夹,并进入其中:

$ cd proj-X.Y.Z
$ mkdir build
$ cd build

最后,配置、编译和安装 PROJ:

$ cmake ..
$ cmake --build .
$ sudo cmake --build . --target install

GDAL

GDAL is an excellent open source geospatial library that has support for reading most vector and raster spatial data formats. Currently, GeoDjango only supports GDAL's vector data capabilities [2]. GEOS and PROJ should be installed prior to building GDAL.

首先下载最新版本的 GDAL 发行版并解压存档:

$ wget https://download.osgeo.org/gdal/X.Y.Z/gdal-X.Y.Z.tar.gz
$ tar xzf gdal-X.Y.Z.tar.gz

对于 GDAL 3.6.x 及更高版本,发布版仅支持使用 CMake 进行构建。要使用 CMake 进行构建,请在 GDAL 目录中创建一个名为 build 的文件夹,并进入其中:

$ cd gdal-X.Y.Z
$ mkdir build
$ cd build

最后,配置、编译和安装 GDAL:

$ cmake ..
$ cmake --build .
$ sudo cmake --build . --target install

如果你有任何问题,请参见下面的故障排除部分,以获得建议和解决方案。

错误调试

找不到 GDAL 库

当 GeoDjango 找不到 GDAL 库时,在你的配置中设置你的 库环境配置 GDAL_LIBRARY_PATH

GDAL_LIBRARY_PATH

If your GDAL library is in a non-standard location, or you don't want to modify the system's library path then the GDAL_LIBRARY_PATH setting may be added to your Django settings file with the full path to the GDAL library. For example:

GDAL_LIBRARY_PATH = '/home/sue/local/lib/libgdal.so'

脚注