# インストール

## インストール

GeoDjangoで開発する為に必要なライブラリをインストールします

* Python3.5以上
* PostgreSQL 9.5以上
  * PostGIS 2.x以上
* OSGeo4W (GISライブラリ)
  * GDAL(Geospatial Data Abstraction Library)&#x20;
  * GEOS (Geometry Engine, Open Source)&#x20;
  * PROJ4 (Cartographic Projections Library)&#x20;

## Pythonのインストール

Python 3.6のインストール方法の説明をWidowsとMac(OSX)の環境で説明します

**Pythonインストール参考**

* python.jpの環境構築ガイド - <https://www.python.jp/install/install.html>
* Python Boot Campのテキスト - <http://pycamp.pycon.jp/textbook/1_install.html>

### **Windows**

Windowsのシステム情報でシステムの種類が32-bitバージョンか64-bitバージョンかを確認してください。\
(例）コントロールパネル>システムとセキュリティ>システムを開く

![](/files/-LF0cR8iG54b8O7FKDXr)

Python for Windowsを <https://www.python.org/downloads/windows/> から最新版の「Latest Python 3 Release - Python 3.6.5」(2018年6月4日現在)をダウンロードしてください。

```
Python Releases for Windows    
    Latest Python 3 Release - Python 3.6.5
    Latest Python 2 Release - Python 2.7.15
```

![](/files/-LF0cR8mFvSPrkM-wNNp)

Widnowsの環境によってダウンロードするファイルが違いますのでご注意ください。

* 64ビット版のWindowsの場合
  * "Windows x86-64 executable installer" (Windows x86-64実行可能インストーラー)をダウンロードしてください。&#x20;
* 32ビット版のWindowsの場合&#x20;
  * "Windows x86 executable installer" (x86実行可能インストーラー)をダウンロードしてください

![](/files/-LF0cR8ohQszxAetbiFS)

インストーラをダブルクリックして実行します。ダイアログが表示されるので、 「Add Python 3.x to Path」を忘れずに必ずチェックしてから「Install Now」をクリックします。

![](/files/-LF0cR8qB1txzQd83-Ao)

インストーラーが実行されるので、終了まで待ちます。

![](/files/-LF0cR8sR0ijUTbD5QqR)

インストーラーが終了するとダイアログが表示されるので「Close」ボタンをクリックして終了します。

![](/files/-LF0cR8uybW3wkTRno-A)

コマンドプロンプトを実行します。\
例）スタートメニュー→すべてのプログラム→アクセサリ→コマンドプロンプト

![](/files/-LF0cR8wQS6FqgUkDsqf)

コマンドプロンプトが実行されます。

![](/files/-LF0cR8ygD_8RY1WBgNJ)

「python -V」を入力して、Pythonがインストールされたバージョンを確認します。

```
例）python -V
```

![](/files/-LF0cR9-uMp-2aBk-xWG)

**注意**\
&#x20;パソコンの環境によって「python」ではなく「python3」となる場合があります。パソコンの環境を確認ください。「python3」だった場合はこれ以降は「python」を「python3」と読みかえてください

```
例）$ python3 -V
```

pipのバージョンも確認します。pipも、Pythonと同様に環境によりpip3となる場合があります。これも「pip」を「pip3」と読みかえてください

```
$ pip3 --version
pip 9.0.3 from xxx (python 3.6)
```

### **Mac(OSX)**

Python for Macを <https://www.python.org/downloads/mac-osx/> から最新版の「Latest Python 3 Release - Python 3.6.5」(2018年6月4日現在)をダウンロードしてください。

```
Python Releases for Mac OS X
    Latest Python 3 Release - Python 3.6.5
    Latest Python 2 Release - Python 2.7.15
```

OSの環境によって、macOS 64-bit/32-bit installer(for Mac OS X 10.6 and later)または、macOS 64-bit installer（for OS X 10.9 and later）を選択してダウンロードしてください。 ダウンロードされたら、pkgファイル（python-3.6.5-macosx10.6.pkg）をクリックしてインストーラーを実行してください。

インストールが正しく行われたか確認するために、 ターミナルを開いて「python3」もしくは「python」コマンドをタイプしてみてください

```
$ python -V
Python 3.6.5
```

**注意**\
&#x20;Macの場合、デフォルトでpython2がインストールされています。必ずバージョンを確認して 「python -V」が2系だった場合は、「python3」を使うようにしてください。「python3」だった場合はこれ以降は「python」を「python3」と読みかえてください

```
$ python -V
Python 2.7.14

$ python3 -V
Python 3.6.5
```

pipのバージョンも確認します。pipも、Pythonと同様に環境によりpip3となる場合があります。これも「pip」を「pip3」と読みかえてください

```
$ pip3 --version
pip 9.0.3 from xxx (python 3.6)
```

## PostgreSQLのインストール

### **Windows**

**参考**<br>

* [GeoDjango Installation - Install Windows](https://docs.djangoproject.com/en/2.0/ref/contrib/gis/install/#windows)

Enterprise DBのWebサイトから PostgreSQL 9.x のインストーラー <https://www.enterprisedb.com/downloads/postgres-postgresql-downloads>をダウンロードします。

![](/files/-LF0cR91PrJyYvG8jWfo)

インストーラを実行し画面の指示に従いデフォルトのオプションでインストールしてください。

![](/files/-LF0cR93LpvTZ80A52I5)

PostgreSQLインストーラはpostgresデータベースのスーパーユーザーを作成します。 パスワードは忘れないようにしてください。

![](/files/-LF0cR957WAQSCVMq8aG)

ポート番号はデフォルトの5432を設定します。

![](/files/-LF0cR97It7Yx7CRmsMs)

localeは「Japan」を選択します。

![](/files/-LF0cR994Wkmg98rPxIJ)

準備よければこれでインストールを開始します。

![](/files/-LF0cR9BsM9SE2rQ09D6)

インストールが開始されるので、インストーラが完了するまで待ちます。

![](/files/-LF0cR9Do62Is82bEjP-)

インストーラが完了すると、終了時にApplication Stack Builder（ASB）を起動するか質問されます。\
&#x20;PostGISをインストールする必要があるため、このチェックボックスをオンにします。

![](/files/-LF0cR9Fjx0HYHWXTkmO)

スタックビルダ(ASB)が起動されるので、インストールされたPostgreSQLを選択します

![](/files/-LF0cR9HeNy14yfT3vUw)

Spatial ExtensionsからPostGIS 2.4を選択します

![](/files/-LF0cR9J0Q49D1Iy3mv7)

下記の拡張の一緒にインストールされます。

```
PostGIS 2.3.7 bundle includes PostGIS 2.3.7 w GDAL 2.2.4, GEOS 3.6.2,
Proj4.9.3, pgRouting 2.6.0, ogr_fdw 1.0.5  spatial foreign data wrapper
extension, and pgPointcloud 1.1.0dev.
http://postgis.net/2018/04/06/postgis-patches/

PostGIS "spatially enables" the PostgreSQL server, allowing it to be used 
as a backend spatial database for geographic information systems (GIS).
PostGIS follows the OpenGIS "Simple Features Specification for SQL" and 
has been certified as compliant with the "Types and Functions" profile. 

https://github.com/pgRouting/pgrouting/releases/tag/v2.6.0
pgRouting extensions PostGIS for building routing applications

https://github.com/pramsey/pgsql-ogr-fdw
ogr_fdw allows you to query flat files, relational databases, and web
services to name a few.  Spatial columns get transformed to PostGIS 
geometry columns.

https://github.com/pgpointcloud/pointcloud
pgPointcloud introduces specialty types for storing point cloud data 
loaded from LIDAR.
```

ダウンロードディレクトリを選択します

![](/files/-LF0cR9Lj7rjknJnJI5n)

ダウンロードされたら「次へ」ボタンを選択します。

![](/files/-LF0cR9NO8sTsiOaTCXK)

ライセンスを読んだら「I Agree」ボタンを選択します。

![](/files/-LF0cR9Pv_Cbq-XhkiMf)

PostGISがチェックされていることを確認し「Next」ボタンを選択します。

![](/files/-LF0cR9RuzQhb9eaAIP6)

インストールディレクトリを確認し「Next」ボタンを選択します。

![](/files/-LF0cR9TDUmrRcudpcbP)

インストールが開始されます。

![](/files/-LF0cR9VWhJM0pOcVVaS)

環境設定の画面で「はい」を選択します。 環境設定GDAL\_DATAに"C:\Program Files\PostgreSQL\9.6\gdal-data"が設定されます

![](/files/-LF0cR9XgGcj0DOFapxy)

デフォルトの設定はそのままで「はい」を選択します。 環境設定POSTGIS\_GDAL\_ENABLED\_DRIVERSに"GTiff PNG JPEG GIF XYZ DTED USGSDEM AAIGrid"が設定されます

![](/files/-LF0cR9Z-9hGFZMq0GzZ)

デフォルトの設定はそのままで「はい」を選択します。 環境設定POSTGIS\_ENABLE\_OUTDB\_RASTERSに"1"が設定されます

![](/files/-LF0cR9aVKD-fFkLyypW)

インストールが終了したら「Close」を選択します。

![](/files/-LF0cR9c22-HbBqxfrfE)

スタックビルダを終了します

![](/files/-LF0cR9eh67ZaznOsd7d)

正常にインストールされた場合、メニューの「PostGIS」のグループが作成されます。 PostgreSQLサーバはWindowsが起動時に自動で実行されるように設定されます。

Windows10の場合

![](/files/-LF0cR9g4znXpRrPyBiG)

#### PostgreSQL/PostGIS環境変数、パスの設定

「ユーザー環境変数」に設定をします。

\[Windowsシステム・ツール] →［コントロールパネル］→［システムとセキュリティ］→［システム］と進み、画面左にある［システムの詳細設定］をクリックする。

Windows10の場合

![](/files/-LF0cR9ixb5R9uRu4JOD)

ウィンドウの［環境変数］ボタンをクリック。

![](/files/-LF0cR9kGtq_D0R673uR)

「C:\Program Files\PostgreSQL\9.6\bin」(PostgreSQLをインストールしたディレクトリ)をPATH変数に追加します。

**「ユーザー環境変数」の一覧に「Path」がある場合**\
&#x20;「Path」を選択した後［編集］ボタンをクリックして「C:\Program Files\PostgreSQL\9.6\bin」を追加します

![](/files/-LF0cR9mY3c5uz36a0th)

**「ユーザー環境変数」の一覧に「Path」がない場合**\
&#x20;\[新規］ボタンをクリックし、変数名に「Path」、変数値に「C:\Program Files\PostgreSQL\9.6\bin」を入力します。

![](/files/-LF0cR9omlZXHFejG9DO)

Windowsメニューから「コマンドプロンプト」を起動して、Pathの設定が有効か確認します。

![](/files/-LF0cR9qGCnVqw8Uo_5n)

「コマンドプロンプト」で「psql -U postgres -l」と入力してインストールの時に設定した管理パスワードを入力してデータベース一覧を表示してみます

![](/files/-LF0cR9s2C8fd2PYW75b)

「コマンドプロンプト」で「ogr2ogr --version」と入力して、GDALのツールがインストールされているかを確認します。

![](/files/-LF0cR9usmcFxk-bqG48)

### **Mac(OSX)**

**参考**<br>

* [GeoDjango Installation - Install Mac](https://docs.djangoproject.com/en/2.0/ref/contrib/gis/install/#macos)

Homebrew (パッケージ管理システム) を使用してインストールをします。 Homebrewをインストールしてない場合は、Homebrew公式サイト <https://brew.sh/index_ja> を参考にしてHomebrewをインストールしてください。

Homebrewインストール参考\
&#x20;MacにHomebrewを導入する方法 - <https://qiita.com/balius_1064/items/ac7dff5ef10eaf69996f>

```
インストール方法
1. AppStoreでXcodeのインストール
2. コマンドライン・デベロッパ・ツールのインストール
    $ xcode-select --install
3. Homebrewのインストール
    $ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
4.確認
    $ brew doctor
    Your system is ready to brew.
となっていれば導入成功です。
```

下記のbrewコマンドを入力してPostgreSQL/PostGIS/GISライブラリのインストール をします。

```
$ brew install postgresql
$ brew install postgis
$ brew install gdal
```

PostgreSQLの起動

```
$ pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

$ psql --version
psql (PostgreSQL) 9.6

データベース確認
$ psql -U postgres -l
                            List of databases
    Name     |  Owner   | Encoding | Collate | Ctype | Access privileges
-------------+----------+----------+---------+-------+-------------------
postgres    | homata   | UTF8     | C       | C     |
template0   | homata   | UTF8     | C       | C     | =c/homata        +
            |          |          |         |       | homata=CTc/homata
template1   | homata   | UTF8     | C       | C     | =c/homata        +
            |          |          |         |       | homata=CTc/homata
(xx rows)
```

PostgreSQLの停止

```
$ pg_ctl -D /usr/local/var/postgres stop
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://homata.gitbook.io/geodjango/qian-zhun-bei/install.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
