GeoJSON Objects对象

On this page本页内容

Overview概述

MongoDB supports the GeoJSON object types listed on this page.MongoDB支持此页面上列出的GeoJSON对象类型。

To specify GeoJSON data, use an embedded document with:要指定GeoJSON数据,请使用具有以下内容的嵌入式文档:

  • a field named type that specifies the GeoJSON object type and一个名为type的字段,指定GeoJSON对象类型,并且
  • a field named coordinates that specifies the object's coordinates.名为coordinates的字段,用于指定对象的坐标。

    If specifying latitude and longitude coordinates, list the longitude first and then latitude:如果指定纬度和经度坐标,请先列出经度,然后列出纬度:

    • Valid longitude values are between -180 and 180, both inclusive.有效的经度值介于-180180之间,两者都包括在内。
    • Valid latitude values are between -90 and 90, both inclusive.有效纬度值介于-9090之间,两者都包括在内。
<field>: { type: <GeoJSON type> , coordinates: <coordinates> }

MongoDB geospatial queries on GeoJSON objects calculate on a sphere; MongoDB uses the WGS84 reference system for geospatial queries on GeoJSON objects.MongoDB地理空间查询GeoJSON对象在球体上计算;MongoDB使用WGS84参考系统对GeoJSON对象进行地理空间查询。

Point

The following example specifies a GeoJSON Point:以下示例指定了GeoJSON

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

LineString

The following example specifies a GeoJSON LineString:以下示例指定了GeoJSONLineString

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

Polygon

Polygons多边形 consist of an array of GeoJSON LinearRing coordinate arrays. 由GeoJSON LinearRing坐标数组组成。These LinearRings are closed LineStrings. 这些LinearRings是闭合的LinearRingsClosed LineStrings have at least four coordinate pairs and specify the same position as the first and last coordinates.闭合LinearRings至少有四个坐标对,并指定与第一个和最后一个坐标相同的位置。

The line that joins two points on a curved surface may or may not contain the same set of co-ordinates that joins those two points on a flat surface. 连接曲面上两点的直线可能包含也可能不包含连接平面上两点的同一组坐标。The line that joins two points on a curved surface will be a geodesic. 连接曲面上两点的线将是测地线。Carefully check points to avoid errors with shared edges, as well as overlaps and other types of intersections.仔细检查点,以避免共享边错误,以及重叠和其他类型的交点。

Polygons with a Single Ring具有单个环的多边形

The following example specifies a GeoJSON Polygon with an exterior ring and no interior rings (or holes). 以下示例指定了一个带有外环而没有内环(或孔)的GeoJSONPolygonThe first and last coordinates must match in order to close the polygon:第一个和最后一个坐标必须匹配才能闭合多边形:

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

For Polygons with a single ring, the ring cannot self-intersect.对于具有单个环的多边形,环不能自相交。

Polygons with Multiple Rings具有多个环的多边形

For Polygons with multiple rings:对于具有多个环的多边形:

  • The first described ring must be the exterior ring.第一个描述的环必须是外环。
  • The exterior ring cannot self-intersect.外环不能自交。
  • Any interior ring must be entirely contained by the outer ring.任何内环都必须完全由外圈包含。
  • Interior rings cannot intersect or overlap each other. 内环不能相互交叉或重叠。Interior rings cannot share an edge.内环不能共享边。

The following example represents a GeoJSON polygon with an interior ring:以下示例表示具有内环的GeoJSON多边形:

{
  type : "Polygon",
  coordinates : [
     [ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0 ] ],
     [ [ 2 , 2 ] , [ 3 , 3 ] , [ 4 , 2 ] , [ 2 , 2 ] ]
  ]
}
Diagram of a Polygon with internal ring.

MultiPoint

Requires Versions需要版本

GeoJSON MultiPoint embedded documents encode a list of points.嵌入的文档对点列表进行编码。

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

MultiLineString

Requires Versions需要版本

The following example specifies a GeoJSON MultiLineString:以下示例指定了GeoJSONMultiLineString

{
  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

Requires Versions

The following example specifies a GeoJSON MultiPolygon:以下示例指定了GeoJSONMultiPolygon

{
  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

Requires Versions

The following example stores coordinates of GeoJSON type GeometryCollection:以下示例存储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 ] ]
       ]
     }
  ]
}
←  Find Restaurants with Geospatial QueriesRead Concern →