GeoDjango のインストール¶
概要¶
通常、GeoDjango のインストールには次のものが必要です:
各要件の詳細とインストール手順については、以下のセクションを参照してください。また、プラットフォーム別の手順もあります:
ソースを使う
GeoDjango はオープンソースの地理空間ソフトウェア技術を最新のものを活用しているため、最新バージョンのライブラリが必要です。プラットフォームにバイナリパッケージが提供されていない場合は、ソースからのインストールが必要になるかもしれません。ソースからライブラリをコンパイルする際には、特に初心者の場合は指示に注意してください。
必要条件¶
Python と Django¶
GeoDjango は Django に同梱されていますので、インストール方法の詳細は Django の インストール手順 を参照してください。
空間データベース (Spatial database)¶
現在サポートされている空間データベースは、PostgreSQL (with PostGIS)、MySQL、Oracle、SQLite (with SpatiaLite) です。
注釈
PostGISは、最も成熟した機能豊富なオープンソースの空間データベースであるため、推奨されています。
The geospatial libraries required for a GeoDjango installation depends on the spatial database used. The following lists the library requirements, supported versions, and any notes for each of the supported database backends:
データベース |
ライブラリの要件 |
サポートされるバージョン |
備考 |
|---|---|---|---|
PostgreSQL |
GEOS, GDAL, PROJ, PostGIS |
14+ |
PostGIS が必要です。 |
MySQL |
GEOS, GDAL |
8.0.11+ |
|
Oracle |
GEOS, GDAL |
19+ |
XE not supported. |
SQLite |
GEOS, GDAL, PROJ, SpatiaLite |
3.31.0+ |
Requires SpatiaLite 4.3+ |
PostgreSQL/PostGIS/GEOS/GDAL の可能な組み合わせについては、OSGeo Wikiの this comparison matrix も参照してください。
インストール¶
地理空間ライブラリ (Geospatial library)¶
データベースのインストール¶
DATABASES の設定¶
ENGINE 設定を、空間 (spatial) バックエンド の1つに設定してください。
django.contrib.gis を INSTALLED_APPS に追加します。¶
他の Django contrib アプリケーションと同様に、設定の INSTALLED_APPS に django.contrib.gis を追加するだけです。これは gis テンプレートを配置するためです。そうしないと、地理管理や KML サイトマップのような機能が正しく動作しません。
トラブルシューティング¶
問題の解決策がここに見つからない場合は、コミュニティに参加してみてください!以下のようなことができます:
GeoDjango フォーラムで質問してください。
File a ticket on the Django trac if you think there's a bug. Make sure to provide a complete description of the problem, versions used, and specify the component as "GIS".
ライブラリの環境設定¶
GeoDjango をインストールする際に最もよくある問題は、外部の共有ライブラリ (例えば GEOS や GDAL) が見つからないというものです [1] 。通常、この問題の原因は、オペレーティングシステムがソースからビルドされたライブラリがインストールされたディレクトリを認識していないことです。
通常、ライブラリのパスは、個々のユーザーごとに環境変数を設定するか、システム全体のライブラリ パスを構成することで設定できます。
LD_LIBRARY_PATH 環境変数¶
A user may set this environment variable to customize the library paths
they want to use. The typical library directory for software
built from source is /usr/local/lib. Thus, /usr/local/lib needs
to be included in the LD_LIBRARY_PATH variable. For example, the user
could place the following in their bash profile:
export LD_LIBRARY_PATH=/usr/local/lib
システムライブラリのパスを設定する¶
On GNU/Linux systems, there is typically a file in /etc/ld.so.conf, which
may include additional paths from files in another directory, such as
/etc/ld.so.conf.d. As the root user, add the custom library path (like
/usr/local/lib) on a new line in ld.so.conf. This is one example of
how to do so:
$ sudo echo /usr/local/lib >> /etc/ld.so.conf
$ sudo ldconfig
For OpenSolaris users, the system library path may be modified using the
crle utility. Run crle with no options to see the current configuration
and use crle -l to set with the new library path. Be very careful when
modifying the system library path:
# crle -l $OLD_PATH:/usr/local/lib
binutils をインストールする¶
GeoDjango uses the find_library function (from the ctypes.util Python
module) to discover libraries. The find_library routine uses a program
called objdump (part of the binutils package) to verify a shared
library on GNU/Linux systems. Thus, if binutils is not installed on your
Linux system then Python's ctypes may not be able to find your library even if
your library path is set correctly and geospatial libraries were built
perfectly.
binutils パッケージは、Debian および Ubuntu システムでは以下のコマンドを使用してインストールできます:
$ sudo apt-get install binutils
Red Hat および CentOS システムでも同様です:
$ sudo yum install binutils
プラットフォーム固有の手順¶
macOS¶
macOS では様々なパッケージングシステムが利用できるため、ユーザは GeoDjango をインストールするのにいくつかの異なる選択肢があります。これらのオプションは以下の通りです:
Postgres.app (最も簡単、推奨)
このセクションには、Python Software Foundation が提供するパッケージから Python のアップグレード版をインストールする手順も含まれていますが、これは必須ではありません。
Python¶
Although macOS comes with Python installed, users can use framework installers provided by the Python Software Foundation. An advantage to using the installer is that macOS's Python will remain "pristine" for internal operating system use.
注釈
コマンドラインで python を入力したときに新しいバージョンの Python が使われるように、.profile ファイルの PATH 環境変数を変更する必要があります:
export PATH=/Library/Frameworks/Python.framework/Versions/Current/bin:$PATH
Postgres.app¶
Postgres.app は PostGIS 拡張を含むスタンドアロンの PostgreSQL サーバです。また、gdal と libgeoip を Homebrew と共にインストールする必要があります。
Postgres.appをインストールした後、コマンドラインからパッケージのプログラムを実行できるように、 .bash_profile に以下を追加してください。 X.Y をインストールした Postgres.app の PostgreSQL のバージョンに置き換えてください:
export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/X.Y/bin
ターミナルプロンプトで which psql と入力して、パスが正しく設定されているかを確認できます。
Homebrew¶
Homebrew は、ソースコードからバイナリおよびパッケージを構築するための「レシピ」を提供します。これは、macOS を実行している Macintosh コンピュータ上で GeoDjango の前提条件のためのレシピを提供しています。Homebrew はソフトウェアをソースコードから構築するため、 Xcode が必要となります。
概要:
$ brew install postgresql
$ brew install postgis
$ brew install gdal
$ brew install libgeoip
Fink¶
Kurt Schwehr は Fink パッケージシステムのユーザーのために GeoDjango パッケージを作成してくれました。使用したい Python のバージョンに応じて、Different packages are available です(django-gis から始まります)。
MacPorts¶
MacPorts may be used to install GeoDjango prerequisites on computers running macOS. Because MacPorts still builds the software from source, Xcode is required.
概要:
$ sudo port install postgresql14-server
$ sudo port install geos
$ sudo port install proj6
$ sudo port install postgis3
$ sudo port install gdal
$ sudo port install libgeoip
注釈
また、MacPorts のプログラムにコマンドラインからアクセスできるように、 .profile の PATH を変更する必要があります:
export PATH=/opt/local/bin:/opt/local/lib/postgresql14/bin
さらに、Python がライブラリを見つけられるように、 DYLD_FALLBACK_LIBRARY_PATH 設定を追加してください:
export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib:/opt/local/lib/postgresql14
Windows¶
GeoDjango を Windows にインストールするには、以下のセクションを順番に進めてください。このチュートリアルでは、各アプリケーションの 64 ビット版をインストールします。
Python¶
64ビット版のPythonをインストールします。詳しくは Python のインストール を参照してください。
PostgreSQL¶
Download the latest PostgreSQL 15.x installer from the EnterpriseDB website. After downloading, run the installer, follow the on-screen directions, and keep the default options unless you know the consequences of changing them.
注釈
PostgreSQL のインストーラーは新しい postgres データベース管理者ユーザーを作成します。パスワードを設定する際に一度だけ入力を求められます。それを忘れないようにしてください!
インストーラーが完了すると、 "Launch Stack Builder at exit?" (スタックビルダーを終了時に起動しますか?)という質問が表示されます。これは、PostGIS をインストールするために必要ですので、チェックを入れたままにしてください。
注釈
インストールが成功すると、PostgreSQL サーバーは Windows サービスとしてシステムが起動するたびにバックグラウンドで実行されます。 スタートメニューグループが作成され、アプリケーションスタックビルダー (ASB) および "SQL Shell" のショートカットが含まれます。 "SQL Shell" を起動すると psql コマンドウィンドウが開きます。
PostGIS¶
Stack Builder (インストーラの外で実行するには ) から、ドロップダウンメニューから を選択して next をクリックします。 メニューツリーを展開し、 を選択します。
next をクリックすると、選択したパッケージと "Download directory" を確認するプロンプトが表示されます。もう一度 next をクリックすると、PostGIS がダウンロードされ、next をクリックして PostGIS のインストーラを開始するよう求められます。インストール中はデフォルトのオプションを選択してください。インストールプロセスでは、4つの Yes/No ダイアログボックスが表示されます。
OSGeo4W¶
The OSGeo4W installer helps to install the PROJ, GDAL, and GEOS libraries required by GeoDjango. First, download the OSGeo4W installer, and run it. Select and click next. In the 'Select Packages' list, ensure that GDAL is selected. If any other packages are enabled by default, they are not required by GeoDjango and may be unchecked safely. After clicking next and accepting the license agreements, the packages will be automatically downloaded and installed, after which you may exit the installer.
Windows 環境変数の変更¶
In order to use GeoDjango, you will need to add your OSGeo4W
directories to your Windows system Path, as well as create GDAL_DATA
and PROJ_LIB environment variables. The following set of commands,
executable with cmd.exe, will set this up. Restart your device
once this is complete for new environment variables to be recognized:
set OSGEO4W_ROOT=C:\OSGeo4W
set GDAL_DATA=%OSGEO4W_ROOT%\apps\gdal\share\gdal
set PROJ_LIB=%OSGEO4W_ROOT%\share\proj
set PATH=%PATH%;%OSGEO4W_ROOT%\bin
reg ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_EXPAND_SZ /f /d "%PATH%"
reg ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v GDAL_DATA /t REG_EXPAND_SZ /f /d "%GDAL_DATA%"
reg ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v PROJ_LIB /t REG_EXPAND_SZ /f /d "%PROJ_LIB%"
注釈
これらのコマンドを実行するには、管理者権限が必要です。これを行うには、管理者としてコマンドプロンプトを実行し、上記のコマンドを入力します。設定を有効にするには、一旦ログアウトし、再度ログインする必要があります。
注釈
OSGeo4W のインストールディレクトリをカスタマイズした場合、それに応じて OSGEO4W_ROOT 変数を変更する必要があります。
Djangoのインストールとデータベースのセットアップ¶
psycopg¶
Pythonモジュール psycopg は Python と PostgreSQL データベースのインターフェースを提供します。 psycopg は Python 仮想環境内で pip を使用してインストールできます:
...\> py -m pip install psycopg
脚注