「PostGIS Layers」の版間の差分

提供: OSGeo.JP Wiki
移動: 案内検索
(タグ・画像調整)
 
(2人の利用者による、間の8版が非表示)
行1: 行1:
<H2>
+
=== PostGIS Layers ===
PostGIS Layers
+
<span id='label_postgis'></span>
</H2>
+
<A NAME="886"></A>
+
<A NAME="107"></A>
+
<A NAME="label_postgis"></A>  
+
  
<P>
+
PostGISレイヤは、PostgreSQLデータベースに格納されているものです。
PostGIS layers are stored in a PostgreSQL database. The advantages of PostGIS
+
PostGISの利点は、空間インデクス、フィルタリング、クエリの機能が提供されている点です。
are the spatial indexing, filtering and query capabilities it provides. Using PostGIS,
+
PostGISを用いると、選択や識別といったベクタ機能が、QGISのOGRレイヤよりも精度良くなります。
vector functions such as select and identify work more accurately than with
+
OGR layers in QGIS.
+
  
<P>
+
PostGISレイヤを使うために必要なことは次の通りです。
To use PostGIS layers you must:<A NAME="109"></A>
+
  
<P>
+
<ul>
 +
<li>QGIS内に、PostgreSQLデータベースに接続するための設定を保存します (まだひとつも設定されていない場合)。
 +
<li>データベースに接続します。
 +
<li>マップに追加するレイヤを選択します。
 +
<li>必要なら、SQLのWHERE節でレイヤからどのフィーチャーをロードするかを定義します。
 +
<li>レイヤをロードします。
 +
</ul>
  
<UL>
+
==== 接続設定を保存する ====
<LI>Create a stored connection in QGIS to the PostgreSQL database (if one is
+
<span id='sec:postgis_stored'></span>
not already defined).<A NAME="111"></A>
+
</LI>
+
<LI>Connect to the database.
+
</LI>
+
<LI>Select the layer to add to the map.
+
</LI>
+
<LI>Optionally provide a SQL where
+
clause to define which features
+
to load from the layer.
+
</LI>
+
<LI>Load the layer.
+
</LI>
+
</UL>
+
  
<P>
+
[[File:mActionAddLayer.png|26px]] PostGISデータソースを使うには、まずはじめに、
<BR><HR>
+
データを持っているPostgreSQLデータベースへの接続を作成する必要があります。
<H3>
+
ツールバーの {{Template:ToolBtnTwo|MActionAddLayer|Add PostGIS Layer}} をクリックするか、
Creating a stored
+
{{Template:MainMenuOpt|レイヤ}} メニューから {{Template:DropMenuOptTwo|MActionAddLayer|Add PostGIS Layer}} を選択するか、{{Template:KeyStroke|D}}キーを押して下さい。
Connection
+
{{Template:Dialog|Add Vector Layer}} ダイアログを開いて、{{Template:RadioButtonOn|データベース}} を選択しても良いです。
</H3>
+
{{Template:Dialog|PostGIS テーブルを追加}} というダイアログが表示されます。
 +
接続マネージャにアクセスするには、
 +
{{Template:Button|新規}} ボタンをクリックします。
 +
{{Template:Dialog|新規 PostGIS 接続を作成}} ダイアログが表示されます。
 +
接続に必要なパラメータを表 [[#tab:postgis_connection_parms|1]]に示します。
  
<P>
+
<table border='1' aligh='center'><caption>'''表 1:''' PostGIS接続パラメータ名</caption><span id='tab:postgis_connection_parms'></span>
[width=0.7cm]mActionAddLayer The first time
+
<tr><td> 名称 <td> この接続に付ける名前。データベース名と同じにすることができます。
you use a PostGIS data source, you must create a connection to the PostgreSQL
+
</tr>
database that contains the data. Begin by clicking on the
+
<tr><td> ホスト
mActionAddLayerAdd a PostGIS Layer toolbar button, selecting the
+
<td> データベースのホスト名。telnet接続を開いたり、pingを打ったりするのに使われるのと同じもので、名前解決可能でなくてはなりません。データベースがQGISと同じコンピュータ上にある場合は、'localhost'するだけです。
mActionAddLayerAdd a PostGIS Layer... option from the Layer menu or typing
+
</tr>
D. You can also open the open the
+
<tr><td> データベース <td> データベース名。</tr>
Add Vector Layer dialog and select Database.
+
<tr><td> ポート <td> PostgreSQLデータベースサーバが待機しているポート番号。デフォルトでは5432になっています。</tr>
The Add PostGIS Table(s) dialog will
+
<tr><td> ユーザ名 <td> データベースにログインするためのユーザ名。</tr>
be displayed. To access the connection manager<A NAME="127"></A>, click on the New button to display the Create a New
+
<tr><td> パスワード <td> ''Username''でデータベースに接続する際に使われるパスワード。 </tr>
PostGIS Connection dialog. The parameters required for a connection are shown
+
<tr><td> SSL mode <td> SSL接続のサーバとのネゴシエート方法。次のオプションがあります。
in table <A HREF="#tab:postgis_connection_parms">1</A>.
+
<ul>
 +
<li>disable: 非暗号化SSL接続のみ試みます。
 +
<li>allow: 非SSQL接続を試し、失敗した場合にSSL接続を試みます。
 +
<li>prefer (デフォルト): SSL接続をためし、失敗した場合に非SSL接続を試みます。
 +
<li>require: SSL接続のみ試みます。
 +
</ul>
 +
接続エディタでSSL接続を切るとPostGISレイヤのレンダリングが強力にスピードアップします。
 +
</table>
  
<P>
+
必要なら、次に示すチェックボックスにチェックを入れることができます。
<BR><P></P>
+
<DIV ALIGN="CENTER">
+
  
 +
<ul>
 +
<li>{{Template:CheckBox|パスワード保存}}
 +
<li>{{Template:CheckBox|Only look in the geometry_columns table}}
 +
<li>{{Template:CheckBox|Only look in the 'public' schema}}
 +
</ul>
  
</DIV><A NAME="tab:postgis_connection_parms"></A><P>
+
全てのパラメータとオプションを設定したら、{{Template:Button|接続テスト}}ボタンをクリックして接続のテストを行うことができます。
<BR>
+
  
<TABLE CELLPADDING=3 BORDER="1">
+
{{Template:Tip
<CAPTION><STRONG>Table 1:</STRONG>
+
|num=9
PostGIS Connection
+
|caption=QGISユーザ設定とセキュリティ
Parameters</CAPTION>
+
|qgistip=ご自身のQGISの設定は、オペレーティングシステムに基づいて保存されます。
<TR><TD ALIGN="LEFT">Name</TD>
+
{{Template:Nix}}では、設定は{{Template:FileName|.qt/qgisrc}}内のホームディレクトリにストアされます。
<TD ALIGN="LEFT" VALIGN="TOP" WIDTH=360>A name for this connection. Can be the same as <SPAN  CLASS="textsl">Database</SPAN>.</TD>
+
{{Template:Win}}では、レジストリに保存されます。
</TR>
+
コンピュータ環境に依存して、QGIS設定内にパスワードを保存することはセキュリティリスクとなるかも知れません。
<TR><TD ALIGN="LEFT">Host <A NAME="138"></A></TD>
+
}}
<TD ALIGN="LEFT" VALIGN="TOP" WIDTH=360>Name of the database host. This must be a resolvable host name the same as
+
would be used to open a telnet connection or ping the host. If the database is
+
on the same computer as QGIS, simply enter 'localhost' here.</TD>
+
</TR>
+
<TR><TD ALIGN="LEFT">Database <A NAME="139"></A></TD>
+
<TD ALIGN="LEFT" VALIGN="TOP" WIDTH=360>Name of the database.</TD>
+
</TR>
+
<TR><TD ALIGN="LEFT">Port <A NAME="140"></A></TD>
+
<TD ALIGN="LEFT" VALIGN="TOP" WIDTH=360>Port number the PostgreSQL database
+
server listens on. The default port is 5432.</TD>
+
</TR>
+
<TR><TD ALIGN="LEFT">Username <A NAME="141"></A></TD>
+
<TD ALIGN="LEFT" VALIGN="TOP" WIDTH=360>User name used to login to the
+
database.</TD>
+
</TR>
+
<TR><TD ALIGN="LEFT">Password <A NAME="142"></A></TD>
+
<TD ALIGN="LEFT" VALIGN="TOP" WIDTH=360>Password used with
+
<SPAN  CLASS="textsl">Username</SPAN> to connect to the database.</TD>
+
</TR>
+
<TR><TD ALIGN="LEFT">SSL mode <A NAME="144"></A></TD>
+
<TD ALIGN="LEFT" VALIGN="TOP" WIDTH=360>How the SSL connection will be negotiated with the server. These are the options:
+
  
<UL>
+
==== PostGIS レイヤのロード ====
<LI>disable: only try an unencrypted SSL connection;
+
</LI>
+
<LI>allow: try a non-SSL connection, if that fails, try an SSL connection;
+
</LI>
+
<LI>prefer (the default): try an SSL connection, if that fails, try a non-SSL connection;
+
</LI>
+
<LI>require: only try an SSL connection.
+
</LI>
+
</UL>
+
Note that massive speedups in PostGIS layer rendering can be achieved by disabling SSL in the connection editor.</TD>
+
</TR>
+
</TABLE>
+
</DIV>
+
<BR>
+
</DIV>
+
  
<P>
+
[[File:MActionAddLayer.png|26px]] ひとつ以上の接続設定を作成したら、PostgreSQLデータベースからレイヤをロードすることができます。
Optional you can activate follwing checkboxes:
+
もちろん、PostgreSQLにデータが存在する必要があります。
 +
データベースにデータをインポートすることについては Section [[#PostgreSQL へのデータのインポート#se:loading_postgis_data|5.2.4]] をご覧下さい。
  
<P>
+
PostGISからレイヤをロードするには、次のステップを実行します。
  
<UL>
+
<ul>
<LI>Save Password
+
<li>{{Template:Dialog|PostGIS テーブルを追加}} ダイアログをまだ開いていない場合は、ツールバーの{{Template:ToolBtnTwo|MActionAddLayer|Add PostGIS Layer}} をクリックします。
</LI>
+
<li>ドロップダウンから接続を選択して{{Template:Button|接続}}ボタンをクリックします。
<LI>Only look in the geometry_columns table
+
<li>使用可能なレイヤの一覧から追加したいレイヤを探します。
</LI>
+
<li>追加したいレイヤをクリックすると選択できます。{{Template:KeyStroke|Shift}}キーを押しながらクリックすると複数レイヤの選択ができます。Section [[クエリビルダ|5.6]] で、より踏み込んだレイヤ定義を行うために使われるPostgreSQLクエリビルダに関する情報を記述しています。
<LI>Only look in the 'public' schema
+
<li>Click on the {{Template:Button|Add}} button to add the layer to the map.
</LI>
+
</ul>
</UL>
+
  
<P>
+
{{Template:Tip
Once all parameters and options are set, you can test the connection by
+
|num=10
clicking on the Test Connect button<A NAME="155"></A>.
+
|caption=PostGISレイヤ
 +
|qgistip=通常PostGISレイヤは、geometry_columnsテーブルのエントリに定義されています。
 +
1.0.0版から、QGISはgeometry_columnsテーブル内にエントリが無いレイヤをロードすることができます。
 +
これは、テーブルとビューの両方について言えます。
 +
空間ビューの定義によって、データを強力に可視化する手段が供給されます。
 +
ビューの生成についてはPostgreSQLマニュアルをご覧下さい。
 +
}}
  
<P>
+
==== PostgreSQL レイヤに関する詳細 ====
<BR>
+
<span id='sec:postgis_details'></span>
 
+
<BR>
+
  
 +
この節では、QGISがPostgreSQLレイヤにどのように接続するかの詳細に触れます。
 +
ほとんどの場合は、QGISは、単純にデータベースのロード可能なテーブル一覧を提示し、求めに応じてロードします。
 +
しかし、PostgreSQLテーブルをQGISにロードする時にトラブルが発生した場合、
 +
この節の情報が、QGISのメッセージを理解する助けとなり、また、PostgreSQLテーブルの変更や、GISがロードできるビューの定義に関する方向性を得る助けとなります。
  
<H3>
+
QGISには、一意のキーとして使用するカラムを持つPostgreSQLレイヤが必要です。
Loading a PostGIS Layer
+
テーブルから見れば、テーブルにはプライマリキーまたは一意制限が課せられているカラムが求められる、という意味です。
</H3>
+
QGISでは、このカラムには、int4(4バイト整数)型である必要があります。
 +
もしくは、ctidカラム(訳注: 物理的な行位置が入っているシステムカラム) を主キーに使うことができます。
 +
テーブルがこれらのアイテムを欠如させると、
 +
oidカラム(訳注: 論理的識別子が入っているシステムカラム、PostgreSQL 8.1.0以降はデフォルトでは作成されなくなりました)が代わりに用いられます。
 +
カラムにインデクスを作成すると実行速度が改善します(PostgreSQLでは主キーには自動的にインデクスが作成されます)。
  
<P>
+
PostgreSQLレイヤがビューの場合、同じ要件となりますが、
[width=0.7cm]mActionAddLayer Once you have one or more
+
ビューに主キーや一意性制限が課せられたカラムはありません。
connections defined, you can load layers from the PostgreSQL database. Of
+
この場合、QGISは適切なテーブルカラムから派生したビューカラムを発見しようと試みます。
course this requires having data in PostgreSQL. See Section
+
ビュー定義SQLの構文解析を行うことで実現しています。
<A HREF="node11.html#sec:loading_postgis_data">5.2.4</A> for a discussion on importing data into the
+
しかし、QGISは、テーブル別名の使用やSQL関数で生成されたカラムなどといった、SQLのいくつかの面を無視します。
database.
+
  
<P>
+
適切なカラムが見つからなかった場合は、QGISはレイヤをロードしません。
To load a layer from PostGIS, perform the following steps:
+
こうなった場合、ビュー定義の変更を行い適切なカラム(int4型で主キーか、一意性制限があるカラムであって、インデクスが作られていることが望ましい)を含ませることで解決します。
  
<P>
+
When dealing with views, QGIS parses the view definition and (ここで切れてます)
  
<UL>
 
<LI>If the Add PostGIS Table(s) dialog is not already open, click on the
 
mActionAddLayerAdd a PostGIS Layer toolbar button.
 
</LI>
 
<LI>Choose the connection from the drop-down list and click Connect.
 
</LI>
 
<LI>Find the layer you wish to add in the list of available layers.
 
</LI>
 
<LI>Select it by clicking on it. You can select multiple layers by holding
 
down the shift key while clicking. See Section <A HREF="node29.html#sec:query_builder">5.6</A> for
 
information on using the PostgreSQL Query Builder to further define the layer.
 
</LI>
 
<LI>Click on the Add button to add the layer to the map.
 
</LI>
 
</UL>
 
  
<P>
+
==== PostgreSQL へのデータのインポート ====
<BR>
+
<span id='sec:loading_postgis_data'></span>
  
 +
===== shp2pgsql =====
 +
データをPostgreSQLにインポートするには、いくつかの方法があります。
 +
PostGISは、{{Template:FileName|shp2pgsql}}というシェープファイルをPostGISにインポートするためのユーティリティを持っています。
 +
たとえば、{{Template:FileName|lakes.shp}}という名前のシェープファイルを、{{Template:UserText|gis_data}}という名前のPostgreSQLデータベースにインポートするには、次のコマンドを使います。
 +
<pre>
 +
  shp2pgsql -s 2964 lakes.shp lakes_new | psql gis_data
 +
</pre>
 +
(訳注: これだと整数がint2型になり、また、空間インデクスが付加されませんので、shp2pgsqlに"-i"および"-I"オプションを付けるべきです)
  
<H3>
+
これによって、{{Template:UserText|lakes_new}}という名前の新しいレイヤがgis_dataデータベース内に作成されます。
Some details about PostgreSQL
+
新しいレイヤは空間参照識別子(SRID)は2964となります。
layers
+
空間参照系と投影法に関する情報については、Section [[いろいろな投影法の利用#label_projections|8]]をご覧下さい。
</H3>
+
  
<P>
+
{{Template:Tip
This section contains some details on how QGIS accesses PostgreSQL
+
|num=11
layers. Most of the time QGIS should simply provide you with a list of
+
|caption=PostGISからのデータセットのエクスポート
database tables that can be loaded, and load them on request. However,
+
|qgistip=インポートツールの{{Template:FileName|shp2pgsql}}と同じような、
if you have trouble loading a PostgreSQL table into QGIS, the information
+
PostGISデータベースをシェープファイルにエクスポートするツールの {{Template:FileName|pgsql2shp}} があります。
below may help you understand any QGIS messages and give you direction on
+
これは、PostGISのディストリビューションに同梱されています。
changing the PostgreSQL table or view definition to allow QGIS to load it.
+
}}
  
<P>
+
===== SPITプラグイン =====
QGIS requires that PostgreSQL layers contain a column that can be
+
[[File:Spiticon.png|26px]] SPIT (Shapefile to PostGIS Import Tools)という名前のプラグインがQGISにあります。
used as a unique key for the layer. For tables this usually means
+
SPITは、一度に複数のシェープファイルをロードでき、スキーマのサポートもします。
that the table needs a primary key, or a column with a unique
+
SPITを使うには、{{Template:MainMenuOpt|プラグイン}}メニューからプラグインマネージャを開き、
constraint on it. In QGIS, this column needs to be of
+
{{Template:CheckBox|SPIT プラグイン}}のチェックボックスにチェックを入れて、{{Template:Button|OK}}をクリックします。
type int4 (an integer of size 4 bytes). Alternatively the ctid column can be used as primary key.
+
SPITアイコンがツールバーに追加されます。
If a table lacks these items,
+
the oid column will be used instead. Performance will be improved if the
+
column is indexed (note that primary keys are automatically indexed in
+
PostgreSQL).
+
  
<P>
+
シェープファイルをインポートするには、ツールバーの{{Template:ToolBtnTwo|Spiticon|SPIT}}ツールをクリックして、
If the PostgreSQL layer is a view, the same requirement exists, but
+
{{Template:Dialog|SPIT - Shapefile to PostGIS Import Tool}}ダイアログを開きます。
views don't have primary keys or columns with unique constraints on
+
接続したいPostGISデータベースを選択して{{Template:Button|接続}}をクリックします。
them. In this case QGIS will try to find a column in the view that is
+
{{Template:Button|追加}}ボタンをクリックしてひとつまたは複数のファイルをキューに追加することができます。
derived from a suitable table column. It does this by parsing the view
+
{{Template:Button|OK}}のボタンをクリックするとファイルの処理が行われます。
definition SQL. However there are several aspects of SQL that QGIS ignores
+
エラー、警告やインポートの進行状況は、シェープファイルごとに表示されます。
- these include the use of table aliases and columns that are generated by
+
SQL functions.
+
  
<P>
+
{{Template:Tip
If a suitable column cannot be found, QGIS will not load the layer. If this
+
|num=12
occurs, the solution is to alter the view so that it does include a suitable
+
|caption=PostgreSQL予約語を含むシェープファイルのインポート
column (a type of int4 and either a primary key or with a unique constraint,
+
|qgistip=PostgreSQLの予約語となるフィールド名を含むシェープファイルがキューに追加されている場合、
preferably indexed).
+
それぞれのフィールドの状態を表示するダイアログが現れます。
  
<P>
+
重要性の高いフィールド名を編集して、
When dealing with views, QGIS parses the view definition and
+
予約後
 +
(または求められる他のフィールド名を変更する)
  
 +
予約語のフィールド名を持つシェープファイルをインポートしようとすると、失敗するでしょう。
 +
}}
  
<H3>
+
===== ogr2ogr =====
Importing Data into PostgreSQL
+
</H3>
+
  
<P>
+
{{Template:FileName|shp2pgsql}}と{{Template:FileName|SPIT}}の他にも、PostGISに地理データを送りこむツール({{Template:FileName|ogr2ogr}}があります。
shp2pgsql
+
これはGDALに入っています。シェープファイルをPostGISにインポートするには、次のコマンドを実行します。
Data can be imported into PostgreSQL using a number of methods. PostGIS
+
includes a utility called shp2pgsql that can be used to import shapefiles into
+
a PostGIS enabled database. For example, to import a shapefile named
+
lakes.shp
+
into a PostgreSQL database named gis_data, use the following command:
+
  
<P>
+
<pre>
<PRE>
+
  shp2pgsql -s 2964 lakes.shp lakes_new | psql gis_data
+
</PRE>
+
 
+
<P>
+
This creates a new layer named lakes_new in the
+
gis_data database. The
+
new layer will have a spatial reference identifier (SRID) of 2964. See Section
+
<A HREF="#label_projections"><IMG  ALIGN="BOTTOM" BORDER="1" ALT="[*]"
+
SRC="/usr/share/latex2html/icons/crossref.png"></A> for more information on spatial reference systems and
+
projections.
+
<BR>
+
<IMG
+
WIDTH="128" HEIGHT="11" ALIGN="BOTTOM" BORDER="0"
+
SRC="img7.png"
+
ALT="\begin{Tip}
+
% latex2html id marker 194\caption{\textsc{Exporting datasets from...
+
...lename{pgsql2shp}. This is shipped within your
+
PostGIS distribution.}
+
\end{Tip}">
+
<BR>
+
 
+
<P>
+
SPIT Plugin
+
[width=0.7cm]spiticon QGIS comes with a
+
plugin named
+
SPIT (Shapefile to PostGIS Import Tool)<A NAME="202"></A>.
+
SPIT can be used to load multiple shapefiles at one time and includes support
+
for schemas. To use SPIT, open the Plugin Manager from the Plugins
+
menu, check the box next to the SPIT plugin and click OK. The SPIT
+
icon will be added to the plugin toolbar<A NAME="206"></A>.
+
 
+
<P>
+
To import a shapefile, click on the spiticonSPIT tool in the
+
toolbar to open the
+
SPIT - Shapefile to PostGIS Import Tool dialog. Select the PostGIS database
+
you want to connect to and click on Connect. Now you can add one or more
+
files to the queue by clicking on the Add button. To process the files,
+
click on the OK button. The progress of the import as well as any
+
errors/warnings will be displayed as each shapefile is processed.
+
 
+
<P>
+
<BR>
+
 
+
 
+
<P>
+
ogr2ogr
+
Beside shp2pgsql and SPIT there is another tool for feeding
+
geodata in PostGIS: ogr2ogr. This is part of your GDAL installation.
+
To import a shapefile into PostGIS, do the following:
+
<PRE>
+
 
   ogr2ogr -f "PostgreSQL" PG:"dbname=postgis host=myhost.de user=postgres \
 
   ogr2ogr -f "PostgreSQL" PG:"dbname=postgis host=myhost.de user=postgres \
 
   password=topsecret" alaska.shp
 
   password=topsecret" alaska.shp
</PRE>
+
</pre>
  
<P>
+
これによって、{{Template:FileName|alaska.shp}}というシェープファイルが、
This will import the shapefile alaska.shp into the PostGIS-database
+
{{Template:UserText|postgres}}というユーザ名と、{{Template:UserText|topsecret}}というパスワードで、{{Template:Server|myhost.de}}というホストにある、{{Template:UserText|postgis}} という PostGISデータベースにインポートされます。
postgis
+
using the user postgres with the password topsecret on host
+
myhost.de.
+
  
<P>
+
OGRはPostgreSQLとともにビルドして、PostGISをサポートするようにしなければなりませんので、ご注意ください。
Note that OGR must be built with PostgreSQL to support PostGIS.
+
次のように入力して確認できます。
You can see this by typing
+
 
<PRE>
+
<pre>
 
ogrinfo --formats | grep -i post
 
ogrinfo --formats | grep -i post
</PRE>
+
</pre>
  
<P>
+
通常使われるINSERT INTOメソッドの替わりにPostgreSQLの{{Template:FileName|COPY}}コマンドを使いたい場合は、
If you like to use PostgreSQL's COPY-command instead of the default
+
次のような環境変数を設定して下さい。(at least available on {{Template:Nix}} and {{Template:OSX}})
INSERT INTO method you can export the following
+
<pre>
environment-variable (at least available on and ):
+
<PRE>
+
 
   export PG_USE_COPY=YES
 
   export PG_USE_COPY=YES
</PRE>
+
</pre>
  
<P>
+
{{Template:FileName|ogr2ogr}}では、{{Template:FileName|shp2pgsql}}のように空間インデクスを生成しません。
ogr2ogr does not create spatial indexes like shp2pgsl
+
インポート後の追加手順として、通常のSQLコマンドの{{Template:FileName|CREATE INDEX}}を使って、手動で生成して下さい
does. You need to create them manually using the normal SQL-command
+
(次節[[#label_improve|5.2.5]]で記述します)
CREATE INDEX afterwards as an extra step (as described in the next
+
section <A HREF="node12.html#label_improve">5.2.5</A>).
+
  
<P>
+
==== 性能の改善 ====
 +
<span id='label_improve'></span>
  
<H3>
+
PostgreSQLデータベースからフィーチャーを検索する際に、特にネットワーク経由の場合には、時間を浪費します。
Improving Performance
+
空間インデクスがデータベース内のそれぞれのレイヤに生成するなら、
</H3>
+
PostgreSQLレイヤの描画性能向上が改善できます。
 +
PostGISは、空間データ検索の速度向上のために、GiST(Generalized Search Tree)の生成をサポートします。
  
<P>
+
GiST生成の構文は、次のようになります<ref>GiSTインデクスの情報は {{Template:URL|http://postgis.refractions.net}} にあるPostGISドキュメントから得ました。</ref>
Retrieving features from a PostgreSQL database can be time consuming,
+
especially over a network. You can improve the drawing performance of
+
PostgreSQL layers by ensuring that a <A NAME="241"></A> spatial
+
index
+
exists on each layer in the database. PostGIS supports creation of a
+
<A NAME="242"></A> GiST
+
(Generalized Search Tree) index to speed up spatial searches of the data.
+
  
<P>
+
<pre>
The syntax for creating a GiST<A NAME="tex2html48"
+
  HREF="footnode.html#foot895"><SUP><IMG  ALIGN="BOTTOM" BORDER="1" ALT="[*]"
+
SRC="/usr/share/latex2html/icons/footnote.png"></SUP></A>index is:
+
 
+
<P>
+
<PRE>
+
 
     CREATE INDEX [indexname] ON [tablename]  
 
     CREATE INDEX [indexname] ON [tablename]  
 
       USING GIST ( [geometryfield] GIST_GEOMETRY_OPS );
 
       USING GIST ( [geometryfield] GIST_GEOMETRY_OPS );
</PRE>
+
</pre>
 +
 
 +
大きなテーブルでは、インデクスの生成に時間がかかります。
 +
インデクスを生成したら、{{Template:UserText|VACUUM ANALYZE}}を実行すべきです。
 +
詳細情報はPostGISドキュメント {{Template:Cite|num=6|ref=PostGISweb}} をご覧下さい。
  
<P>
+
次にGiSTインデクス生成の例を示します。
Note that for large tables, creating the index can take a long time. Once the
+
index is created, you should perform a VACUUM ANALYZE. See the
+
PostGIS documentation [#!PostGISweb!#] for more information.
+
  
<P>
+
<pre>
The following is an example of creating a GiST index:
+
<PRE>
+
 
gsherman@madison:~/current$ psql gis_data
 
gsherman@madison:~/current$ psql gis_data
 
Welcome to psql 8.3.0, the PostgreSQL interactive terminal.
 
Welcome to psql 8.3.0, the PostgreSQL interactive terminal.
行356: 行242:
 
gis_data=# \q
 
gis_data=# \q
 
gsherman@madison:~/current$
 
gsherman@madison:~/current$
</PRE>
+
</pre>
 
+
<P>
+
<BR><HR>
+
 
+
 
+
<H3>
+
Vector layers crossing 180<SPAN CLASS="MATH"></SPAN> longitude
+
</H3>
+
 
+
<P>
+
Many GIS packages don't wrap vector maps, with a geographic reference system
+
(lat/lon), crossing the 180 longitude line. As result, if
+
we open such map in QGIS, we will see two far, distinct locations, that
+
should show near each other. In Figure <A HREF="#fig:vector_not_wrapping">8</A> the tiny
+
point on the far left of the map canvas (Chatham Islands), should be within
+
the grid, right of New Zealand main islands.
+
 
+
<P>
+
 
+
<DIV ALIGN="CENTER"><A NAME="fig:vector_not_wrapping"></A><A NAME="896"></A>
+
<TABLE>
+
<CAPTION ALIGN="BOTTOM"><STRONG>Figure:</STRONG>
+
Map in lat/lon crossing the 180 longitude line
+
  </CAPTION>
+
<TR><TD>
+
<DIV ALIGN="CENTER">
+
</DIV>
+
<P></P>
+
<DIV ALIGN="CENTER">  [clip=true, width=]vectorNotWrapping
+
  
</DIV></TD></TR>
 
</TABLE>
 
</DIV>
 
  
<P>
+
----
A workaround is to transform the longitude values using PostGIS and the
+
</reference>
<SPAN  CLASS="textbf">STShiftLongitude</SPAN>
+
<A NAME="tex2html54"
+
  HREF="footnode.html#foot897"><SUP><IMG  ALIGN="BOTTOM" BORDER="1" ALT="[*]"
+
SRC="/usr/share/latex2html/icons/footnote.png"></SUP></A>function. This function reads every point/vertex in every component of every
+
feature in a geometry, and if the longitude coordinate is &lt; 0 adds
+
360 to it. The result would be a 0 - 360 version of
+
the data to be plotted in a 180 centric map.
+
  
<P>
+
<!-- 1.1.0マニュアルにないため、コメントアウトします。次期Versionのマニュアルに移行するため、このコメントは削除しないでください
 +
=== 経度180度をまたぐベクタレイヤ ===
  
<DIV ALIGN="CENTER"><A NAME="fig:vector_wrapping"></A><A NAME="898"></A>
+
(PDFファイルには存在しません)
<TABLE>
+
<CAPTION ALIGN="BOTTOM"><STRONG>Figure:</STRONG>
+
Map crossing the 180 longitude line
+
after applying the <SPAN  CLASS="textbf">STShiftLongitude</SPAN>
+
function</CAPTION>
+
<TR><TD>
+
<DIV ALIGN="CENTER">
+
</DIV>
+
<P></P>
+
<DIV ALIGN="CENTER">  [clip=true, width=9cm]vectorWrapping
+
  
</DIV></TD></TR>
+
多くのGIS製品では、地理参照系(緯度/経度)で、経度180度をまたぐベクタ地図をラップできません。
</TABLE>
+
QGISで開くと、お互いの位置は近いはずなのに二つの離れた別個の位置となります。
</DIV>
+
図8で、地図の左端にある小さい点(チャタム諸島)は、ニュージーランドの主たる島の右側のグリッド内にあるべきものです。
  
<P>
+
回避策は、PostGISのST_ShiftLongitude関数を使用して経度値を変換することです。
Usage
+
この関数はすべてのフィーチャーのすべての構成要素のすべてのポイント/バーテックスを読み込んで、経度 &lt; 0 なら360を足すものです。
 +
この処理の結果、180度を中心とした地図にプロットされた、0 - 360 版ができます。
  
<P>
+
[図: ST_ShiftLongitude関数を適用した後の経度180度を超えた地図]
  
<UL>
+
使用法は次の通りです。
<LI>Import data to PostGIS (<A HREF="node11.html#sec:loading_postgis_data">5.2.4</A>) using for
+
example the PostGIS Manager plugin or the SPIT plugin
+
</LI>
+
<LI>Use the PostGIS command line interface to issue the following command
+
(this is an example where "TABLE" is the actual name of your PostGIS table)
+
<BR><TT>gis_data=# update TABLE set the_geom=ST_shift_longitude(the_geom);</TT>
+
</LI>
+
<LI>If everything went right you should receive a confirmation about the
+
number of features that were updated, then you'll be able to load the map and
+
see the difference (Figure <A HREF="#fig:vector_wrapping">9</A>)
+
</LI>
+
</UL>
+
  
<P>
+
* PostGIS ManagerプラグインやSPITプラグインを使うなどしてPostGISにデータをインポートします([[PostGIS レイヤのロード]]参照)。
 +
* PostGISのコマンドラインインタフェースを使って、次のコマンドを実行します
 +
(これは実際のテーブル名が"TABLE"であるとした場合の例です)
 +
<tt>gis_data=# update TABLE set the_geom=ST_shift_longitude(the_geom);</tt>
 +
* 全てうまくいけば、更新したフィーチャー数に関する確認情報を受信します。マップをロードして違いを見ることができます(図9)。
 +
-->

2009年10月4日 (日) 16:55時点における最新版

PostGIS Layers

PostGISレイヤは、PostgreSQLデータベースに格納されているものです。 PostGISの利点は、空間インデクス、フィルタリング、クエリの機能が提供されている点です。 PostGISを用いると、選択や識別といったベクタ機能が、QGISのOGRレイヤよりも精度良くなります。

PostGISレイヤを使うために必要なことは次の通りです。

  • QGIS内に、PostgreSQLデータベースに接続するための設定を保存します (まだひとつも設定されていない場合)。
  • データベースに接続します。
  • マップに追加するレイヤを選択します。
  • 必要なら、SQLのWHERE節でレイヤからどのフィーチャーをロードするかを定義します。
  • レイヤをロードします。

接続設定を保存する

MActionAddLayer.png PostGISデータソースを使うには、まずはじめに、 データを持っているPostgreSQLデータベースへの接続を作成する必要があります。 ツールバーの MActionAddLayer.png Add PostGIS Layer をクリックするか、 レイヤ メニューから MActionAddLayer.png Add PostGIS Layer を選択するか、Dキーを押して下さい。 Add Vector Layer ダイアログを開いて、RadioButtonOn.png データベース を選択しても良いです。 PostGIS テーブルを追加 というダイアログが表示されます。 接続マネージャにアクセスするには、 新規 ボタンをクリックします。 新規 PostGIS 接続を作成 ダイアログが表示されます。 接続に必要なパラメータを表 1に示します。

表 1: PostGIS接続パラメータ名
名称 この接続に付ける名前。データベース名と同じにすることができます。
ホスト データベースのホスト名。telnet接続を開いたり、pingを打ったりするのに使われるのと同じもので、名前解決可能でなくてはなりません。データベースがQGISと同じコンピュータ上にある場合は、'localhost'するだけです。
データベース データベース名。
ポート PostgreSQLデータベースサーバが待機しているポート番号。デフォルトでは5432になっています。
ユーザ名 データベースにログインするためのユーザ名。
パスワード Usernameでデータベースに接続する際に使われるパスワード。
SSL mode SSL接続のサーバとのネゴシエート方法。次のオプションがあります。
  • disable: 非暗号化SSL接続のみ試みます。
  • allow: 非SSQL接続を試し、失敗した場合にSSL接続を試みます。
  • prefer (デフォルト): SSL接続をためし、失敗した場合に非SSL接続を試みます。
  • require: SSL接続のみ試みます。

接続エディタでSSL接続を切るとPostGISレイヤのレンダリングが強力にスピードアップします。

必要なら、次に示すチェックボックスにチェックを入れることができます。

  • CheckBox.pngパスワード保存
  • CheckBox.pngOnly look in the geometry_columns table
  • CheckBox.pngOnly look in the 'public' schema

全てのパラメータとオプションを設定したら、接続テストボタンをクリックして接続のテストを行うことができます。


Tip 9 QGISユーザ設定とセキュリティ


ご自身のQGISの設定は、オペレーティングシステムに基づいて保存されます。 Nix.png では、設定は.qt/qgisrc内のホームディレクトリにストアされます。 Win.png では、レジストリに保存されます。 コンピュータ環境に依存して、QGIS設定内にパスワードを保存することはセキュリティリスクとなるかも知れません。



PostGIS レイヤのロード

MActionAddLayer.png ひとつ以上の接続設定を作成したら、PostgreSQLデータベースからレイヤをロードすることができます。 もちろん、PostgreSQLにデータが存在する必要があります。 データベースにデータをインポートすることについては Section 5.2.4 をご覧下さい。

PostGISからレイヤをロードするには、次のステップを実行します。

  • PostGIS テーブルを追加 ダイアログをまだ開いていない場合は、ツールバーのMActionAddLayer.png Add PostGIS Layer をクリックします。
  • ドロップダウンから接続を選択して接続ボタンをクリックします。
  • 使用可能なレイヤの一覧から追加したいレイヤを探します。
  • 追加したいレイヤをクリックすると選択できます。Shiftキーを押しながらクリックすると複数レイヤの選択ができます。Section 5.6 で、より踏み込んだレイヤ定義を行うために使われるPostgreSQLクエリビルダに関する情報を記述しています。
  • Click on the Add button to add the layer to the map.

Tip 10 PostGISレイヤ


通常PostGISレイヤは、geometry_columnsテーブルのエントリに定義されています。 1.0.0版から、QGISはgeometry_columnsテーブル内にエントリが無いレイヤをロードすることができます。 これは、テーブルとビューの両方について言えます。 空間ビューの定義によって、データを強力に可視化する手段が供給されます。 ビューの生成についてはPostgreSQLマニュアルをご覧下さい。



PostgreSQL レイヤに関する詳細

この節では、QGISがPostgreSQLレイヤにどのように接続するかの詳細に触れます。 ほとんどの場合は、QGISは、単純にデータベースのロード可能なテーブル一覧を提示し、求めに応じてロードします。 しかし、PostgreSQLテーブルをQGISにロードする時にトラブルが発生した場合、 この節の情報が、QGISのメッセージを理解する助けとなり、また、PostgreSQLテーブルの変更や、GISがロードできるビューの定義に関する方向性を得る助けとなります。

QGISには、一意のキーとして使用するカラムを持つPostgreSQLレイヤが必要です。 テーブルから見れば、テーブルにはプライマリキーまたは一意制限が課せられているカラムが求められる、という意味です。 QGISでは、このカラムには、int4(4バイト整数)型である必要があります。 もしくは、ctidカラム(訳注: 物理的な行位置が入っているシステムカラム) を主キーに使うことができます。 テーブルがこれらのアイテムを欠如させると、 oidカラム(訳注: 論理的識別子が入っているシステムカラム、PostgreSQL 8.1.0以降はデフォルトでは作成されなくなりました)が代わりに用いられます。 カラムにインデクスを作成すると実行速度が改善します(PostgreSQLでは主キーには自動的にインデクスが作成されます)。

PostgreSQLレイヤがビューの場合、同じ要件となりますが、 ビューに主キーや一意性制限が課せられたカラムはありません。 この場合、QGISは適切なテーブルカラムから派生したビューカラムを発見しようと試みます。 ビュー定義SQLの構文解析を行うことで実現しています。 しかし、QGISは、テーブル別名の使用やSQL関数で生成されたカラムなどといった、SQLのいくつかの面を無視します。

適切なカラムが見つからなかった場合は、QGISはレイヤをロードしません。 こうなった場合、ビュー定義の変更を行い適切なカラム(int4型で主キーか、一意性制限があるカラムであって、インデクスが作られていることが望ましい)を含ませることで解決します。

When dealing with views, QGIS parses the view definition and (ここで切れてます)


PostgreSQL へのデータのインポート

shp2pgsql

データをPostgreSQLにインポートするには、いくつかの方法があります。 PostGISは、shp2pgsqlというシェープファイルをPostGISにインポートするためのユーティリティを持っています。 たとえば、lakes.shpという名前のシェープファイルを、gis_dataという名前のPostgreSQLデータベースにインポートするには、次のコマンドを使います。

  shp2pgsql -s 2964 lakes.shp lakes_new | psql gis_data

(訳注: これだと整数がint2型になり、また、空間インデクスが付加されませんので、shp2pgsqlに"-i"および"-I"オプションを付けるべきです)

これによって、lakes_newという名前の新しいレイヤがgis_dataデータベース内に作成されます。 新しいレイヤは空間参照識別子(SRID)は2964となります。 空間参照系と投影法に関する情報については、Section 8をご覧下さい。


Tip 11 PostGISからのデータセットのエクスポート


インポートツールのshp2pgsqlと同じような、 PostGISデータベースをシェープファイルにエクスポートするツールの pgsql2shp があります。 これは、PostGISのディストリビューションに同梱されています。



SPITプラグイン

Spiticon.png SPIT (Shapefile to PostGIS Import Tools)という名前のプラグインがQGISにあります。 SPITは、一度に複数のシェープファイルをロードでき、スキーマのサポートもします。 SPITを使うには、プラグインメニューからプラグインマネージャを開き、 CheckBox.pngSPIT プラグインのチェックボックスにチェックを入れて、OKをクリックします。 SPITアイコンがツールバーに追加されます。

シェープファイルをインポートするには、ツールバーのSpiticon.png SPITツールをクリックして、 SPIT - Shapefile to PostGIS Import Toolダイアログを開きます。 接続したいPostGISデータベースを選択して接続をクリックします。 追加ボタンをクリックしてひとつまたは複数のファイルをキューに追加することができます。 OKのボタンをクリックするとファイルの処理が行われます。 エラー、警告やインポートの進行状況は、シェープファイルごとに表示されます。


Tip 12 PostgreSQL予約語を含むシェープファイルのインポート


PostgreSQLの予約語となるフィールド名を含むシェープファイルがキューに追加されている場合、 それぞれのフィールドの状態を表示するダイアログが現れます。

重要性の高いフィールド名を編集して、 予約後 (または求められる他のフィールド名を変更する)

予約語のフィールド名を持つシェープファイルをインポートしようとすると、失敗するでしょう。



ogr2ogr

shp2pgsqlSPITの他にも、PostGISに地理データを送りこむツール(ogr2ogrがあります。 これはGDALに入っています。シェープファイルをPostGISにインポートするには、次のコマンドを実行します。

  ogr2ogr -f "PostgreSQL" PG:"dbname=postgis host=myhost.de user=postgres \
  password=topsecret" alaska.shp

これによって、alaska.shpというシェープファイルが、 postgresというユーザ名と、topsecretというパスワードで、myhost.deというホストにある、postgis という PostGISデータベースにインポートされます。

OGRはPostgreSQLとともにビルドして、PostGISをサポートするようにしなければなりませんので、ご注意ください。 次のように入力して確認できます。

ogrinfo --formats | grep -i post

通常使われるINSERT INTOメソッドの替わりにPostgreSQLのCOPYコマンドを使いたい場合は、 次のような環境変数を設定して下さい。(at least available on Nix.png and Osx.png )

  export PG_USE_COPY=YES

ogr2ogrでは、shp2pgsqlのように空間インデクスを生成しません。 インポート後の追加手順として、通常のSQLコマンドのCREATE INDEXを使って、手動で生成して下さい (次節5.2.5で記述します)。

性能の改善

PostgreSQLデータベースからフィーチャーを検索する際に、特にネットワーク経由の場合には、時間を浪費します。 空間インデクスがデータベース内のそれぞれのレイヤに生成するなら、 PostgreSQLレイヤの描画性能向上が改善できます。 PostGISは、空間データ検索の速度向上のために、GiST(Generalized Search Tree)の生成をサポートします。

GiST生成の構文は、次のようになります<ref>GiSTインデクスの情報は http://postgis.refractions.net にあるPostGISドキュメントから得ました。</ref>。

    CREATE INDEX [indexname] ON [tablename] 
      USING GIST ( [geometryfield] GIST_GEOMETRY_OPS );

大きなテーブルでは、インデクスの生成に時間がかかります。 インデクスを生成したら、VACUUM ANALYZEを実行すべきです。 詳細情報はPostGISドキュメント [6] をご覧下さい。

次にGiSTインデクス生成の例を示します。

gsherman@madison:~/current$ psql gis_data
Welcome to psql 8.3.0, the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms
        \h for help with SQL commands
        \? for help with psql commands
        \g or terminate with semicolon to execute query
        \q to quit

gis_data=# CREATE INDEX sidx_alaska_lakes ON alaska_lakes
gis_data-# USING GIST (the_geom GIST_GEOMETRY_OPS);
CREATE INDEX
gis_data=# VACUUM ANALYZE alaska_lakes;
VACUUM
gis_data=# \q
gsherman@madison:~/current$



</reference>