Completed
Push — master ( e6b269...e0437e )
by Michal
04:02
created

ShapeRecord::_saveHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
/**
3
 * BytesFall ShapeFiles library
4
 *
5
 * The library implements the 2D variants of the ShapeFile format as defined in
6
 * http://www.esri.com/library/whitepapers/pdfs/shapefile.pdf.
7
 * The library currently supports reading and editing of ShapeFiles and the
8
 * Associated information (DBF file).
9
 *
10
 * @package bfShapeFiles
11
 * @version 0.0.2
12
 * @link http://bfshapefiles.sourceforge.net/
13
 * @license http://www.gnu.org/licenses/gpl-2.0.html GPLv2-or-later
14
 *
15
 * Copyright 2006-2007 Ovidio <ovidio AT users.sourceforge.net>
16
 *
17
 * This program is free software; you can redistribute it and/or
18
 * modify it under the terms of the GNU General Public License
19
 * as published by the Free Software Foundation.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, you can download one from
28
 * http://www.gnu.org/copyleft/gpl.html.
29
 *
30
 */
31
namespace ShapeFile;
32
33
class ShapeRecord {
34
    private $SHPFile = NULL;
35
    private $DBFFile = NULL;
36
37
    public $recordNumber = NULL;
38
    private $shapeType = NULL;
39
40
    public $lastError = "";
41
42
    public $SHPData = array();
43
    public $DBFData = array();
44
45
    /**
46
     * @param integer $shapeType
47
     */
48
    public function __construct($shapeType) {
49
        $this->shapeType = $shapeType;
50
    }
51
52
    public function loadFromFile(&$SHPFile, &$DBFFile) {
53
        $this->SHPFile = $SHPFile;
54
        $this->DBFFile = $DBFFile;
55
        $this->_loadHeaders();
56
57
        switch ($this->shapeType) {
58
            case 0:
59
                $this->_loadNullRecord();
60
                break;
61
            case 1:
62
                $this->_loadPointRecord();
63
                break;
64
            case 21:
65
                $this->_loadPointMRecord();
66
                break;
67
            case 11:
68
                $this->_loadPointZRecord();
69
                break;
70
            case 3:
71
                $this->_loadPolyLineRecord();
72
                break;
73
            case 23:
74
                $this->_loadPolyLineMRecord();
75
                break;
76
            case 13:
77
                $this->_loadPolyLineZRecord();
78
                break;
79
            case 5:
80
                $this->_loadPolygonRecord();
81
                break;
82
            case 25:
83
                $this->_loadPolygonMRecord();
84
                break;
85
            case 15:
86
                $this->_loadPolygonZRecord();
87
                break;
88
            case 8:
89
                $this->_loadMultiPointRecord();
90
                break;
91
            case 28:
92
                $this->_loadMultiPointMRecord();
93
                break;
94
            case 18:
95
                $this->_loadMultiPointZRecord();
96
                break;
97
            default:
98
                $this->setError(sprintf("The Shape Type '%s' is not supported.", $this->shapeType));
99
                break;
100
        }
101
        $this->_loadDBFData();
102
    }
103
104
    public function saveToFile(&$SHPFile, &$DBFFile, $recordNumber) {
105
        $this->SHPFile = $SHPFile;
106
        $this->DBFFile = $DBFFile;
107
        $this->recordNumber = $recordNumber;
108
        $this->_saveHeaders();
109
110
        switch ($this->shapeType) {
111
            case 0:
112
                $this->_saveNullRecord();
0 ignored issues
show
Unused Code introduced by
The call to the method ShapeFile\ShapeRecord::_saveNullRecord() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
113
                break;
114
            case 1:
115
                $this->_savePointRecord();
116
                break;
117
            case 21:
118
                $this->_savePointMRecord();
119
                break;
120
            case 11:
121
                $this->_savePointZRecord();
122
                break;
123
            case 3:
124
                $this->_savePolyLineRecord();
125
                break;
126
            case 23:
127
                $this->_savePolyLineMRecord();
128
                break;
129
            case 13:
130
                $this->_savePolyLineZRecord();
131
                break;
132
            case 5:
133
                $this->_savePolygonRecord();
134
                break;
135
            case 25:
136
                $this->_savePolygonMRecord();
137
                break;
138
            case 15:
139
                $this->_savePolygonZRecord();
140
                break;
141
            case 8:
142
                $this->_saveMultiPointRecord();
143
                break;
144
            case 28:
145
                $this->_saveMultiPointMRecord();
146
                break;
147
            case 18:
148
                $this->_saveMultiPointZRecord();
149
                break;
150
            default:
151
                $this->setError(sprintf("The Shape Type '%s' is not supported.", $this->shapeType));
152
                break;
153
        }
154
        $this->_saveDBFData();
155
    }
156
157
    public function updateDBFInfo($header) {
158
        $tmp = $this->DBFData;
159
        unset($this->DBFData);
160
        $this->DBFData = array();
161
        reset($header);
162
        while (list($key, $value) = each($header)) {
0 ignored issues
show
Unused Code introduced by
The assignment to $key is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
163
            $this->DBFData[$value[0]] = (isset($tmp[$value[0]])) ? $tmp[$value[0]] : "";
164
        }
165
    }
166
167
    private function _loadHeaders() {
168
        $this->recordNumber = Util::loadData("N", fread($this->SHPFile, 4));
169
        $tmp = Util::loadData("N", fread($this->SHPFile, 4)); //We read the length of the record
0 ignored issues
show
Unused Code introduced by
$tmp is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
170
        $this->shapeType = Util::loadData("V", fread($this->SHPFile, 4));
171
    }
172
173
    private function _saveHeaders() {
174
        fwrite($this->SHPFile, pack("N", $this->recordNumber));
175
        fwrite($this->SHPFile, pack("N", $this->getContentLength()));
176
        fwrite($this->SHPFile, pack("V", $this->shapeType));
177
    }
178
179
    private function _loadPoint() {
180
        $data = array();
181
182
        $data["x"] = Util::loadData("d", fread($this->SHPFile, 8));
183
        $data["y"] = Util::loadData("d", fread($this->SHPFile, 8));
184
185
        return $data;
186
    }
187
188
    private function _loadPointM() {
189
        $data = array();
190
191
        $data["x"] = Util::loadData("d", fread($this->SHPFile, 8));
192
        $data["y"] = Util::loadData("d", fread($this->SHPFile, 8));
193
        $data["m"] = Util::loadData("d", fread($this->SHPFile, 8));
194
195
        return $data;
196
    }
197
198
    private function _loadPointZ() {
199
        $data = array();
200
201
        $data["x"] = Util::loadData("d", fread($this->SHPFile, 8));
202
        $data["y"] = Util::loadData("d", fread($this->SHPFile, 8));
203
        $data["z"] = Util::loadData("d", fread($this->SHPFile, 8));
204
        $data["m"] = Util::loadData("d", fread($this->SHPFile, 8));
205
206
        return $data;
207
    }
208
209
    private function _savePoint($data) {
210
        fwrite($this->SHPFile, Util::packDouble($data["x"]));
211
        fwrite($this->SHPFile, Util::packDouble($data["y"]));
212
    }
213
214
    private function _savePointM($data) {
215
        fwrite($this->SHPFile, Util::packDouble($data["x"]));
216
        fwrite($this->SHPFile, Util::packDouble($data["y"]));
217
        fwrite($this->SHPFile, Util::packDouble($data["m"]));
218
    }
219
220
    private function _savePointZ($data) {
221
        fwrite($this->SHPFile, Util::packDouble($data["x"]));
222
        fwrite($this->SHPFile, Util::packDouble($data["y"]));
223
        fwrite($this->SHPFile, Util::packDouble($data["z"]));
224
        fwrite($this->SHPFile, Util::packDouble($data["m"]));
225
    }
226
227
    private function _saveMeasure($data) {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
228
        fwrite($this->SHPFile, Util::packDouble($data["m"]));
229
    }
230
231
    private function _saveZCoordinate($data) {
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
232
        fwrite($this->SHPFile, Util::packDouble($data["z"]));
233
    }
234
235
    private function _loadNullRecord() {
236
        $this->SHPData = array();
237
    }
238
239
    private function _saveNullRecord() {
240
        //Don't save anything
241
    }
242
243
    private function _loadPointRecord() {
244
        $this->SHPData = $this->_loadPoint();
245
    }
246
247
    private function _loadPointMRecord() {
248
        $this->SHPData = $this->_loadPointM();
249
    }
250
251
    private function _loadPointZRecord() {
252
        $this->SHPData = $this->_loadPointZ();
253
    }
254
255
    private function _savePointRecord() {
256
        $this->_savePoint($this->SHPData);
257
    }
258
259
    private function _savePointMRecord() {
260
        $this->_savePointM($this->SHPData);
261
    }
262
263
    private function _savePointZRecord() {
264
        $this->_savePointZ($this->SHPData);
265
    }
266
267
    private function _loadMultiPointRecord() {
268
        $this->SHPData = array();
269
        $this->SHPData["xmin"] = Util::loadData("d", fread($this->SHPFile, 8));
270
        $this->SHPData["ymin"] = Util::loadData("d", fread($this->SHPFile, 8));
271
        $this->SHPData["xmax"] = Util::loadData("d", fread($this->SHPFile, 8));
272
        $this->SHPData["ymax"] = Util::loadData("d", fread($this->SHPFile, 8));
273
274
        $this->SHPData["numpoints"] = Util::loadData("V", fread($this->SHPFile, 4));
275
276
        for ($i = 0; $i <= $this->SHPData["numpoints"]; $i++) {
1 ignored issue
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...
277
            $this->SHPData["points"][] = $this->_loadPoint();
278
        }
279
    }
280
281
    /**
282
     * @param string $type
283
     */
284
    private function _loadMultiPointMZRecord($type) {
285
286
        $this->SHPData[$type."min"] = Util::loadData("d", fread($this->SHPFile, 8));
287
        $this->SHPData[$type."max"] = Util::loadData("d", fread($this->SHPFile, 8));
288
289
        for ($i = 0; $i <= $this->SHPData["numpoints"]; $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...
290
            $this->SHPData["points"][$i][$type] = Util::loadData("d", fread($this->SHPFile, 8));
291
        }
292
    }
293
294
    private function _loadMultiPointMRecord() {
295
        $this->_loadMultiPointRecord();
296
297
        $this->_loadMultiPointMZRecord("m");
298
    }
299
300
    private function _loadMultiPointZRecord() {
301
        $this->_loadMultiPointRecord();
302
303
        $this->_loadMultiPointMZRecord("z");
304
        $this->_loadMultiPointMZRecord("m");
305
    }
306
307
    private function _saveMultiPointRecord() {
308
        fwrite($this->SHPFile, pack("dddd", $this->SHPData["xmin"], $this->SHPData["ymin"], $this->SHPData["xmax"], $this->SHPData["ymax"]));
309
310
        fwrite($this->SHPFile, pack("V", $this->SHPData["numpoints"]));
311
312
        for ($i = 0; $i <= $this->SHPData["numpoints"]; $i++) {
1 ignored issue
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...
313
            $this->_savePoint($this->SHPData["points"][$i]);
314
        }
315
    }
316
317
    /**
318
     * @param string $type
319
     */
320
    private function _saveMultiPointMZRecord($type) {
321
322
        fwrite($this->SHPFile, pack("dd", $this->SHPData[$type."min"], $this->SHPData[$type."max"]));
323
324
        for ($i = 0; $i <= $this->SHPData["numpoints"]; $i++) {
1 ignored issue
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...
325
            fwrite($this->SHPFile, Util::packDouble($this->SHPData["points"][$type]));
326
        }
327
    }
328
329
    private function _saveMultiPointMRecord() {
330
        $this->_saveMultiPointRecord();
331
332
        $this->_saveMultiPointMZRecord("m");
333
    }
334
335
    private function _saveMultiPointZRecord() {
336
        $this->_saveMultiPointRecord();
337
338
        $this->_saveMultiPointMZRecord("z");
339
        $this->_saveMultiPointMZRecord("m");
340
    }
341
342
    private function _loadPolyLineRecord() {
343
        $this->SHPData = array();
344
        $this->SHPData["xmin"] = Util::loadData("d", fread($this->SHPFile, 8));
345
        $this->SHPData["ymin"] = Util::loadData("d", fread($this->SHPFile, 8));
346
        $this->SHPData["xmax"] = Util::loadData("d", fread($this->SHPFile, 8));
347
        $this->SHPData["ymax"] = Util::loadData("d", fread($this->SHPFile, 8));
348
349
        $this->SHPData["numparts"]  = Util::loadData("V", fread($this->SHPFile, 4));
350
        $this->SHPData["numpoints"] = Util::loadData("V", fread($this->SHPFile, 4));
351
352
        for ($i = 0; $i < $this->SHPData["numparts"]; $i++) {
1 ignored issue
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...
353
            $this->SHPData["parts"][$i] = Util::loadData("V", fread($this->SHPFile, 4));
354
        }
355
356
        $firstIndex = ftell($this->SHPFile);
357
        $readPoints = 0;
358
        reset($this->SHPData["parts"]);
359
        while (list($partIndex, $partData) = each($this->SHPData["parts"])) {
0 ignored issues
show
Unused Code introduced by
The assignment to $partData is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
360
            if (!isset($this->SHPData["parts"][$partIndex]["points"]) || !is_array($this->SHPData["parts"][$partIndex]["points"])) {
361
                $this->SHPData["parts"][$partIndex] = array();
362
                $this->SHPData["parts"][$partIndex]["points"] = array();
363
            }
364
            while (!in_array($readPoints, $this->SHPData["parts"]) && ($readPoints < ($this->SHPData["numpoints"])) && !feof($this->SHPFile)) {
365
                $this->SHPData["parts"][$partIndex]["points"][] = $this->_loadPoint();
366
                $readPoints++;
367
            }
368
        }
369
370
        fseek($this->SHPFile, $firstIndex + ($readPoints * 16));
371
    }
372
373
    /**
374
     * @param string $type
375
     */
376
    private function _loadPolyLineMZRecord($type) {
377
378
        $this->SHPData[$type."min"] = Util::loadData("d", fread($this->SHPFile, 8));
379
        $this->SHPData[$type."max"] = Util::loadData("d", fread($this->SHPFile, 8));
380
381
        $firstIndex = ftell($this->SHPFile);
382
        $readPoints = 0;
383
        reset($this->SHPData["parts"]);
384
        while (list($partIndex, $partData) = each($this->SHPData["parts"])) {
0 ignored issues
show
Unused Code introduced by
The assignment to $partData is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
385
            while (!in_array($readPoints, $this->SHPData["parts"]) && ($readPoints < ($this->SHPData["numpoints"])) && !feof($this->SHPFile)) {
386
                $this->SHPData["parts"][$partIndex]["points"][$readPoints][$type] = Util::loadData("d", fread($this->SHPFile, 8));
387
                $readPoints++;
388
            }
389
        }
390
391
        fseek($this->SHPFile, $firstIndex + ($readPoints * 24));
392
    }
393
394
    private function _loadPolyLineMRecord() {
395
        $this->_loadPolyLineRecord();
396
397
        $this->_loadPolyLineMZRecord("m");
398
    }
399
400
    private function _loadPolyLineZRecord() {
401
        $this->_loadPolyLineRecord();
402
403
        $this->_loadPolyLineMZRecord("z");
404
        $this->_loadPolyLineMZRecord("m");
405
    }
406
407
    private function _savePolyLineRecord() {
408
        fwrite($this->SHPFile, pack("dddd", $this->SHPData["xmin"], $this->SHPData["ymin"], $this->SHPData["xmax"], $this->SHPData["ymax"]));
409
410
        fwrite($this->SHPFile, pack("VV", $this->SHPData["numparts"], $this->SHPData["numpoints"]));
411
412
        for ($i = 0; $i < $this->SHPData["numparts"]; $i++) {
413
            fwrite($this->SHPFile, pack("V", count($this->SHPData["parts"][$i]) - 1));
414
        }
415
416
        foreach ($this->SHPData["parts"] as $partData) {
417
            reset($partData["points"]);
418
            while (list($pointIndex, $pointData) = each($partData["points"])) {
0 ignored issues
show
Unused Code introduced by
The assignment to $pointIndex is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
419
                $this->_savePoint($pointData);
420
            }
421
        }
422
    }
423
424
    /**
425
     * @param string $type
426
     */
427
    private function _savePolyLineMZRecord($type) {
428
        fwrite($this->SHPFile, pack("dd", $this->SHPData[$type."min"], $this->SHPData[$type."max"]));
429
430
        foreach ($this->SHPData["parts"] as $partData) {
431
            reset($partData["points"]);
432
            while (list($pointIndex, $pointData) = each($partData["points"])) {
0 ignored issues
show
Unused Code introduced by
The assignment to $pointIndex is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
433
                fwrite($this->SHPFile, Util::packDouble($pointData[$type]));
434
            }
435
        }
436
    }
437
438
    private function _savePolyLineMRecord() {
439
        $this->_savePolyLineRecord();
440
441
        $this->_savePolyLineMZRecord("m");
442
    }
443
444
    private function _savePolyLineZRecord() {
445
        $this->_savePolyLineRecord();
446
447
        $this->_savePolyLineMZRecord("z");
448
        $this->_savePolyLineMZRecord("m");
449
    }
450
451
    private function _loadPolygonRecord() {
452
        $this->_loadPolyLineRecord();
453
    }
454
455
    private function _loadPolygonMRecord() {
456
        $this->_loadPolyLineMRecord();
457
    }
458
459
    private function _loadPolygonZRecord() {
460
        $this->_loadPolyLineZRecord();
461
    }
462
463
    private function _savePolygonRecord() {
464
        $this->_savePolyLineRecord();
465
    }
466
467
    private function _savePolygonMRecord() {
468
        $this->_savePolyLineMRecord();
469
    }
470
471
    private function _savePolygonZRecord() {
472
        $this->_savePolyLineZRecord();
473
    }
474
475
    private function _adjustBBox($point) {
476
        //Adds a new point to the selected part
477
        if (!isset($this->SHPData["xmin"]) || ($this->SHPData["xmin"] > $point["x"])) {
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...
478
            $this->SHPData["xmin"] = $point["x"];
479
        }
480
        if (!isset($this->SHPData["ymin"]) || ($this->SHPData["ymin"] > $point["y"])) {
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...
481
            $this->SHPData["ymin"] = $point["y"];
482
        }
483
        if (isset($point["m"]) && (!isset($this->SHPData["mmin"]) || ($this->SHPData["mmin"] > $point["m"]))) {
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...
484
            $this->SHPData["mmin"] = $point["m"];
485
        }
486
        if (isset($point["z"]) && (!isset($this->SHPData["zmin"]) || ($this->SHPData["zmin"] > $point["z"]))) {
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...
487
            $this->SHPData["zmin"] = $point["z"];
488
        }
489
        if (!isset($this->SHPData["xmax"]) || ($this->SHPData["xmax"] < $point["x"])) {
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...
490
            $this->SHPData["xmax"] = $point["x"];
491
        }
492
        if (!isset($this->SHPData["ymax"]) || ($this->SHPData["ymax"] < $point["y"])) {
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...
493
            $this->SHPData["ymax"] = $point["y"];
494
        }
495
        if (isset($point["m"]) && (!isset($this->SHPData["mmax"]) || ($this->SHPData["mmax"] < $point["m"]))) {
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...
496
            $this->SHPData["mmax"] = $point["m"];
497
        }
498
        if (isset($point["z"]) && (!isset($this->SHPData["zmax"]) || ($this->SHPData["zmax"] < $point["z"]))) {
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...
499
            $this->SHPData["zmax"] = $point["z"];
500
        }
501
    }
502
503
    public function addPoint($point, $partIndex = 0) {
504
        switch ($this->shapeType) {
505
            case 0:
506
                //Don't add anything
507
                break;
508
            case 1:
509
            case 11:
510
            case 21:
511
                if (in_array($this->shapeType, array(11, 21)) && !isset($point["m"])) {
1 ignored issue
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...
512
                    $point["m"] = 0.0; // no_value
513
                }
514
                if (in_array($this->shapeType, array(11)) && !isset($point["z"])) {
1 ignored issue
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...
515
                    $point["z"] = 0.0; // no_value
516
                }
517
                $this->_adjustBBox($point);
518
519
                //Substitutes the value of the current point
520
                $this->SHPData = $point;
521
                break;
522
            case 3:
523
            case 5:
524
            case 13:
525
            case 15:
526
            case 23:
527
            case 25:
528
                if (in_array($this->shapeType, array(13, 15, 23, 25)) && !isset($point["m"])) {
1 ignored issue
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...
529
                    $point["m"] = 0.0; // no_value
530
                }
531
                if (in_array($this->shapeType, array(13, 15)) && !isset($point["z"])) {
1 ignored issue
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...
532
                    $point["z"] = 0.0; // no_value
533
                }
534
535
                $this->_adjustBBox($point);
536
537
                //Adds a new point to the selected part
538
                $this->SHPData["parts"][$partIndex]["points"][] = $point;
539
540
                $this->SHPData["numparts"] = count($this->SHPData["parts"]);
541
                $this->SHPData["numpoints"] = 1 + (isset($this->SHPData["numpoints"]) ? $this->SHPData["numpoints"] : 0);
542
                break;
543
            case 8:
544
            case 18:
545
            case 28:
546
                if (in_array($this->shapeType, array(18, 28)) && !isset($point["m"])) {
1 ignored issue
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...
547
                    $point["m"] = 0.0; // no_value
548
                }
549
                if (in_array($this->shapeType, array(18)) && !isset($point["z"])) {
1 ignored issue
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...
550
                    $point["z"] = 0.0; // no_value
551
                }
552
553
                $this->_adjustBBox($point);
554
555
                //Adds a new point
556
                $this->SHPData["points"][] = $point;
557
                $this->SHPData["numpoints"] = 1 + (isset($this->SHPData["numpoints"]) ? $this->SHPData["numpoints"] : 0);
558
                break;
559
            default:
560
                $this->setError(sprintf("The Shape Type '%s' is not supported.", $this->shapeType));
561
                break;
562
        }
563
    }
564
565
    public function deletePoint($pointIndex = 0, $partIndex = 0) {
566
        switch ($this->shapeType) {
567
            case 0:
568
                //Don't delete anything
569
                break;
570
            case 1:
571
            case 11:
572
            case 21:
573
                //Sets the value of the point to zero
574
                $this->SHPData["x"] = 0.0;
575
                $this->SHPData["y"] = 0.0;
576
                if (in_array($this->shapeType, array(11, 21))) {
577
                    $this->SHPData["m"] = 0.0;
578
                }
579
                if (in_array($this->shapeType, array(11))) {
580
                    $this->SHPData["z"] = 0.0;
581
                }
582
                break;
583
            case 3:
584
            case 5:
585
            case 13:
586
            case 15:
587
            case 23:
588
            case 25:
589
                //Deletes the point from the selected part, if exists
590
                if (isset($this->SHPData["parts"][$partIndex]) && isset($this->SHPData["parts"][$partIndex]["points"][$pointIndex])) {
591
                    for ($i = $pointIndex; $i < (count($this->SHPData["parts"][$partIndex]["points"]) - 1); $i++) {
592
                        $this->SHPData["parts"][$partIndex]["points"][$i] = $this->SHPData["parts"][$partIndex]["points"][$i + 1];
593
                    }
594
                    unset($this->SHPData["parts"][$partIndex]["points"][count($this->SHPData["parts"][$partIndex]["points"]) - 1]);
595
596
                    $this->SHPData["numparts"] = count($this->SHPData["parts"]);
597
                    $this->SHPData["numpoints"]--;
598
                }
599
                break;
600
            case 8:
601
            case 18:
602
            case 28:
603
                //Deletes the point, if exists
604
                if (isset($this->SHPData["points"][$pointIndex])) {
605
                    for ($i = $pointIndex; $i < (count($this->SHPData["points"]) - 1); $i++) {
606
                        $this->SHPData["points"][$i] = $this->SHPData["points"][$i + 1];
607
                    }
608
                    unset($this->SHPData["points"][count($this->SHPData["points"]) - 1]);
609
610
                    $this->SHPData["numpoints"]--;
611
                }
612
                break;
613
            default:
614
                $this->setError(sprintf("The Shape Type '%s' is not supported.", $this->shapeType));
615
                break;
616
        }
617
    }
618
619
    public function getContentLength() {
620
        // The content length for a record is the length of the record contents section measured in 16-bit words.
621
        // one coordinate makes 4 16-bit words (64 bit double)
622
        switch ($this->shapeType) {
623
            case 0:
624
                $result = 0;
625
                break;
626
            case 1:
627
                $result = 10;
628
                break;
629
            case 21:
630
                $result = 10 + 4;
631
                break;
632
            case 11:
633
                $result = 10 + 8;
634
                break;
635
            case 3:
636
            case 5:
637
                $result = 22 + 2 * count($this->SHPData["parts"]);
638
                for ($i = 0; $i < count($this->SHPData["parts"]); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
639
                    $result += 8 * count($this->SHPData["parts"][$i]["points"]);
640
                }
641
                break;
642
            case 23:
643
            case 25:
1 ignored issue
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...
644
                $result = 22 + (2 * 4) + 2 * count($this->SHPData["parts"]);
645
                for ($i = 0; $i < count($this->SHPData["parts"]); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
646
                    $result += (8 + 4) * count($this->SHPData["parts"][$i]["points"]);
647
                }
648
                break;
649
            case 13:
650
            case 15:
1 ignored issue
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...
651
                $result = 22 + (4 * 4) + 2 * count($this->SHPData["parts"]);
652
                for ($i = 0; $i < count($this->SHPData["parts"]); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
653
                    $result += (8 + 8) * count($this->SHPData["parts"][$i]["points"]);
654
                }
655
                break;
656
            case 8:
657
                $result = 20 + 8 * count($this->SHPData["points"]);
658
                break;
659
            case 28:
1 ignored issue
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...
660
                $result = 20 + (2 * 4) + (8 + 4) * count($this->SHPData["points"]);
661
                break;
662
            case 18:
1 ignored issue
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...
663
                $result = 20 + (4 * 4) + (8 + 8) * count($this->SHPData["points"]);
664
                break;
665
            default:
666
                $result = false;
667
                $this->setError(sprintf("The Shape Type '%s' is not supported.", $this->shapeType));
668
                break;
669
        }
670
        return $result;
671
    }
672
673
    private function _loadDBFData() {
674
        $this->DBFData = @dbase_get_record_with_names($this->DBFFile, $this->recordNumber);
675
        unset($this->DBFData["deleted"]);
676
    }
677
678
    private function _saveDBFData() {
679
        unset($this->DBFData["deleted"]);
680
        if ($this->recordNumber <= dbase_numrecords($this->DBFFile)) {
681
            if (!dbase_replace_record($this->DBFFile, array_values($this->DBFData), $this->recordNumber)) {
682
                $this->setError("I wasn't possible to update the information in the DBF file.");
683
            }
684
        } else {
685
            if (!dbase_add_record($this->DBFFile, array_values($this->DBFData))) {
686
                $this->setError("I wasn't possible to add the information to the DBF file.");
687
            }
688
        }
689
    }
690
691
    /**
692
     * @param string $error
693
     */
694
    public function setError($error) {
695
        $this->lastError = $error;
696
        return false;
697
    }
698
}
699