Completed
Push — master ( 2e3006...6d7353 )
by Michal
09:16 queued 05:53
created

ShapeRecord::_savePointMRecord()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
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
        // Adjusts bounding box based on point
477
        $directions = array('x', 'y', 'z', 'm');
478
        foreach ($directions as $direction) {
479
            if (!isset($point[$direction])) {
480
                continue;
481
            }
482
            $min = $direction . 'min';
483
            $max = $direction . 'max';
484 View Code Duplication
            if (!isset($this->SHPData[$min]) || ($this->SHPData[$min] > $point[$direction])) {
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...
485
                $this->SHPData[$min] = $point[$direction];
486
            }
487 View Code Duplication
            if (!isset($this->SHPData[$max]) || ($this->SHPData[$max] < $point[$direction])) {
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...
488
                $this->SHPData[$max] = $point[$direction];
489
            }
490
        }
491
    }
492
493
    public function addPoint($point, $partIndex = 0) {
494
        switch ($this->shapeType) {
495
            case 0:
496
                //Don't add anything
497
                break;
498
            case 1:
499
            case 11:
500
            case 21:
501
                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...
502
                    $point["m"] = 0.0; // no_value
503
                }
504
                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...
505
                    $point["z"] = 0.0; // no_value
506
                }
507
508
                //Substitutes the value of the current point
509
                $this->SHPData = $point;
510
                $this->_adjustBBox($point);
511
                break;
512
            case 3:
513
            case 5:
514
            case 13:
515
            case 15:
516
            case 23:
517
            case 25:
518
                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...
519
                    $point["m"] = 0.0; // no_value
520
                }
521
                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...
522
                    $point["z"] = 0.0; // no_value
523
                }
524
525
                $this->_adjustBBox($point);
526
527
                //Adds a new point to the selected part
528
                $this->SHPData["parts"][$partIndex]["points"][] = $point;
529
530
                $this->SHPData["numparts"] = count($this->SHPData["parts"]);
531
                $this->SHPData["numpoints"] = 1 + (isset($this->SHPData["numpoints"]) ? $this->SHPData["numpoints"] : 0);
532
                break;
533
            case 8:
534
            case 18:
535
            case 28:
536
                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...
537
                    $point["m"] = 0.0; // no_value
538
                }
539
                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...
540
                    $point["z"] = 0.0; // no_value
541
                }
542
543
                $this->_adjustBBox($point);
544
545
                //Adds a new point
546
                $this->SHPData["points"][] = $point;
547
                $this->SHPData["numpoints"] = 1 + (isset($this->SHPData["numpoints"]) ? $this->SHPData["numpoints"] : 0);
548
                break;
549
            default:
550
                $this->setError(sprintf("The Shape Type '%s' is not supported.", $this->shapeType));
551
                break;
552
        }
553
    }
