mysql存儲字段類型 :
Geometry
Geometry是幾何對象的基類, 也就是說Point, LineString, Polygon都是Geometry的子類,Point
點對象, 有一個坐標(biāo)值碎乃,沒有長度、面積惠奸、邊界梅誓。
數(shù)據(jù)格式為『經(jīng)度(longitude)在前,維度(latitude)在后,用空格分隔』 例: POINT(121.213342 31.234532)LineString
線對象, 由一系列點連接而成梗掰。
如果線從頭至尾沒有交叉嵌言,那就是簡單的(simple)
如果起點和終點重疊,那就是封閉的(closed)
數(shù)據(jù)格式為『點與點之間用逗號分隔及穗;一個點中的經(jīng)緯度用空格分隔摧茴,與POINT格式一致』例:LINESTRING(121.342423 31.542423,121.345664 31.246790,121.453178 31.456862)Polygon
多邊形對象」÷剑可以是一個實心平面形苛白,即沒有內(nèi)部邊界,也可以有空洞焚虱,類似紐扣
數(shù)據(jù)格式為
『實心型: 一個表示外部邊界的LineString和0個表示內(nèi)部邊界的LineString組成』例:
POLYGON((121.342423 31.542423,121.345664 31.246790,121.453178 31.456862),(121.563633 31.566652,121.233565 31.234565,121.568756 31.454367))
『紐扣型: 一個表示外部邊界的LineString和多個表示內(nèi)部邊界的LineString組成』例: POLYGON((0 0,10 0, 10 10, 0 10))MultiPoint, MultiLineString, MultiPolygon, GeometryCollection
為以上對象的集合购裙。
數(shù)據(jù)格式為下例
MULTIPOINT(0 0, 20 20, 60 60)
MULTILINESTRING((10 10, 20 20), (15 15, 30 15))
MULTIPOLYGON(((0 0,10 0,10 10,0 10,0 0)),((5 5,7 5,7 7,5 7, 5 5)))
GEOMETRYCOLLECTION(POINT(10 10), POINT(30 30), LINESTRING(15 15, 20 20))
常用函數(shù) :
一、幾何對象屬性查詢函數(shù):
Geometry(為基類函數(shù), 點線面都可用)
- Dimension(g) : 返回幾何對象g的維數(shù), 點為0, 線為1, 多邊形為2
- Envelope(g): 返回幾何對象g的最小邊界矩形(xy的極值點)著摔。如果對象為點則返回該點對象缓窜,如果對象為線和多邊形則返回極值xy坐標(biāo)構(gòu)造成的矩形Polygon
- GeometryType(g): 返回幾何對象g的類型名稱, 點為POINT, 線為LINEPOINT, 多邊形為POLYGON
- IsClosed(g): 返回幾何對象g是否封閉 ,條件為該線對象首尾point重合則為封閉, 封閉為1, 不封閉為0, 如果幾何對象不為線對象的話, 返回為null
- IsSimple(g): 返回幾何對象g是否簡單, 條件為該線對象路徑?jīng)]有交叉則為簡單, 簡單為1, 不簡單為0, 如果幾何對象不為線對象的話, 返回為null
Point
- X(p): 返回該點X坐標(biāo)
- Y(p): 返回改點Y坐標(biāo)
LineString
- EndPoint(line): 返回對象line的最后一個點Point
- StartPoint(line): 返回對象line的第一個點Point
- PointN(line, N): 返回對象line中第N個點定续,N從1開始
Polygon
- ExteriorRing(poly): 返回多邊形對象poly的外輪廓線,類型為LineString
- InteriorRingN(poly, N): 返回對象poly的第N個空洞輪廓線摹察,N從1開始
- NumInteriorRings(poly): 返回對象poly的空洞個數(shù)
二倡鲸、返回新的幾何對象
- st_union(g1, g2): 返回 面1和面2的并集
- st_difference(g1, g2): 返回 面1 - (面1和面2的交集)
- st_intersection(g1, g2): 返回 面1和面2的交集
三峭状、查詢幾何對象關(guān)系
-
ST_Contains(a,b): 如果幾何對象a完全包含幾何對象b, 則返回1, 否則0
-
ST_Crosses(a,b): 如果a橫跨b,則返回1,否則返回0
-
ST_Disjoint(a,b): 如果a和b不相交,則返回1.否則返回0
-
ST_Equals(a,b): 如果a和b有相同的幾何描述,則返回1, 否則返回0; 例如一棟樓的兩層xy坐標(biāo)描述一致,所以返回為1
- ST_Intersects(a,b): 與ST_Disjoint結(jié)果完全相反
- ST_Overlaps(a,b): 兩個維度相同的幾何對象相交的交集是一樣維度的幾何對象時, 返回1 , 否則返回0
-
ST_Touches(a,b): 幾何對象a交且只交于b的邊界時, 返回1, 否則0
- ST_Within(a,b): 與ST_Contains(a,b)結(jié)果完全相反
四、數(shù)值計算
- ST_Distance_Sphere(POINT(30 40), POINT(20 30)):計算兩點之間距離(千米)
五胆敞、描述語言轉(zhuǎn)化成幾何對象
- geomfromtext(''): 空間函數(shù)中, 參數(shù)不可直接寫空間描述格式, 需要用geomfromtext('')來將描述語言轉(zhuǎn)化成函數(shù)的對象,例如, 要查找test表中, 所有和 POLYGON((4 4, 4 6, 6 6, 6 4,4 4)) 相交的多邊形, 則sql寫為
select polygon1 from test where st_disjoint(geomfromtext('POLYGON((4 4, 4 6, 6 6, 6 4,4 4))'),polygon1) = 0
六移层、附:MySQL空間相關(guān)函數(shù)一覽表
The following table lists each spatial function and provides a short description of each one.
Name | Description
-
Area()
Return Polygon or MultiPolygon area -
AsBinary(), AsWKB()
Convert from internal geometry format to WKB -
AsText(), AsWKT()
Convert from internal geometry format to WKT -
Buffer()
Return geometry of points within given distance from geometry -
Centroid()
Return centroid as a point -
Contains()
Whether MBR of one geometry contains MBR of another -
Crosses()
Whether one geometry crosses another -
Dimension()
Dimension of geometry -
Disjoint()
Whether MBRs of two geometries are disjoint -
EndPoint()
End Point of LineString -
Envelope()
Return MBR of geometry -
Equals()
Whether MBRs of two geometries are equal -
ExteriorRing()
Return exterior ring of Polygon -
GeomCollFromText(),GeometryCollectionFromText()
Return geometry collection from WKT -
GeomCollFromWKB(),GeometryCollectionFromWKB()
Return geometry collection from WKB -
GeometryCollection()
Construct geometry collection from geometries -
GeometryN()
Return N-th geometry from geometry collection -
GeometryType()
Return name of geometry type -
GeomFromText(),GeometryFromText()
Return geometry from WKT -
GeomFromWKB(),GeometryFromWKB()
Return geometry from WKB -
GLength()
Return length of LineString -
InteriorRingN()
Return N-th interior ring of Polygon -
Intersects()
Whether MBRs of two geometries intersect -
IsClosed()
Whether a geometry is closed and simple -
IsEmpty()
Placeholder_function -
IsSimple()
Whether a geometry is simple -
LineFromText(),LineStringFromText()
Construct LineString from WKT -
LineFromWKB(),LineStringFromWKB()
Construct LineString from WKB -
LineString()
Construct LineString from Point values -
MBRContains()
Whether MBR of one geometry contains MBR of another -
MBRDisjoint()
Whether MBRs of two geometries are disjoint -
MBREqual()
Whether MBRs of two geometries are equal -
MBRIntersects()
Whether MBRs of two geometries intersect -
MBROverlaps()
Whether MBRs of two geometries overlap -
MBRTouches()
Whether MBRs of two geometries touch -
MBRWithin()
Whether MBR of one geometry is within MBR of another -
MLineFromText(),MultiLineStringFromText()
Construct MultiLineString from WKT -
MLineFromWKB(),MultiLineStringFromWKB()
Construct MultiLineString from WKB -
MPointFromText(),MultiPointFromText()
Construct MultiPoint from WKT -
MPointFromWKB(),MultiPointFromWKB()
Construct MultiPoint from WKB -
MPolyFromText(),MultiPolygonFromText()
Construct MultiPolygon from WKT -
MPolyFromWKB(),MultiPolygonFromWKB()
Construct MultiPolygon from WKB -
MultiLineString()
Contruct MultiLineString from LineString values -
MultiPoint()
Construct MultiPoint from Point values -
MultiPolygon()
Construct MultiPolygon from Polygon values -
NumGeometries()
Return number of geometries in geometry collection -
NumInteriorRings()
Return number of interior rings in Polygon -
NumPoints()
Return number of points in LineString -
Overlaps()
Whether MBRs of two geometries overlap -
Point()
Construct Point from coordinates -
PointFromText()
Construct Point from WKT -
PointFromWKB()
Construct Point from WKB -
PointN()
Return N-th point from LineString -
PolyFromText(),PolygonFromText()
Construct Polygon from WKT -
PolyFromWKB()
,PolygonFromWKB()
Construct Polygon from WKB -
Polygon()
Construct Polygon from LineString arguments -
SRID()
Return spatial reference system ID for geometry -
ST_Area()
Return Polygon or MultiPolygon area -
ST_AsBinary()
, ST_AsWKB() Convert from internal geometry format to WKB -
ST_AsText(), ST_AsWKT()
Convert from internal geometry format to WKT -
ST_Buffer()
Return geometry of points within given distance from geometry -
ST_Centroid()
Return centroid as a point -
ST_Contains()
Whether one geometry contains another -
ST_Crosses()
Whether one geometry crosses another -
ST_Difference()
Return point set difference of two geometries -
ST_Dimension()
Dimension of geometry -
ST_Disjoint()
Whether one geometry is disjoint from another -
ST_Distance()
The distance of one geometry from another -
ST_EndPoint()
End Point of LineString -
ST_Envelope()
Return MBR of geometry -
ST_Equals()
Whether one geometry is equal to another -
ST_ExteriorRing()
Return exterior ring of Polygon -
ST_GeomCollFromText(),ST_GeometryCollectionFromText(),ST_GeomCollFromTxt()
Return geometry collection from WKT -
ST_GeomCollFromWKB(),ST_GeometryCollectionFromWKB()
Return geometry collection from WKB -
ST_GeometryN()
Return N-th geometry from geometry collection -
ST_GeometryType()
Return name of geometry type -
ST_GeomFromText(),ST_GeometryFromText()
Return geometry from WKT -
ST_GeomFromWKB(),ST_GeometryFromWKB()
Return geometry from WKB -
ST_InteriorRingN()
Return N-th interior ring of Polygon -
ST_Intersection()
Return point set intersection of two geometries -
ST_Intersects()
Whether one geometry intersects another -
ST_IsClosed()
Whether a geometry is closed and simple -
ST_IsEmpty()
Placeholder_function -
ST_IsSimple()
Whether a geometry is simple -
ST_LineFromText(),ST_LineStringFromText()
Construct LineString from WKT -
ST_LineFromWKB(),ST_LineStringFromWKB()
Construct LineString from WKB -
ST_NumGeometries()
Return number of geometries in geometry collection -
ST_NumInteriorRing(),ST_NumInteriorRings()
Return number of interior rings in Polygon -
ST_NumPoints()
Return number of points in LineString -
ST_Overlaps()
Whether one geometry overlaps another -
ST_PointFromText()
Construct Point from WKT -
ST_PointFromWKB()
Construct Point from WKB -
ST_PointN()
Return N-th point from LineString -
ST_PolyFromText(),ST_PolygonFromText()
Construct Polygon from WKT -
ST_PolyFromWKB(),ST_PolygonFromWKB()
Construct Polygon from WKB -
ST_SRID()
Return spatial reference system ID for geometry -
ST_StartPoint()
Start Point of LineString -
ST_SymDifference()
Return point set symmetric difference of two geometries -
ST_Touches()
Whether one geometry touches another -
ST_Union()
Return point set union of two geometries -
ST_Within()
Whether one geometry is within another -
ST_X()
Return X coordinate of Point -
ST_Y()
Return Y coordinate of Point -
StartPoint()
Start Point of LineString -
Touches()
Whether one geometry touches another -
Within()
Whether MBR of one geometry is within MBR of another -
X()
Return X coordinate of Point -
Y()
Return Y coordinate of Point