Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Written by Dan Mandle Feb 12, 2012 (http://dan.mandle.me [email protected])
- Problem: Merging a new encoded polyline with an existing encoded polyline
- 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
- 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.
- Implementation below.
- // These are the coordinates we'll be testing with.
- 44.678720,-74.984321
- 44.683534,-74.811572
- 44.622449,-74.383040
- 44.523953,-74.306565
- 44.438534,-74.226583
- 44.336263,-74.139649
- 44.317269,-74.134061
- 44.321748,-74.114340
- 44.212654,-73.597762
- 44.310283,-73.558930
- */
- require_once('/path/to/class.polylineEncoder.php'); //the encoder from http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/
- // Get the existing encoded polyline, moving forward, prob stored in a database, for for now, just a file
- $fh = fopen("encodedGPS.txt", "r");
- $sTheData = fread($fh, filesize("encodedGPS.txt"));
- $theData = unserialize ($sTheData);
- fclose($fh);
- $existingPolyline = $theData->points; // Should be this: _iuoG`kthMa]uv`@x|JkurApfR}|MztO{rN
- /*
- Here are the coodinates from that encoded polyline:
- 44.678720,-74.984321
- 44.683534,-74.811572
- 44.622449,-74.383040
- 44.523953,-74.306565
- 44.438534,-74.226583
- */
- $newPoints = file('/path/to/test_data.csv', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); //grab the new coordinates
- /* test_data.csv:
- 44.336263,-74.139649
- 44.317269,-74.134061
- 44.321748,-74.114340
- 44.212654,-73.597762
- 44.310283,-73.558930
- These are the new points to be appended to the existing encoded polyline
- */
- $lastCoord = "44.438534,-74.226583"; //this will be saved from last encoding, for for this example, it's static
- array_unshift($newPoints,$lastCoord); // add it to the front of the array of points
- foreach ($newPoints as $key => $point)
- {
- $points[$key] = explode(',', $point); // break out coordinate pairs into seperate arrays becuase that's how the encoder likes them
- }
- $lastCoordArray[0] = explode(',', $lastCoord); // put the last coordiate into its own nested array
- $encoder = new PolylineEncoder();
- $polyline = $encoder->encode($points); // encode the new polyline with the last point from the existing polyline and the new points
- $encoder2 = new PolylineEncoder();
- $encodedLastCoord = $encoder2->encode($lastCoordArray); // encode just the last point from the existing polyline so that we can pull it out
- $encodedWithLastCoord =$polyline->points;
- $newPolyline = str_replace($encodedLastCoord->points, "", $encodedWithLastCoord); // remove the last coordinate from the existing polyline from the beginning of this new polyline
- $finalEncodedPolyline = $existingPolyline.$newPolyline; // glue the new encoded polyline to the existing polyline
- $polyline->points = $finalEncodedPolyline; // put it back in the array
- // some info to debug/verify
- echo "<br />Orig Polyline: ".$existingPolyline;
- echo "<br />New Polyline: ".$encodedWithLastCoord;
- echo "<br />Encoded last point: ".$encodedLastCoord->points; //should be: ykfnGdk`dM;
- echo "<br />New polyline without encoded last: ".$newPolyline;
- echo "<br />Final Product: ".$finalEncodedPolyline;
- echo "<br /><br />_iuoG`kthMa]uv`@x|JkurApfR}|MztO{rNd~Rk~OvuB{a@_[izBxhTq{cBeaRwqF <<< that's what it should be<br />";
- echo $finalEncodedPolyline." <<<< that's what it is";
- // Write it back to the file for the map to read!
- /*$myFile = "/path/to/encodedGPS.txt";
- $sPolyline = serialize($polyline);
- $fh = fopen($myFile, 'w') or die("can't open file");
- fwrite($fh, $sPolyline);
- fclose($fh);
- */
- // Figure out a place to save the new last coordinate pair so that we can do this again.
- echo "Encoding Complete";
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement