地理空间

了解如何在 Redis 中使用地理空间字段和执行地理空间查询

Redis 查询引擎支持地理空间数据。此功能 用于存储地理位置和几何形状 在 JSON 对象的字段中。

注意:
注意不要混淆地理空间索引 功能,以及 Redis 也支持的 Geospatial 数据类型。尽管 这两个功能,数据类型是为了更简单的使用 cases,并且没有格式选项和查询的范围 在 Redis 查询引擎中可用。

您可以为这些字段编制索引并使用查询来查找对象 通过它们的位置或形状与其他形状的关系。 例如,如果您添加一组商店的位置,则可以 找到用户位置 5 公里范围内的所有商店或确定 哪些位于特定城镇的边界内。

Redis 使用坐标点来表示地理空间位置。 您可以存储单个点,但您也可以 使用一组点来定义多边形形状( town)。您可以查询多种类型的交互 在点和形状之间,例如点是否位于 形状或两个形状是否重叠。

Redis 可以将坐标解释为地理经度 和纬度或平面上的笛卡尔坐标。 地理坐标非常适合大型实际位置 和区域(例如城镇和国家/地区)。笛卡尔坐标 更适合于较小的区域(如建筑物中的房间) 或用于游戏、模拟和其他人工场景。

存储地理空间数据

Redis 支持两种不同的地理空间数据架构类型

  • GEO:这使用一种简单的格式,其中单个地理空间 点指定为数字经纬度对。

  • GEOSHAPERedis 社区版 支持GEOSHAPEv7.2 及更高版本中的索引。 此函数使用熟知文本 (WKT) 格式的子集,通过任一地理位置指定点和多边形 coordinates 或 Cartesian 坐标。一个GEOSHAPEfield 支持比GEO, 例如,检查一个形状是否重叠或包含另一个形状。

以下部分更详细地介绍了这些架构类型。

GEO

一个GEOindex 允许您将地理空间数据表示为 包含经纬度对的字符串(例如 “-104.991531, 39.742043”) 或这些的 JSON 数组 字符串。请注意,经度值在 字符串。

例如,您可以为location字段的 JSON 对象 如下所示GEO:

{
    "description": "Navy Blue Slippers",
    "price": 45.99,
    "city": "Denver",
    "location": "-104.991531, 39.742043"
}

{
    "description": "Bright Red Boots",
    "price": 185.75,
    "city": "Various",
    "location": [
        "-104.991531, 39.742043",
        "-105.0618814,40.5150098"
    ]
}

GEO fields allow only basic point and radius queries. For example, the query below finds products within a 100 mile radius of Colorado Springs (Longitude=-104.800644, Latitude=38.846127).

FT.SEARCH productidx '@location:[-104.800644 38.846127 100 mi]'

See Geospatial queries for more information about the available query options and see Geospatial indexing for examples of indexing GEO fields.

GEOSHAPE

Fields indexed as GEOSHAPE support the POINT and POLYGON primitives from the Well-Known Text representation of geometry. The POINT primitive defines a single point in a similar way to a GEO field. The geom field of the example JSON object shown below specifies a point (in Cartesian coordinates, using the standard x,y order):

{
    "name": "Purple Point",
    "geom": "POINT (2 2)"
}

The POLYGON primitive can approximate the outline of any shape using a sequence of points. Specify the coordinates of the corners in the order they occur around the shape (either clockwise or counter-clockwise) and ensure the shape is "closed" by making the final coordinate exactly the same as the first.

Note that POLYGON requires double parentheses around the coordinate list. This is because you can specify additional shapes as a comma-separated list that define "holes" within the enclosing polygon. The holes must have the opposite winding order to the outer polygon (so, if the outer polygon uses a clockwise winding order, the holes must use counter-clockwise). The geom field of the example JSON object shown below specifies a square using Cartesian coordinates in a clockwise winding order:

{
    "name": "Green Square",
    "geom": "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))"
}

The following examples define one POINT and three POLYGON primitives, which are shown in the image below:

POINT (2 2)
POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))
POLYGON ((2 2.5, 2 3.5, 3.5 3.5, 3.5 2.5, 2 2.5))
POLYGON ((3.5 1, 3.75 2, 4 1, 3.5 1))

You can run various types of queries against a geospatial index. For example, the query below returns one primitive that lies within the boundary of the green square (from the example above) but omits the square itself:

> FT.SEARCH geomidx "(-@name:(Green Square) @geom:[WITHIN $qshape])" PARAMS 2 qshape "POLYGON ((1 1, 1 3, 3 3, 3 1, 1 1))" RETURN 1 name DIALECT 2

1) (integer) 1
2) "shape:4"
3) 1) "name"
   2) "[\"Purple Point\"]"

There are four query operations that you can use with GEOSHAPE fields:

  • WITHIN: Find points or shapes that lie entirely within an enclosing shape that you specify in the query.
  • CONTAINS: Find shapes that completely contain the specified point or shape.
  • INTERSECTS: Find shapes whose boundary overlaps another specified shape.
  • DISJOINT: Find shapes whose boundary does not overlap another specified shape.

See Geospatial queries for more information about these query types and see Geospatial indexing for examples of indexing GEOSHAPE fields.

Limitations of geographical coordinates

Planet Earth is actually shaped more like an ellipsoid than a perfect sphere. The spherical coordinate system used by Redis Query Engine is a close approximation to the shape of the Earth but not exact. For most practical uses of geospatial queries, the approximation works very well, but you shouldn't rely on it if you need very precise location data (for example, to track the GPS locations of boats in an emergency response system).

RATE THIS PAGE
Back to top ↑