Skip to content

Instantly share code, notes, and snippets.

@acleon
Last active March 29, 2024 10:49
Show Gist options
  • Save acleon/0237a69dc75a23fb7dc81724c961b1ba to your computer and use it in GitHub Desktop.
Save acleon/0237a69dc75a23fb7dc81724c961b1ba to your computer and use it in GitHub Desktop.
Create geofence from point and distance
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
<?php
use Location\Bearing\BearingEllipsoidal;
use Location\Coordinate;
use Location\Formatter\Polygon\GeoJSON;
use Location\Polygon;
// Let's start at the church of St Mary-le-Bow in London...
$maryLeBow = new Coordinate(51.51371421689447, -0.09365209962470467);
// One definition of a London cockney was that they were born in
// earshot of that church's bells. Let's say that the bells ring
// out for 3 miles (around 5000 meters)...
$distance = 5000;
// We can create a polygon to hold our definition of cockney, a geofence
// of 5000 meters around St Mary-le-Bow.
$cockney = new Polygon();
// Create a bearing calculator to use
$calculator = new BearingEllipsoidal();
// Iterate from a starting bearing of 360 (due north) and then decrease
// our bearing by 10 degrees each time. We decrease from 360 rather than
// increase from 0 so that we're following the "right-hand rule" where
// coordinates for outer polygons are in counter-clockwise order.
for ($bearing = 360; $bearing >= 0; $bearing -= 10) {
// Use the calculator to get a new point 5000 meters from Mary-le-Bow
// in our bearing direction and add it to the polygon. The result is
// a 36-point polygon forming a rough circle around the church.
$cockney->addPoint($calculator->calculateDestination($maryLeBow, $bearing % 360, $distance));
}
// Create a new GeoJSON formatter to output our geofence polygon
echo (new GeoJSON())->format($cockney);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment