Passed
Push — 4.x ( 01787d...c37faa )
by Doug
12:53
created

Repository::getCoordinateReferenceSystems()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 34
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 9
c 0
b 0
f 0
dl 0
loc 34
ccs 0
cts 8
cp 0
rs 9.9666
cc 4
nc 3
nop 1
crap 20
1
<?php
2
/**
3
 * PHPCoord.
4
 *
5
 * @author Doug Wright
6
 */
7
declare(strict_types=1);
8
9
namespace PHPCoord\EPSG;
10
11
use SQLite3;
12
13
/**
14
 * @internal
15
 */
16
class Repository
17
{
18
    /** @var SQLite3 */
19
    private static $connection;
20
21
    /** @var array */
22
    private static $unitsOfMeasureData = [];
23
24
    /** @var array */
25
    private static $primeMeridianData = [];
26
27
    /** @var array */
28
    private static $ellipsoidData = [];
29
30
    /** @var array */
31
    private static $datumData = [];
32
33
    /** @var array */
34
    private static $coordinateSystemData = [];
35
36
    /** @var array */
37
    private static $coordinateSystemAxisData = [];
38
39
    /** @var array */
40
    private static $coordinateReferenceSystemData = [];
41
42
    private function getConnection(): SQLite3
43
    {
44
        if (!static::$connection instanceof SQLite3) {
0 ignored issues
show
introduced by
static::connection is always a sub-type of SQLite3.
Loading history...
Bug introduced by
Since $connection is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $connection to at least protected.
Loading history...
45
            static::$connection = new SQLite3(__DIR__ . '/../../resources/epsg/epsg.sqlite', SQLITE3_OPEN_READONLY);
46
            static::$connection->enableExceptions(true);
47
        }
48
49 638
        return static::$connection;
50
    }
51 638
52
    public function getEllipsoids(bool $includeDeprecated = false): array
53
    {
54
        if (!static::$ellipsoidData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression static::ellipsoidData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
Bug introduced by
Since $ellipsoidData is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $ellipsoidData to at least protected.
Loading history...
55
            $connection = $this->getConnection();
56
            $sql = '
57
            SELECT
58
                el.ellipsoid_code,
59
                el.ellipsoid_name,
60
                el.semi_major_axis,
61
                el.semi_minor_axis,
62
                el.inv_flattening,
63
                el.uom_code
64
            FROM epsg_ellipsoid el
65
        ';
66
67
            if ($includeDeprecated === false) {
68
                $sql .= ' WHERE el.deprecated = 0';
69
            }
70
71
            $result = $connection->query($sql);
72
73
            while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
74
                // some ellipsoids are defined via inverse flattening and the DB doesn't store the calculated data...
75
                if (!$row['semi_minor_axis']) {
76
                    $row['semi_minor_axis'] = $row['semi_major_axis'] - ($row['semi_major_axis'] / $row['inv_flattening']);
77
                }
78
79
                static::$ellipsoidData[$row['ellipsoid_code']] = $row;
80 638
            }
81
        }
82
83 796
        return static::$ellipsoidData;
84
    }
85 796
86
    public function getDatums(bool $includeDeprecated = false): array
87
    {
88
        if (!static::$datumData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression static::datumData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
Bug introduced by
Since $datumData is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $datumData to at least protected.
Loading history...
89
            $connection = $this->getConnection();
90
            $sql = "
91
            SELECT
92
                d.datum_code,
93
                d.datum_name,
94
                d.datum_type,
95
                d.ellipsoid_code,
96
                d.prime_meridian_code,
97
                d.conventional_rs_code,
98
                d.frame_reference_epoch,
99
                d.deprecated
100
            FROM epsg_datum d
101
            WHERE d.datum_type != 'engineering'
102
        ";
103
104
            if ($includeDeprecated === false) {
105
                $sql .= ' WHERE d.deprecated = 0';
106
            }
107
108
            $result = $connection->query($sql);
109
110
            while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
111
                static::$datumData[$row['datum_code']] = $row;
112 796
            }
113
        }
114
115 760
        return static::$datumData;
116
    }
117 760
118
    public function getUnitsOfMeasure(bool $includeDeprecated = false): array
119
    {
120
        if (!static::$unitsOfMeasureData) {
0 ignored issues
show
Bug introduced by
Since $unitsOfMeasureData is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $unitsOfMeasureData to at least protected.
Loading history...
Bug Best Practice introduced by
The expression static::unitsOfMeasureData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
121
            $connection = $this->getConnection();
122
            $sql = '
123
            SELECT
124
                m.uom_code,
125
                m.unit_of_meas_name,
126
                m.unit_of_meas_type,
127
                m.target_uom_code,
128
                m.factor_b,
129
                m.factor_c,
130
                m.deprecated
131
            FROM epsg_unitofmeasure m
132
            ';
133
134
            if ($includeDeprecated === false) {
135
                $sql .= ' AND m.deprecated = 0';
136
            }
137
138
            $result = $connection->query($sql);
139
140
            while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
141
                static::$unitsOfMeasureData[$row['uom_code']] = $row;
142 760
            }
143
        }
144
145 598
        return static::$unitsOfMeasureData;
146
    }
147 598
148
    public function getPrimeMeridians(bool $includeDeprecated = false): array
149
    {
150
        if (!static::$primeMeridianData) {
0 ignored issues
show
Bug introduced by
Since $primeMeridianData is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $primeMeridianData to at least protected.
Loading history...
Bug Best Practice introduced by
The expression static::primeMeridianData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
151
            $connection = $this->getConnection();
152
            $sql = '
153
            SELECT
154
                p.prime_meridian_code,
155
                p.prime_meridian_name,
156
                p.greenwich_longitude,
157
                p.uom_code,
158
                p.deprecated
159
            FROM epsg_primemeridian p
160
            ';
161
162
            if ($includeDeprecated === false) {
163
                $sql .= ' AND p.deprecated = 0';
164
            }
165
166
            $result = $connection->query($sql);
167
168
            while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
169
                static::$primeMeridianData[$row['prime_meridian_code']] = $row;
170 598
            }
171
        }
172
173
        return static::$primeMeridianData;
174
    }
175
176
    public function getCoordinateSystems(bool $includeDeprecated = false): array
177
    {
178
        if (!static::$coordinateSystemData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression static::coordinateSystemData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
Bug introduced by
Since $coordinateSystemData is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $coordinateSystemData to at least protected.
Loading history...
179
            $connection = $this->getConnection();
180
            $sql = "
181
            SELECT
182
                cs.coord_sys_code,
183
                cs.coord_sys_name,
184
                cs.coord_sys_type,
185
                cs.dimension,
186
                cs.deprecated
187
            FROM epsg_coordinatesystem cs
188
            JOIN epsg_coordinatereferencesystem crs ON crs.coord_sys_code = cs.coord_sys_code AND crs.coord_ref_sys_kind != 'engineering'
189
            WHERE cs.coord_sys_type != 'ordinal'
190
            ";
191
192
            if ($includeDeprecated === false) {
193
                $sql .= ' AND cs.deprecated = 0';
194
            }
195
196
            $result = $connection->query($sql);
197
198
            while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
199
                $row['axes'] = $this->getCoordinateSystemAxes()[$row['coord_sys_code']];
200
                static::$coordinateSystemData[$row['coord_sys_code']] = $row;
201
            }
202
        }
203
204
        return static::$coordinateSystemData;
205
    }
206
207
    public function getCoordinateReferenceSystems(bool $includeDeprecated = false): array
208
    {
209
        if (!static::$coordinateReferenceSystemData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression static::coordinateReferenceSystemData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
Bug introduced by
Since $coordinateReferenceSystemData is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $coordinateReferenceSystemData to at least protected.
Loading history...
210
            $connection = $this->getConnection();
211
            $sql = "
212
            SELECT
213
                crs.coord_ref_sys_code,
214
                crs.coord_ref_sys_kind,
215
                crs.coord_ref_sys_name,
216
                crs.coord_sys_code,
217
                crs.datum_code,
218
                crs.base_crs_code,
219
                crs.projection_conv_code,
220
                crs.cmpd_horizcrs_code,
221
                crs.cmpd_vertcrs_code,
222
                crs.deprecated
223
            FROM epsg_coordinatereferencesystem crs
224
            WHERE crs.coord_ref_sys_kind NOT IN ('engineering', 'derived')
225
            AND (crs.cmpd_horizcrs_code IS NULL OR crs.cmpd_horizcrs_code NOT IN (SELECT coord_ref_sys_code FROM epsg_coordinatereferencesystem WHERE coord_ref_sys_kind = 'engineering'))
226
            AND (crs.cmpd_vertcrs_code IS NULL OR crs.cmpd_vertcrs_code NOT IN (SELECT coord_ref_sys_code FROM epsg_coordinatereferencesystem WHERE coord_ref_sys_kind = 'engineering'))
227
            ";
228
229
            if ($includeDeprecated === false) {
230
                $sql .= ' AND crs.deprecated = 0';
231
            }
232
233
            $result = $connection->query($sql);
234
235
            while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
236
                static::$coordinateReferenceSystemData[$row['coord_ref_sys_code']] = $row;
237
            }
238
        }
239
240
        return static::$coordinateReferenceSystemData;
241
    }
242
243
    private function getCoordinateSystemAxes(): array
244
    {
245
        if (!static::$coordinateSystemAxisData) {
0 ignored issues
show
Bug Best Practice introduced by
The expression static::coordinateSystemAxisData of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
Bug introduced by
Since $coordinateSystemAxisData is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $coordinateSystemAxisData to at least protected.
Loading history...
246
            $connection = $this->getConnection();
247
            $sql = '
248
            SELECT
249
                a.coord_sys_code,
250
                a.coord_axis_orientation,
251
                a.coord_axis_abbreviation,
252
                an.coord_axis_name,
253
                a.uom_code,
254
                a.coord_axis_order
255
            FROM epsg_coordinateaxis a
256
            JOIN epsg_coordinateaxisname an on a.coord_axis_name_code = an.coord_axis_name_code
257
            ORDER BY a.coord_axis_order
258
            ';
259
260
            $result = $connection->query($sql);
261
262
            while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
263
                if (isset(static::$coordinateSystemAxisData[$row['coord_sys_code']])) {
264
                    static::$coordinateSystemAxisData[$row['coord_sys_code']][] = $row;
265
                } else {
266
                    static::$coordinateSystemAxisData[$row['coord_sys_code']] = [$row];
267
                }
268
            }
269
        }
270
271
        return static::$coordinateSystemAxisData;
272
    }
273
}
274