554
555
    public function deletePoint($pointIndex = 0, $partIndex = 0) {
556
        switch ($this->shapeType) {
557
            case 0:
558
                //Don't delete anything
559
                break;
560
            case 1:
561
            case 11:
562
            case 21:
563
                //Sets the value of the point to zero
564
                $this->SHPData["x"] = 0.0;
565
                $this->SHPData["y"] = 0.0;
566
                if (in_array($this->shapeType, array(11, 21))) {
567
                    $this->SHPData["m"] = 0.0;
568
                }
569
                if (in_array($this->shapeType, array(11))) {
570
                    $this->SHPData["z"] = 0.0;
571
                }
572
                break;
573
            case 3:
574
            case 5:
575
            case 13:
576
            case 15:
577
            case 23:
578
            case 25:
579
                //Deletes the point from the selected part, if exists
580
                if (isset($this->SHPData["parts"][$partIndex]) && isset($this->SHPData["parts"][$partIndex]["points"][$pointIndex])) {
581
                    for ($i = $pointIndex; $i < (count($this->SHPData["parts"][$partIndex]["points"]) - 1); $i++) {
582
                        $this->SHPData["parts"][$partIndex]["points"][$i] = $this->SHPData["parts"][$partIndex]["points"][$i + 1];
583
                    }
584
                    unset($this->SHPData["parts"][$partIndex]["points"][count($this->SHPData["parts"][$partIndex]["points"]) - 1]);
585
586
                    $this->SHPData["numparts"] = count($this->SHPData["parts"]);
587
                    $this->SHPData["numpoints"]--;
588
                }
589
                break;
590
            case 8:
591
            case 18:
592
            case 28:
593
                //Deletes the point, if exists
594
                if (isset($this->SHPData["points"][$pointIndex])) {
595
                    for ($i = $pointIndex; $i < (count($this->SHPData["points"]) - 1); $i++) {
596
                        $this->SHPData["points"][$i] = $this->SHPData["points"][$i + 1];
597
                    }
598
                    unset($this->SHPData["points"][count($this->SHPData["points"]) - 1]);
599
600
                    $this->SHPData["numpoints"]--;
601
                }
602
                break;
603
            default:
604
                $this->setError(sprintf("The Shape Type '%s' is not supported.", $this->shapeType));
605
                break;
606
        }
607
    }
608
609
    public function getContentLength() {
610
        // The content length for a record is the length of the record contents section measured in 16-bit words.
611
        // one coordinate makes 4 16-bit words (64 bit double)
612
        switch ($this->shapeType) {
613
            case 0:
614
                $result = 0;
615
                break;
616
            case 1:
617
                $result = 10;
618
                break;
619
            case 21:
620
                $result = 10 + 4;
621
                break;
622
            case 11:
623
                $result = 10 + 8;
624
                break;
625
            case 3:
626
            case 5:
627
                $result = 22 + 2 * count($this->SHPData["parts"]);
628
                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...
629
                    $result += 8 * count($this->SHPData["parts"][$i]["points"]);
630
                }
631
                break;
632
            case 23:
633
            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...
634
                $result = 22 + (2 * 4) + 2 * count($this->SHPData["parts"]);
635
                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...
636
                    $result += (8 + 4) * count($this->SHPData["parts"][$i]["points"]);
637
                }
638
                break;
639
            case 13:
640
            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...
641
                $result = 22 + (4 * 4) + 2 * count($this->SHPData["parts"]);
642
                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...
643
                    $result += (8 + 8) * count($this->SHPData["parts"][$i]["points"]);
644
                }
645
                break;
646
            case 8:
647
                $result = 20 + 8 * count($this->SHPData["points"]);
648
                break;
649
            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...
650
                $result = 20 + (2 * 4) + (8 + 4) * count($this->SHPData["points"]);
651
                break;
652
            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...
653
                $result = 20 + (4 * 4) + (8 + 8) * count($this->SHPData["points"]);
654
                break;
655
            default:
656
                $result = false;
657
                $this->setError(sprintf("The Shape Type '%s' is not supported.", $this->shapeType));
658
                break;
659
        }
660
        return $result;
661
    }
662
663
    private function _loadDBFData() {
664
        $this->DBFData = @dbase_get_record_with_names($this->DBFFile, $this->recordNumber);
665
        unset($this->DBFData["deleted"]);
666
    }
667
668
    private function _saveDBFData() {
669
        unset($this->DBFData["deleted"]);
670
        if ($this->recordNumber <= dbase_numrecords($this->DBFFile)) {
671
            if (!dbase_replace_record($this->DBFFile, array_values($this->DBFData), $this->recordNumber)) {
672
                $this->setError("I wasn't possible to update the information in the DBF file.");
673
            }
674
        } else {
675
            if (!dbase_add_record($this->DBFFile, array_values($this->DBFData))) {
676
                $this->setError("I wasn't possible to add the information to the DBF file.");
677
            }
678
        }
679
    }
680
681
    /**
682
     * @param string $error
683
     */
684
    public function setError($error) {
685
        $this->lastError = $error;
686
        return false;
687
    }
688
}
689