Contributing Advent 12: Geospatial algorithms
This is technically not a new contribution, but one I've been working on and off at for the past months. The geospatial PHP extension contains a whole bunch of algorithms that deal with geospatial problems. The Haversine and Vincenty functions calculate distances on a globe, an implementation of the Helmert transformation algorithm converts between different datums, and some general functions to convert from f.e. 16°45'12" N to 16.7533° N.
These are all useful functions, but I'd like to talk about an algorithm that I've added some time ago now. It is an implementation of Ramer-Douglas-Peucker that can be used to simplify a polyline. I have used that for the display of the timezone boundaries that I show in one of my maps examples: http://maps.derickrethans.nl/?l=timezone&lat=51.5&lon=0&zoom=8. The boundary data that I have for the UK for example, is so complex that it takes quite a bit of time to render them on the map. On higher zoom levels where that amount of detail simply does not show up, we can of course do with a simpler line, and that is what RDP helps us to achieve.
The algorithm has as input an array of coordinate pairs:
$points = [
[ 0, 10 ],
[ 1, 6 ],
[ 3, 0 ],
[ 5, 6 ],
[ 7, 8 ],
[ 8, 7 ],
[ 9, 4 ],
[ 13, 10 ],
];
As well as a parameter (called ε) that determines the maximum distance from a line that a point is allowed to be so that it is included in the returned array. The algorithm is recursive and will for each segment—starting with the whole array:
-
Find the further point from the line as marked by the first and last point
-
If the point is closer than ε to the line segment then any points not currently marked to keep can be discarded without the simplified curve being worse than ε.
-
If the point furthest from the line segment is greater than ε from the approximation then that point must be kept. The algorithm recursively calls itself with the first point and the worst point and then with the worst point and the last point (which includes marking the worst point being marked as kept).
With the above $points array, we can run the function like:
$points = rdp_simplify( $points, 2.75 );
We are left with:
$points = [
[ 0, 10 ],
[ 3, 0 ],
[ 5, 6 ],
[ 9, 4 ],
[ 13, 10 ],
];
The image below shows the effect of ε = 2.75 and other values:
In case you do not want to install a PHP extension to get that functionality, I also have an example implementation in PHP at https://github.com/derickr/3angle/blob/master/rdp.php, however, the time it takes to run the algorithm in PHP is about the same as the browser needs to draw the additional points of the lines.
Expect to see an updated version in the geospatial extension that also handles multi-polygons in the future. This should speed up running this over the large amount of UK polygons, as there are plenty of islands that also need to be done.
Life Line
If I would like (to rent) a desk for working for an afternoon in Amsterdam North, or near Centraal, what would be a good place?
Updated a marina
I walked 9.7km in 1h46m04s
Updated an information and a bench
Created 2 benches
Created 4 picnic_tables, a bench, and a fitness_station; Updated a pub and a sport club
Created 2 benches and 2 waste_baskets; Updated 4 benches, a bus_stop, and a cafe; Confirmed a dentist
Created 3 waste_baskets, 2 main entrances, and a bench; Deleted a cycle_barrier, a bench, and a waste_basket
Created an information; Updated a waste_basket and a bench
Updated 2 waste_baskets and a bench
Created 3 benches
Created a waste_basket; Updated 2 benches and a tree
I hiked 17.0km in 3h52m14s
I walked 3.5km in 35m31s
Created a main entrance and a home entrance
Created an entrance
Updated a house building
Created an entrance
I walked 5.8km in 1h15m06s
I've just finished reading "A Cheese-Monger's Tour de France", by Ned Palmer.
Now I want to try many of those! 🧀
I'm thrilled to announce that I'll be speaking at the 23rd edition of #phpday, the international PHP conference in Italy, organised by @grusp.
I’ll be presenting a talk titled: "Better Debugging With Xdebug".
It's in Verona, Italy, on May 14-15th 2026.
You can use my speaker’s discount code "speaker_10OFF" for 10% off at https://www.phpday.it/tickets/?utm_medium=organic&utm_source=linkedin&utm_campaign=post-speaker
I walked 5.5km in 1h11m00s
I walked 1.1km in 9m37s
Merged pull request #1066
PHP 8.6: Changes to opcache optimisations wrt function arguments


Shortlink
This article has a short URL available: https://drck.me/adv1312-af4