GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Utils::createPolygonFromJson()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.1481

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 4
cts 6
cp 0.6667
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
crap 2.1481
1
<?php
2
3
namespace GeoTimeZone\Geometry;
4
5
use Exception;
6
use geoPHP;
7
8
class Utils
9
{
10
    const POLYGON_GEOJSON_NAME = "Polygon";
11
    const POINT_WKT_NAME = "POINT";
12
    const FEATURE_COLLECTION_GEOJSON_NAME = "FeatureCollection";
13
    const FEATURE_GEOJSON_NAME = "Feature";
14
    const WKT_EXTENSION = "wkt";
15
    const GEOJSON_EXTENSION = "json";
16
    const NOT_FOUND_IN_FEATURES = "notFoundInFeatures";
17
    
18
    /**
19
     * Convert array of coordinates to polygon structured json array
20
     * @param $polygonPoints
21
     * @return array
22
     */
23 7
    public function createPolygonJsonFromPoints($polygonPoints)
24
    {
25
        return array(
26 7
            'type' => self::POLYGON_GEOJSON_NAME,
27 7
            'coordinates' => $this->structurePolygonCoordinates($polygonPoints)
28
        );
29
    }
30
    
31
    /**
32
     * Structure polygon coordinates as geoPHP needs
33
     * @param $polygonPoints
34
     * @return array
35
     */
36 7
    protected function structurePolygonCoordinates($polygonPoints)
37
    {
38 7
        $structuredCoordinates = array();
39 7
        foreach ($polygonPoints as $points) {
40 7
            if (count($points) == 2) {
41 7
                $structuredCoordinates[] = $polygonPoints;
42 7
                break;
43
            }
44 1
            $structuredCoordinates[] = $points;
45
        }
46 7
        return $structuredCoordinates;
47
    }
48
    
49
    /**
50
     * Create polygon geometry object from polygon points array
51
     * @param $polygonPoints
52
     * @return bool|geoPHP::GeometryCollection|mixed
0 ignored issues
show
Documentation introduced by
The doc-type bool|geoPHP::GeometryCollection|mixed could not be parsed: Unknown type name "geoPHP::GeometryCollection" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
53
     */
54
    protected function createPolygonFromPoints($polygonPoints)
55
    {
56
        $polygonData = $this->createPolygonJsonFromPoints($polygonPoints);
57
        return $this->createPolygonFromJson(json_encode($polygonData));
58
    }
59
    
60
    /**
61
     * Create polygon geometry object from structured polygon data (as json)
62
     * @param $polygonJson
63
     * @return bool|geoPHP::GeometryCollection|mixed
0 ignored issues
show
Documentation introduced by
The doc-type bool|geoPHP::GeometryCollection|mixed could not be parsed: Unknown type name "geoPHP::GeometryCollection" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
64
     */
65 7
    public function createPolygonFromJson($polygonJson)
66
    {
67 7
        $polygon = null;
68
        try {
69 7
            $polygon = geoPHP::load($polygonJson, self::GEOJSON_EXTENSION);
70
        } catch (Exception $exception) {
71
            echo $exception->getMessage();
72
        }
73 7
        return $polygon;
74
    }
75
    
76
    /**
77
     * Adapt quadrant bounds to polygon array format
78
     * @param $quadrantBounds
79
     * @return array
80
     */
81
    public function adaptQuadrantBoundsToPolygon($quadrantBounds)
82
    {
83
        return array(
84
            array(
85
                array($quadrantBounds[0], $quadrantBounds[1]),
86
                array($quadrantBounds[0], $quadrantBounds[3]),
87
                array($quadrantBounds[2], $quadrantBounds[3]),
88
                array($quadrantBounds[2], $quadrantBounds[1]),
89
                array($quadrantBounds[0], $quadrantBounds[1])
90
            )
91
        );
92
    }
93
    
94
    /**
95
     * Create polygon object from quadrant bounds
96
     * @param $quadrantBounds
97
     * @return mixed
98
     */
99
    public function getQuadrantPolygon($quadrantBounds)
100
    {
101
        $polygonPoints = $this->adaptQuadrantBoundsToPolygon($quadrantBounds);
102
        return $this->createPolygonFromPoints($polygonPoints);
103
    }
104
    
105
    /**
106
     * Structure features data
107
     * @param $features
108
     * @return array
109
     */
110
    protected function structureFeatures($features)
111
    {
112
        $structuredFeatures = array();
113
        foreach ($features as $feature) {
114
            $structuredFeatures[] = $this->structureOneFeature($feature);
115
        }
116
        return $structuredFeatures;
117
    }
118
    
119
    /**
120
     * Structure an isolated feature
121
     * @param $feature
122
     * @return array
123
     */
124
    protected function structureOneFeature($feature)
125
    {
126
        $structuredFeature = array(
127
            "type" => self::FEATURE_GEOJSON_NAME,
128
            "geometry" => array(
129
                "type" => $feature['type'],
130
                "coordinates" => $feature['coordinates']
131
            ),
132
            "properties" => $feature['properties']
133
        );
134
        return $structuredFeature;
135
    }
136
    
137
    /**
138
     * Create feature collection array from features list
139
     * @param $features
140
     * @return array
141
     */
142
    public function getFeatureCollection($features)
143
    {
144
        $featuresCollection = array(
145
            "type" => self::FEATURE_COLLECTION_GEOJSON_NAME,
146
            "features" => $this->structureFeatures($features)
147
        );
148
        return $featuresCollection;
149
    }
150
    
151
    /**
152
     * Get intersection data json from two different geometry features
153
     * @param $geoFeaturesJsonA
154
     * @param $geoFeaturesJsonB
155
     * @return mixed
156
     */
157
    public function intersection($geoFeaturesJsonA, $geoFeaturesJsonB)
158
    {
159
        $polygonA = $this->createPolygonFromJson($geoFeaturesJsonA);
160
        $polygonB = $this->createPolygonFromJson($geoFeaturesJsonB);
161
        $intersectionData = $polygonA->intersection($polygonB);
162
        return $intersectionData->out(self::GEOJSON_EXTENSION, true);
163
    }
164
    
165
    /**
166
     * Check if a particular object point is IN the indicated polygon (source: https://github.com/sookoll/geoPHP.git)
167
     * and if it is not contained inside, it checks the boundaries
168
     * @param $point
169
     * @param $polygon
170
     * @return mixed
171
     */
172 7
    protected function isInPolygon($point, $polygon)
173
    {
174 7
        $isInside = false;
175 7
        foreach ($polygon->components as $component) {
176 7
            $polygonPoints = $component->getComponents();
177 7
            $numPoints = count($polygonPoints);
178 7
            $pointIdxBack = $numPoints - 1;
179 7
            for ($pointIdx = 0; $pointIdx < $numPoints; $pointIdx++) {
180 7
                if ($this->isInside($point, $polygonPoints[$pointIdx], $polygonPoints[$pointIdxBack])) {
181 6
                    $isInside = true;
182 6
                    break;
183
                }
184 7
                $pointIdxBack = $pointIdx;
185
            }
186
        }
187 7
        return $isInside;
188
    }
189
    
190
    /**
191
     * Check if point is ON the boundaries of the polygon
192
     * @param $point
193
     * @param $polygon
194
     * @return mixed
195
     */
196 3
    protected function isOnPolygonBoundaries($point, $polygon)
197
    {
198 3
        return $polygon->pointOnVertex($point);
199
    }
200
    
201
    /**
202
     * Check if the polygonA intersects with polygonB
203
     * @param $polygonJsonA
204
     * @param $polygonBoundsB
205
     * @return mixed
206
     * @internal param $polygonA
207
     * @internal param $polygonB
208
     */
209
    public function intersectsPolygons($polygonJsonA, $polygonBoundsB)
210
    {
211
        $polygonA = $this->createPolygonFromJson(json_encode($polygonJsonA));
212
        $polygonB = $this->getQuadrantPolygon($polygonBoundsB);
213
        return $polygonA->intersects($polygonB);
214
    }
215
    
216
    /**
217
     * Check if the polygonA is within polygonB
218
     * @param $polygonBoundsOrigin
219
     * @param $polygonJsonDest
220
     * @return mixed
221
     * @internal param $polygonA
222
     * @internal param $polygonB
223
     */
224
    public function withinPolygon($polygonBoundsOrigin, $polygonJsonDest)
225
    {
226
        $polygonDest = $this->createPolygonFromJson(json_encode($polygonJsonDest));
227
        $polygonOrig = $this->getQuadrantPolygon($polygonBoundsOrigin);
228
        return $polygonOrig->within($polygonDest);
229
    }
230
    
231
    /**
232
     * Create a point geometry object from coordinates (latitude, longitude)
233
     * @param $latitude
234
     * @param $longitude
235
     * @return bool|geoPHP::GeometryCollection|mixed
0 ignored issues
show
Documentation introduced by
The doc-type bool|geoPHP::GeometryCollection|mixed could not be parsed: Unknown type name "geoPHP::GeometryCollection" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
236
     */
237 7
    protected function createPoint($latitude, $longitude)
238
    {
239 7
        $point = null;
240
        try {
241 7
            $formattedPoint = self::POINT_WKT_NAME . "({$longitude} {$latitude})";
242 7
            $point = geoPHP::load($formattedPoint, self::WKT_EXTENSION);
243
        } catch (Exception $exception) {
244
            echo $exception->getMessage();
245
        }
246 7
        return $point;
247
    }
248
    
249
    /**
250
     * Check if point (latitude, longitude) is IN a particular features polygon
251
     * @param $features
252
     * @param $latitude
253
     * @param $longitude
254
     * @return null|string
255
     */
256 7
    public function isPointInQuadrantFeatures($features, $latitude, $longitude)
257
    {
258 7
        $timeZone = self::NOT_FOUND_IN_FEATURES;
259 7
        $point = $this->createPoint($latitude, $longitude);
260 7
        if ($point != null) {
261 7
            foreach ($features['features'] as $feature) {
262 7
                foreach ($feature['geometry']['coordinates'] as $polygonFeatures) {
263 7
                    $polygon = $this->createPolygonFromJson(
264 7
                        json_encode($this->createPolygonJsonFromPoints(
265 7
                            $polygonFeatures
266
                        ))
267
                    );
268 7
                    if ($this->isInPolygon($point, $polygon) ||
269 7
                        $this->isOnPolygonBoundaries($point, $polygon)) {
270 6
                        $timeZone = $feature['properties']['tzid'];
271 7
                        break;
272
                    }
273
                }
274
            }
275
        }
276 7
        return $timeZone;
277
    }
278
    
279
    /**
280
     * Check if the point is between two points from the polygon
281
     * @param $point
282
     * @param $currentPolygonPoint
283
     * @param $backPolygonPoint
284
     * @return bool
285
     */
286 7
    protected function isInside($point, $currentPolygonPoint, $backPolygonPoint)
287
    {
288 7
        return ($currentPolygonPoint->y() > $point->y()) != ($backPolygonPoint->y() > $point->y()) &&
289
            (
290 7
                $point->x() < ($backPolygonPoint->x() - $currentPolygonPoint->x()) *
291 7
                ($point->y() - $currentPolygonPoint->y()) / ($backPolygonPoint->y() - $currentPolygonPoint->y()) +
292 7
                $currentPolygonPoint->x()
293
            );
294
    }
295
}
296