mysql存储字段类型 :
Geometry
Geometry是几何对象的基类, 也就是说Point, LineString, Polygon都是Geometry的子类,Point
点对象, 有一个坐标值,没有长度、面积、边界。
数据格式为『经度(longitude)在前,维度(latitude)在后,用空格分隔』 例: POINT(121.213342 31.234532)LineString
线对象, 由一系列点连接而成。
如果线从头至尾没有交叉,那就是简单的(simple)
如果起点和终点重叠,那就是封闭的(closed)
数据格式为『点与点之间用逗号分隔;一个点中的经纬度用空格分隔,与POINT格式一致』例:LINESTRING(121.342423 31.542423,121.345664 31.246790,121.453178 31.456862)Polygon
多边形对象。可以是一个实心平面形,即没有内部边界,也可以有空洞,类似纽扣
数据格式为
『实心型: 一个表示外部边界的LineString和0个表示内部边界的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和多个表示内部边界的LineString组成』例: POLYGON((0 0,10 0, 10 10, 0 10))MultiPoint, MultiLineString, MultiPolygon, GeometryCollection
为以上对象的集合。
数据格式为下例
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))
常用函数 :
一、几何对象属性查询函数:
Geometry(为基类函数, 点线面都可用)
- Dimension(g) : 返回几何对象g的维数, 点为0, 线为1, 多边形为2
- Envelope(g): 返回几何对象g的最小边界矩形(xy的极值点)。如果对象为点则返回该点对象,如果对象为线和多边形则返回极值xy坐标构造成的矩形Polygon
- GeometryType(g): 返回几何对象g的类型名称, 点为POINT, 线为LINEPOINT, 多边形为POLYGON
- IsClosed(g): 返回几何对象g是否封闭 ,条件为该线对象首尾point重合则为封闭, 封闭为1, 不封闭为0, 如果几何对象不为线对象的话, 返回为null
- IsSimple(g): 返回几何对象g是否简单, 条件为该线对象路径没有交叉则为简单, 简单为1, 不简单为0, 如果几何对象不为线对象的话, 返回为null
Point
- X(p): 返回该点X坐标
- Y(p): 返回改点Y坐标
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的空洞个数
二、返回新的几何对象
- st_union(g1, g2): 返回 面1和面2的并集
- st_difference(g1, g2): 返回 面1 - (面1和面2的交集)
- st_intersection(g1, g2): 返回 面1和面2的交集
三、查询几何对象关系
- 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坐标描述一致,所以返回为1
- ST_Intersects(a,b): 与ST_Disjoint结果完全相反
- ST_Overlaps(a,b): 两个维度相同的几何对象相交的交集是一样维度的几何对象时, 返回1 , 否则返回0
- ST_Touches(a,b): 几何对象a交且只交于b的边界时, 返回1, 否则0
- ST_Within(a,b): 与ST_Contains(a,b)结果完全相反
四、数值计算
- ST_Distance_Sphere(POINT(30 40), POINT(20 30)):计算两点之间距离(千米)
五、描述语言转化成几何对象
- geomfromtext(''): 空间函数中, 参数不可直接写空间描述格式, 需要用geomfromtext('')来将描述语言转化成函数的对象,例如, 要查找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空间相关函数一览表
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