What Geofencing Is and How Different Industries Use It

Page content

A geofence is a virtual boundary defined around a real-world geographic area. When a tracked object (a phone, a truck, a piece of farm equipment) crosses that boundary, something triggers: a push notification, a dispatch alert, a compliance log entry, an irrigation valve.

The geofence is defined as a polygon with geographic coordinates, typically a sequence of latitude-longitude pairs forming a closed shape.

The Geometry Underneath

The operational question is: is this point inside or outside that shape right now?

The classic algorithm for answering it is ray casting, first described by Moshe Shimrat in a 1962 paper in Communications of the ACM. Cast a ray from the point in any direction, typically east. Count how many times that ray crosses the polygon boundary. Each crossing switches you from one region to the other: inside to outside, or outside to inside. An odd number of crossings means you’re still inside after all that switching; an even number means the crossings cancelled out and you ended up outside. It handles convex and concave polygons equally well, which matters when your fence follows a city block, a county line, or an irregular delivery zone.

Circular fences skip the polygon math: check whether the distance from the test point to the center is less than the radius. For a store, a parking lot, or a building entrance, a flat-earth approximation is close enough. For larger areas, you need the Haversine formula or a full geodesic distance calculation to account for Earth’s curvature.

How Different Industries Use It

Fleet and logistics companies frequently use geofencing. When a truck enters a delivery zone, the dispatcher gets notified. When a vehicle leaves an approved route corridor, a compliance alert fires. Hazardous materials transport, armored vehicles, and pharmaceutical distribution all carry regulatory requirements around route adherence. In those cases, the geofence is the mechanism that generates the audit trail.

Retail uses geofencing for proximity triggers: send a discount notification when a customer’s phone enters the parking lot, or flag when a known customer crosses into a competitor’s location. The direction of the trigger isn’t always in the customer’s favor, though. There were recent reports that Target’s app showed higher prices when it detected you were physically inside a store, compared to browsing from home. The precision requirements here aren’t related to regulatory requirements, though, so circular fences centered on a store address usually work fine.

Agriculture has used geofencing since GPS became reliably accurate for civilian use after the U.S. government turned off Selective Availability in May 2000. Irrigation zones, pesticide application boundaries, and equipment operating areas are all polygon fences. A tractor’s auto-guidance system uses the same point-in-polygon logic to decide whether a pass is inside or outside a treatment zone. The scale difference from a retail push notification is large, but the core math is identical.

Financial services uses location checks for fraud detection. If a card transaction appears in Durham, NC, and another one surfaces 90 minutes later in Buenos Aires, the combination of transaction speed and distance is a signal. That’s not pure geofencing; it’s a spatial-temporal check. But the containment question, “was this transaction inside the customer’s expected region?”, is a geofence check at its core.

Smart home and IoT devices use presence detection: home automation that triggers when your phone crosses the neighborhood boundary on your commute home. Emergency management uses geofencing to target Wireless Emergency Alerts to affected areas. Wildlife researchers use it to log when tagged animals cross habitat boundaries.

Evaluating Geofences in a Spatial Database

Most production geofence implementations are stored in a spatial database. PostGIS handles polygon containment through ST_Contains and ST_Within, and distance-based circular fences through ST_DWithin. At scale, a GiST index does the heavy lifting: the query planner uses bounding-box pre-filtering to discard impossible candidates before running the exact containment check, which keeps evaluation fast even with thousands of fences.

Fitting Geofencing to Your Architecture

Geofencing fits well when you need event-driven responses to location changes and your boundaries are relatively stable. A fleet company with a few hundred delivery zones, or a retailer with store footprints, is a natural fit for PostGIS. The fences live near other relational data and the evaluation frequency is manageable. A GiST spatial index is enough.

Scaling can become a concern when boundaries change frequently, when you have millions of them, or when evaluation needs to happen faster than a round-trip to the database allows. High-frequency GPS streams from thousands of vehicles often push evaluation closer to the source: embedded in mobile clients, in stream-processing pipelines with in-memory spatial indexes, or in purpose-built geofencing services. At that point, PostGIS is still a good store for the fence definitions, but it may not be where you run every containment check.

The PostGIS function reference covers the full set of spatial predicates. If you’re storing fence polygons in PostGIS and haven’t settled on a coordinate type yet, the choice between geometry and geography matters more than it looks. I covered that in PostGIS Geography vs. Geometry: Choosing the Right Type for Your Spatial Data.