×
Community Blog ApsaraDB for Redis 6.2: Enhanced Geographic Location Features

ApsaraDB for Redis 6.2: Enhanced Geographic Location Features

This article analyzes Alibaba Cloud Tair's two new powerful functions and application scenarios in geographical locations.

By Fanche

The ApsaraDB for Redis (Redis) community has released the Release Candidate (RC) 1 Edition of Redis 6.2. In this release, the Alibaba Cloud Tair Team and the Alibaba Cloud Memory Database Research and Development Team are responsible for the on-cloud ApsaraDB for Redis Community Edition and the ApsaraDB for Redis Enhanced Edition (Tair). This team has contributed a large number of high-quality code and functions to the community. Among them, Alibaba Cloud has contributed two important and powerful APIs, GEOSEARCH and GEOSEARCHSTORE, to improve the query capability of geographic locations.

Based on the analysis of the two new APIs, this article analyzes the Redis application in geographic location and extends the introduction of Alibaba Cloud Tair's more powerful functions and application scenarios in geographical locations.

Detailed Introduction of GEOSEARCH of Redis 6.2

Redis 3.2 has added APIs related to geographic locations:

  • GEOADD adds the given spatial element (latitude, longitude, and name) to the specified key.
  • GEOPOS returns the positions (longitude and latitude) of all elements at the given location from the key.
  • GEODIST returns the distance between two given locations.
  • GEORADIUS centers on the given longitude and latitude and returns all location elements whose distance from the center does not exceed the given maximum distance.
  • GEORADIUSBYMEMBER, which is similar to GEORADIUS, specifies a member of GEO set as the center.
  • GEOHASH returns the GeoHash representation of one or more location elements.

As a result of the acceleration of the localization internet services, businesses based on geographic location have developed rapidly, such as same city purchases, community group purchases, food purchases, and electronic bicycle fences. In the past, open-source Redis could only search circular areas and could not meet users' search requirements. In the latest version, Redis 6.2, the Tair team has contributed the rectangle search capability of the ApsaraDB for Redis Enhanced Edition (Tair) to the community.

1
Figure 1: The contribution of Alibaba Cloud Tair to Redis 6.2 geographic location capabilities

The new GEOSEARCH and GEOSEARCHSTORE commands have richer syntax to meet the application requirements of rectangle searches.

2
Figure 2: Rectangle Search vs. Circle Search

GEOSEARCH key 
[FROMMEMBER member] [FROMLONLAT long lat] 
[BYRADIUS radius unit] [BYBOX width height unit] 
[WITHCOORD] [WITHDIST] [WITHHASH] 
[COUNT count] [ASC|DESC]

This API returns all members located within the boundaries of the area delimited by a given shape in the geospatial information ordered set filled with GEOADD.

There are two specific ways in the search center:

  • FROMMEMBER reads the latitude and longitude from existing keys.
  • FROMLONLAT passes the latitude and longitude from user parameters.

The search conditions are described below:

  • BYRADIUS uses a circle search based on the specified radius length. The effect of the command is equivalent to a GEORADIUS command.
  • BYBOX uses a rectangle search based on the given width and height. A rectangle is an axisymmetric rectangle.

The following are more optional parameters:

  • WITHCOORD returns the longitude and latitude coordinates of the matched string.
  • WITHDIST returns the distance. The unit of distance is converted according to the radius, height, or width.
  • WITHHASH returns the value calculated by GeoHash.
  • COUNT: only returns count elements. Note: The count only filters after all the searches are completed. The CPU consumption for the search cannot be reduced. However, if the returned elements are less, the network bandwidth utilization will be reduced correspondingly.
  • ASC | DESC sorts points by a distance that meets the specified query condition.

GEOSEARCHSTORE is similar to GEOSEARCH, except it stores the search results in a specified key.

As shown in the figure below, in a search for a square (orange area) and its interior circle (blue area), we can see that the points edge 1 and edge 2 can be located by specifying the rectangle in BYBOX format. Edge 1 and edge 2 are located in a square but not in the interior circle.

3
Figure 3: Geographic Location Search

127.0.0.1:6379> GEOADD Sicily 13.361389 38.115556 "Palermo" 15.087269 37.502669 "Catania"
(integer) 2
127.0.0.1:6379> GEOADD Sicily 12.758489 38.788135 "edge1" 17.241510 38.788135 "edge2"
(integer) 2
127.0.0.1:6379> GEOSEARCH Sicily FROMLONLAT 15 37 BYRADIUS 200 km ASC
1) "Catania"
2) "Palermo"
127.0.0.1:6379> GEOSEARCH Sicily FROMLONLAT 15 37 BYBOX 400 400 km ASC
1) "Catania"
2) "Palermo"
3) "edge2"
4) "edge1"

Disadvantages of Redis GEO

In terms of the implementation principle, the bottom layer of Redis's GEO function is based on the GeoHash algorithm, which converts geographic coordinates into 52-bit integer numbers and stores them in the Sorted Set. As a result of the characteristics of GeoHash, the numerical values are more similar as the geographic locations are closer. You can run the ZRANGEBYSCORE key min max WITHSCORES command to query points within the Sorted Set range and check whether the distances are within the query range.

Although the latest 6.2 RC 1 version supports the circle and rectangle searches, Redis GEO still has many limitations in different scenarios, for example:

  • What should I do if the search region is an irregular polygon?
  • What can I do if I want to search for the relationship between a linestring and a polygon (for example, the relationship between the delivery clerk's trajectory and the region) and the relationship between a polygon and a polygon?

We need to re-examine the principles of Geographic Information System (GIS) to solve these problems. The GeoHash principle currently used in Redis reduces the dimension of two-dimensional coordinates and represents them as a long number or Base32 code. However, this index is only suitable for querying the relationship between points and points. For more complex relationships between linestrings and polygons, a more complex index structure, RTree, is required.[1] RTree allows you to store the minimum bounding box of multiple irregular polygons in an RTree for retrieval.

4
Figure 4: RTree Principle

Due to the complex computational and storage indexes, traditional relational databases, such as PostGIS and on-disk databases, are inefficient when the concurrency of a geographic location is high and the query is performed. You can store structured data in the in-memory database and use the high throughput engine of Tair to improve storage and query efficiency.

Introduction to Alibaba Cloud TairGIS

TairGIS is a module of Alibaba Cloud ApsaraDB for Redis Enhanced Edition (Tair). It uses R-tree indexes and supports a variety of API queries of GIS.[2] TairGIS is compatible with Redis GEO. In addition, it supports linestring and polygon queries. It is more powerful and more widely applicable to various geographic location-based applications and businesses.

TairGIS provides the following features:

  • Uses the RTree indexes
  • Supports point, linestring, and polygon-related queries (containing, intersecting, and adjacent)
  • Compatible with Redis GEO

The following shows how TairGIS is compatible with the Redis GEO example above and adds linestring and polygon search:

// TairGIS can add: POINT, LINESTRING, and POLYGON.
127.0.0.1:7379> GIS.ADD soriy Palermo 'POINT (13.361389 38.115556) 'catania 'POINT (15.087269 37.502669)'// Add points
(integer) 2
127.0.0.1:7379> GIS.ADD Sicily edge1 'POINT (12.758489 38.788135)' edge2 'POINT (17.241510 38.788135)'
(integer) 2
127.0.0.1:7379> GIS.SEARCH Sicily RADIUS 15 37 200km // Query by radius
1) (integer) 2
2) 1) "Palermo"
   2) "POINT(13.361389 38.115556)"
   3) "Catania"
   4) "POINT(15.087269 37.502669)"
127.0.0.1:7379> GIS.ADD Sicily polygon 'POLYGON ((13.361389 38.115556, 15.087269 37.502669, 17.241510 38.788135))'// Add the triangles of Palermo, Catania and edge2
(integer) 1
127.0.0.1:7379> GIS.SEARCH Sicily GEOM "POLYGON (12.7484 35.2018, 12.7484 38.7981, 17.2515 38.7981, 17.2515 35.2018))" // Equivalent to BYBOX 400 400 km. TairGIS can specify any shape of polygon query, not limited to rectangle
1) (integer) 5
2) 1) "Palermo"
   2) "POINT(13.361389 38.115556)"
   3) "Catania"
   4) "POINT(15.087269 37.502669)"
   5) "edge1"
   6) "POINT(12.758489 38.788135)"
   7) "edge2"
   8) "POINT(17.24151 38.788135)"
   9) "polygon"
   10) "POLYGON((13.361389 38.115556,15.087269 37.502669,17.24151 38.788135))"
127.0.0.1:7379> GIS.SEARCH family GEOM "POLYGON ((12.7484 35.2018, 12.7484 38.7981, 17.2515 38.7981))" // If you use the triangle query, edge2 is excluded because the polygon and the triangle overlap
1) (integer) 4
2) 1) "Palermo"
   2) "POINT(13.361389 38.115556)"
   3) "Catania"
   4) "POINT(15.087269 37.502669)"
   5) "edge1"
   6) "POINT(12.758489 38.788135)"
   7) "polygon"
   8) "POLYGON((13.361389 38.115556,15.087269 37.502669,17.24151 38.788135))"
127.0.0.1:7379>

The APIs of TairGIS are listed below:

  • GIS.ADD adds points, linestrings, or polygons to the key. Each key is an RTree index area.
  • GIS.GET obtains the added points, linestrings, or polygons.
  • GIS.DEL deletes the added points, linestrings, or polygons.
  • GIS.WITHIN specifies an area, which can be an irregular graph, and searches the points, linestrings, or polygons within this area.
  • GIS.CONTAINS specifies an area and identifies the set of points, linestrings, or polygons that contain this area.
  • GIS.INTERSECTS specifies an area and identifies the set of points, linestrings, or polygons that intersect with this area.
  • GIS.SEARCH specifies the center and radius for the circle search, which is similar to the Near semantics of Redis syntax.
  • GIS.GETALL obtains all points, linestrings, or polygons of a key. You can use options to set the return format.

TairGIS enables the construction of a wide range of geographic location-based applications:

Local Life

In Taobao's store service, TairGIS judges the relationship between the merchant service scope (one polygon) and the consumer (one point) to determine whether the consumer is in the merchant's service scope.

5
Figure 5: Build a local life application

Electronic Fence

The electronic fences judge whether the shared bicycle is in a forbidden parking area and whether it has been returned to the correct area by accessing the containment relationship between the point and the polygon. The electronic fences have a wide range of uses, including safety applications and determining no-fly zones for Unmanned Aerial Vehicle (UAV) takeoff.

6
Figure 6: Build a shared bicycle return area application

Epidemic Prevention and Control

During an epidemic, TairGIS can determine whether a user trajectory (a line) passes through an epidemic area (a polygon). Line and polygon intersection determines whether the user can pass through the epidemic area. For more information, please see Monitor User Trajectories by Using TairGIS.

7
Figure 7: Build a user trajectory determination application

Alibaba Cloud ApsaraDB for Redis Enhanced Edition (Tair) includes the powerful geographic location capabilities of TairGIS and provides a limited-time offer for specific quantities. You can get a 50% discount for new yearly purchases or renewals and a 30% discount for monthly purchases. Redis is fully-compatible with TairGIS and provides higher performance, higher bandwidth, a greater number of connections, and richer and more practical data structures. It has multiple enterprise-level capabilities, such as global distributed multiple activities and data flashback.

References:

[1] Introduction to the working principle of RTree: Guttman, A. (1984). "R-Trees: A Dynamic Index Structure for Spatial Searching"

[2]. TairGIS command document: https://www.alibabacloud.com/help/doc-detail/145971.htm

0 0 0
Share on

ApsaraDB

376 posts | 57 followers

You may also like

Comments

ApsaraDB

376 posts | 57 followers

Related Products

  • ApsaraDB for Redis

    A key value database service that offers in-memory caching and high-speed access to applications hosted on the cloud

    Learn More
  • Tair

    Tair is a Redis-compatible in-memory database service that provides a variety of data structures and enterprise-level capabilities.

    Learn More
  • ApsaraDB for HBase

    ApsaraDB for HBase is a NoSQL database engine that is highly optimized and 100% compatible with the community edition of HBase.

    Learn More
  • ApsaraDB for OceanBase

    A financial-grade distributed relational database that features high stability, high scalability, and high performance.

    Learn More