DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Geospatial Data: Apache Spark vs. PostGIS

Trending

  • API Design
  • Understanding Europe's Cyber Resilience Act and What It Means for You
  • Automated Testing Lifecycle
  • Top 8 Conferences Developers Can Still Attend

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.

Adetola Adewodu user avatar by
Adetola Adewodu
·
Jun. 05, 17 · Tutorial
Like (4)
Save
Tweet
Share
12.94K Views

Join the DZone community and get the full member experience.

Join For Free

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.

PostGIS

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

Opinions expressed by DZone contributors are their own.

Related

  • Geospatial Data: Apache Spark vs. PostGIS

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: