function simplify_RDP($vertices, $tolerance) { // if this is a multilinestring, then we call ourselves one each segment individually, collect the list, and return that list of simplified lists if (is_array($vertices[0][0])) { $multi = array(); foreach ($vertices as $subvertices) { $multi[] = simplify_RDP($subvertices, $tolerance); } return $multi; } $tolerance2 = $tolerance * $tolerance; // okay, so this is a single linestring and we simplify it individually return _segment_RDP($vertices, $tolerance2); }
$pFFA = new adriangibbons\phpFITFileAnalysis(__DIR__ . $file, $options); } catch (Exception $e) { echo 'caught exception: ' . $e->getMessage(); die; } // Google Static Maps API $position_lat = $pFFA->data_mesgs['record']['position_lat']; $position_long = $pFFA->data_mesgs['record']['position_long']; $lat_long_combined = []; foreach ($position_lat as $key => $value) { // Assumes every lat has a corresponding long $lat_long_combined[] = [$position_lat[$key], $position_long[$key]]; } $delta = 0.0001; do { $RDP_LatLng_coord = simplify_RDP($lat_long_combined, $delta); // Simplify the array of coordinates using the Ramer-Douglas-Peucker algorithm. $delta += 0.0001; // Rough accuracy somewhere between 4m and 12m depending where in the World coordinates are, source http://en.wikipedia.org/wiki/Decimal_degrees $polylineEncoder = new PolylineEncoder(); // Create an encoded string to pass as the path variable for the Google Static Maps API foreach ($RDP_LatLng_coord as $RDP) { $polylineEncoder->addPoint($RDP[0], $RDP[1]); } $map_encoded_polyline = $polylineEncoder->encodedString(); $map_string = '&path=color:red%7Cenc:' . $map_encoded_polyline; } while (strlen($map_string) > 1800); // Google Map web service URL limit is 2048 characters. 1800 is arbitrary attempt to stay under 2048 $LatLng_start = implode(',', $lat_long_combined[0]); $LatLng_finish = implode(',', $lat_long_combined[count($lat_long_combined) - 1]); $map_string .= '&markers=color:red%7Clabel:F%7C' . $LatLng_finish . '&markers=color:green%7Clabel:S%7C' . $LatLng_start;
<?php // an example of simplifying a multiline using Line_DouglasPeucker.php // this multiline was decoded from some JSON from ArcGIS, and is a trail in Colorado // it's expressed here in PHP literal syntax, but more realistically you'd get something // much like this after using json_decode() from ArcGIS, or by decoding a Leaflet feature's coordinates $before = array(array(array(-106.92121, 39.29517), array(-106.92121, 39.29527), array(-106.92127, 39.29536), array(-106.92215, 39.29605), array(-106.92277, 39.29652), array(-106.92344, 39.29691), array(-106.92408, 39.29725), array(-106.92463, 39.29748), array(-106.92543, 39.29775), array(-106.92623, 39.29801)), array(array(-106.93027, 39.29995), array(-106.93004, 39.29975), array(-106.92957, 39.29941), array(-106.92902, 39.29909), array(-106.92824, 39.29884), array(-106.92804, 39.29875), array(-106.92784, 39.29859), array(-106.92766, 39.29849), array(-106.92645, 39.29808), array(-106.92623, 39.29801)), array(array(-106.92623, 39.29801), array(-106.92601, 39.29842), array(-106.92591, 39.29845), array(-106.92562, 39.29847), array(-106.92534, 39.29849), array(-106.92518, 39.29848), array(-106.92512, 39.29845))); // simplify to a 30-meter tolerance at this latitude (Colorado, USA) require_once 'douglaspeuker.php'; $tolerance = 2.0E-5; $after = simplify_RDP($before, $tolerance); // print some results $length_before = strlen(json_encode($before)); $length_after = strlen(json_encode($after)); print "Before: {$length} bytes as JSON\n"; print "\n"; print "After: {$length} bytes as JSON\n";