Last active
March 29, 2024 10:49
-
-
Save acleon/0237a69dc75a23fb7dc81724c961b1ba to your computer and use it in GitHub Desktop.
Create geofence from point and distance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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