Advertisement
danmandle

Merge Google Maps encoded polylines

Feb 12th, 2012
2,012
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.40 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. Written by Dan Mandle Feb 12, 2012 (http://dan.mandle.me dan@mandle.me)
  5.  
  6. Problem: Merging a new encoded polyline with an existing encoded polyline
  7.  
  8. Solution: keep last coord pair from existing polyline, encode just that pair and save it for later, encode all new coords with coord from exising polyline at the beginning of this new encoding. find the string of the last coordinate pair, and remove it from the new encoded polyline and stick the new encoded polyline onto the back of the existing polyline
  9.  
  10. Things to know: What the encoding does is calculate the offset (distance from x,y) and converts that value to ASCII. The problem is the first coordinate is calculated from 0,0 so if you were just to put two encoded polylines together, where you added the new one wouldn't be offset from the existing, but offset from 0,0 causing a large jump in the polyline. What we need to do is find out which charactors in the encoded polyline are the offset from 0,0 and remove them. Then you can append the new line to the old line, and it will be offset properly.
  11.  
  12. Implementation below.
  13.  
  14. // These are the coordinates we'll be testing with.
  15. 44.678720,-74.984321
  16. 44.683534,-74.811572
  17. 44.622449,-74.383040
  18. 44.523953,-74.306565
  19. 44.438534,-74.226583
  20. 44.336263,-74.139649
  21. 44.317269,-74.134061
  22. 44.321748,-74.114340
  23. 44.212654,-73.597762
  24. 44.310283,-73.558930
  25.  
  26. */
  27.  
  28. require_once('/path/to/class.polylineEncoder.php'); //the encoder from http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/
  29.  
  30. // Get the existing encoded polyline, moving forward, prob stored in a database, for for now, just a file
  31. $fh = fopen("encodedGPS.txt", "r");
  32. $sTheData = fread($fh, filesize("encodedGPS.txt"));
  33. $theData = unserialize ($sTheData);
  34. fclose($fh);
  35.  
  36. $existingPolyline = $theData->points; // Should be this: _iuoG`kthMa]uv`@x|JkurApfR}|MztO{rN
  37. /*
  38. Here are the coodinates from that encoded polyline:
  39.     44.678720,-74.984321
  40.     44.683534,-74.811572
  41.     44.622449,-74.383040
  42.     44.523953,-74.306565
  43.     44.438534,-74.226583
  44. */
  45.  
  46. $newPoints = file('/path/to/test_data.csv', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); //grab the new coordinates
  47. /* test_data.csv:
  48.     44.336263,-74.139649
  49.     44.317269,-74.134061
  50.     44.321748,-74.114340
  51.     44.212654,-73.597762
  52.     44.310283,-73.558930
  53.    
  54.     These are the new points to be appended to the existing encoded polyline
  55. */
  56.  
  57. $lastCoord = "44.438534,-74.226583"; //this will be saved from last encoding, for for this example, it's static
  58.  
  59. array_unshift($newPoints,$lastCoord); // add it to the front of the array of points
  60.  
  61. foreach ($newPoints as $key => $point)
  62. {
  63.   $points[$key] = explode(',', $point); // break out coordinate pairs into seperate arrays becuase that's how the encoder likes them
  64. }
  65.  
  66. $lastCoordArray[0] = explode(',', $lastCoord); // put the last coordiate into its own nested array
  67.  
  68. $encoder = new PolylineEncoder();
  69. $polyline = $encoder->encode($points); // encode the new polyline with the last point from the existing polyline and the new points
  70.  
  71. $encoder2 = new PolylineEncoder();
  72. $encodedLastCoord = $encoder2->encode($lastCoordArray); // encode just the last point from the existing polyline so that we can pull it out
  73.  
  74. $encodedWithLastCoord =$polyline->points;
  75.  
  76. $newPolyline = str_replace($encodedLastCoord->points, "", $encodedWithLastCoord); // remove the last coordinate from the existing polyline from the beginning of this new polyline
  77.  
  78. $finalEncodedPolyline = $existingPolyline.$newPolyline; // glue the new encoded polyline to the existing polyline
  79.  
  80. $polyline->points = $finalEncodedPolyline; // put it back in the array
  81.  
  82. // some info to debug/verify
  83. echo "<br />Orig Polyline: ".$existingPolyline;
  84. echo "<br />New Polyline: ".$encodedWithLastCoord;
  85. echo "<br />Encoded last point: ".$encodedLastCoord->points; //should be: ykfnGdk`dM;
  86. echo "<br />New polyline without encoded last: ".$newPolyline;
  87. echo "<br />Final Product: ".$finalEncodedPolyline;
  88. echo "<br /><br />_iuoG`kthMa]uv`@x|JkurApfR}|MztO{rNd~Rk~OvuB{a@_[izBxhTq{cBeaRwqF <<< that's what it should be<br />";
  89. echo $finalEncodedPolyline." <<<< that's what it is";
  90.  
  91. // Write it back to the file for the map to read!
  92. /*$myFile = "/path/to/encodedGPS.txt";
  93. $sPolyline = serialize($polyline);
  94. $fh = fopen($myFile, 'w') or die("can't open file");
  95. fwrite($fh, $sPolyline);
  96. fclose($fh);
  97.  
  98. */
  99.  
  100. // Figure out a place to save the new last coordinate pair so that we can do this again.
  101.  
  102. echo "Encoding Complete";
  103.  
  104. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement