Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How To Do Simple Geofencing With PostGIS

DZone's Guide to

How To Do Simple Geofencing With PostGIS

This tutorial will explain how geofencing works and show you how to implement it in a mobile app for functions like finding your current location.

· Mobile Zone
Free Resource

Launching an app doesn’t need to be daunting. Whether you’re just getting started or need a refresher on mobile app testing best practices, this guide is your resource! Brought to you in partnership with Perfecto

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 geofences for software applications. One of these tools is PostGIS.

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 a geofence, a point and radius are needed. The point should consist of a longitude and latitude. The radius is the 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, the geometry type is a point, with the center being longitude = -77.18 and latitude = 39.01. 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.

(-77.18, 39.01)

That is expanded by a radius.

1609.34

Then, to create a row that includes 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:

limit 1;

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 here or email me at adetola@adelabs.com.

Keep up with the latest DevTest Jargon with the latest Mobile DevTest Dictionary. Brought to you in partnership with Perfecto.

Topics:
mobile ,geofencing ,postgis

Published at DZone with permission of Adetola Adewodu. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}