Mysql空間數據,空間索引,Spatial Data,Spatial Index


前文:

  這兩天因為項目原因看了一下MySQL的空間索引,發現網上的資料不多,查了一下官方文檔,為了強化記憶做了一個簡單的翻譯。基本上理解了mysql空間索引的要點。謹以此紀。

 

Extensions for Spatial Data

Open Geospatial Consortium (OGC) 是一個由超過兩百五十個公司,機構,大學組成的致力於發展管理空間數據的解決方案的組織。
 
OGC 發布了 OpenGIS® Implementation Standard for Geographic information - Simple feature access - Part 2: SQL option , 這個文檔建議了幾種方法擴展RDBMS來支持空間數據。文檔查看: http://www.opengeospatial.org/standards/sfs.
 
遵循OGC文檔,MySQL以 SQL with Geometry Types (幾何類型SQL)環境的一個子集實現了空間擴展。這個環境是指SQL環境擴展的一系列幾何類型。一個 geometry-valued(幾何-值)列由一個帶有幾何類型的列實現。規格文檔描述了一系列的SQL 幾何類型,以及用於創建,分析幾何數值的方法。
 
MySQL spatial extensions實現了空間特性的生成,存儲和分析。
 
  • 用於表示空間值得數據類型 Data types for representing spatial values

  • 用於操作空間值得方法Functions for manipulating spatial values

  • 提高訪問空間列速度的空間索引Spatial indexing for improved access times to spatial columns 

 
數據類型和方法在 MyISAM,  InnoDB, NDB, and ARCHIVE中可用。至於空間索引, MyISAM and InnoDB支持空間和非空間索引,其他引擎支持非空間索引
 
geographic feature(地理特性)  是指世界上的所有東西都有一個location(位置)。這個特性可以是:
 
        
  • 一個實體,例如一座山,一個池塘,一個城市 An entity. For example, a mountain, a pond, a city.

  • 一個空間,例如城區,熱帶地區A space. For example, town district, the tropics.

  • 一個確定的位置。例如一個特定兩條街交叉的十字路口A definable location. For example, a crossroad, as a particular place where two streets intersect. 

 
地理定義為一個點point,或者由點聚合起來表示的任意東西的位置
 

MySQL GIS Conformance(一致性) and Compatibility(兼容性)

Mysql 沒有實現以下GIS特性:
   
     額外的Metadata Views
    
     OpenGIS中 LineString  and MultiLineString的方法length() 應該使用ST_Length()
 

Additional Resources

Spatial Data Types

    MySQL包含OpenGIS 類相對應的數據類型,其中一些類型保存單一幾何值
  • GEOMETRY 幾何體

  • POINT 點

  • LINESTRING 線

  • POLYGON 多邊形

     

GEOMETRY可以存儲任意幾何類型。其他的類型需要特定的幾何類型
其他類型用於保存集合
    
  • MULTIPOINT 多點

  • MULTILINESTRING 多線

  • MULTIPOLYGON 多個多邊形

  • GEOMETRYCOLLECTION 幾何集合

 
GEOMETRYCOLLECTION可以存放任意類型的集合。其他類型需要特定幾何類型
 

12.5.2 The OpenGIS Geometry Model

OGC's SQL with Geometry Types推薦的幾何類型基於OpenGIS Geometry Model.在這個模型中每個幾何包含以下通用的屬性:
    
  • 它由一個空間關系系統關聯起來,這個系統描述了對象定義的坐標空間。

  • 他屬於一些幾何類

12.5.2.1 The Geometry Class Hierarchy幾何類層級

  • Geometry (noninstantiable)幾何

    • Point (instantiable)點

    • Curve (noninstantiable)曲線

      • LineString (instantiable)線串

        • Line線

        • LinearRing線圈

    • Surface (noninstantiable) 面

      • Polygon (instantiable)多邊形

    • GeometryCollection (instantiable)幾何集合

      • MultiPoint (instantiable)多點

      • MultiCurve (noninstantiable)多曲線

        • MultiLineString (instantiable)多線串

      • MultiSurface (noninstantiable)多面

        • MultiPolygon (instantiable) 多個多邊形

 
所有的noninstantiable不可創建,instantiable可創建,所有的類有屬性,可實例化類還包含認證(判斷有效的類實例的規則)
 
Geometry是基本類,他是一個虛擬類。Geometry可實例化子類限制在0、1/2維幾何對象,這個對象存在於一個二維坐標空間。所有可實例化幾何類被定義可以判斷幾何類型實例是否是 topologically closed(拓撲閉,也就是說所有的幾何類型包含他們的邊界)
 
Geometry有以下子類;  Point,  Curve, Surface, and GeometryCollection
        
  • 點表示0維對象 Point represents zero-dimensional objects.

  • Curve曲線代表一維對象,他有LineString線串子類(子-子類=》Line 線,LinearRing線圈)

  • 面Surface表示一個二 維對象,他有Polygon多邊形這個子類

  • GeometryCollection幾何集合有專門的0/1/2維集合類叫做MultiPoint, MultiLineString, and MultiPolygon 來建立PointsLineStrings, and Polygons集合相應的幾何模型, .MultiCurve and MultiSurface 是做為處理 Curves and Surfaces幾何的接口引進的虛擬父類。

 

12.5.2.2 Geometry Class

Geometry是層級中的root class,他是不可實例化類,但是包含一些屬性,這些屬性是所有Geometry子類共有的。
    Geometry Properties
            
  •  Its type. 每一個geometry屬於層級中的某一個可實例化類。

  • Its SRID, or Spatial Reference Identifier.空間關系標示,這個值標識了geometr關聯的空間關系系統,系統描述了幾何對象在其中定義的坐標空間。

    In MySQL, the SRID value是一個整數關聯到幾何value.所有的計算通過歐幾里得(平面)幾何所得.  SRID最大值是232−1. 如果提供了一個更大值只會取其低32位。

  • Its coordinates 在他的空間關系系統中的坐標。由雙精度(8-byte)數據表示. 所有的非空幾何體包含至少一對(X,Y)坐標.空幾何體不包含任何坐標.

    坐標Coordinates和SRID相關 .例如,在不同的坐標系統,兩個對象即使這兩個對象有相同的坐標但是距離可能會不同,,因為在平面 planar 坐標系統的距離和在地理測量geodetic 系統(地球表面上的系統坐標)的距離是不同的東西.

  • Its interior內部, boundary邊界, and exterior外部.

    每一個幾何占據了一些空間中的位置。 幾何體的外部是指所有沒有被幾何體占用的空間.內部是指幾何體占據的空間。邊界是指內部和外部間的交接處

  • Its MBR (最小邊框矩形minimum bounding rectangle), 或者envelope封閉線. 這個是邊界幾何體,由最大的坐標和最小的坐標組成(X,Y):

    ((MINX MINY, MAXX MINY, MAXX MAXY, MINX MAXY, MINX MINY))
    
  • Whether the value is simple or nonsimple.  (LineStringMultiPointMultiLineString) 類型的幾何值既不是simple 也不是 nonsimple.每個類型自行決定自己是simple or nonsimple.

  • Whether the value is closed or not closed. (LineStringMultiString) 的Geometry values 既不是closed 也不是 not closed. .每個類型自行決定自己是 closed or not closed.

  • Whether the value is empty or nonempty 如果一個幾何體不包含任何點則為empty. empty幾何體的內部外部和邊界都沒有定義 ( 用 NULL 表示).一個空幾何體總是simple的包含的區域為0。

  • Its dimension維度. 維度有 −1, 0, 1, or 2:

    • −1 空幾何體

    • 0  沒有長度沒有空間的幾何體

    • 1 非零長度但是0空間的幾何體

    • 2 非零空間的幾何體

    Point 是zero. LineString 是 1. Polygon 是 2. MultiPoint, MultiLineString, and MultiPolygon由他們包含的組件維度決定

12.5.2.3 Point Class

           A Point 是指在坐標系統中單一的位置的幾何體

Point Examples

  • 想象一個大比例尺的世界地圖,一個點可以用來表示一個城市

  • 在一個城市地圖,一個點可以用來表示一個公交站

Point Properties

  • X-coordinate value.

  • Y-coordinate value.

  • 維度為0

  • 邊界為空

 

12.5.2.4 Curve Class

A Curve 是一維幾何體, 通常表示一個序列的點. 其子類定義了點和點之間的插入類型 。Curve  是一個不可實例化的類。

Curve Properties

  • 包含他的所有點的坐標

  • 1維度的幾何體

  • 如果不通過同一個點兩次則為simple, 例外的情況是它的開始和結束是同一個點

  • 起始點和終結點相同的話為closed

  • closed的邊界為空

  • nonclosed 的邊界視為他的兩個端點

  • 如果既是simple又是closed的則為LinearRing

12.5.2.5 LineString Class

A LineString 是兩點之間的線.

LineString Examples

  • 世界地圖上,LineString 可以表示河流

  • 城市地圖上,可以表示街道

LineString Properties

  • A LineString has coordinates of segments, defined by each consecutive pair of points.

  • A LineString is a Line if it consists of exactly two points.

  • A LineString is a LinearRing if it is both closed and simple. 

 
 

12.5.2.6 Surface Class

A Surface 是二維幾何體。不可實例化. 他的可實例化子類是Polygon.

Surface Properties

  • 二維幾何體.

  • The OpenGIS specification定義一個簡單的Surface是由單一的“patch”組成,這個 “patch”是由一個外部邊界和零個或多個內部邊界關聯起來

  • simple Surface 的邊界是由一系列的closed curves對應的外部和內部邊界

 

12.5.2.7 Polygon Class

A Polygon是一個平面Surface 表示一個多邊的幾何體。 由一個外部邊界和0個或者多個內部邊界組成,每一個內部邊界表示幾何體中的一個孔

Polygon Examples

  • On a region map, Polygon objects could represent forests, districts, and so on.

Polygon Assertions

  • 邊界由一系列的LinearRing對象組成它的外部和內部邊界

  • 沒有交叉的圈. 圈可能會相交在一點,但是只能是切點。

  • A Polygon has no lines, spikes, or punctures.

  • A Polygon has an interior that is a connected point set.

  • 可能有孔. The exterior of a Polygon with holes is not connected. Each hole defines a connected component of the exterior.

前面的判斷使得Polygon成為simple幾何體. 

 

12.5.2.8 GeometryCollection Class

A GeometryCollection 是一個任意數量幾何體的集合

所有的子項必須在同一個空間關系系統(相同的坐標空間). 除此之外沒有其他額外約束, 但是GeometryCollection的子類可能限制子項資格. 限制條件基於:

  • 子項類型 (for example, a MultiPoint may contain only Point elements)

  • 維度

  • 約束元素之間的空間重疊的程度

 

12.5.2.9 MultiPoint Class

A MultiPoint是一個由Point 子項組成的集合. 這些點無論如何沒有聯系或者順序.

MultiPoint Examples

  • 世界地圖上,可以表示一列的小島嶼

  • 城市地圖上,可以表示售票網店

MultiPoint Properties

  • 零維度

  • 如果沒有兩點是相同的則為simple。

  • 邊界為空

 

12.5.2.10 MultiCurve Class

A MultiCurve 是有Curve 組成的一個集合。 MultiCurve 不可實例化。

MultiCurve Properties

  • 一維度

  • 當且僅當他的子項都是simple他為simple。任意兩個子項之間唯一的交叉發生在兩個子項的邊界點上

  • A MultiCurve boundary is obtained by applying the “mod 2 union rule” (also known as the “odd-even rule”): A point is in the boundary of a MultiCurve if it is in the boundaries of an odd number of Curve elements.

  • A MultiCurve is closed if all of its elements are closed.

  • The boundary of a closed MultiCurve is always empty.

12.5.2.11 MultiLineString Class

A MultiLineString is a MultiCurve geometry collection composed of LineString elements.

MultiLineString Examples

  • 在一個地區地圖上,可以用來表示河流系統或者鐵路系統

 

12.5.2.12 MultiSurface Class

A MultiSurface is a geometry collection composed of surface elements. 不可實例化.唯一可實例化的子類是 MultiPolygon.

MultiSurface Assertions

  • 其中的Surfaces沒有內部相交

  • 其中的Surface相交點數有限.

12.5.2.13 MultiPolygon Class

A MultiPolygon is a MultiSurface object composed of Polygon elements.

MultiPolygon Examples

  • 可以用來表示地區湖系統

MultiPolygon Assertions

  • 沒有兩個Polygon內部相交。

  • 兩個Polygon不可以相交(相交也是在前面一個條件中禁止的)或者接觸無限個點

  • 一個MultiPolygon可能沒有切線,spikes,或者穿孔.  MultiPolygon 是 一個規則的,closed的點集合

  • A MultiPolygon有不止一個Polygon有不連接的內部. The number of connected components of the interior of a MultiPolygon is equal to the number of Polygon values in theMultiPolygon.

MultiPolygon Properties

  • 二維

  • A MultiPolygon的邊界是一系列的closed curves (LineString values) corresponding to the boundaries of its Polygon elements.

  • Each Curve in the boundary of the MultiPolygon is in the boundary of exactly one Polygon element.

  • Every Curve in the boundary of an Polygon element is in the boundary of the MultiPolygon

 
 

12.5.3 Using Spatial Data

這個章節描述如何創建包含空間數據類型列的表,以及如何操作空間信息.
 

12.5.3.1 Supported Spatial Data Formats

有兩個標准的空間數據格式被用來表示集合對象Two standard spatial data formats are used to represent geometry objects in queries:

  • Well-Known Text (WKT) format

  • Well-Known Binary (WKB) format

在內部,MySQL存儲幾何值到一個既不是WKT也不是WKB的格式。

 

12.5.3.1.1 Well-Known Text (WKT) Format
    The Well-Known Text (WKT) 用來表示將ASCII表格中的幾何數據換成幾何值。OpenGIS規格提供了一個巴科斯范式語法來定義寫入WKT值得規則。

Examples of WKT representations of geometry objects:

  • A Point:

    POINT(15 20)
    

    點坐標之間沒有用逗號分隔。這個和  SQL Point() 方法不同,后者需要用逗號隔開坐標.注意使用適當的語法到給定空間操作的上下文。例如下面的語句都是從Point對象中取出X坐標。第一個直接使用Point() 函數,第二個使用WKT的GeomFormText()轉換Point
    The first produces the object directly using the Point() function. The second uses a WKT representation converted to a Point with GeomFromText().

    mysql> SELECT ST_X(Point(15, 20));
    +---------------------+
    | ST_X(POINT(15, 20)) |
    +---------------------+
    |                  15 |
    +---------------------+
    
    mysql> SELECT ST_X(ST_GeomFromText('POINT(15 20)'));
    +---------------------------------------+
    | ST_X(ST_GeomFromText('POINT(15 20)')) |
    +---------------------------------------+
    |                                    15 |
    +---------------------------------------+
    
  • A LineString有四個點:

    LINESTRING(0 0, 10 10, 20 25, 50 60)
    

    點和點直接由逗號分開

  • A 帶有一個外部圈和一個內部圈的Polygon

    POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7, 5 5))
    
  • 多點:

    MULTIPOINT(0 0, 20 20, 60 60)
    

    在5.7.9中,空間函數例如 ST_MPointFromText() and ST_GeomFromText() 接受WKT格式化的MutilPoint數值中使用圓括號把點包裹起來.例如下面兩個函數調用都是有效的。但是,在5.7.9之前的版本,第二種方式會產生錯誤:

    ST_MPointFromText('MULTIPOINT (1 1, 2 2, 3 3)')
    ST_MPointFromText('MULTIPOINT ((1 1), (2 2), (3 3))')
    

    在5.7.9中,輸出的MultiPoint值會使用圓括號把每個點包裹起來

    mysql> SET @mp = 'MULTIPOINT(1 1, 2 2, 3 3)';
    mysql> SELECT ST_AsText(ST_GeomFromText(@mp));
    +---------------------------------+
    | ST_AsText(ST_GeomFromText(@mp)) |
    +---------------------------------+
    | MULTIPOINT((1 1),(2 2),(3 3))   |
    +---------------------------------+
    

    5.7.9之前的輸出不會帶有括號

    mysql> SET @mp = 'MULTIPOINT(1 1, 2 2, 3 3)';
    mysql> SELECT ST_AsText(ST_GeomFromText(@mp));
    +---------------------------------+
    | ST_AsText(ST_GeomFromText(@mp)) |
    +---------------------------------+
    | MULTIPOINT(1 1,2 2,3 3)         |
    +---------------------------------+
    
  • A MultiLineString with two LineString values:

    MULTILINESTRING((10 10, 20 20), (15 15, 30 15))
    
  • A MultiPolygon with two Polygon values:

    MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0)),((5 5,7 5,7 7,5 7, 5 5)))
    
  • A GeometryCollection consisting of two Point values and one LineString:

    GEOMETRYCOLLECTION(POINT(10 10), POINT(30 30), LINESTRING(15 15, 20 20))
    
