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,
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.
Redis 3.2 has added APIs related to geographic locations:
GEOADDadds the given spatial element (latitude, longitude, and name) to the specified key.
GEOPOSreturns the positions (longitude and latitude) of all elements at the given location from the key.
GEODISTreturns the distance between two given locations.
GEORADIUScenters 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.
GEOHASHreturns 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.
Figure 1: The contribution of Alibaba Cloud Tair to Redis 6.2 geographic location capabilities
GEOSEARCHSTORE commands have richer syntax to meet the application requirements of rectangle searches.
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
There are two specific ways in the search center:
FROMMEMBERreads the latitude and longitude from existing keys.
FROMLONLATpasses the latitude and longitude from user parameters.
The search conditions are described below:
BYRADIUSuses a circle search based on the specified radius length. The effect of the command is equivalent to a
BYBOXuses a rectangle search based on the given width and height. A rectangle is an axisymmetric rectangle.
The following are more optional parameters:
WITHCOORDreturns the longitude and latitude coordinates of the matched string.
WITHDISTreturns the distance. The unit of distance is converted according to the radius, height, or width.
WITHHASHreturns 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 | DESCsorts 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.
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"
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:
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. RTree allows you to store the minimum bounding box of multiple irregular polygons in an RTree for retrieval.
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.
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. 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:
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.ADDadds points, linestrings, or polygons to the key. Each key is an RTree index area.
GIS.GETobtains the added points, linestrings, or polygons.
GIS.DELdeletes the added points, linestrings, or polygons.
GIS.WITHINspecifies an area, which can be an irregular graph, and searches the points, linestrings, or polygons within this area.
GIS.CONTAINSspecifies an area and identifies the set of points, linestrings, or polygons that contain this area.
GIS.INTERSECTSspecifies an area and identifies the set of points, linestrings, or polygons that intersect with this area.
GIS.SEARCHspecifies the center and radius for the circle search, which is similar to the Near semantics of Redis syntax.
GIS.GETALLobtains 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:
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.
Figure 5: Build a local life application
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.
Figure 6: Build a shared bicycle return area application
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.
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.
 Introduction to the working principle of RTree: Guttman, A. (1984). "R-Trees: A Dynamic Index Structure for Spatial Searching"
. TairGIS command document: https://www.alibabacloud.com/help/doc-detail/145971.htm
Alibaba Clouder - September 2, 2020
ApsaraDB - June 22, 2021
ApsaraDB - October 24, 2018
ApsaraDB - October 21, 2020
digoal - December 21, 2020
Alibaba Cloud Community - March 25, 2022
A key value database service that offers in-memory caching and high-speed access to applications hosted on the cloudLearn More
ApsaraDB for HBase is a NoSQL database engine that is highly optimized and 100% compatible with the community edition of HBase.Learn More
A financial-grade distributed relational database that features high stability, high scalability, and high performance.Learn More
A database engine fully compatible with Apache Cassandra with enterprise-level SLA assurance.Learn More
More Posts by ApsaraDB