安装地理空间库Link to this heading

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

程序

描述

必须的

支持的版本

GEOS

几何引擎开源

3.9, 3.8, 3.7, 3.6, 3.5

PROJ

制图投影库

是(仅 PostgreSQL 和 SQLite)

7.x. 6.x, 5.x, 4.x

GDAL

地理空间数据抽象库

3.2, 3.1, 3.0, 2.4, 2.3, 2.2, 2.1, 2.0

GeoIP

基于 IP 的地理定位库

2

PostGIS

PostgreSQL 的空间扩展

是(仅 PostgreSQL)

3.0, 2.5, 2.4, 2.3

SpatiaLite

SQLite 的空间扩展

是(仅 SQLite)

4.3

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

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

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

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

从源码构建Link to this heading

在 UNIX 和 GNU/Linux 系统上从源码安装时,请仔细按照安装说明,并按照给定的顺序安装库。 如果使用 MySQL 或 Oracle 作为空间数据库,则只需要 GEOS。

GEOSLink to this heading

GEOS 是一个用于执行几何运算的 C++ 库,是 GeoDjango 默认使用的内部几何表示法(它的背后是 “惰性” 的几何图形)。 具体来说,在 Python 中使用 ctypes 直接调用 C API 库(如 libgeos_c.so)。

首先,从 GEOS 网站下载 GEOS,并解压源文件:

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

接下来,切换到解压 GEOS 的目录下,运行配置脚本、编译、安装:

Code
$ cd geos-X.Y.Z
$ ./configure
$ make
$ sudo make install
$ cd ..

错误调试Link to this heading

找不到 GEOS 库Link to this heading

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

Text
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_PATHLink to this heading

如果你的 GEOS 库在非标准位置,或者你不想修改系统的库路径,那么可以在你的 Django 配置文件中添加 GEOS_LIBRARY_PATH 配置,并将 GEOS C 库的完整路径添加进去。 例如:

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

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

PROJLink to this heading

PROJ is a library for converting geospatial data to different coordinate reference systems.

First, download the PROJ source code:

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

... and datum shifting files (download proj-datumgrid-X.Y.tar.gz for PROJ < 7.x) [1]:

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

Next, untar the source code archive, and extract the datum shifting files in the data subdirectory (use nad subdirectory for PROJ < 6.x). This must be done prior to configuration:

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

Finally, configure, make and install PROJ:

Code
$ ./configure
$ make
$ sudo make install
$ cd ..

GDALLink to this heading

GDAL 是一个优秀的开源地理空间库,它支持读取大多数矢量和栅格空间数据格式。目前 GeoDjango 只支持 GDAL 的矢量数据 的功能 [2] 。 在构建 GDAL 之前,应该安装 GEOSPROJ

首先下载最新的 GDAL 版本,解压压缩包:

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

配置、编译和安装:

Code
$ ./configure
$ make # Go get some coffee, this takes a while.
$ sudo make install
$ cd ..

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

错误调试Link to this heading

找不到 GDAL 库Link to this heading

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

GDAL_LIBRARY_PATHLink to this heading

如果你的 GDAL 库在非标准位置,或者你不想修改系统的库路径,那么可以在你的 Django 配置文件中添加 GDAL_LIBRARY_PATH 设置,并添加 GDAL 库的完整路径。 例如:

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

脚注