The wide use of Geospatial Information Systems (GIS) has led to a number of features, including geofencing. In this case, geofencing is the tracking of an object in and out of a geofence. A Geofence is a circle or polygon around a given area on a map. A circle can show areas that can track the entrance and exit from the marked area.
There are tools that can allow users to create and use geo fences for software applications. One of these tools is PostGIS.
PostGIS is a geospatial extension of Postgres. You can find a description of PostGIS here. Also, some of the methods are mentioned here as well. There are other methods that are good for geofencing as well. In fact, PostGIS can be used to create a geofence and to detect if a point is within a geofence.
Creating a Geofence Using the St_Buffer Method
To create geofence, a point and radius is needed. The point should consist of a longitude and latitude. And the radius is distance from the circle’s center to the circle’s perimeter. With these parameters, the ST_Buffer(geometry,distance) function can be used to create a polygon or geofence. ST_Buffer(geometry,distance) takes a geometry type and a “buffer” distance. The result is a polygon with a boundary the buffer distance away from the geometry type. The geometry type in this case is the point. The buffer distance is the circle’s radius. For the example below, geometry type is a pointwith center being longitude = -77.18 and latitude = 39.01. The the buffer distance or radius is 1609.34 (1609.34 meters or 1 mile).
ST_Buffer (geography (st_point (-77.18, 39.01)), 1609.34))
The result will be a circle with the center being the point
that is expanded by a radius
Then to create a row that include the longitude, latitude, radius and polygon, the following insert statement would work.
Insert into geofence(longitude, latitude, radius, polygon) values (-77.18 , 39.01, radius, ST_Buffer (geography (st_point (-77.18, 39.01)), 1609.34))
Also, notice geography is used to cast the point as a geography data type. The geography is a data type used to represent the round-earth coordinate system. For this example, it is used to make sure the point of longitude and latitude coordinates are used in the query.
Geofencing Using the ST_DWithin Method
If a point is within a geofence, a point must be within distance to the center of the geofence. In this case, the radius is the distance to the center that is used to create the geofence.
For example, given a point with the longitude = -77.18 and latitude = 39.00. To check if a point is within the distance of one another point, the following works:
WHERE ST_DWithin ( geography (ST_Point(longitude,latitude)), geography (ST_Point(-77.18, 39.00)), radius)
Then to limit the number of results returned to one, the following can be done:
And with the given select statement using longitude, latitude, radius columns in the geofence table:
SELECT * FROM geofence WHERE ST_DWithin ( geography (ST_Point(longitude,latitude)), geography (ST_Point(-77.18, 39.00)), radius) limit 1;
If a row is returned, the point is in a geofence. If there is no row returned, the point is not in any geofence.
With this information, a web service or an application can be created. Therefore, a geofencing application can be created with PostGIS as the database. An example of a geofencing application is an application to track drones. It can be used to show drones the areas where they can fly or not fly because they are unsafe. Also, geofencing can also be used for defense, oil and gas, and other industries.
If you are interested in discussing more, feel free to contact me on here or email me at email@example.com.