This topic describes how to use the TairGIS data structure of Tair to implement local purchase services.
Background information
The local purchase services on e-commerce platforms are provided based on the information about the nearest store that provides what a consumer wants.
Every store that signs up for e-commerce platforms has an available scope for online ordering. The scope may be a specific district, an irregularly shaped area, or a sphere with a given radius around the store. If an online order is placed within the available scope of a store, the store takes the order. Otherwise, the store is unavailable. This scenario requires the system to determine whether an online order is placed within the available scope of a store.
Legacy solutions to this scenario typically use MySQL or PostGIS, which provides a complete set of APIs to handle geographic information system (GIS) data. However, queries can be slow in MySQL and PostGIS due to the fact that these database systems store data on disks, especially in scenarios that involve a large data volume.
In this case, you can use TairGIS instead. TairGIS is a data structure that uses R-tree indexes and executes queries that involve CONTAINS, WITHIN, or INTERSECTS within milliseconds. For more information, see GIS.
Solution
Add the available scopes of stores of a brand to TairGIS.
Query available stores that sit near a consumer based on the location of the consumer.
Sample code
In this example, Python 3.8 is used, and Tair-py is installed. You can run the pip3 install tair
command to install Tair-py.
# -*- coding: utf-8 -*-
# !/usr/bin/env python
from typing import Dict
from tair import Tair
from tair import ResponseError
def get_tair() -> Tair:
"""
This method is used to connect to a Tair instance.
* host: the endpoint that is used to connect to the Tair instance.
* port: the port number that is used to connect to the Tair instance. Default value: 6379.
* password: the password of the default database account of the Tair instance. If you want to connect to the Tair instance by using a custom database account, you must specify the password in the username:password format.
"""
tair: Tair = Tair(
host="r-8vb************.redis.zhangbei.rds.aliyuncs.com",
port=6379,
db=0,
password="D******23",
decode_responses=True
)
return tair
def add_brand_store(brandID: str, mapping: Dict[str, str]) -> bool:
"""
This method uses the GIS.ADD command to add the information about the available scopes of stores of a brand to TairGIS.
"""
try:
tair = get_tair()
ret = tair.gis_add(brandID, mapping)
return ret == 1
except ResponseError as e:
print(e)
return False
def get_brand_all(brandID):
"""
This method uses the GIS.GETALL command to query the available scopes of all stores of a brand.
"""
try:
tair = get_tair()
return tair.gis_getall(brandID)
except:
return None
def get_service_store(brandID: str, user_location: str):
"""
Query available stores that sit near a consumer based on the location of the consumer.
"""
try:
tair = get_tair()
return tair.gis_contains(brandID, user_location)
except:
return None
if __name__ == "__main__":
tair = get_tair()
# Add the available scopes of two stores of brand1.
add_brand_store(
"brand1",
{
"store_1": "POLYGON ((120.14772 30.19513, 120.15370 30.17838, 120.19385 30.18011, 120.18853 30.20817))",
"store_2": "POLYGON ((120.18986 30.20852, 120.19651 30.17988, 120.22512 30.17978, 120.21667 30.22292))"
},
)
print("Query all the stores of brand1 and their available scopes:")
print(get_brand_all('brand1'))
print("Query the available stores for a consumer who is located at coordinates (120.19707, 30.19539):")
print(get_service_store('brand1', 'POINT(120.19707 30.19539)'))
Sample success output:
Query all the stores of brand1 and their available scopes.
['store_1', 'POLYGON((120.14772 30.19513,120.1537 30.17838,120.19385 30.18011,120.18853 30.20817))', 'store_2', 'POLYGON((120.18986 30.20852,120.19651 30.17988,120.22512 30.17978,120.21667 30.22292))']
Query the available stores for a consumer who is located at coordinates (120.19707, 30.19539).
[1, ['store_2', 'POLYGON((120.18986 30.20852,120.19651 30.17988,120.22512 30.17978,120.21667 30.22292))']]
Result: The available store for the consumer who is located at coordinates (120.19707, 30.19539) is store_2
.
Summary
The geospatial data structure TairGIS can help merchants registered with e-commerce platforms accurately and efficiently deliver local purchase services.