MongoDB CRUD操作:地理位置查询中的GeoJSON对象

作者 : admin 本文共2288个字,预计阅读时间需要6分钟 发布时间: 2024-06-3 共2人阅读

MongoDB CRUD操作:地理位置查询中的GeoJSON对象

文章目录

  • MongoDB CRUD操作:地理位置查询中的GeoJSON对象
    • Point类型
    • LineString类型
    • Polygon(多边形)类型
      • 单环多边形
      • 多环多边形
    • MultiPoint类型
    • MultiLineString类型
    • MultiPolygon类型
    • GeometryCollection

MongoDB 支持GeoJSON的对象类型有:Point、LineString、Polygon、MultiPoint、MultiLineString、MultiPolygon、GeometryCollection等,本文针对每种类型进行说明。

MongoDB CRUD操作:地理位置查询中的GeoJSON对象插图

要指定GeoJSON的数据需要使用内嵌文档,其形式如下:

<field>: { type: <GeoJSON type> , coordinates: <coordinates> }
  • type字段,指定GeoJSON对象类型
  • coordinates字段,用于指定对象的坐标

指定经纬度坐标时,先经度,后纬度,其中经度值介于-180和180之间(含),纬度值介于-90和90之间(含)。

MongoDB对GeoJSON对象的地理空间查询以球面为计算单位,使用WGS84参考系。

Point类型

下面的示例展示了一个GeoJSON点:

{ type: "Point", coordinates: [ 40, 5 ] }

LineString类型

下面的示例展示了一个GeoJSON LineString:

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

Polygon(多边形)类型

Polygon类型由GeoJSON LinearRing坐标数组构成,这些LinearRing是闭合的,闭合线串至少有四个坐标对,并指定与第一个和最后一个坐标相同的位置。连接曲面上两个点的线可能包含也可能不包含连接平面上这两个点的同一组坐标,连接曲面上两点的线为测地线,应用时要注意仔细检查点以避免共享边、重叠和其他类型交叉点的错误。

单环多边形

下面的示例指定了一个具有外环但没有内环(或孔)的 GeoJSON 多边形。第一个和最后一个坐标必须匹配才能闭合多边形:

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

对于单环多边形,环不能自交。

多环多边形

对于具有多个环的多边形:

  • 第一个描述的环必须是外环
  • 外环不能自相交
  • 任何内环必须完全包含在外环内
  • 内环不能相互交叉或重叠,内环不能共用边缘

下面的示例表示具有内环的GeoJSON 多边形:

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

MongoDB CRUD操作:地理位置查询中的GeoJSON对象插图(1)

MultiPoint类型

需要2dsphere索引,GeoJSON MultiPoint 内嵌文档对点列表进行编码。

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

MultiLineString类型

需要2dsphere索引,下面的示例为 GeoJSON 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类型

需要2dsphere索引,下面的示例展示了GeoJSON MultiPolygon类型:

{
  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

需要2dsphere索引,下面的示例存储 GeoJSON 类型 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 ] ]
       ]
     }
  ]
}
本站无任何商业行为
个人在线分享 » MongoDB CRUD操作:地理位置查询中的GeoJSON对象
E-->