12.5.3.1.2 Well-Known Binary (WKB) Format

The Well-Known Binary (WKB) 使用轉換幾何數據成二進制流representation of geometric values is used for exchanging geometry data as binary streams represented by BLOB values containing geometric WKB information. WKB 使用1-byte 無符號整數,4-byte無符號整數,和8-byte 雙精度數字。一個byte是8bit。例如,一個WKB值表示POINT(1,1)用21byte組成這個序列,每一個由兩個十六進制表示。

0101000000000000000000F03F000000000000F03F

這個序列由以下組件組成:

Byte order:   01
WKB type:     01000000
X coordinate: 000000000000F03F
Y coordinate: 000000000000F03F

Component representation is as follows:

  • byte是由0/1表示低位高位存儲。 這種方法也被稱為Network Data Representation (NDR) and External Data Representation (XDR), respectively.

  • The WKB type是用來表明幾何體類型. 值從1到7分別表示 Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, and GeometryCollection.

  • 一個Point值有X,Y坐標。每個坐標由一個雙精度數值表示

 

12.5.3.2 Creating Spatial Columns

MySQL 提供了一個標准的方法去為幾何類型創建空間列。例如, 在 CREATE TABLE or ALTER TABLE. 空間列在 MyISAM, InnoDB, NDB, and ARCHIVE tables 中支持. 

  • 使用 CREATE TABLE 語法創建一個帶有空間列的表

    CREATE TABLE geom (g GEOMETRY);
    
  • 使用 ALTER TABLE 語法添加或者刪除一個表中的空間列

    ALTER TABLE geom ADD pt POINT;
    ALTER TABLE geom DROP pt;
    
 

12.5.3.3 填充Populating Spatial Columns

創建空間列之后,你可以填充進去空間數據。數值必須存儲為內部幾何格式的值。但是你可以轉換成 Well-Known Text (WKT) or Well-Known Binary (WKB) 格式。下面例子展示如何插入幾何值到一個表中。使用WKT轉換成內部幾何格式:

  • 直接在 INSERT  語句中執行轉換

    INSERT INTO geom VALUES (ST_GeomFromText('POINT(1 1)'));
    
    SET @g = 'POINT(1 1)';
    INSERT INTO geom VALUES (ST_GeomFromText(@g));
    
  • INSERT 之前執行轉換

    SET @g = ST_GeomFromText('POINT(1 1)');
    INSERT INTO geom VALUES (@g);
    

下面的例子插入了更加復雜的幾何數據到表中

    SET @g = 'LINESTRING(0 0,1 1,2 2)';
   INSERT INTO geom VALUES (ST_GeomFromText(@g));

   SET @g = 'POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7, 5 5))';
   INSERT INTO geom VALUES (ST_GeomFromText(@g));

  SET @g ='GEOMETRYCOLLECTION(POINT(1 1),LINESTRING(0 0,1 1,2 2,3 3,4 4))';
  INSERT INTO geom VALUES (ST_GeomFromText(@g));

前面的語句使用了ST_GeomFromText() 來創建幾何值。你可以使用type-specific(類型定義)函數//這里留有疑義,下面的例子和前面的相同都是使用ST_GeomFromText() 但是我感覺更像是要使用類似POINT() 函數:

SET @g = 'POINT(1 1)';
INSERT INTO geom VALUES (ST_PointFromText(@g));

SET @g = 'LINESTRING(0 0,1 1,2 2)';
INSERT INTO geom VALUES (ST_LineStringFromText(@g));

SET @g = 'POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7, 5 5))';
INSERT INTO geom VALUES (ST_PolygonFromText(@g));

SET @g =
'GEOMETRYCOLLECTION(POINT(1 1),LINESTRING(0 0,1 1,2 2,3 3,4 4))';
INSERT INTO geom VALUES (ST_GeomCollFromText(@g));

