※当サイトの記事には、広告・プロモーションが含まれます。

PostgreSQLで地理情報が扱えるようになるという拡張機能PostGISを導入してみる

www.itmedia.co.jp

クラウドランニングコストがえぐいて...

諸々で毎月およそ200万円って、これが現実ってやつですか...クラウドのサーバー費用だけで、毎月およそ110万円を超えるコストですか...絶句するしかない。

「地理情報(GI:Geographic Information)」とは?

英語版のWikipediaによると、

Geographic data and information is defined in the ISO/TC 211 series of standards as data and information having an implicit or explicit association with a location relative to Earth (a geographic location or geographic position).

https://en.wikipedia.org/wiki/Geographic_data_and_information

It is also called geospatial data and information, georeferenced data and information, as well as geodata and geoinformation.

https://en.wikipedia.org/wiki/Geographic_data_and_information

⇧「地理データ、および、地理情報」として、「ISO/TC 211」で定義されてるらしい。

「ISO/TC 211」はと言うと、

ISO/TC 211 is a standard technical committee formed within ISO, tasked with covering the areas of digital geographic information (such as used by geographic information systems) and geomatics. It is responsible for preparation of a series of International Standards and Technical Specifications numbered in the number range starting at ISO-19101. The Chair of the committee was 1994-2016: Olaf Østensen; during 2017-2018: Christina Wasström; and from 2019 Agneta Gren Engberg.

https://en.wikipedia.org/wiki/ISO/TC_211_Geographic_information/Geomatics

⇧ デジタルな地理情報と地理学を網羅することを目的にしてると言ったところでしょうか。

「地理情報システム(GIS:Geographic Information System)」とは?

国土交通省の説明によると、

www.mlit.go.jp

  GIS(地理情報システム)とは、地理空間情報活用基本法(平成19年法律第63号)第2条において、「地理空間情報の地理的な把握又は分析を可能とするため、電磁的方式により記録された地理空間情報を電子計算機を使用して電子地図上で一体的に処理する情報システム」と定義されています。

国土情報:GISとは - 国土交通省

 地理空間情報、すなわち位置とそれに関する様々な情報のデータとしては、地形図や都市計画図、土地利用図などの基盤となる地図データや、地形をありのままに写しとった空中写真データ、その上に重ねる植生や気象などの人工衛星等による観測データ、道路や河川などの台帳データ、人口や農業などの統計データ、固定資産や顧客リストなどの各種データベース、GPS全地球測位システム)で観測された車両や携帯電話の位置情報など多様な種類があります。

国土情報:GISとは - 国土交通省

⇧ 日本の中では、日本の法律で定義されてる模様。

日本における「地理空間情報」の定義がよく分からないですが、「『地理空間情報』というデータを処理するシステム」が「地理情報システム(GIS:Geographic Information System)」ですと。

国によって「地理情報システム(GIS:Geographic Information System)」の定義が変わってきそうね。

PostgreSQL拡張機能PostGISとは?

一応、公式のドキュメントによると、

github.com

PostGIS spatial database extension to PostgreSQL [mirror]

https://github.com/postgis/postgis

postgis.net

PostGIS extends the capabilities of the PostgreSQL relational database by adding support storing, indexing and querying geographic data.

https://postgis.net/

PostgreSQLで地理情報を扱えるようにするPostgreSQL拡張機能らしい。

PostgreSQLのドキュメントによりますと、

www.postgresql.org

⇧「pg_available_extensions」でインストール可能な拡張機能の一覧を確認できるらしい、つまり、利用可能な拡張機能の全量が把握できると。

で、

www.postgresql.org

⇧「pg_extensions」で既にインストール済みの拡張機能の一覧を確認できるらしい。

拡張機能が有効か無効かの確認方法については、公式のドキュメントに記載が無いっぽいのだが...

「pg_extensions」で表示されるカラムにstatusとかあれば、有効・無効が分かりやすいと思うのだけど、

なので、推測になってしまうのですが、インストール済みの拡張機能は「無効」になることは無いということかと。

「無効」になるのは、拡張機能が削除された時で、「pg_extensions」で表示されなくなるということかと。つまり、

  • 「pg_extensions」で表示される
    →有効
  • 「pg_extensions」で表示されない
    →無効

と判断する感じになるんかね?

PostgreSQLで地理情報が扱えるようになるという拡張機能PostGISを導入してみる

まずは、PostgreSQLで利用可能な「拡張機能」の一覧を確認してみますか。

PostgreSQLについては、

ts0818.hatenablog.com

⇧ 上記の記事の時に導入したものを利用します。

「WSL 2(Windows SubSystem for Linux 2)」のUbuntuを起動して、

SQLクライアントツールから、PostgreSQLに接続します。

今回は「A5:SQL Mk-2」を利用します。

PostgreSQLに接続後、

SELECT * FROM pg_available_extensions
ORDER BY name;

name	default_version	installed_version	comment
adminpack	2.1	« NULL »	administrative functions for PostgreSQL
amcheck	1.3	« NULL »	functions for verifying relation integrity
autoinc	1.0	« NULL »	functions for autoincrementing fields
bloom	1.0	« NULL »	bloom access method - signature file based index
btree_gin	1.3	« NULL »	support for indexing common datatypes in GIN
btree_gist	1.7	« NULL »	support for indexing common datatypes in GiST
citext	1.6	« NULL »	data type for case-insensitive character strings
cube	1.5	« NULL »	data type for multidimensional cubes
dblink	1.2	« NULL »	connect to other PostgreSQL databases from within a database
dict_int	1.0	« NULL »	text search dictionary template for integers
dict_xsyn	1.0	« NULL »	text search dictionary template for extended synonym processing
earthdistance	1.1	« NULL »	calculate great-circle distances on the surface of the Earth
file_fdw	1.0	« NULL »	foreign-data wrapper for flat file access
fuzzystrmatch	1.1	« NULL »	determine similarities and distance between strings
hstore	1.8	« NULL »	data type for storing sets of (key, value) pairs
insert_username	1.0	« NULL »	functions for tracking who changed a table
intagg	1.1	« NULL »	integer aggregator and enumerator (obsolete)
intarray	1.5	« NULL »	functions, operators, and index support for 1-D arrays of integers
isn	1.2	« NULL »	data types for international product numbering standards
lo	1.1	« NULL »	Large Object maintenance
ltree	1.2	« NULL »	data type for hierarchical tree-like structures
moddatetime	1.0	« NULL »	functions for tracking last modification time
old_snapshot	1.0	« NULL »	utilities in support of old_snapshot_threshold
pageinspect	1.11	« NULL »	inspect the contents of database pages at a low level
pg_buffercache	1.3	« NULL »	examine the shared buffer cache
pg_freespacemap	1.2	« NULL »	examine the free space map (FSM)
pg_prewarm	1.2	« NULL »	prewarm relation data
pg_stat_statements	1.10	« NULL »	track planning and execution statistics of all SQL statements executed
pg_surgery	1.0	« NULL »	extension to perform surgery on a damaged relation
pg_trgm	1.6	« NULL »	text similarity measurement and index searching based on trigrams
pg_visibility	1.2	« NULL »	examine the visibility map (VM) and page-level visibility info
pg_walinspect	1.0	« NULL »	functions to inspect contents of PostgreSQL Write-Ahead Log
pgcrypto	1.3	« NULL »	cryptographic functions
pgrowlocks	1.2	« NULL »	show row-level locking information
pgstattuple	1.5	« NULL »	show tuple-level statistics
plpgsql	1.0	1.0	PL/pgSQL procedural language
postgres_fdw	1.1	« NULL »	foreign-data wrapper for remote PostgreSQL servers
refint	1.0	« NULL »	functions for implementing referential integrity (obsolete)
seg	1.4	« NULL »	data type for representing line segments or floating-point intervals
sslinfo	1.2	« NULL »	information about SSL certificates
tablefunc	1.0	« NULL »	functions that manipulate whole tables, including crosstab
tcn	1.0	« NULL »	Triggered change notifications
tsm_system_rows	1.0	« NULL »	TABLESAMPLE method which accepts number of rows as a limit
tsm_system_time	1.0	« NULL »	TABLESAMPLE method which accepts time in milliseconds as a limit
unaccent	1.1	« NULL »	text search dictionary that removes accents
uuid-ossp	1.1	« NULL »	generate universally unique identifiers (UUIDs)
xml2	1.1	« NULL »	XPath querying and XSLT

⇧ え~っと...、「PostGIS」が存在しないんだが...

gisgeeks.com

⇧ 上記サイト様によりますと、自分で拡張機能をダウンロードしてくる必要があるらしい。PostgreSQLがデフォルトで用意している拡張機能では無い場合は、自分でダウンロードしてくる感じになるんかね?

PostGISのドキュメントにも、

postgis.net

If you are running PostgreSQL as a service from a cloud provider (not installing it yourself) then you probably already have PostGIS installed, and you just need to enable it in your database.

https://postgis.net/documentation/getting_started/

クラウドなどのマネージドデータベースのようなものであれば、既にPostGISがインストール済みになるけど、それ以外はPostGISのインストールをしろってことなんかね?

だとしたら、「pg_available_extensions」から取得できる情報の意味って何なのか...

気を取り直して、別にコマンドプロンプトを起ち上げて、「WSL 2(Windows SubSystem for Linux 2)」のUbuntuSSHログイン。

まずは、Ubuntuのパッケージ一覧を更新。

sudo apt update  

PostGIS」をインストール。

sudo apt install postgis postgresql-15-postgis-3   

で、データベースに再接続して、確認したところ、「PostGIS」がインストール可能なライブラリとして認識されました。

PostgreSQLに、拡張機能PostGIS」をインストール。

CREATE EXTENSION postgis; 

拡張機能PostGIS」が有効になったようです。

PostgreSQLに地理情報に関するデータ型が追加される

拡張機能PostGIS」をインストールすることで、地理情報に関するデータ型がPostgreSQLに追加されるようです。

拡張機能PostGIS」のドキュメントよると、

postgis.net

The Open Geospatial Consortium (OGC) developed the Simple Features Access standard (SFA) to provide a model for geospatial data. It defines the fundamental spatial type of Geometry, along with operations which manipulate and transform geometry values to perform spatial analysis tasks. PostGIS implements the OGC Geometry model as the PostgreSQL data types geometry and geography.

https://postgis.net/docs/using_postgis_dbmanagement.html

拡張機能PostGIS」を追加することで、PostgreSQLとしては、

  • geometry
  • geography

という2つのデータ型が使えるようになるようです。

ただ、

www.postgresql.org

PostgreSQLのドキュメントを見た感じでは、「8.8. Geometric Types」って説明はあることから、「geometry」はありそうなんだけど、「geography」は見当たなそうなんですよね...

まぁ、そもそも、「PostGIS」のドキュメントの方でも、

postgis.net

⇧「geography」が見当たらんだけどね...

何やら、

gis.stackexchange.com

GEOMETRY and GEOGRAPHY are different PostgreSQL TYPES; exposed as higher level (SQL), user-defined composite types to the psql environment, but implemented as lower level (Cbase types.

https://gis.stackexchange.com/questions/367366/is-the-geometry4326-an-alias-for-geography-datatype

PostgreSQLとして「geometry」と「geography」は異なるとは言ってるんだけども、PostgreSQL のドキュメントのどこに「geography」についての記載があるのかは結局のところ分からずですわ...

次回は、実際に、拡張機能PostGIS」をインストールしたことでPostgreSQL に追加されたデータ型のカラムを含むテーブルを作成して、地理情報を扱ってみますかね。

それにしても、PostgreSQL拡張機能の導入の方法、分かり辛い...

毎度モヤモヤ感が半端ない...

今回はこのへんで。