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
Merged pull request #1055
Fixed issue #2387: Remove INI entries for changed and removed Xdebug …
Merged pull request #1053
Reimplement PR #1052 with normal style
Add missing section comment
Merge branch 'xdebug_3_5'
Merged pull request #1054
Change error retrieval method in ctrl_socket.c
Pink Sky at Sunset
I took this photo over the Christmas period in the Dutch city of Breda.
I walked 8.5km in 1h25m28s
I walked 8.1km in 1h21m10s
I walked 0.8km in 9m03s
I walked 4.8km in 50m12s
Went for a 20k walk through Bushy Park, along the Thames, and through Richmond Park and Wimbledon Common. It was a bit nippy!
I hiked 19.3km in 3h52m02s
Updated a pub
I walked 4.6km in 44m50s
I walked 4.9km in 47m58s
Update Westbourne Green area, now that it is open
I walked 11.9km in 2h3m03s
I walked 9.8km in 1h47m38s
I walked 10.2km in 1h34m25s
Whoop! FOSDEM travel and hotel booked. See you in Brussels at the end of January?
I walked 10.6km in 1h48m23s
I walked 3.0km in 33m38s



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