地理空间数据Geometry在MySQL中使用(一)

在MySQL中,Geometry等几何对象可以用来表示地理位置,即用几何对象表示地理空间数据。

在MySQL中支持的几何数据类型包括Geometry、Point、LineString、Polygon以及集合类型的MultiPoint、MultiLineString、MultiPolygon、GeometryCollection。其中Geometry可以表示任意一种几何类型,即在MySQL中,如果一个字段类型是Geometry,则可以存储Point、LineString等其它几何类型的值。其他的几种则需要固定有效的表示格式。

一、数据格式
在MySQL中有3种表达几何对象的格式:
    WKT(文本格式)
    WKB(二进制格式)
    MySQL内部存储格式
WKT是文本格式,因此可以直接使用文本来表示几何数据,实现数据的插入与编辑。

二、常用函数
ST_GEOMFROMTEXT
用于将几何数据从可读的文本类型转换成内部存储的二进制类型。
示例:

INSERT INTO `t_geo_test2`  (id,name,geometry_1)VALUES (1, 'G1', geomFromText('POINT(121.474103 31.232862)'));
ST_ASTEXT

将几何数据转换成可读的文本类型。
示例:
SELECT ST_ASTEXT(linestring_1) from t_geo_test2;
 
ST_GEOHASH
可以将一个地理位置(Point)转换为一个指定长度的字符串,只有Point中存储的是经纬度即第一第二坐标范围分别在(-180,180)与[-90,90]内时才可以转换成功。
SELECT ST_GeoHash(geometry_1,8)  FROM  t_geo_test2 where  id =1;
 
Geohash官网:https://en.wikipedia.org/wiki/Geohash
在PG中使用的是postgis,相关对比见:https://yq.aliyun.com/articles/73995

三、数据SQL

/*
 Navicat Premium Data Transfer

 Source Server         : mysql
 Source Server Type    : MySQL
 Source Server Version : 50723
 Source Host           : 172.20.20.11:3306
 Source Schema         : testdepdb

 Target Server Type    : MySQL
 Target Server Version : 50723
 File Encoding         : 65001

 Date: 17/12/2018 16:28:33
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for t_geo_test2
-- ----------------------------
DROP TABLE IF EXISTS `t_geo_test2`;
CREATE TABLE `t_geo_test2`  (
  `id` int(11) NOT NULL,
  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `geometry_1` geometry NULL,
  `point_1` point NULL,
  `linestring_1` linestring NULL,
  `polygon_1` polygon NULL,
  `multipoint_1` multipoint NULL,
  `multilinestring_1` multilinestring NULL,
  `multipolygon_1` multipolygon NULL,
  `geometrycollection_1` geometrycollection NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of t_geo_test2
-- ----------------------------
INSERT INTO `t_geo_test2` VALUES (1, 'P1', ST_GeomFromText('POINT(121.474 31.2329)'), ST_GeomFromText('POINT(121.474 31.2329)'), ST_GeomFromText('LINESTRING(1 3, 12 5, 12 7)'), ST_GeomFromText('POLYGON((121.474 31.2345, 121.472 31.2333, 121.471 31.2315, 121.472 31.2302, 121.473 31.2304, 121.476 31.232, 121.474 31.2345))'), ST_GeomFromText('MULTIPOINT(0 0, 20 20, 60 60)'), ST_GeomFromText('MULTILINESTRING((10 10, 20 20), (15 15, 30 15))'), ST_GeomFromText('MULTIPOLYGON(((0 0, 10 0, 10 10, 0 10, 0 0)), ((5 5, 7 5, 7 7, 5 7, 5 5)))'), ST_GeomFromText('GEOMETRYCOLLECTION(POINT(10 10), POINT(30 30), LINESTRING(15 15, 20 20))'));
INSERT INTO `t_geo_test2` VALUES (2, 'L1', ST_GeomFromText('LINESTRING(121.342 31.5424, 121.346 31.2468, 121.453 31.4569)'), ST_GeomFromText('POINT(-3 -5)'), ST_GeomFromText('LINESTRING(1 3, 12 5, 12 7)'), NULL, NULL, NULL, NULL, NULL);
INSERT INTO `t_geo_test2` VALUES (3, 'P1', ST_GeomFromText('POLYGON((121.474 31.2345, 121.472 31.2333, 121.471 31.2315, 121.472 31.2302, 121.473 31.2304, 121.476 31.232, 121.474 31.2345))'), ST_GeomFromText('POINT(1 1)'), ST_GeomFromText('LINESTRING(121.342 31.5424, 121.346 31.2468, 121.453 31.4569)'), NULL, NULL, NULL, NULL, NULL);
INSERT INTO `t_geo_test2` VALUES (4, 'MP1', ST_GeomFromText('MULTIPOINT(0 0, 20 20, 60 60)'), ST_GeomFromText('POINT(6 7)'), NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `t_geo_test2` VALUES (5, 'ML1', ST_GeomFromText('MULTILINESTRING((10 10, 20 20), (15 15, 30 15))'), ST_GeomFromText('POINT(4 6)'), NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `t_geo_test2` VALUES (6, 'MPG1', ST_GeomFromText('MULTIPOLYGON(((0 0, 10 0, 10 10, 0 10, 0 0)), ((5 5, 7 5, 7 7, 5 7, 5 5)))'), ST_GeomFromText('POINT(2 5)'), NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `t_geo_test2` VALUES (7, 'G1', ST_GeomFromText('GEOMETRYCOLLECTION(POINT(10 10), POINT(30 30), LINESTRING(15 15, 20 20))'), ST_GeomFromText('POINT(1 3)'), NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `t_geo_test2` VALUES (8, 'P1', NULL, ST_GeomFromText('POINT(-3 -5)'), NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `t_geo_test2` VALUES (9, 'P1', NULL, NULL, ST_GeomFromText('LINESTRING(1 3, 12 5, 12 7)'), NULL, NULL, NULL, NULL, NULL);
INSERT INTO `t_geo_test2` VALUES (10, 'P1', NULL, NULL, NULL, ST_GeomFromText('POLYGON((121.474 31.2345, 121.472 31.2333, 121.471 31.2315, 121.472 31.2302, 121.473 31.2304, 121.476 31.232, 121.474 31.2345))'), NULL, NULL, NULL, NULL);
INSERT INTO `t_geo_test2` VALUES (17, 'P1', ST_GeomFromText('GEOMETRYCOLLECTION(POINT(10 10), POINT(30 30), LINESTRING(15 15, 20 20))'), NULL, NULL, NULL, NULL, NULL, NULL, NULL);
INSERT INTO `t_geo_test2` VALUES (18, 'P1', NULL, NULL, NULL, NULL, NULL, NULL, NULL, ST_GeomFromText('GEOMETRYCOLLECTION(POINT(10 10), POINT(30 30), LINESTRING(15 15, 20 20))'));

SET FOREIGN_KEY_CHECKS = 1;

四、数据预览

五、数据类型说明

类型 含义 说明 示例
Geometry 间数据 任意一种空间类型
Point 坐标值 POINT(121.474 31.2329)
LineString 线 线,由一系列点连接而成 LINESTRING(3 0,  3 3, 3 5)
Polygon 多边形 由多条线组成 POLYGON((1 1, 2 1, 2 2,  1 2, 1 1))
MultiPoint 点集合 集合类,包含多个点 MULTIPOINT(0 0, 20 20, 60 60)
MultiLineString 线集合 集合类,包含多条线 MULTILINESTRING((10 10, 20 20), (15 15, 30 15))
MultiPolygon 多边形集合 集合类,包含多个多边形 MULTIPOLYGON(((0 0, 10 0, 10 10, 0 10, 0 0)), ((5 5, 7 5, 7 7, 5 7, 5 5)))
GeometryCollection 空间数据集合 集合类,可以包括多个点、线、多边形 GEOMETRYCOLLECTION(POINT(10 10), POINT(30 30), LINESTRING(15 15, 20 20))

六、参考

https://www.linuxidc.com/Linux/2018-09/154191.htm

https://yq.aliyun.com/articles/50625

https://blog.csdn.net/rosejeck/article/details/82962511

https://blog.csdn.net/MinjerZhang/article/details/78137795

https://blog.csdn.net/varyall/article/details/80308295

你可能感兴趣的:(数据库,MySQL)