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
Created a bench and a crossing
Created 2 benches and a crossing; Updated 5 cushions, a post_box, and a crossing
Created a crossing; Updated a cushion
If you were wondering whether the www.php.net & downloads.php.net services weren't responding very well in the last 6 hours — thousands of requests/sec to https://www.php.net/ 's root.
The server's load was 720, didn't die, but CDN connections to it timed out.
Now there is a caching strategy in place for a selected set of resources.
Updated a bench
Created 3 benches; Updated 10 benches
Updated a bench
Updated a bus_stop
Created a bench and a waste_basket; Updated 6 bus_stops and a crossing
Created 2 waste_baskets and a recycling; Updated 2 bicycle_parkings and a recycling
Updated a fast_food, a funeral_directors shop, and 2 other objects; Confirmed a fast_food and a hairdresser shop
Created an information; Updated 3 benches and 2 waste_baskets
Updated 2 benches and a waste_basket
Updated a bench
Created a waste_basket and an information
Created a waste_basket
I hiked 18.0km in 4h1m52s
I walked 1.4km in 17m19s
I walked 4.5km in 1h21m49s
I just made and ate, a bowl full of bacon fried Brussels Sprouts. Not under duress, and out of my own free will.
Added new residential building
Created a hairdresser shop; Confirmed a convenience shop and a dry_cleaning shop
Created a building_materials shop, a vacant shop, and 4 other objects; Confirmed a hairdresser shop, a cafe, and 2 other objects
I walked 8.3km in 1h33m44s



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