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
{
"type":"FeatureCollection",
"features":[
{
"type":"Feature",
"properties":{
},
"geometry":{
"coordinates":[
[
[
-0.09365209962467438,
51.558654593845496
],
[
-0.10617136382247229,
51.557971183635146
],
[
-0.11830913972151091,
51.55594177797345
],
[
-0.12969566311540492,
51.55262821175705
],
[
-0.13998424561019743,
51.5481314301052
],
[
-0.14886189489846346,
51.542588389225116
],
[
-0.15605887044720881,
51.53616785590004
],
[
-0.1613568787964777,
51.52906523798585
],
[
-0.16459565962665132,
51.521496608030176
],
[
-0.16567776823852612,
51.51369210631598
],
[
-0.1645714197450512,
51.505888927589325
],
[
-0.16131132271079096,
51.49832410702523
],
[
-0.15599749287830883,
51.49122732543967
],
[
-0.14879209887336683,
51.48481395143504
],
[
-0.1399144495543639,
51.47927852934888
],
[
-0.1296342854684414,
51.47478890702547
],
[
-0.11826358354717573,
51.47148117714999
],
[
-0.10614712388296067,
51.469455580890696
],
[
-0.09365209962467438,
51.46877349367216
],
[
-0.0811570753663881,
51.469455580890696
],
[
-0.06904061570227482,
51.47148117714999
],
[
-0.057669913781009136,
51.47478890702547
],
[
-0.047389749695086615,
51.47927852934888
],
[
-0.0385121003760837,
51.48481395143504
],
[
-0.03130670637114172,
51.49122732543967
],
[
-0.025992876538659594,
51.49832410702523
],
[
-0.022732779504399312,
51.505888927589325
],
[
-0.021626431010924405,
51.51369210631598
],
[
-0.022708539622799232,
51.521496608030176
],
[
-0.025947320452972822,
51.52906523798586
],
[
-0.03124532880224173,
51.53616785590004
],
[
-0.03844230435098709,
51.542588389225116
],
[
-0.04731995363915135,
51.5481314301052
],
[
-0.05760853613404561,
51.55262821175705
],
[
-0.06899505952783784,
51.55594177797345
],
[
-0.08113283542697826,
51.557971183635146
],
[
-0.09365209962467438,
51.558654593845496
]
]
],
"type":"Polygon"
}
}
]
}
<?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