Completed
Push — master ( 51422a...969f07 )
by Marcus
02:07
created

SimplifyDouglasPeucker::douglasPeucker()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 30
Code Lines 17

Duplication

Lines 8
Ratio 26.67 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 8
loc 30
rs 8.5806
cc 4
eloc 17
nc 6
nop 1
1
<?php
2
/**
3
 * Simplify Polyline with the Douglas-Peucker-Algorithm
4
 *
5
 * The Algorithm is described here:
6
 * http://en.wikipedia.org/wiki/Ramer%E2%80%93Douglas%E2%80%93Peucker_algorithm
7
 *
8
 * The formula for the Perpendicular Distance is described here:
9
 * http://biodiversityinformatics.amnh.org/open_source/pdc/documentation.php
10
 *
11
 * @author    Marcus Jaschen <[email protected]>
12
 * @license   https://opensource.org/licenses/GPL-3.0 GPL
13
 * @link      https://github.com/mjaschen/phpgeo
14
 */
15
16
namespace Location\Processor\Polyline;
17
18
use Location\Coordinate;
19
use Location\Line;
20
use Location\Polyline;
21
22
/**
23
 * Simplify Polyline with the Douglas-Peucker-Algorithm
24
 *
25
 * @author   Marcus Jaschen <[email protected]>
26
 * @license  https://opensource.org/licenses/GPL-3.0 GPL
27
 * @link     https://github.com/mjaschen/phpgeo
28
 */
29
class SimplifyDouglasPeucker implements SimplifyInterface
30
{
31
    /**
32
     * @var float
33
     */
34
    private $tolerance;
35
36
    /**
37
     * @param float $tolerance the perpendicular distance threshold in meters
38
     */
39
    public function __construct($tolerance)
40
    {
41
        $this->tolerance = $tolerance;
42
    }
43
44
    /**
45
     * @param \Location\Polyline $polyline
46
     *
47
     * @return \Location\Polyline
48
     */
49 View Code Duplication
    public function simplify(Polyline $polyline)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
    {
51
        $resultPolyline = new Polyline();
52
        $simplifiedLine = $this->douglasPeucker($polyline->getPoints());
53
54
        foreach ($simplifiedLine as $point) {
55
            $resultPolyline->addPoint($point);
56
        }
57
58
        return $resultPolyline;
59
    }
60
61
    /**
62
     * @param array $line
63
     *
64
     * @return array
65
     */
66
    protected function douglasPeucker(array $line)
67
    {
68
        $distanceMax = 0;
69
        $index       = 0;
70
71
        $lineSize = count($line);
72
73 View Code Duplication
        for ($i = 1; $i <= ($lineSize - 1); $i ++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
            $distance = $this->getPerpendicularDistance($line[$i], new Line($line[0], $line[$lineSize - 1]));
75
76
            if ($distance > $distanceMax) {
77
                $index       = $i;
78
                $distanceMax = $distance;
79
            }
80
        }
81
82
        if ($distanceMax > $this->tolerance) {
83
            $lineSplitFirst  = array_slice($line, 0, $index);
84
            $lineSplitSecond = array_slice($line, $index, $lineSize);
85
86
            $resultsSplit1  = $this->douglasPeucker($lineSplitFirst);
87
            $resultsSplit2 = $this->douglasPeucker($lineSplitSecond);
88
89
            array_pop($resultsSplit1);
90
91
            return array_merge($resultsSplit1, $resultsSplit2);
92
        }
93
94
        return [$line[0], $line[$lineSize - 1]];
95
    }
96
97
    /**
98
     * @param Coordinate $point
99
     * @param Line $line
100
     *
101
     * @return number
102
     */
103 View Code Duplication
    protected function getPerpendicularDistance(Coordinate $point, Line $line)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
    {
105
        $ellipsoid = $point->getEllipsoid();
106
107
        $ellipsoidRadius = $ellipsoid->getArithmeticMeanRadius();
108
109
        $firstLinePointLat = $this->deg2radLatitude($line->getPoint1()->getLat());
110
        $firstLinePointLng = $this->deg2radLongitude($line->getPoint1()->getLng());
111
112
        $firstLinePointX = $ellipsoidRadius * cos($firstLinePointLng) * sin($firstLinePointLat);
113
        $firstLinePointY = $ellipsoidRadius * sin($firstLinePointLng) * sin($firstLinePointLat);
114
        $firstLinePointZ = $ellipsoidRadius * cos($firstLinePointLat);
115
116
        $secondLinePointLat = $this->deg2radLatitude($line->getPoint2()->getLat());
117
        $secondLinePointLng = $this->deg2radLongitude($line->getPoint2()->getLng());
118
119
        $secondLinePointX = $ellipsoidRadius * cos($secondLinePointLng) * sin($secondLinePointLat);
120
        $secondLinePointY = $ellipsoidRadius * sin($secondLinePointLng) * sin($secondLinePointLat);
121
        $secondLinePointZ = $ellipsoidRadius * cos($secondLinePointLat);
122
123
        $pointLat = $this->deg2radLatitude($point->getLat());
124
        $pointLng = $this->deg2radLongitude($point->getLng());
125
126
        $pointX = $ellipsoidRadius * cos($pointLng) * sin($pointLat);
127
        $pointY = $ellipsoidRadius * sin($pointLng) * sin($pointLat);
128
        $pointZ = $ellipsoidRadius * cos($pointLat);
129
130
        $normalizedX = $firstLinePointY * $secondLinePointZ - $firstLinePointZ * $secondLinePointY;
131
        $normalizedY = $firstLinePointZ * $secondLinePointX - $firstLinePointX * $secondLinePointZ;
132
        $normalizedZ = $firstLinePointX * $secondLinePointY - $firstLinePointY * $secondLinePointX;
133
134
        $length = sqrt($normalizedX * $normalizedX + $normalizedY * $normalizedY + $normalizedZ * $normalizedZ);
135
136
        $normalizedX /= $length;
137
        $normalizedY /= $length;
138
        $normalizedZ /= $length;
139
140
        $thetaPoint = $normalizedX * $pointX + $normalizedY * $pointY + $normalizedZ * $pointZ;
141
142
        $length = sqrt($pointX * $pointX + $pointY * $pointY + $pointZ * $pointZ);
143
144
        $thetaPoint /= $length;
145
146
        $distance = abs((M_PI / 2) - acos($thetaPoint));
147
148
        return $distance * $ellipsoidRadius;
149
    }
150
151
    /**
152
     * @param float $latitude
153
     *
154
     * @return float
155
     */
156
    protected function deg2radLatitude($latitude)
157
    {
158
        return deg2rad(90 - $latitude);
159
    }
160
161
    /**
162
     * @param float $longitude
163
     *
164
     * @return float
165
     */
166
    protected function deg2radLongitude($longitude)
167
    {
168
        if ($longitude > 0) {
169
            return deg2rad($longitude);
170
        }
171
172
        return deg2rad($longitude + 360);
173
    }
174
}
175