MySQL8.0学习记录06 - 数据类型之空间数据类型与MONGO地理空间类型的简单对比

与MySQL类似,Mongo也支持空间地理位置的类型Point、LineString、Polygon、MultiPoint、MultiLineString、MultiPolygon、GeometryCollection。

Mongo有集合类型GeometryCollection,但是没有一个可以指代任意(Point、LineString、Polygon)的Geometry。

基础

MongoDB对GeoJSON对象的地理空间查询是在一个球体上计算,使用WGS84参考系统。而在MySQL中默认是笛卡尔平面,也可以指定SRID属性为4326指明WGS84参考系。目前没看到Mongo可以另外指定参考系的资料。

Mongo的各类型的定义也非常简单,不像MySQL还需要使用ST_GeomFromText。其定义的语法:

{ type: <GeoJSON type> , coordinates: <coordinates> }

Mongo GeoJSON具体类型

Point


{type: "Point",coordinates: [-73.856077, 40.848447]}

LineString


{ type: "LineString", coordinates: [ [ 40, 5 ], [ 41, 6 ] ] }

Polygon

{
  type: "Polygon",
  coordinates: [ [ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0  ] ] ]
}

包含多个边界的Polygon

{
  type : "Polygon",
  coordinates : [
     [ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0 ] ],
     [ [ 2 , 2 ] , [ 3 , 3 ] , [ 4 , 2 ] , [ 2 , 2 ] ]
  ]
}

MultiPoint

{
  type: "MultiPoint",
  coordinates: [
     [ -73.9580, 40.8003 ],
     [ -73.9498, 40.7968 ],
     [ -73.9737, 40.7648 ],
     [ -73.9814, 40.7681 ]
  ]
}

MultiLineString

{
  type: "MultiLineString",
  coordinates: [
     [ [ -73.96943, 40.78519 ], [ -73.96082, 40.78095 ] ],
     [ [ -73.96415, 40.79229 ], [ -73.95544, 40.78854 ] ],
     [ [ -73.97162, 40.78205 ], [ -73.96374, 40.77715 ] ],
     [ [ -73.97880, 40.77247 ], [ -73.97036, 40.76811 ] ]
  ]
}

MultiPolygon

注意与包含多个边界的Polygon的区别

{
  type: "MultiPolygon",
  coordinates: [
     [ [ [ -73.958, 40.8003 ], [ -73.9498, 40.7968 ], [ -73.9737, 40.7648 ], [ -73.9814, 40.7681 ], [ -73.958, 40.8003 ] ] ],
     [ [ [ -73.958, 40.8003 ], [ -73.9498, 40.7968 ], [ -73.9737, 40.7648 ], [ -73.958, 40.8003 ] ] ]
  ]
}

GeometryCollection

{
  type: "GeometryCollection",
  geometries: [
     {
       type: "MultiPoint",
       coordinates: [
          [ -73.9580, 40.8003 ],
          [ -73.9498, 40.7968 ],
          [ -73.9737, 40.7648 ],
          [ -73.9814, 40.7681 ]
       ]
     },
     {
       type: "MultiLineString",
       coordinates: [
          [ [ -73.96943, 40.78519 ], [ -73.96082, 40.78095 ] ],
          [ [ -73.96415, 40.79229 ], [ -73.95544, 40.78854 ] ],
          [ [ -73.97162, 40.78205 ], [ -73.96374, 40.77715 ] ],
          [ [ -73.97880, 40.77247 ], [ -73.97036, 40.76811 ] ]
       ]
     }
  ]
}

Mongo GeoJSON的索引

Mongo中的索引有两种2dsphere2d,2dsphere支持球面几何的查询而2d仅支持二维平面几何的查询;

索引的创建比较简单:

db.collection.createIndex( { <location field> : "2dsphere" } )

db.collection.createIndex( { <location field> : "2d" } )

MySQL的索引只有SPATIAL INDEX,但是可以用不同的函数来进行不同的操作,比如ST_Distance() 、ST_Distance_Sphere() 、ST_HausdorffDistance() 等。

Mongo GeoJSON查询操作

主要的查询操作有

  • $geoIntersects,几何相交,注意只支持2dsphere索引类型
  • $geoWithin
  • $near
  • $nearSphere

配合各种Geometry Specifiers,Mongo可以做到各式各样的查询。具体参见query-geospatial

MySQL也提供了一大批函数来支持各种查询,比如ST_Intersects()、ST_Within()、ST_Distance_Sphere()、ST_Overlaps() 、ST_Contains() 等等。列表参见spatial-function-reference

MySQL和Mongo的查询都是非常丰富的。

MySQL 转GeoJSON

MySQL中提供了ST_AsGeoJSON,可以将几何/地理数据类型转成GeoJSON。

如下,其中2指精度,四舍五入

SELECT ST_AsGeoJSON(ST_GeomFromText('POINT(11.11111 12.22222)'),2);
-- {"type": "Point", "coordinates": [11.11, 12.22]}

再比如:

SELECT ST_AsGeoJSON(ST_GeomFromText('GEOMETRYCOLLECTION(POINT(1 1),LINESTRING(0 0,1 1,2 2,3 3,4 4))'),2);

输出如下:

{"type": "GeometryCollection", "geometries": [{"type": "Point", "coordinates": [1.0, 1.0]}, {"type": "LineString", "coordinates": [[0.0, 0.0], [1.0, 1.0], [2.0, 2.0], [3.0, 3.0], [4.0, 4.0]]}]}

MySQL提供了将GeoJSON转几何/地理数据类型的函数ST_GeomFromGeoJSON

Mongo的 legacy coordinate pairs

除了GeoJSON,Mongo还支持传统的坐标对的方式,其定义方式是:

<field>: [<longitude>, <latitude> ]

<field>: { <field1>: <longitude>, <field2>: <latitude> }

这种方式也可以使用平面的2d索引

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值