Completed
Push — master ( aec035...22a1ab )
by Marcus
02:38
created

Processor/Polyline/SimplifyDouglasPeucker.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Line;
19
use Location\Polyline;
20
use Location\Utility\PerpendicularDistance;
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
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 View Code Duplication
    protected function douglasPeucker(array $line)
0 ignored issues
show
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...
67
    {
68
        $distanceMax = 0;
69
        $index       = 0;
70
71
        $lineSize = count($line);
72
73
        $pdCalc = new PerpendicularDistance();
74
75
        for ($i = 1; $i <= ($lineSize - 1); $i ++) {
76
            $distance = $pdCalc->getPerpendicularDistance($line[$i], new Line($line[0], $line[$lineSize - 1]));
77
78
            if ($distance > $distanceMax) {
79
                $index       = $i;
80
                $distanceMax = $distance;
81
            }
82
        }
83
84
        if ($distanceMax > $this->tolerance) {
85
            $lineSplitFirst  = array_slice($line, 0, $index);
86
            $lineSplitSecond = array_slice($line, $index, $lineSize);
87
88
            $resultsSplit1  = $this->douglasPeucker($lineSplitFirst);
89
            $resultsSplit2 = $this->douglasPeucker($lineSplitSecond);
90
91
            array_pop($resultsSplit1);
92
93
            return array_merge($resultsSplit1, $resultsSplit2);
94
        }
95
96
        return [$line[0], $line[$lineSize - 1]];
97
    }
98
}
99