+ All Categories
Home > Technology > MySQL 5.7 GIS-Norvald H. Ryeng

MySQL 5.7 GIS-Norvald H. Ryeng

Date post: 14-Jul-2015
Category:
Upload: -
View: 165 times
Download: 0 times
Share this document with a friend
Popular Tags:
39
Transcript
Page 1: MySQL 5.7 GIS-Norvald H. Ryeng
Page 2: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

MySQL 5.7 GIS

Norvald H. Ryeng Software Engineer

Page 3: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

Safe Harbor Statement

The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.

3

Page 4: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

MySQL 5.7 GIS : 議程

1

2

3

4

5

GIS簡介

常用的名詞和概念

MySQL 5.7有那些新功能

幾個真實的案例

MySQL GIS接下來會有什麼新發展

4

Page 5: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

GIS簡介

5

Page 6: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

“所有的事物都是相關的,但較靠近的事物比獨立的事物有更多關係.”

– Waldo Tobler, “A Computer Movie Simulating Urban Growth in the Detroit Region.” Economic Geography 46 (1970), p.236

6

Tobler的第一地理定律

Page 7: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

這是什麼?

• 地理資訊系統

–功能: 地理空間代表真實世界實體的特性 • 通常是某種類型的地圖

–屬性: 描述特性的非空間資料 • 名/值對用以描述位置和使資料可做分組

• 資料格式 –向量資料: 點, 線, 和多邊形

• 通常和關聯式資料庫一起用, 例如MySQL

–格柵資料 : 網格矩陣內的格子在其中放空間資料之專題層 • 通常用於空拍和衛星影像

7

Page 8: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

用來做什麼?

• 定位服務

–某個東西在那裡?

–要如何從A點到B點?

–離我最近的”東西”在那裡 ?

–各位置或點相關的詳情為何?

• 了解並管理地球

–農業資料, 自然資源管理

–經濟計劃和發展

–教育

–科學

8

* Source: ESRI

Page 9: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

如何使用?

9

Store Collect

Analyze Visualize

• 收集空間資料

–免費的(OSM, NGOs, 等.), 非免費/商務的

–客製資料來源

• 存放資料 –在MySQL的表內

• 分析資料 –以SQL查詢分析資料以取得有用的關係

• 圖示資料

–提供的圖含有查出的屬性和關係結果

Page 10: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

常用的名詞和觀念

10

Page 11: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

標準定義組織

• Open Geospatial Consortium (OGC)

–定義和維護GIS的ISO SQL標準

–另外還有其他的事: transformations, markup languages (KML, GML, etc.), 呈現, …

• European Petroleum Survey Group (EPSG)

–主管諸如座標參照系統等事情的機構 • CRS/EPSGID/SRID

–現為OGP的一部份

• Environmental Systems Research Institute

–一個實務上提供標準的商業公司 • 制定了非常普遍的Shapefile (.shp) 格式

• 開發了非常普遍的ArcGIS軟體

11

Page 12: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

常用的名詞

• 座標

–在平面空間的x,y,z座標 (四維是m或measure)

– MySQL目前只支援 2維 (x,y) 座標

• 投射 –使球形的表面可以用平面的格式呈現

–自球面的位置建立”平”的或二維的地圖的必要性

• 座標參照系統 (CRS/SRS/EPSGID/SRID)

–定義一個點在那裡—以經度和緯度座標來代表被標定在實際地球上的點,和定義它和其他點的關係

–也用於計算距離

12

Page 13: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

資料格式

• 向量

–點,線,和多邊形

–位置(x,y,z) 是相對於座標系統

–通常用於資料庫伺服器

–包括 .Shp, .OSM, .KML, .GeoJSON, …

• 格柵

–在網格矩陣的格子, 固定到一個定錨上 (例如. {1,1} 格)

–通常用於空拍,衛星,和其他影像

–包括.tiff, .jpg, .gif, 和其他點陣為基礎的格式

13

Page 14: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

資料來源

• 免費 – OpenStreetMap

– Governments 和NGOs

– Universities (UCGIS) 和其他非營利性組織

• 商業/免費 – Data Depot, Geography Network, Land Info, Macon, NEXRAIN, SPOT image, …

• 客製的

–來自各種來源的地理編碼, 例如用戶產生的影像和GPS資料 • 現今大多數媒體是自動加地理標籤的: tweets, photos, Facebook posts, …

–用ArcGIS, QGIS, GRASS,等建立客製化地圖

14

Page 15: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

資料遷移

• OSGeo 專案 – Geospatial Data Abstraction Library (GDAL/OGR)

• 將各種向量格式資料匯入

• 將格柵式資料轉成向量格式

• ESRI

– ArcGIS • ArcSDE地理資料庫抽象層和資料庫伺服器直接對接

• 在不同的檔案格式間轉換

• Open Street Map

–匯入OSM資料的Perl (OsmDB.pm和Java (Osmosis)工具

15

Page 16: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

MySQL 5.7有什麼新功能

16

Page 17: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

整合 Boost.Geometry

• 替換的客製程式碼

–用來做幾何形狀呈現

–用來做比較

• 符合OGC規定 –改進性能

• Boost.Geometry 包括 –業界和行業專家

–活動頻繁且緊密的社群

• 我們也是Boost.Geometry的貢献者! –兩個全職開發者在上頭貢献

17

Page 18: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

在InnoDB上加空間索引

• 基於R-tree

–完全支援交易

–下達鎖定指令以防止幽靈資料

–記錄包含最小邊界框(minimum bounding box) • 小且緊密

–目前只支援二維資料 • 未來會加上三維的支援

–支援歷史空間索引DDL語法

18

Page 19: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

其他的功能

• GeoHash

–在產生的哈希值加B-tree索引

–在exact matches時查找快速

–在proximity searches時不是非常精確

• GeoJSON

• Additional functions

– ST_IsValid(), ST_IsSimple(), …

• 有限度的支援SRID

19

{ "type": "Feature", "geometry": { "type": "Point", "coordinates": [125.6, 10.1] }, "properties": { "name": "Dinagat Islands" } }

GeoJSON Example

Page 20: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

實際的範例

20

Page 21: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

一個起始點

• 我以前在紐約布魯林的公寓 – 33 Withers Street

Brooklyn, NY 11211

– POINT(<LONG>,<LAT>) • -73.951353,40.716914

21

https://www.google.com/maps/place/33+Withers+St,+Brooklyn,+NY+11211/@40.7169144,-73.9513538

Page 22: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

應用系統的案例

• 我餓了想找個泰式餐廰

–附近有那些泰式餐廰?

–那個是最近的?

–我可以看看菜單,聯絡資訊,客人評價等資訊嗎?

–要如何到那裡?

22

Page 23: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

由下列地方取得一些資料

• 下載一份NYC OSM extract: – http://osm-extracted-metros.s3.amazonaws.com/new-york.osm.bz2

• 用客製化的OsmDB.pm Perl模組匯入資料 – http://wiki.openstreetmap.org/wiki/OsmDB.pm (original)

– https://www.dropbox.com/s/l17vj3wf9y13tee/osmdb-scripts.tar.gz (customized) • 建立一個名為‘geom’的幾何形狀欄位

• 在 ‘geom’ 欄位上建立空間索引

23

mysql -e "create database nyosm" bunzip2 new-york.osm.bz2 ./bulkDB.pl new-york.osm nyosm

Page 24: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

產生的Schema

• http://wiki.openstreetmap.org/wiki/Elements

24

mysql> show tables; +-----------------+ | Tables_in_nyosm | +-----------------+ | nodes | | nodetags | | relationmembers | | relations | | relationtags | | waynodes | | ways | | waytags | +-----------------+

– 我們的範例會著重在節點和節點標籤上

– 節點

• 一個點或位置

– 節點標籤

• 有關各地點的Metadata

• X name/value pairs

Page 25: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

標籤資料的反正規化

• 大輻簡化查詢

• 使我們能用全文檢索的索引 –同時改善性能

• 更能摹擬以osm2pgsql建立的schema – http://wiki.openstreetmap.org/wiki/Osm2pgsql/schema#planet_osm_nodes

25

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

Page 26: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

最後的節點表

26

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

Page 27: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

建立距離計算功能

• 各種大圓(orthodrome)距離計算公式

– Haversine, Spherical Law of Cosines (我選擇), …

– http://en.wikipedia.org/wiki/Great-circle_distance

–必需計算兩個幾何形狀間的距離 • Need goes away when we support Geography and/or Projections (ST_Transform)

27

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)));

Page 28: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

為我們的搜尋建立邊界框

• 用r-tree索引來縮小區域

–使未來的空間參照系統容易支援 • WGS84或SRID 4326 是最常見的

• 需要用某些額外的地理性公式

• http://www.movable-type.co.uk/scripts/latlong.html

• Need should go away with full SRID support

28

${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)

Page 29: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

計算結果

• 我們最後的查詢,搜尋半徑約在10公里之內的目標

29

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

Page 30: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

檢視結果

30

*************************** 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 ***************************

Page 31: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

對應結果

31

• 從我的老地方 – -73.951353,40.716914

• 到太泰餐廰 – -73.958637,40.717174

• 地圖APIs

– Google, Bing, Apple, …

https://www.google.com/maps/dir/40.716914,+-73.951353/40.717174,+-73.958637

Page 32: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

MySQL GIS接下來會有什麼

32

Page 33: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

存儲的加強

• 加強R-tree

–支援三維

• 改良的存儲

–儘可能存固定長度

–壓縮透通化

–改善BLOB的處理

• 改進併行處理能力

33

Page 34: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

地理

• 地理類型

• 理功能

• 使距離的計算非常精確 –簡單的呼叫ST_Distance() 以算出公尺數

34

Page 35: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

更多的功能

• 投射 – ST_Transform()

• 支援三維和Geodetic

• OGC標準的結構資料納入Information_Schema中

• 支援空間參考系統

–從WGS84 (SRID 4326)開始

• 更多的性能優化

• 您還想看到什麼? –請讓我們知道!

35

Page 36: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

附錄 : Workbench Spatial Browser

• Workbench 6.2的新功能

36

Page 37: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

附錄 : 更進一步的資訊

• Manual

– http://dev.mysql.com/doc/refman/5.7/en/spatial-extensions.html

• Community forum

– http://forums.mysql.com/list.php?23

• Boost.Geometry – http://www.boost.org/libs/geometry

• Report GIS bugs and submit feature requests

– http://bugs.mysql.com/

37

Page 38: MySQL 5.7 GIS-Norvald H. Ryeng

Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |

Safe Harbor Statement

The preceding is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle.

38

Page 39: MySQL 5.7 GIS-Norvald H. Ryeng

Recommended