MYSQL空间数据操作常用函数

  1. 创建列表

DROP TABLE IF EXISTS `tb_point`; #判断如果存在同名表格则drop创建新表
CREATE TABLE `tb_point` (
  `timestamp` date NOT NULL COMMENT '
时间戳',
  `point`
point NOT NULL COMMENT '经纬度', #空间数据field,notnull设置是必须的
  `type` char(20) character set utf8 default '' COMMENT '点类型',
  `name` varchar(50) character set utf8 NOT NULL COMMENT '点名称',
  `introduce` text character set utf8 COMMENT '介绍,包含文字、视频、目录,json格式',
  `attention` int(5) NOT NULL default '0' COMMENT '关注人数',
  `score` tinyint(2) NOT NULL default '0' COMMENT '评分,用于协同过滤推荐',
  `status` tinyint(2) NOT NULL default '2' COMMENT '状态,用于管理员添加',
  `uri` varchar(255) character set utf8 default NULL COMMENT '维基uri',
 
PRIMARY KEY  (`timestamp`,`point`(25)), #geometry数据类型不能作为primary key
  KEY `index_timestamp` USING BTREE (`timestamp`),
  KEY `index_name` USING HASH (`name`)
)
ENGINE=MyISAM DEFAULT CHARSET=latin1; #选用MYISAM和INNODB都可以,但是MYISAM支持全文检索,如果检索调用较多就用MYISAM,写入频繁就用INNODB

 

 

2.Insert示例

INSERT INTO `tb_point` VALUES ('1900-01-01', GeomFromText('POINT(123.462202 41.804471)'), '景点', '沈阳故宫', '{\"text\":\"1900年左右的沈阳故宫文字介绍\",\"picture\":\"\",\"video\":\"\"}', '1000', '0', '1', null); # GEOMFROMTEXT函数实现WKT到数据库内部几何格式的转换。而GEOMFROMWKB函数用于转换WKB。

3.select示例

SELECT timestamp,AsText(point),type,name,introduce,attention,score,uri FROM tb_point order by AsText(point),`timestamp` DESC ;# 获取文本空间数据 WKT AsText 函数将其转换为字符串格式 AsBinary获取二进制空间数据 WKB 

4. Envelope

Envelope(g)            

返回几何值g的最小边界矩形(MBR)。结果以Polygon值的形式返回。

多边形(polygon)是由边界框的顶点定义的:

POLYGON((MINX MINY, MAXX MINY, MAXX MAXY, MINX MAXY, MINX MINY))

mysql> SELECT AsText(Envelope(GeomFromText('LineString(1 1,2 2)')));

+-------------------------------------------------------+

| AsText(Envelope(GeomFromText('LineString(1 1,2 2)'))) |

+-------------------------------------------------------+

| POLYGON((1 1,2 1,2 2,1 2,1 1))                        |

+-------------------------------------------------------+

5. 用以下SQL从数据表中获得空间数据

SELECT id,name,ASTEXT(pnt),ASTEXT(line),ASTEXT(pgn) from `test`;

ASTEXT函数的功能与GEOMFROMTEXT的功能恰好相反,就是将数据从内部格式转换为WKT;相应的ASBINARY可以转换为WKB。

6. 包含关系

  • MBRContains(g1,g2)

返回10以指明g1的最小边界矩形是否包含g2的最小边界矩形。

  • MBRWithin(g1,g2)

返回10以指明g1的最小边界矩形是否位于g2的最小边界矩形内。

7.Table 12.19 Spatial Functions

Name

Description

Area() (deprecated 5.7.6)

Return Polygon or MultiPolygon area

AsBinary()AsWKB() (deprecated 5.7.6)

Convert from internal geometry format to WKB

AsText()AsWKT() (deprecated 5.7.6)

Convert from internal geometry format to WKT

Buffer() (deprecated 5.7.6)

Return geometry of points within given distance from geometry

Centroid() (deprecated 5.7.6)

Return centroid as a point

Contains() (deprecated 5.7.6)

Whether MBR of one geometry contains MBR of another

ConvexHull() (deprecated 5.7.6)

Return convex hull of geometry

Crosses() (deprecated 5.7.6)

Whether one geometry crosses another

Dimension() (deprecated 5.7.6)

Dimension of geometry

Disjoint() (deprecated 5.7.6)

Whether MBRs of two geometries are disjoint

Distance() (deprecated 5.7.6)

The distance of one geometry from another

EndPoint() (deprecated 5.7.6)

End Point of LineString

Envelope() (deprecated 5.7.6)

Return MBR of geometry

Equals() (deprecated 5.7.6)

Whether MBRs of two geometries are equal

ExteriorRing() (deprecated 5.7.6)

Return exterior ring of Polygon

GeomCollFromText()GeometryCollectionFromText()(deprecated 5.7.6)

Return geometry collection from WKT

GeomCollFromWKB()GeometryCollectionFromWKB() (deprecated 5.7.6)

Return geometry collection from WKB

GeometryCollection()

Construct geometry collection from geometries

GeometryN() (deprecated 5.7.6)

Return N-th geometry from geometry collection

GeometryType() (deprecated 5.7.6)

Return name of geometry type

GeomFromText()GeometryFromText() (deprecated 5.7.6)

Return geometry from WKT

GeomFromWKB()GeometryFromWKB() (deprecated 5.7.6)

Return geometry from WKB

GLength() (deprecated 5.7.6)

Return length of LineString

InteriorRingN() (deprecated 5.7.6)

Return N-th interior ring of Polygon

Intersects() (deprecated 5.7.6)

Whether MBRs of two geometries intersect

IsClosed() (deprecated 5.7.6)

Whether a geometry is closed and simple

IsEmpty() (deprecated 5.7.6)

Placeholder function

IsSimple() (deprecated 5.7.6)

Whether a geometry is simple

LineFromText()LineStringFromText() (deprecated 5.7.6)

Construct LineString from WKT

LineFromWKB()LineStringFromWKB() (deprecated 5.7.6)

Construct LineString from WKB

LineString()

Construct LineString from Point values

MBRContains()

Whether MBR of one geometry contains MBR of another

MBRCoveredBy()

Whether one MBR is covered by another

MBRCovers()

Whether one MBR covers another

MBRDisjoint()

Whether MBRs of two geometries are disjoint

MBREqual() (deprecated 5.7.6)

Whether MBRs of two geometries are equal

MBREquals()

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

Return Polygon or MultiPolygon area

ST_AsBinary()ST_AsWKB()

Convert from internal geometry format to WKB

ST_AsGeoJSON()

Generate GeoJSON object from geometry

ST_AsText()ST_AsWKT()

Convert from internal geometry format to WKT

ST_Buffer()

Return geometry of points within given distance from geometry

ST_Buffer_Strategy()

Produce strategy option for ST_Buffer()

ST_Centroid()

Return centroid as a point

ST_Contains()

Whether one geometry contains another

ST_ConvexHull()

Return convex hull of geometry

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_Distance_Sphere()

Minimum distance on earth between two geometries

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_GeoHash()

Produce a geohash value

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_GeomFromGeoJSON()

Generate geometry from GeoJSON object

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_IsValid()

Whether a geometry is valid

ST_LatFromGeoHash()

Return latitude from geohash value

ST_Length()

Return length of LineString

ST_LineFromText()ST_LineStringFromText()

Construct LineString from WKT

ST_LineFromWKB()ST_LineStringFromWKB()

Construct LineString from WKB

ST_LongFromGeoHash()

Return longitude from geohash value

ST_MakeEnvelope()

Rectangle around two points

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

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_PointFromGeoHash()

Convert geohash value to POINT value

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_Simplify()

Return simplified geometry

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_Validate()

Return validated geometry

ST_Within()

Whether one geometry is within another

ST_X()

Return X coordinate of Point

ST_Y()

Return Y coordinate of Point

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

最后求关注,求点赞,欢迎大家关注我的公众号
在这里插入图片描述
记录所学所用,包括但不限于遥感、地信、气象、生态环境,机器学习知识,相关文献阅读,编程代码实现。偶尔荒腔走板的聊聊其他。欢迎不同领域的朋友们加入进来,多多交流。

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值