-
Notifications
You must be signed in to change notification settings - Fork 3
/
class.encoded-polyline-stitcher.php
96 lines (87 loc) · 3.16 KB
/
class.encoded-polyline-stitcher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/**
*
* @since 2013-03-24
* @author Dan Mandle http://dan.mandle.me
* @see http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/
*
*
** DON'T BE A DICK PUBLIC LICENSE
*
*** http://www.dbad-license.org
*** Version 1, December 2009
*
** Copyright (C) 2013 Dan Mandle http://dan.mandle.me
*
* Everyone is permitted to copy and distribute verbatim or modified
* copies of this license document, and changing it is allowed as long
* as the name is changed.
*
** DON'T BE A DICK PUBLIC LICENSE
** TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
*
* 1. Do whatever you like with the original work, just don't be a dick.
*
* Being a dick includes - but is not limited to - the following instances:
*
* 1a. Outright copyright infringement - Don't just copy this and change the name.
* 1b. Selling the unmodified original with no work done what-so-ever, that's REALLY being a dick.
* 1c. Modifying the original work to contain hidden harmful content. That would make you a PROPER dick.
*
* 2. If you become rich through modifications, related works/services, or supporting the original work,
* share the love. Only a dick would make loads off this work and not buy the original works
* creator(s) a pint.
*
* 3. Code is provided with no warranty. Using somebody else's code and bitching when it goes wrong makes
* you a DONKEY dick. Fix the problem yourself. A non-dick would submit the fix back.
*
*/
require_once(dirname(__FILE__).'/class.polylineEncoder.php');
class PolylineUtilities extends PolylineEncoder{
public function stitchPolylines($existingPolyline, $newPolyline, $lastCoords = NULL, $newPolylineFromOrigin = FALSE){
// existingPolyline: string
// newPolyline: string
// optional lastCoords: string OR array(lat,lon)
// newPolylineFromOrigin: bool if new polyline is from origin or starting from lastCoords
if(!$newPolylineFromOrigin){
if(isset($lastCoords)){
if(!is_array($lastCoords)){
$lastCoords[0] = explode(',', lastCoords);
}
else{
$lastCoords[0] = $lastCoords;
}
}
else{
$lastCoords[0] = array_pop(decodePolyline($existingPolyline));
}
$lastCoordsEncoded = $this->encode($lastCoords);
$stichedPolyline = $existingPolyline.str_replace($lastCoordsEncoded, '', $newPolyline);
}
else{
if(isset($lastCoords)){
if(!is_array($lastCoords)){
$lastCoords[0] = explode(',', lastCoords);
}
else{
$lastCoords[0] = $lastCoords;
}
}
else{
$lastCoords[0] = array_pop(decodePolyline($existingPolyline));
}
$firstCoords[0] = array_shift(decodePolyline($newPolyline));
$lastCoordsEncoded = $this->encode($lastCoords);
$firstCoordsEncoded = $this->encode($firstCoords);
$lastAndFirstEncoded = $this->encode(array($lastCoords, $firstCoords));
$stitchedPolyline = $existingPolyline
. str_replace($lastCoordsEncoded, '', $lastAndFirstEncoded)
. str_replace($firstCoordsEncoded, '', $newPolyline);
}
return $stitchedPolyline;
}
public function decode($polyline){
require_once(dirname(__FILE__).'/decodePolylineToArray.php');
return decodePolylineToArray($polyline);
}
}