More Related Content
More from yoyamasaki (20)
MySQL 5.7で刷新されたGIS機能のご紹介:FOSS4G Tokaiでの発表資料
- 1. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. |
MySQL 5.7で刷新されたGIS機能のご紹介
Yoshiaki Yamasaki / 山﨑 由章
MySQL Senior Sales Consultant, Asia Pacific and Japan
updated: 2017/12/02
- 2. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
Safe Harbor Statement
以下の事項は、弊社の一般的な製品の方向性に関する概要を説明するものです。
また、情報提供を唯一の目的とするものであり、いかなる契約にも組み込むことはでき
ません。以下の事項は、マテリアルやコード、機能を提供することをコミットメントするも
のではない為、購買決定を行う際の判断材料になさらないで下さい。
オラクル製品に関して記載されている機能の開発、リリースおよび時期については、
弊社の裁量により決定されます。
2
- 4. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
今日伝えたいこと
• MySQLにもGIS機能があります!
• ご意見下さい!
– 本日は終日会場にいます
– 懇親会も参加します
• 試してみて気づいた点があれば、是非フィードバック下さい!
– フィードバックできるページ
• MySQL Bugs
https://bugs.mysql.com/
– バグ報告だけでなく、機能追加リクエストも受け付けています
4
- 5. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
参考情報
• MySQLのインストール方法の参考資料
– MySQL 5.7入門セミナー講演資料 (インストール、アーキテクチャ基礎編)
https://www.mysql.com/jp/why-mysql/presentations/mysql-57-for-beginners-install-ja/
• サンプルデータベース
– ダウンロード先
https://dev.mysql.com/doc/index-other.html
– “sakila database”にgeometory型のデータが含まれています
• ”address”テーブルの”location”列
5
- 6. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
MySQLのGIS機能の歴史
6
- 7. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
要点
• MySQL 5.7では、GIS関連機能が刷新され、InnoDBで以下の機能が
利用できるようになった
– GEOMETRYデータ型
– 空間インデックス(Rツリーインデックス)
– ST_Contains()、ST_Distance()などのSpatial関数
– GeoHash関数
– GeoJSON関数
• MySQL 5.7で、ジオメトリ同士の関係を比較する関数について、
プレフィックスが付かない関数は今後廃止予定となった
– Contains()、Distance()、など
7
- 8. Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 8
MySQLの歴史
4.0
全文検索 (MyISAM)
複数テーブルUPDATE/DELETE
組み込みライブラリ型サーバ
OracleMySQL Sun
3.23
MyISAM
InnoDB
レプリケーション
5.1
プラグガブル・
ストレージエンジン・
アーキテクチャ
パーティショニング
タスクスケジューラ
5.6
全文検索(InnoDB)
memcached API
UNDO表領域
Global Transaction ID
マルチスレッドスレーブ
オンラインALTER TABLE
トランスポータブル表領域
5.5
InnoDBがデフォルトに
準同期型レプリケーション
PERFORMANCE_SCHEMA
1.0-3.22以前
ストレージエンジン (ISAM, HEAP)
マルチスレッド
Windows対応/64bit対応
日本語文字コード (SJIS/UJIS)
5.0
ストアドプロシージャ
ストアドファンクション
カーソル/トリガ/ビュー
XAトランザクション
INFORMATION_SCHEMA
4.1
GIS(MyISAM)
Unicode対応
サブクエリ
CSV, ARCHIVE
ndbcluster
1995 2000 2005 2010 2015
5.7
2015年10月21日 GA
全文検索CJK対応/GIS (InnoDB)
新コストモデル オプティマイザ
ロスレス レプリケーション
マルチソース レプリケーション
グループ レプリケーション
セキュリティ強化
データディクショナリ
NoSQLオプション
・ 1995年:スウェーデンにてMySQL AB設立
・(2005年:オラクルがInnobase Oyを買収)
・ 2008年:Sun MicrosystemsがMySQL ABを買収
・ 2010年:オラクルがSun Microsystemsを買収
8.0
2017年9月21日 RC
トランザクショナルDD
MySQLドキュメントストア
Unicode 9.0サポート
国ごとの照合順序を追加
CTE、Window関数
GIS機能の拡張 (InnoDB)
- 9. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
GIS機能の歴史
• MySQL 4.1
– MyISAMでのみGIS機能が使えるようになった
• geometryデータ型
• Spatialインデックス
• Spatial関数
• MySQL 5.0
– InnoDBでもgeometryデータ型、Spatial関数を扱えるようになった
• Spatialインデックスは使用できず
※地理情報システムに関するオープンな規格であるOpenGISに基づいた実装が
されていたが、MySQL開発チームによる独自実装であった
9
- 10. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
GIS機能の歴史
• MySQL 5.7
– Boost.GeometryというC++のオープンソースライブラリを採用して独自実装をやめ、
InnoDBでGIS関連機能を再実装した
• geometryデータ型
• Spatialインデックス
• Spatial関数(使える関数の種類も増加)
• GeoHashサポート
• GeoJSONサポート
※Boost.Geometryコミュニティとも活発に交流し、MySQLチームから
Boost.Geometryへのコントリビュートも行っている
10
- 11. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
GIS機能の歴史
• MySQL 5.7
– 挙動が不明確で分かり難いSpatial関数は非推奨となった
• 例:Contains()は、以下のどちらの関数と同じ動きをするか分かり難いので廃止予定となった
– MBRContains
– ST_Contains()
– SRIDについては現時点で利用できる機能は限られている
• SRID(Spatial Reference ID:空間参照ID):特定の測地系、座標系に関連付けられた一意なID
• ST_GeomFromWKB(ST_AsBinary(geometry),srid)
11
- 12. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
GIS機能の歴史
• 現在開発中のMySQL 8.0では、引き続きGIS機能の拡張を実施中
– OpenGIS標準準拠
– 演算、データ変換に役立つ各種のSpatial関数
• st_x(geom, x)
• st_y(geom, y)
• st_srid(geom, srid)
– MySQL 5.7で非推奨になった関数の廃止
–Geography サポート
• st_distance() 関数
–Spatial Data、Spatial Index、Spatial関数のSRIDサポート
12
- 14. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
geometryデータ型
• OpenGISジオメトリモデルに基づいた以下のデータが使用可能
– GEOMETRY、GEOMETRYCOLLECTION
– 点:POINT、MULTIPOINT
– 線:LINESTRING、MULTILINESTRING
– 多角形:POLYGON、MULTIPOLYGON
※GEOMETRYには、POINT、LINESTRING、POLYGONを格納可能
※GEOMETRYCOLLECTIONには、MULTIPOINT、MULTILINESTRING、MULTIPOLYGONを
格納可能
14
- 15. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
補足:MySQL 5.7で扱えるのはベクターデータ
• ベクター形式
– 点、線、面(ポリゴン)で表現される
– 一般的にデータベースで扱われるデータ形式
– .Shp, .OSM, .KML, .GeoJSON, など
• ラスター形式
– セルの塗りつぶしで表現される
– 人工衛星、イメージファイルなどで使用される
データ形式
– .tiff, .jpg, .gif, など
15
- 16. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
Spatialインデックス
• MBR(Minimum Bounding Rectangle:最小外接矩形)に基づく
Rツリーインデックス
16
(0,0) (0,1) (0,2)
(1,0) (1,1) (1,2)
(2,0) (2,1) (2,2)
GEOMETRY型の図形
MBR
(隣接する最小の長方形)
- 17. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
Spatialインデックス
• Rツリーインデックスでは、MBR同士の
抱合関係を保持
• MBRContains()、MBRWithin()、
ST_Contains()、ST_Within()などの
関数で利用される
– ST_関数の場合は、最初のスクリーニング
ステップでRツリーインデックスが使用され、
その後正確な形に基づいた判定がされる
• 現時点では2次元データのみをサポート
17
- 18. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
補足:MBRとST_の違い(例:Contains関数)
• MBRContains(g1,g2)
– g1の最小外接矩形にg2の最小外接矩形が含まれているかどうかを示す
• ST_Contains(g1,g2)
– g1にg2が含まれているかどうかを示す
18
- 19. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
Spatial関数
• ジオメトリデータを扱える各種の関数
• 例
– ST_Distance()
• 2つのジオメトリを入力し、2地点間の距離を出力
(単位:角度)
– ST_Distance_Sphere()
• 2つのジオメトリを入力し、2地点間の距離を地球を球体であると仮定して計算して出力
(単位:メートル)
– ST_Contains()
• 前ページ参照
19
- 20. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
GeoHash
• 経緯度の情報を文字列化したもの
– 例:愛知大学 名古屋キャンパス 講義棟(経度:136.884242、緯度:35.161329)の
GeoHash
⇒ xn324bujd4<<後略>>
• 特徴
– 文字列なので、データベースで扱いやすい
(インデックスも活用可能)
– GeoHashの情報は点ではなく範囲(矩形のエリア)を表す
– 文字列の桁数で精度を変えることができる
• 10桁を超えると矩形の一辺が1m以下になるので、経緯度の変わりとしても十分利用可能
• 近接エリアの判定等に利用可能
20
- 21. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
GeoHash
• MySQL 5.7では、GeoHashを扱うための関数が使用できる
– ST_GeoHash():経度、緯度(POINT型のデータでも可)を入力し、GeoHashを出力
– ST_LatFromGeoHash():GeoHashを入力し、経度を出力
– ST_LongFromGeoHash():GeoHashを入力し、緯度を出力
– ST_PointFromGeoHash():GeoHashを入力し、POINT型のデータを出力
21
※詳細を解説しているマニュアル
MySQL 5.7 Reference Manual:13.15.10 Spatial Geohash Functions
https://dev.mysql.com/doc/refman/5.7/en/spatial-geohash-functions.html#function_st-geohash
- 22. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
GeoJSON
• 空間データを扱うためのフォーマットの一つ
• JSONによる表現で空間データを扱える
– 例:愛知大学 名古屋キャンパス 講義棟(経度:136.884242、緯度:35.161329)の
GeoJSONによる表現
⇒ {"type": "Point", "coordinates": [136.884242, 35.161329]}
22
- 23. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
GeoJSON
• MySQL 5.7では、GeoJSONを扱うための関数が使用できる
– ST_AsGeoJSON():ジオメトリ型のデータを入力し、GeoJSONデータを出力
– ST_GeomFromGeoJSON():GeoJSONデータを入力し、ジオメトリ型のデータを出力
23
※詳細を解説しているマニュアル
MySQL 5.7 Reference Manual:13.15.11 Spatial GeoJSON Functions
https://dev.mysql.com/doc/refman/5.7/en/spatial-geojson-functions.html
- 24. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
MySQL Workbenchの活用
24
- 25. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
MySQL Workbench
• 管理ツール
• SQLエディタ
• Performance Dashboard
• Visual Explain
• GIS Viewer
• ER図作成、フォワード/
リバースエンジニアリング、
など豊富な機能
25
データベースアーキテクト、開発者、DBA のための統合ビジュアルツール
商用版のみの機能:
DBドキュメント出力、データモデルの検証、
MySQL Enterprise Backup GUI、MySQL Enterprise Audit GUI
- 26. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
MySQL Workbench – 起動後の画面
• クライアントPCにインストールし、
MySQLデータベースへの
接続定義を作成して接続
26
- 27. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
MySQL Workbench – 管理
• サーバーステータス
• クライアントコネクション
一覧
• ユーザー管理
• データDump/Import
• サーバー起動/停止
• パフォーマンスレポート
• パフォーマンススキーマの
設定変更、など
27
- 28. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
MySQL Workbench – 開発支援:SQL Editor
• SQLエディタ
– キーワードハイライト
– SQL整形
– スニペット、など
• Visual Explain
• オブジェクト確認
• データ確認/編集
• オブジェクト作成、など
28
- 29. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
MySQL Workbench – モデリング
• ER図作成
• フォワードエンジニアリング
• リバースエンジニアリング
• ドキュメント出力(商用版のみ)
• データモデルの検証
(商用版のみ)
• データモデルとDBの同期、
など
29
- 30. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
MySQL Workbench – データベース・マイグレーション・ウィザード
• 移行元
– Microsoft SQL Server, PostgreSQL,
Sybase ASE, Sybase SQL Anywhere,
SQLite, Microsoft Access, MySQL, and more
• オブジェクトとデータの移行
• MySQLバージョンアップグレード
(テスト環境用)
30
- 31. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
"Spatial Viewer" and "Geometry Viewer"
• ジオメトリ型のデータを図示可能
31
- 32. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
Appendix:Some Real World Examples
32
- 33. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
A Starting Point
• My old apartment in
Brooklyn, NY
– 33 Withers Street
Brooklyn, NY 11211
– POINT(<LONG>,<LAT>)
• -73.951353,40.716914
33
https://www.google.com/maps/place/33+Withers+St,+Brooklyn,+NY+11211/@40.7169144,-73.9513538
- 34. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
The Application Use Case
• I’m hungry and in the mood for Thai food
– What Thai restaurants are around me?
– What’s the closest one?
– Can I see the menu, contact info, yelp ratings, etc.?
– How would I get there?
34
- 35. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
Getting Some Data In
• Download a NYC OSM extract:
– http://osm-extracted-metros.s3.amazonaws.com/new-york.osm.bz2
• Import the data using a customized OsmDB.pm Perl module
– http://wiki.openstreetmap.org/wiki/OsmDB.pm (original)
– https://www.dropbox.com/s/l17vj3wf9y13tee/osmdb-scripts.tar.gz (customized)
• Creates a Geometry column named ‘geom’
• Creates a spatial index on the ‘geom’ column
35
mysql -e "create database nyosm"
bunzip2 new-york.osm.bz2
./bulkDB.pl new-york.osm nyosm
- 36. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
The Generated Schema
• http://wiki.openstreetmap.org/wiki/Elements
36
mysql> show tables;
+-----------------+
| Tables_in_nyosm |
+-----------------+
| nodes |
| nodetags |
| relationmembers |
| relations |
| relationtags |
| waynodes |
| ways |
| waytags |
+-----------------+
– We’ll focus on nodes and nodetags for our
examples
– Nodes
• A point or location
– Nodetags
• Metadata about each location
• X name/value pairs
- 37. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
De-normalizing the Tag Data
• Greatly simplify our query
• Allow for the use of a full-text index
– Also improves performance
• Mimic better schema created by osm2pgsql
– http://wiki.openstreetmap.org/wiki/Osm2pgsql/schema#planet_osm_nodes
37
mysql> alter table nodes add column tags text, add fulltext index(tags);
mysql> update nodes set tags=(SELECT group_concat(concat(k, "=", v) SEPARATOR
';') from nodetags where nodetags.id=nodes.id group by nodes.id);
* Source: ESRI
- 38. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
Final Nodes Table
38
mysql> show create table nodes¥G
*************************** 1. row ***************************
Table: nodes
Create Table: CREATE TABLE `nodes` (
`id` bigint(20) DEFAULT NULL,
`geom` geometry NOT NULL,
`user` varchar(50) DEFAULT NULL,
`version` int(11) DEFAULT NULL,
`timestamp` varchar(20) DEFAULT NULL,
`uid` int(11) DEFAULT NULL,
`changeset` int(11) DEFAULT NULL,
`tags` text,
UNIQUE KEY `i_nodeids` (`id`),
SPATIAL KEY `i_geomidx` (`geom`),
FULLTEXT KEY `tags` (`tags`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
- 39. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
Creating a Distance Calculation Function
• Various great circle (orthodrome) distance formulas
– Haversine, Spherical Law of Cosines (my choice), …
– http://en.wikipedia.org/wiki/Great-circle_distance
– Necessary for calculating distances between two Geometries
• Need goes away when we support Geography and/or Projections (ST_Transform)
39
mysql> CREATE FUNCTION slc (lat1 double, lon1 double, lat2 double, lon2 double)
RETURNS double
RETURN 6371 * acos(cos(radians(lat1)) * cos(radians(lat2)) * cos(radians(lon2)
- radians(lon1)) + sin(radians(lat1)) * sin(radians(lat2)));
- 40. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
Creating a Bounding Box For Our Search
• Utilize the r-tree index by limiting area
– Easy with future spatial reference systems support
• WGS84 or SRID 4326 being the most common
– Need to use some additional geographic formulas
• http://www.movable-type.co.uk/scripts/latlong.html
• Need should go away with full SRID support
40
${origlon} = -73.951368
${origlat} = 40.716743
${lon1} = ${origlon} + (${distance_in_km}/abs(cos(radians({$origlat}))*111))
${lat1} = ${origlat} + (${distance_in_km}/111)
${lon2} = ${origlon} - (${distance_in_km}/abs(cos(radians({$origlat}))*111))
${lat2} = ${origlat} - (${distance_in_km}/111)
- 41. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
Calculating the Results
• Our final query, searching within ~ 10km radius
41
mysql> SELECT id,
slc(40.716743, -73.951368, y(geom), x(geom))*1000 as distance_in_meters,
tags, ST_AsText(geom)
FROM nodes
WHERE ST_Contains(ST_Envelope(linestring(point((-73.951368+(10/111)),
(40.716743+(10/111))), point((-73.951368-(10/111)), (40.716743-(10/111))))),
geom)
AND match(tags) against ("+thai +restaurant" IN BOOLEAN MODE)
ORDER BY distance_in_meters¥G
- 42. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
Examining the Results
42
*************************** 1. row ***************************
id: 888976948
distance_in_meters: 614.4973960877276
tags: addr:street=Bedford Avenue;amenity=restaurant;name=Tai
Thai;addr:housenumber=206;phone=7185995556
astext(geom): POINT(-73.958637 40.717174)
*************************** 2. row ***************************
id: 2178443635
distance_in_meters: 2780.87697408101
tags: microbrewery=no;website=http://www.onemorethai.net/;name=One
More Thai;amenity=restaurant;opening_hours=12:00-22:30;cuisine=thai;phone=(212)
228-8858
astext(geom): POINT(-73.983871 40.7210541)
*************************** 3. row ***************************
…
- 43. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
Mapping the Results
43
• From my old place
– -73.951353,40.716914
• To Tai Thai
– -73.958637,40.717174
• Maps APIs
– Google, Bing, Apple, …
https://www.google.com/maps/dir/40.716914,+-73.951353/40.717174,+-73.958637
- 44. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
Appendix:参考情報
44
- 45. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
参考情報
• マニュアル
– http://dev.mysql.com/doc/refman/5.7/en/spatial-extensions.html
• コミュニティフォーラム
– http://forums.mysql.com/list.php?23
• Boost.Geometry
– http://www.boost.org/libs/geometry
• バグ報告&機能追加リクエスト
– http://bugs.mysql.com/
45
- 46. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
参考情報
• MySQL 5.7 and GIS, an Example
http://mysqlserverteam.com/mysql-5-7-and-gis-an-example/
• Importing Raster Based Spatial Data into MySQL 5.7
http://mysqlserverteam.com/importing-raster-based-spatial-data-into-
mysql-5-7/
• MySQL GIS: Boost.Geometry
– http://mysqlserverteam.com/making-use-of-boost-geometry-in-mysql-gis/
– http://mysqlserverteam.com/why-boost-geometry-in-mysql/
– http://mysqlserverteam.com/building-mysql-with-boost/
• MySQL GIS: InnoDB R-Tree
– http://mysqlserverteam.com/innodb-spatial-indexes-in-5-7-4-lab-release/
46
- 47. Copyright © 2017, Oracle and/or its affiliates. All rights reserved.
参考情報
• Spatial Data in MySQL 8.0
https://www.percona.com/live/e17/sites/default/files/slides/Spatial%20Data
%20in%20MySQL%208.0%20-%20FileId%20-%20115506.pdf
• MySLQ 8.0 GIS Demos
https://github.com/nryeng/mysql-8.0-gis-demos
47