一個應用程序想要在查詢服務器的時候使用WKB 表示幾何數值.下面有幾個方法實現這個需求。例如

  • 插入一個POINT(1 1)使用二進制語法:

    mysql> INSERT INTO geom VALUES
        -> (ST_GeomFromWKB(0x0101000000000000000000F03F000000000000F03F));
    
  • 一個ODBC應用可以發送一個WKB表達式,並把他綁定到一個占位符上,使用BLOB 類型:

    INSERT INTO geom VALUES (ST_GeomFromWKB(?))
    

    其他程序接口可能支持類似的占位符機制

  • C 語言中,你可以使用 mysql_real_escape_string() 跳過一個二進制數值,包含結果到一個查詢字符串中

 

12.5.3.4 讀取Fetching Spatial Data

Geometry數值存儲在表格中可以讀取到內部格式,你可以轉換成WKT或者WKB格式

  • 讀取空間數據到內部格式:

    讀出來的內部格式在表和表之間傳遞很有用

    CREATE TABLE geom2 (g GEOMETRY) SELECT g FROM geom;
    
  • 讀取到WKT格式:

     ST_AsText() 方法將內部格式轉換成WKT字符串

    SELECT ST_AsText(g) FROM geom;
    
  • 讀取到WKB格式:

    ST_AsBinary() 方法將內部格式轉換到一個 包含WKB值得 BLOB

    SELECT ST_AsBinary(g) FROM geom;
    

12.5.3.5 優化空間分析Optimizing Spatial Analysis

對於MyISAM and (as of MySQL 5.7.5) InnoDB tables,在包含空間數據的列中執行搜索操作 可以通過使用SPATIAL indexes(空間索引)進行優化. 典型的操作是:

  • Point 搜索:搜索包含一個給定的點的所有對象

  • 地區搜索:搜索與給定區域重疊的所有對象

MySQL使用 R-Trees with quadratic splitting(二次分割) 來創建空間索引到空間列上.一個空間索引是使用minimum bounding rectangle (最小外包矩形MBR)建立的.對於大多數幾何體來說, the MBR 是一個最小的可以包含這個幾何體的矩形.對於一個水平或者垂直的linestring, the MBR是一個變化的linestring內部 的矩形。對於一個point, the MBR是一個變化的點內的矩形.

同樣也可能創建普通索引到空間列上,在一個非空間索引, you must declare a prefix for any spatial column except for POINT columns.

MyISAM and InnoDB 都支持 SPATIAL and non-SPATIAL indexes.

 
12.5.3.6 Creating Spatial Indexes
 
For MyISAM and (as of MySQL 5.7.5) InnoDB tables, MySQL可以使用類似普通索引的語法創建空間索引, 但是需要使用  SPATIAL關鍵字.索引項必須是非空。下面的例子展示如何創建空間索引
 
    With CREATE TABLE:
 
     CREATE TABLE geom (g GEOMETRY NOT NULL, SPATIAL INDEX(g)) ENGINE=MyISAM;
 
    With ALTER TABLE:
 
     ALTER TABLE geom ADD SPATIAL INDEX(g);
 
    With CREATE INDEX:
 
     CREATE SPATIAL INDEX sp_index ON geom (g);
 
SPATIAL INDEX 創建了一個R-tree 索引。對於支持非空間索引的存儲引擎,引擎創建一個B-tree索引. B-tree 索引在准確值查找上是很有用的,但在范圍掃描就不一樣了。
 
 
刪除spatial indexes, use ALTER TABLE or DROP INDEX:
 
    With ALTER TABLE:
 
    ALTER TABLE geom DROP INDEX g;
 
    With DROP INDEX:
 
     DROP INDEX sp_index ON geom;
 
Example: 假設一個 table geom包含超過32,000 geometries,存放在類型為GEOMETRY 的  g列 .table有一個AUTO_INCREMENT列fid做為對象ID。
 
mysql> DESCRIBE geom;
+-------+----------+------+-----+---------+----------------+
 | Field   |    Type     |  Null |  Key | Default | Extra                 |
+-------+----------+------+-----+---------+----------------+
 | fid      | int(11)     |           | PRI  | NULL     | auto_increment |
 | g        | geometry |           |        |              |                          |
+-------+----------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
 
mysql> SELECT COUNT(*) FROM geom;
+----------+
| count(*)   |
+----------+
|    32376    |
+----------+
1 row in set (0.00 sec)
 
添加一個空間索引到g。
 
mysql> ALTER TABLE geom ADD SPATIAL INDEX(g) ENGINE=MyISAM;
Query OK, 32376 rows affected (4.05 sec)
Records: 32376  Duplicates: 0  Warnings: 0
 

12.5.3.7 Using Spatial Indexes

 
優化器調查是否可用空間索引參與搜索查詢,這個查詢在WHERE子句中使用函數如MBRContains()或MBRWithin()。以下查詢找到所有對象在給定的矩形:

 
mysql> SET @poly =
    -> 'Polygon((30000 15000,
                 31000 15000,
                 31000 16000,
                 30000 16000,
                 30000 15000))';
mysql> SELECT fid,ST_AsText(g) FROM geom WHERE
    -> MBRContains(ST_GeomFromText(@poly),g);
