MySQL/POINT:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
小 文字替换 -“{{leftrightflex|”替换为“{{Double-columns|”
 
(未显示同一用户的5个中间版本)
第4行: 第4行:
创建测试表和字段。
创建测试表和字段。


{{leftrightflex|<syntaxhighlight lang="mysql">
{{Double-columns|<syntaxhighlight lang="mysql">
CREATE TABLE `test-location` (
CREATE TABLE `test-location` (
   `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '序号',
   `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '序号',
第14行: 第14行:
</pre>}}}}
</pre>}}}}


{{leftrightflex|<syntaxhighlight lang="mysql">
{{Double-columns|<syntaxhighlight lang="mysql">
INSERT INTO `test-location` (location, text) VALUES  
INSERT INTO `test-location` (location, text) VALUES  
   (ST_GeomFromText('POINT(100 200)'), '测试点1'),
   (ST_GeomFromText('POINT(100 200)'), '测试点1'),
第36行: 第36行:
);
);
  </syntaxhighlight>
  </syntaxhighlight>
使用 mysql{{Version|5.6.48}} 测试坐标点。
{{Double-columns|<syntaxhighlight lang="mysql">
select geomfromtext('Point(10.1234 20.5678)');
</syntaxhighlight>|{{consoleout|<pre>
AAAAAAEBAAAAHqfoSC4/JEA2qz5XW5E0QA==
</pre>}}}}
使用 mysql{{Version|8.0.28}} 测试坐标点。
{{Double-columns|<syntaxhighlight lang="mysql">
select st_geomfromtext('Point(10.1234 20.5678)');
</syntaxhighlight>|{{consoleout|<pre>
+-------------------------------------------+
| st_geomfromtext('Point(10.2045 34.2379)') |
+-------------------------------------------+
| POINT(10.2045 34.2379)                    |
+-------------------------------------------+
1 row in set (0.00 sec)
</pre>}}}}


== 函数 ==
== 函数 ==
=== Point(X,Y) ===
Point(X,Y) 是具有精确度的数字的构造函数,不需要先转换为文本,所以速度更快。
=== ST_GeomFromText ===
=== ST_GeomFromText ===
ST_GeomFromText(wkt, srid)可以返回 MySQL 支持的任何空间类型,可以用 WKT 表示。
ST_GeomFromText(wkt, srid)可以返回 MySQL 支持的任何空间类型,可以用 WKT 表示。


=== ST_PointFromText ===
=== ST_PointFromText ===
ST_PointFromText(wkt, srid)POINT 来自 Well-known text的强类型构造函数。


== 查询数据 ==
== 查询数据 ==
{{leftrightflex|<syntaxhighlight lang="mysql">
{{Double-columns|<syntaxhighlight lang="mysql">
SELECT
SELECT
   id,
   id,
第63行: 第85行:


ST_AsText
ST_AsText
{{leftrightflex|<syntaxhighlight lang="mysql">
{{Double-columns|<syntaxhighlight lang="mysql">
SELECT id, ST_AsText(location), text FROM `test-location`;</syntaxhighlight>|{{consoleout|<pre>
SELECT id, ST_AsText(location), text FROM `test-location`;</syntaxhighlight>|{{consoleout|<pre>
+----+---------------------+------------+
+----+---------------------+------------+
第75行: 第97行:


== 更新数据 ==
== 更新数据 ==
{{leftrightflex|<syntaxhighlight lang="mysql">
{{Double-columns|<syntaxhighlight lang="mysql">
update `test-location` set location=st_GeomFromText('POINT(123 100)') where id =1;</syntaxhighlight>|{{consoleout|<pre>
update `test-location` set location=st_GeomFromText('POINT(123 100)') where id =1;</syntaxhighlight>|{{consoleout|<pre>
Query OK, 1 row affected (0.01 sec)
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0
Rows matched: 1  Changed: 1  Warnings: 0
</pre>}}}}
</pre>}}}}
== Well-known binary (WKB) ==
Well-known binary<ref>wikipedia. Well-known text representation of geometry[OL]. https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry#Well-known_binary.</ref>。
{| class="wikitable"
|+ Geometry types, and WKB integer codes
|-
! Type !! 2D !! Z !! M !! ZM
|-
| Geometry
| <code>0000</code> || <code>1000</code> || <code>2000</code> || <code>3000</code>
|-
| Point
| <code>0001</code> || <code>1001</code> || <code>2001</code> || <code>3001</code>
|-
| LineString
| <code>0002</code> || <code>1002</code> || <code>2002</code> || <code>3002</code>
|-
| Polygon
| <code>0003</code> || <code>1003</code> || <code>2003</code> || <code>3003</code>
|-
| MultiPoint
| <code>0004</code> || <code>1004</code> || <code>2004</code> || <code>3004</code>
|-
| MultiLineString
| <code>0005</code> || <code>1005</code> || <code>2005</code> || <code>3005</code>
|-
| MultiPolygon
| <code>0006</code> || <code>1006</code> || <code>2006</code> || <code>3006</code>
|-
| GeometryCollection
| <code>0007</code> || <code>1007</code> || <code>2007</code> || <code>3007</code>
|-
| CircularString
| <code>0008</code> || <code>1008</code> || <code>2008</code> || <code>3008</code>
|-
| CompoundCurve
| <code>0009</code> || <code>1009</code> || <code>2009</code> || <code>3009</code>
|-
| CurvePolygon
| <code>0010</code> || <code>1010</code> || <code>2010</code> || <code>3010</code>
|-
| MultiCurve
| <code>0011</code> || <code>1011</code> || <code>2011</code> || <code>3011</code>
|-
| MultiSurface
| <code>0012</code> || <code>1012</code> || <code>2012</code> || <code>3012</code>
|-
| Curve
| <code>0013</code> || <code>1013</code> || <code>2013</code> || <code>3013</code>
|-
| Surface
| <code>0014</code> || <code>1014</code> || <code>2014</code> || <code>3014</code>
|-
| PolyhedralSurface
| <code>0015</code> || <code>1015</code> || <code>2015</code> || <code>3015</code>
|-
| TIN
| <code>0016</code> || <code>1016</code> || <code>2016</code> || <code>3016</code>
|-
| Triangle
| <code>0017</code> || <code>1017</code> || <code>2017</code> || <code>3017</code>
|-
| Circle
| <code>0018</code> || <code>1018</code> || <code>2018</code> || <code>3018</code>
|-
| GeodesicString
| <code>0019</code> || <code>1019</code> || <code>2019</code> || <code>3019</code>
|-
| EllipticalCurve
| <code>0020</code> || <code>1020</code> || <code>2020</code> || <code>3020</code>
|-
| NurbsCurve
| <code>0021</code> || <code>1021</code> || <code>2021</code> || <code>3021</code>
|-
| Clothoid
| <code>0022</code> || <code>1022</code> || <code>2022</code> || <code>3022</code>
|-
| SpiralCurve
| <code>0023</code> || <code>1023</code> || <code>2023</code> || <code>3023</code>
|-
| CompoundSurface
| <code>0024</code> || <code>1024</code> || <code>2024</code> || <code>3024</code>
|-
| BrepSolid
| <code></code> || <code>1025</code> || <code></code> || <code></code>
|-
| AffinePlacement
| <code>102</code> || <code>1102</code> || <code></code> || <code></code>
|}

2026年11月26日 (四) 05:28的最新版本

测试数据

mysql 之后的版本,空间数据类型的操作函数在命名上统一在前面加上ST,如 GeomFromText 改为 ST_GeomFromText。

创建测试表和字段。

CREATE TABLE `test-location` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '序号',
  `location` point NOT NULL COMMENT '经纬度',
  `text` varchar(50) DEFAULT NULL COMMENT '描述',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4;
Query OK, 0 rows affected (0.08 sec)
INSERT INTO `test-location` (location, text) VALUES 
  (ST_GeomFromText('POINT(100 200)'), '测试点1'),
  (ST_GeomFromText('POINT(300 500)'), '测试点2');
Query OK, 2 rows affected (0.02 sec)
Records: 2  Duplicates: 0  Warnings: 0

添加数据还可以使用其他语句。

SET @tempdata = 'POINT(100 100)';
INSERT INTO `test-location`(location, text) VALUES (
  ST_GeomFromText(@tempdata),
  '测试点3'
);
/** or **/
SET @tempdata = ST_GeomFromText('POINT(100 100)');
INSERT INTO `test-location`(location, text) VALUES (
  @tempdata,
  '测试点4'
);

使用 mysql 测试坐标点。

select geomfromtext('Point(10.1234 20.5678)');
AAAAAAEBAAAAHqfoSC4/JEA2qz5XW5E0QA==

使用 mysql 测试坐标点。

select st_geomfromtext('Point(10.1234 20.5678)');
+-------------------------------------------+
| st_geomfromtext('Point(10.2045 34.2379)') |
+-------------------------------------------+
| POINT(10.2045 34.2379)                    |
+-------------------------------------------+
1 row in set (0.00 sec)

函数

Point(X,Y)

Point(X,Y) 是具有精确度的数字的构造函数,不需要先转换为文本,所以速度更快。

ST_GeomFromText

ST_GeomFromText(wkt, srid)可以返回 MySQL 支持的任何空间类型,可以用 WKT 表示。

ST_PointFromText

ST_PointFromText(wkt, srid)POINT 来自 Well-known text的强类型构造函数。

查询数据

SELECT
  id,
  ST_x(location) x,
  ST_y(location) y,
  location,
  text
FROM `test-location`;
+----+-----+-----+----------------+------------+
| id | x   | y   | location       | text       |
+----+-----+-----+----------------+------------+
|  1 | 100 | 200 | POINT(100 200) | 测试点1 |
|  2 | 300 | 500 | POINT(300 500) | 测试点2 |
+----+-----+-----+----------------+------------+
2 rows in set (0.01 sec)

ST_AsText

SELECT id, ST_AsText(location), text FROM `test-location`;
+----+---------------------+------------+
| id | ST_AsText(location) | text       |
+----+---------------------+------------+
|  1 | POINT(100 200)      | 测试点1 |
|  2 | POINT(300 500)      | 测试点2 |
+----+---------------------+------------+
2 rows in set (0.00 sec)

更新数据

update `test-location` set location=st_GeomFromText('POINT(123 100)') where id =1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0