Completed
Push — master ( 723696...0885cd )
by Antoine
02:44
created

BoundingBox::addCoordinate()   B

Complexity

Conditions 6
Paths 17

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 6.0061

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 17
cts 18
cp 0.9444
rs 8.439
c 0
b 0
f 0
cc 6
eloc 18
nc 17
nop 1
crap 6.0061
1
<?php
2
3
/*
4
 * This file is part of the Geotools library.
5
 *
6
 * (c) Antoine Corcy <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace League\Geotools\BoundingBox;
13
14
use League\Geotools\Coordinate\CoordinateInterface;
15
use League\Geotools\Polygon\PolygonInterface;
16
17
/**
18
 * @author Gabriel Bull <[email protected]>
19
 */
20
class BoundingBox implements BoundingBoxInterface
21
{
22
    /**
23
     * @var PolygonInterface
24
     */
25
    private $polygon;
26
27
    /**
28
     * The latitude of the north coordinate
29
     *
30
     * @var float|string|integer
31
     */
32
    private $north;
33
34
    /**
35
     * The longitude of the east coordinate
36
     *
37
     * @var float|string|integer
38
     */
39
    private $east;
40
41
    /**
42
     * The latitude of the south coordinate
43
     *
44
     * @var float|string|integer
45
     */
46
    private $south;
47
48
    /**
49
     * The longitude of the west coordinate
50
     *
51
     * @var float|string|integer
52
     */
53
    private $west;
54
55
    /**
56
     * @var boolean
57
     */
58
    private $hasCoordinate = false;
59
60
    /**
61
     * @var integer
62
     */
63
    private $precision = 8;
64
65
    /**
66
     * @param PolygonInterface|CoordinateInterface $object
67
     */
68 15
    public function __construct($object = null)
69
    {
70 15
        if ($object instanceof PolygonInterface) {
71 15
            $this->setPolygon($object);
72
        } elseif ($object instanceof CoordinateInterface) {
73 1
            $this->addCoordinate($object);
74 2
        } elseif (null !== $object) {
75 1
            throw new \InvalidArgumentException;
76
        }
77 15
    }
78
79 15
    private function createBoundingBoxForPolygon()
80
    {
81 15
        $this->hasCoordinate = false;
82 15
        $this->west = $this->east = $this->north = $this->south = null;
83 15
        foreach ($this->polygon->getCoordinates() as $coordinate) {
84 9
            $this->addCoordinate($coordinate);
85
        }
86 15
    }
87
88
    /**
89
     * @param CoordinateInterface $coordinate
90
     */
91 10
    private function addCoordinate(CoordinateInterface $coordinate)
92
    {
93 10
        $latitude  = $coordinate->getLatitude();
94 10
        $longitude = $coordinate->getLongitude();
95
96 10
        if (!$this->hasCoordinate) {
97 10
            $this->setNorth($latitude);
98 10
            $this->setSouth($latitude);
99 10
            $this->setEast($longitude);
100 10
            $this->setWest($longitude);
101
        } else {
102 9
            if (bccomp($latitude, $this->getSouth(), $this->getPrecision()) === -1) {
103 9
                $this->setSouth($latitude);
104
            }
105 9
            if (bccomp($latitude, $this->getNorth(), $this->getPrecision()) === 1) {
106 9
                $this->setNorth($latitude);
107
            }
108 9
            if (bccomp($longitude, $this->getEast(), $this->getPrecision()) === 1) {
109 9
                $this->setEast($longitude);
110
            }
111 9
            if (bccomp($longitude, $this->getWest(), $this->getPrecision()) === -1) {
112
                $this->setWest($longitude);
113
            }
114
        }
115
116 10
        $this->hasCoordinate = true;
117 10
    }
118
119
    /**
120
     * @param  CoordinateInterface $coordinate
121
     * @return bool
122
     */
123 2
    public function pointInBoundingBox(CoordinateInterface $coordinate)
124
    {
125
        if (
126 2
            bccomp($coordinate->getLatitude(), $this->getSouth(), $this->getPrecision()) === -1 ||
127 2
            bccomp($coordinate->getLatitude(), $this->getNorth(), $this->getPrecision()) === 1 ||
128 2
            bccomp($coordinate->getLongitude(), $this->getEast(), $this->getPrecision()) === 1 ||
129 2
            bccomp($coordinate->getLongitude(), $this->getWest(), $this->getPrecision()) === -1
130
        ) {
131 1
            return false;
132
        }
133
134 1
        return true;
135
    }
136
137
    /**
138
     * @return PolygonInterface
139
     */
140
    public function getPolygon()
141
    {
142
        return $this->polygon;
143
    }
144
145
    /**
146
     * @param  PolygonInterface $polygon
147
     * @return $this
148
     */
149 15
    public function setPolygon(PolygonInterface $polygon)
150
    {
151 15
        $this->polygon = $polygon;
152 15
        $this->createBoundingBoxForPolygon();
153
154 15
        return $this;
155
    }
156
157
    /**
158
     * {@inheritDoc}
159
     */
160 9
    public function getNorth()
161
    {
162 9
        return $this->north;
163
    }
164
165
    /**
166
     * @param  float|string|integer $north
167
     * @return $this
168
     */
169 10
    public function setNorth($north)
170
    {
171 10
        $this->north = $north;
172
173 10
        return $this;
174
    }
175
176
    /**
177
     * {@inheritDoc}
178
     */
179 9
    public function getEast()
180
    {
181 9
        return $this->east;
182
    }
183
184
    /**
185
     * @param  float|string|integer $east
186
     * @return $this
187
     */
188 10
    public function setEast($east)
189
    {
190 10
        $this->east = $east;
191
192 10
        return $this;
193
    }
194
195
    /**
196
     * {@inheritDoc}
197
     */
198 9
    public function getSouth()
199
    {
200 9
        return $this->south;
201
    }
202
203
    /**
204
     * @param  float|string|integer $south
205
     * @return $this
206
     */
207 10
    public function setSouth($south)
208
    {
209 10
        $this->south = $south;
210
211 10
        return $this;
212
    }
213
214
    /**
215
     * {@inheritDoc}
216
     */
217 9
    public function getWest()
218
    {
219 9
        return $this->west;
220
    }
221
222
    /**
223
     * @param  float|string|integer $west
224
     * @return $this
225
     */
226 10
    public function setWest($west)
227
    {
228 10
        $this->west = $west;
229
230 10
        return $this;
231
    }
232
233
    /**
234
     * @return integer
235
     */
236 9
    public function getPrecision()
237
    {
238 9
        return $this->precision;
239
    }
240
241
    /**
242
     * @param  integer $precision
243
     * @return $this
244
     */
245
    public function setPrecision($precision)
246
    {
247
        $this->precision = $precision;
248
249
        return $this;
250
    }
251
}
252