+-----+---------------------------------------------------------------+
| fid | ST_AsText(g)                                                  |
+-----+---------------------------------------------------------------+
|  21 | LINESTRING(30350.4 15828.8,30350.6 15845,30333.8 15845,30 ... |
|  22 | LINESTRING(30350.6 15871.4,30350.6 15887.8,30334 15887.8, ... |
|  23 | LINESTRING(30350.6 15914.2,30350.6 15930.4,30334 15930.4, ... |
|  24 | LINESTRING(30290.2 15823,30290.2 15839.4,30273.4 15839.4, ... |
|  25 | LINESTRING(30291.4 15866.2,30291.6 15882.4,30274.8 15882. ... |
|  26 | LINESTRING(30291.6 15918.2,30291.6 15934.4,30275 15934.4, ... |
| 249 | LINESTRING(30337.8 15938.6,30337.8 15946.8,30320.4 15946. ... |
|   1 | LINESTRING(30250.4 15129.2,30248.8 15138.4,30238.2 15136. ... |
|   2 | LINESTRING(30220.2 15122.8,30217.2 15137.8,30207.6 15136, ... |
|   3 | LINESTRING(30179 15114.4,30176.6 15129.4,30167 15128,3016 ... |
|   4 | LINESTRING(30155.2 15121.4,30140.4 15118.6,30142 15109,30 ... |
|   5 | LINESTRING(30192.4 15085,30177.6 15082.2,30179.2 15072.4, ... |
|   6 | LINESTRING(30244 15087,30229 15086.2,30229.4 15076.4,3024 ... |
|   7 | LINESTRING(30200.6 15059.4,30185.6 15058.6,30186 15048.8, ... |
|  10 | LINESTRING(30179.6 15017.8,30181 15002.8,30190.8 15003.6, ... |
|  11 | LINESTRING(30154.2 15000.4,30168.6 15004.8,30166 15014.2, ... |
|  13 | LINESTRING(30105 15065.8,30108.4 15050.8,30118 15053,3011 ... |
| 154 | LINESTRING(30276.2 15143.8,30261.4 15141,30263 15131.4,30 ... |
| 155 | LINESTRING(30269.8 15084,30269.4 15093.4,30258.6 15093,30 ... |
| 157 | LINESTRING(30128.2 15011,30113.2 15010.2,30113.6 15000.4, ... |
+-----+---------------------------------------------------------------+
20 rows in set (0.00 sec)
 
使用EXPLAIN 檢查這個查詢被執行的方式
 
mysql> SET @poly =
    -> 'Polygon((30000 15000,
                 31000 15000,
                 31000 16000,
                 30000 16000,
                 30000 15000))';
mysql> EXPLAIN SELECT fid,ST_AsText(g) FROM geom WHERE
    -> MBRContains(ST_GeomFromText(@poly),g)\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: geom
         type: range
possible_keys: g
          key: g
      key_len: 32
          ref: NULL
         rows: 50
        Extra: Using where
1 row in set (0.00 sec)
 
如果不使用空間索引的情況:
 
mysql> SET @poly =
    -> 'Polygon((30000 15000,
                 31000 15000,
                 31000 16000,
                 30000 16000,
                 30000 15000))';
mysql> EXPLAIN SELECT fid,ST_AsText(g) FROM g IGNORE INDEX (g) WHERE
    -> MBRContains(ST_GeomFromText(@poly),g)\G
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: geom
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 32376
        Extra: Using where
1 row in set (0.00 sec)
 
   執行不帶空間索引的SELECT語句仍然獲得了相同的數據但導致執行時間從0.00 增加到 0.46 秒:
 
mysql> SET @poly =
    -> 'Polygon((30000 15000,
                 31000 15000,
                 31000 16000,
                 30000 16000,
                 30000 15000))';
mysql> SELECT fid,ST_AsText(g) FROM geom IGNORE INDEX (g) WHERE
    -> MBRContains(ST_GeomFromText(@poly),g);
+-----+---------------------------------------------------------------+
| fid | ST_AsText(g)                                                  |
+-----+---------------------------------------------------------------+
|   1 | LINESTRING(30250.4 15129.2,30248.8 15138.4,30238.2 15136. ... |
|   2 | LINESTRING(30220.2 15122.8,30217.2 15137.8,30207.6 15136, ... |
|   3 | LINESTRING(30179 15114.4,30176.6 15129.4,30167 15128,3016 ... |
|   4 | LINESTRING(30155.2 15121.4,30140.4 15118.6,30142 15109,30 ... |
|   5 | LINESTRING(30192.4 15085,30177.6 15082.2,30179.2 15072.4, ... |
|   6 | LINESTRING(30244 15087,30229 15086.2,30229.4 15076.4,3024 ... |
|   7 | LINESTRING(30200.6 15059.4,30185.6 15058.6,30186 15048.8, ... |
|  10 | LINESTRING(30179.6 15017.8,30181 15002.8,30190.8 15003.6, ... |
|  11 | LINESTRING(30154.2 15000.4,30168.6 15004.8,30166 15014.2, ... |
|  13 | LINESTRING(30105 15065.8,30108.4 15050.8,30118 15053,3011 ... |
|  21 | LINESTRING(30350.4 15828.8,30350.6 15845,30333.8 15845,30 ... |
|  22 | LINESTRING(30350.6 15871.4,30350.6 15887.8,30334 15887.8, ... |
|  23 | LINESTRING(30350.6 15914.2,30350.6 15930.4,30334 15930.4, ... |
|  24 | LINESTRING(30290.2 15823,30290.2 15839.4,30273.4 15839.4, ... |
|  25 | LINESTRING(30291.4 15866.2,30291.6 15882.4,30274.8 15882. ... |
|  26 | LINESTRING(30291.6 15918.2,30291.6 15934.4,30275 15934.4, ... |
| 154 | LINESTRING(30276.2 15143.8,30261.4 15141,30263 15131.4,30 ... |
| 155 | LINESTRING(30269.8 15084,30269.4 15093.4,30258.6 15093,30 ... |
| 157 | LINESTRING(30128.2 15011,30113.2 15010.2,30113.6 15000.4, ... |
| 249 | LINESTRING(30337.8 15938.6,30337.8 15946.8,30320.4 15946. ... |
+-----+---------------------------------------------------------------+
20 rows in set (0.46 sec)
 
 

13.15 Spatial Analysis Functions

     

MySQL提供了在空間數據上執行各種操作的函數。根據操作的類型這些函數可以被分成幾個大類
  • 創建各種格式幾何圖形函數 (WKT, WKB, internal)
  • 幾何圖形格式之間的轉換函數
  • 幾何的定性或定量屬性的訪問函數
  • 描述兩個圖形之間的關系函數
  •  從現有的創建新的幾何圖形函數

 

13.15.1 Spatial Function Reference

The following table lists each spatial function and provides a short description of each one.

Table 13.19 Spatial Functions

Name Description
Area() (棄用 5.7.6) Return Polygon or MultiPolygon area
AsBinary(), AsWKB() (棄用5.7.6) Convert from internal geometry format to WKB
AsText(), AsWKT() (棄用 5.7.6) Convert from internal geometry format to WKT
Buffer() (棄用5.7.6) Return geometry of points within given distance from geometry
Centroid() (棄用5.7.6) Return centroid as a point
Contains() (棄用5.7.6) Whether MBR of one geometry contains MBR of another
ConvexHull() (棄用5.7.6) Return convex hull of geometry
Crosses() (棄用5.7.6) Whether one geometry crosses another
Dimension() (棄用5.7.6) Dimension of geometry
Disjoint() (棄用5.7.6) Whether MBRs of two geometries are disjoint
Distance() (棄用5.7.6) The distance of one geometry from another
EndPoint() (棄用5.7.6) End Point of LineString
Envelope() (棄用 5.7.6) Return MBR of geometry
Equals() (棄用 5.7.6) Whether MBRs of two geometries are equal
ExteriorRing() (棄用5.7.6) Return exterior ring of Polygon
GeomCollFromText(), GeometryCollectionFromText() (棄用5.7.6) Return geometry collection from WKT
GeomCollFromWKB(), GeometryCollectionFromWKB() (棄用5.7.6) Return geometry collection from WKB
GeometryCollection()
從幾何圖形構造幾何圖形集合
GeometryN() (棄用5.7.6) Return N-th geometry from geometry collection
GeometryType() (棄用5.7.6) Return name of geometry type
GeomFromText(), GeometryFromText() (棄用5.7.6) Return geometry from WKT
GeomFromWKB(), GeometryFromWKB() (棄用5.7.6) Return geometry from WKB
GLength() (棄用5.7.6) Return length of LineString
InteriorRingN() (棄用5.7.6) Return N-th interior ring of Polygon
Intersects() (棄用5.7.6) Whether MBRs of two geometries intersect
IsClosed() (棄用5.7.6) Whether a geometry is closed and simple
IsEmpty() (棄用5.7.6) Placeholder function
IsSimple() (棄用5.7.6) Whether a geometry is simple
LineFromText(), LineStringFromText() (棄用5.7.6) Construct LineString from WKT
LineFromWKB(), LineStringFromWKB() (棄用5.7.6) Construct LineString from WKB
LineString()

構造 LineString from Point values

MBRContains() 一個幾何的MBR包含了另一個的MBR
MBRCoveredBy() 一個MBR是否被另一個覆蓋
MBRCovers() 一個MBR是否覆蓋了另一個
MBRDisjoint() 兩個幾何體的MBR是否分離
MBREqual() (棄用 5.7.6) Whether MBRs of two geometries are equal
MBREquals() 兩個幾何體的MBR是否相同
MBRIntersects() 兩個幾何體的MBR是否相交
MBROverlaps() 兩個幾何體的MBR是否重疊
MBRTouches() 兩個幾何體的MBR是否觸碰
MBRWithin() 一個幾何體的MBR是否在另一個的MBR里面
MLineFromText(), MultiLineStringFromText() (deprecated 5.7.6) Construct MultiLineString from WKT
MLineFromWKB(), MultiLineStringFromWKB() (deprecated 5.7.6) Construct MultiLineString from WKB
MPointFromText(), MultiPointFromText() (deprecated 5.7.6) Construct MultiPoint from WKT
MPointFromWKB(), MultiPointFromWKB() (deprecated 5.7.6) Construct MultiPoint from WKB
MPolyFromText(), MultiPolygonFromText() (deprecated 5.7.6) Construct MultiPolygon from WKT
MPolyFromWKB(), MultiPolygonFromWKB() (deprecated 5.7.6) Construct MultiPolygon from WKB
MultiLineString() Contruct MultiLineString from LineString values
MultiPoint() Construct MultiPoint from Point values
MultiPolygon() Construct MultiPolygon from Polygon values
NumGeometries() (deprecated 5.7.6) Return number of geometries in geometry collection
NumInteriorRings() (deprecated 5.7.6) Return number of interior rings in Polygon
NumPoints() (deprecated 5.7.6) Return number of points in LineString
Overlaps() (deprecated 5.7.6) Whether MBRs of two geometries overlap
Point() Construct Point from coordinates
PointFromText() (deprecated 5.7.6) Construct Point from WKT
PointFromWKB() (deprecated 5.7.6) Construct Point from WKB
PointN() (deprecated 5.7.6) Return N-th point from LineString
PolyFromText(), PolygonFromText() (deprecated 5.7.6) Construct Polygon from WKT
PolyFromWKB(), PolygonFromWKB() (deprecated 5.7.6) Construct Polygon from WKB
Polygon() Construct Polygon from LineString arguments
SRID() (deprecated 5.7.6) Return spatial reference system ID for geometry
ST_Area() 返回 Polygon or MultiPolygon 范圍
ST_AsBinary(), ST_AsWKB() 將內部格式轉換成WKB
ST_AsGeoJSON() 從幾何體中生成GeoJSON
ST_AsText(), ST_AsWKT() 將內部格式轉換成WKT
ST_Buffer() 返回給定幾何體給定距離內的幾何體的點
ST_Buffer_Strategy() ST_Buffer()生成策略選項
ST_Centroid() 返回幾何中心點
ST_Contains() 是否一個幾何體包含另外一個
ST_ConvexHull() Return convex hull of geometry
ST_Crosses() 是否一個幾何體和其他的交叉
ST_Difference() 返回兩個幾何體不同的點集合
ST_Dimension() 幾何體維度
ST_Disjoint() 一個幾何體是否和另一個分離
ST_Distance() 兩個幾何體的距離
ST_Distance_Sphere() 兩個幾何體在地球上的最小距離
ST_EndPoint() 返回LineString的結束點
ST_Envelope() 返回幾何體MBR
ST_Equals() 兩個幾何體是否相等
ST_ExteriorRing() 返回Polygon的外部圈
ST_GeoHash() 生成geohash值
ST_GeomCollFromText(), ST_GeometryCollectionFromText(), ST_GeomCollFromTxt() 返回從 WKT生成的幾何體集合
ST_GeomCollFromWKB(), ST_GeometryCollectionFromWKB() 返回從 WKB生成的幾何體集合
ST_GeometryN() 返回集合中第N個幾何體
ST_GeometryType() 返回幾何體類型
ST_GeomFromGeoJSON() 從GeoJSON對象生成幾何體
ST_GeomFromText(), ST_GeometryFromText() 從 WKT返回幾何體
ST_GeomFromWKB(), ST_GeometryFromWKB() 從WKB返回幾何體
ST_InteriorRingN() 返回Polygon的第N個內部環
ST_Intersection() 返回兩個幾何體的交叉點集合
ST_Intersects() 一個幾何體是否和另一個交叉
ST_IsClosed() 一個幾何體是否是simple且closed
ST_IsEmpty() Placeholder function
ST_IsSimple() Whether a geometry is simple
ST_IsValid() Whether a geometry is valid
ST_LatFromGeoHash() 從geohash返回緯度
ST_Length() Return length of LineString
ST_LineFromText(), ST_LineStringFromText() Construct LineString from WKT
ST_LineFromWKB(), ST_LineStringFromWKB() Construct LineString from WKB
ST_LongFromGeoHash() 從geohash返回經度
ST_MakeEnvelope() 兩點之間的矩形
ST_MLineFromText(), ST_MultiLineStringFromText() Construct MultiLineString from WKT
ST_MLineFromWKB(), ST_MultiLineStringFromWKB() Construct MultiLineString from WKB
ST_MPointFromText(), ST_MultiPointFromText() Construct MultiPoint from WKT
ST_MPointFromWKB(), ST_MultiPointFromWKB() Construct MultiPoint from WKB
ST_MPolyFromText(), ST_MultiPolygonFromText() Construct MultiPolygon from WKT
ST_MPolyFromWKB(), ST_MultiPolygonFromWKB() Construct MultiPolygon from WKB
ST_NumGeometries() 返回集合中的幾何體個數
ST_NumInteriorRing(), ST_NumInteriorRings()  返回Polygon內部環的個數
ST_NumPoints() 返回LineString中點的個數
ST_Overlaps() 是否一個幾何體和另一個重疊
ST_PointFromGeoHash() 從geohash轉換成POINT值
ST_PointFromText() 從WKT生成POINT
ST_PointFromWKB() 從WKB生成POINT
ST_PointN() 返回LineString的第N點
ST_PolyFromText(), ST_PolygonFromText() Construct Polygon from WKT
ST_PolyFromWKB(), ST_PolygonFromWKB() Construct Polygon from WKB
ST_Simplify() 返回簡化的幾何體
ST_SRID() 返回幾何體的空間關系系統ID
ST_StartPoint() LineString的開始點
ST_SymDifference() Return point set symmetric difference of two geometries
ST_Touches() 一個幾何體是否觸碰到另一個
ST_Union() 返回兩個幾何體所有點的聯合集合
ST_Validate() Return validated geometry
ST_Within() 一個幾何體是否在另一個中
ST_X() 返回點的X坐標
ST_Y() 返回點的Y坐標
StartPoint() (deprecated 5.7.6) Start Point of LineString
Touches() (deprecated 5.7.6) Whether one geometry touches another
Within() (deprecated 5.7.6) Whether MBR of one geometry is within MBR of another
X() (deprecated 5.7.6) Return X coordinate of Point
Y() (deprecated 5.7.6) Return Y coordinate of Point 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM