TairGIS is a geospatial data structure in Tair (Enterprise Edition) that stores and queries geographic shapes — points, lines, and polygons. It delivers high performance in high-concurrency scenarios, making it suitable for real-time location-based services (LBS) applications such as geofencing. This topic shows how to use TairGIS to build a geofence.
How it works
A geofence is a virtual boundary around a geographic area. TairGIS uses WKT (Well-Known Text), an open standard for representing geometric shapes as text strings, to define and query these boundaries.
TairGIS supports three WKT geometry types relevant to geofencing:
Set up a geofence
Step 1: Add the geofence boundary
Use GIS.ADD to store a geographic area under a named key. The following example adds a school boundary as a polygon:
GIS.ADD test_app school_location 'POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))'
Step 2: Check whether a device is inside the geofence
Depending on how you obtain the device's location, use one of the following methods.
GPS-based location (point)
GPS returns a precise coordinate. Pass it as a WKT POINT to GIS.CONTAINS:
GIS.CONTAINS test_app 'POINT (40.086631 30.138141)'
GIS.CONTAINS returns the names of all stored geometries that contain the given shape. If the point is outside all stored geometries, the response is an empty array.
Carrier-based location (polygon)
When using telecom carrier data, the reported location may be a sector or the full coverage area of a base station rather than a precise point. Represent this coverage area as a WKT POLYGON:
GIS.CONTAINS test_app 'POLYGON ((10 22, 30 45, 16 53, 10 22))'
If the coverage polygon coincides with school_location, the response includes school_location, indicating the device is within or near the geofence.
Step 3: Detect enter and exit events
GIS.CONTAINS returns a snapshot of the current state. Call it repeatedly at a fixed interval and compare consecutive results to detect state changes:
| Previous result | Current result | Event | Action |
|---|---|---|---|
school_location |
empty array | Device exited the geofence | Trigger exit alert |
| empty array | school_location |
Device entered the geofence | Trigger entry notification |
For example, in a school safety application, poll the student's location at a fixed interval. When GIS.CONTAINS transitions from returning school_location to returning an empty array during monitored hours, trigger the exit alert.
You can also run the GIS.WITHIN and GIS.INTERSECTS commands to check whether a device is within a digital fence. For full command reference, see TairGIS commands.
What's next
-
Explore the full TairGIS command reference for additional geometry operations.