Flat vs. Sphere
For the preparation of my Finding Pubs... (and other things) presentation that I gave at the PHP NorthWest usergroup some time ago I created a demo that shows all my flickr photos on a map. For example like:
This example shows photos in the UK, Norway and Sweden as well as in Paris.
In order to not find too many photos that are not going to be in the portion of the world that the display covers, I add a bounding box to my find() query:
$polygon = GeoJSONPolygon::createFromBounds(
min( 90, (float) $_GET['n'] ),
min( 180, (float) $_GET['e']) ,
max( -90, (float) $_GET['s'] ),
max( -180, (float) $_GET['w'] )
);
$c = $d->selectCollection( 'flickr' );
$query = array(
LOC => array(
'$geoWithin' => array(
'$geometry' => $polygon->getGeoJSON(),
),
),
);
$s = $c->find( $query )->limit( 8000 );
The North, East, South and West points are being passed as argument to the fetch-poi.php script like: http://127.0.1.4/maps-flickr/fetch-poi.php?n=65.44000165965534&e=32.51953125&s=46.07323062540838&w=-19.51171875
Now if we scroll up a bit to include Iceland in the map, then we see that the photos in Paris disappear:
How is that possible? Let's first see what our polygon looks like:
<?php
include 'classes.php';
$_GET = array(
'n' => 65.44000165965534,
'e' => 32.51953125,
's' => 46.07323062540838,
'w' => -19.51171875,
);
$polygon = GeoJSONPolygon::createFromBounds(
min( 90, (float) $_GET['n'] ),
min( 180, (float) $_GET['e']) ,
max( -90, (float) $_GET['s'] ),
max( -180, (float) $_GET['w'] )
);
var_dump($polygon);
?>
Which outputs:
class GeoJSONPolygon#1 (1) {
public $pg =>
array(1) {
[0] =>
array(5) {
[0] =>
array(2) {
[0] => double(32.51953125) [1] => double(65.440001659655)
}
[1] =>
array(2) {
[0] => double(-19.51171875) [1] => double(65.440001659655)
}
[2] =>
array(2) {
[0] => double(-19.51171875) [1] => double(46.073230625408)
}
[3] =>
array(2) {
[0] => double(32.51953125) [1] => double(46.073230625408)
}
[4] =>
array(2) {
[0] => double(32.51953125) [1] => double(65.440001659655)
}
}
}
}
The 5 array elements in here reflect in order the North East point, the North West point, the South West point, the South East point and again the North East point. If we draw lines between those points on the map, we find:
Which instantly shows you why I got missing photos! As you can see, the shortest line between vertical lines is a straight line on a sphere, but the horizontal lines are not straight. The shortest path on a sphere, as expressed with a Spherical Mercator projection is often a curve, called the Great Circle path. Because MongoDB's 2dsphere index is a true spherical index (as opposed to the 2d index which cheats), the polygon it uses to find the photos is not a square as you can see in the image above. This illustrates the main difference between spherical geospatial queries, and ones that only deal with a flat rectangular map.
Leaflet, the mapping library that I use, does not actually draw lines as Great Circle paths, so I had to calculate them myself. The maps-great-circle code on GitHub shows how I did that. Basically I created points along the Northern and Southern border on the exact latitude.
If you create one intermediate point, the result is:
And with five intermediate points you hardly notice the problem anymore:
Play around with the gc_segments URL parameter to see for yourself:
-
http://maps.derickrethans.nl/?l=flickr,gc&zoom=5&gc_segments=1
-
http://maps.derickrethans.nl/?l=flickr,gc&zoom=5&gc_segments=2
-
http://maps.derickrethans.nl/?l=flickr,gc&zoom=5&gc_segments=3
-
http://maps.derickrethans.nl/?l=flickr,gc&zoom=5&gc_segments=10
Next up in the series is "Showing all the World's Timezones" which will explain how I created the tiles that highlight the timezones at http://maps.derickrethans.nl/?l=timezones&lon=50&lat=30&zoom=3
Life Line
In more info innocent times, the hat riots caused some spankings...
100 Years Ago Men and Boys Fought on the Streets of New York Over Wearing Straw Hats Past Summer | The New York Public Library
https://www.nypl.org/blog/2022/09/23/straw-hat-riots-nycI hiked 15.5km in 3h12m14s
š· Mushroom Village
š© Weg van den Prins Willemsberg, Ellecom, Nederland
I walked 2.4km in 59m34s
š· National Cycle Network 6
š© Watford, United Kingdom
I walked 7.6km in 1h54m50s
š· Tufted Duck Pair
š© Outer Circle, City of Westminster, United Kingdom
RE: https://phpc.social/@Xdebug/115662135830755552
I have just released Xdebug 3.5.0!
In the next few weeks I will create some content (text, and perhaps video) highlighting some new features in more detail.
Please share it with the world!
The master branch is now for Xdebug 3.6, targetting PHP 8.6
Back to -dev
Tweak release instructions a little
Go with 3.5.0
Tweak message IDs and severities for control socket log entries
I walked 8.1km in 1h26m40s
I walked 1.1km in 9m28s
I walked 8.5km in 1h30m56s
My whisky of the month for December 2025, is a 15yo Aultmore bottled by Cadenhead's.
Fixed off-by-one error in address length name for control socket on Lā¦
Merged pull request #1050
Fixed control socket name by removing silly trailing things (by not pā¦




Shortlink
This article has a short URL available: https://drck.me/gc-a98