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(); |
|
|
|
|
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)) { |
|
|
|
|
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 |
|
|
|
|
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 _loadNullRecord() { |
228
|
|
|
$this->SHPData = array(); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
private function _saveNullRecord() { |
232
|
|
|
//Don't save anything |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
private function _loadPointRecord() { |
236
|
|
|
$this->SHPData = $this->_loadPoint(); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
private function _loadPointMRecord() { |
240
|
|
|
$this->SHPData = $this->_loadPointM(); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
private function _loadPointZRecord() { |
244
|
|
|
$this->SHPData = $this->_loadPointZ(); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
private function _savePointRecord() { |
248
|
|
|
$this->_savePoint($this->SHPData); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
private function _savePointMRecord() { |
252
|
|
|
$this->_savePointM($this->SHPData); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
private function _savePointZRecord() { |
256
|
|
|
$this->_savePointZ($this->SHPData); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
private function _loadMultiPointRecord() { |
260
|
|
|
$this->SHPData = array(); |
261
|
|
|
$this->SHPData["xmin"] = Util::loadData("d", fread($this->SHPFile, 8)); |
262
|
|
|
$this->SHPData["ymin"] = Util::loadData("d", fread($this->SHPFile, 8)); |
263
|
|
|
$this->SHPData["xmax"] = Util::loadData("d", fread($this->SHPFile, 8)); |
264
|
|
|
$this->SHPData["ymax"] = Util::loadData("d", fread($this->SHPFile, 8)); |
265
|
|
|
|
266
|
|
|
$this->SHPData["numpoints"] = Util::loadData("V", fread($this->SHPFile, 4)); |
267
|
|
|
|
268
|
|
|
for ($i = 0; $i <= $this->SHPData["numpoints"]; $i++) { |
|
|
|
|
269
|
|
|
$this->SHPData["points"][] = $this->_loadPoint(); |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @param string $type |
275
|
|
|
*/ |
276
|
|
|
private function _loadMultiPointMZRecord($type) { |
277
|
|
|
|
278
|
|
|
$this->SHPData[$type."min"] = Util::loadData("d", fread($this->SHPFile, 8)); |
279
|
|
|
$this->SHPData[$type."max"] = Util::loadData("d", fread($this->SHPFile, 8)); |
280
|
|
|
|
281
|
|
|
for ($i = 0; $i <= $this->SHPData["numpoints"]; $i++) { |
|
|
|
|
282
|
|
|
$this->SHPData["points"][$i][$type] = Util::loadData("d", fread($this->SHPFile, 8)); |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
private function _loadMultiPointMRecord() { |
287
|
|
|
$this->_loadMultiPointRecord(); |
288
|
|
|
|
289
|
|
|
$this->_loadMultiPointMZRecord("m"); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
private function _loadMultiPointZRecord() { |
293
|
|
|
$this->_loadMultiPointRecord(); |
294
|
|
|
|
295
|
|
|
$this->_loadMultiPointMZRecord("z"); |
296
|
|
|
$this->_loadMultiPointMZRecord("m"); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
private function _saveMultiPointRecord() { |
300
|
|
|
fwrite($this->SHPFile, pack("dddd", $this->SHPData["xmin"], $this->SHPData["ymin"], $this->SHPData["xmax"], $this->SHPData["ymax"])); |
301
|
|
|
|
302
|
|
|
fwrite($this->SHPFile, pack("V", $this->SHPData["numpoints"])); |
303
|
|
|
|
304
|
|
|
for ($i = 0; $i <= $this->SHPData["numpoints"]; $i++) { |
|
|
|
|
305
|
|
|
$this->_savePoint($this->SHPData["points"][$i]); |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* @param string $type |
311
|
|
|
*/ |
312
|
|
|
private function _saveMultiPointMZRecord($type) { |
313
|
|
|
|
314
|
|
|
fwrite($this->SHPFile, pack("dd", $this->SHPData[$type."min"], $this->SHPData[$type."max"])); |
315
|
|
|
|
316
|
|
|
for ($i = 0; $i <= $this->SHPData["numpoints"]; $i++) { |
|
|
|
|
317
|
|
|
fwrite($this->SHPFile, Util::packDouble($this->SHPData["points"][$type])); |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
private function _saveMultiPointMRecord() { |
322
|
|
|
$this->_saveMultiPointRecord(); |
323
|
|
|
|
324
|
|
|
$this->_saveMultiPointMZRecord("m"); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
private function _saveMultiPointZRecord() { |
328
|
|
|
$this->_saveMultiPointRecord(); |
329
|
|
|
|
330
|
|
|
$this->_saveMultiPointMZRecord("z"); |
331
|
|
|
$this->_saveMultiPointMZRecord("m"); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
private function _loadPolyLineRecord() { |
335
|
|
|
$this->SHPData = array(); |
336
|
|
|
$this->SHPData["xmin"] = Util::loadData("d", fread($this->SHPFile, 8)); |
337
|
|
|
$this->SHPData["ymin"] = Util::loadData("d", fread($this->SHPFile, 8)); |
338
|
|
|
$this->SHPData["xmax"] = Util::loadData("d", fread($this->SHPFile, 8)); |
339
|
|
|
$this->SHPData["ymax"] = Util::loadData("d", fread($this->SHPFile, 8)); |
340
|
|
|
|
341
|
|
|
$this->SHPData["numparts"] = Util::loadData("V", fread($this->SHPFile, 4)); |
342
|
|
|
$this->SHPData["numpoints"] = Util::loadData("V", fread($this->SHPFile, 4)); |
343
|
|
|
|
344
|
|
|
for ($i = 0; $i < $this->SHPData["numparts"]; $i++) { |
|
|
|
|
345
|
|
|
$this->SHPData["parts"][$i] = Util::loadData("V", fread($this->SHPFile, 4)); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
$firstIndex = ftell($this->SHPFile); |
349
|
|
|
$readPoints = 0; |
350
|
|
|
reset($this->SHPData["parts"]); |
351
|
|
|
while (list($partIndex, $partData) = each($this->SHPData["parts"])) { |
|
|
|
|
352
|
|
|
if (!isset($this->SHPData["parts"][$partIndex]["points"]) || !is_array($this->SHPData["parts"][$partIndex]["points"])) { |
353
|
|
|
$this->SHPData["parts"][$partIndex] = array(); |
354
|
|
|
$this->SHPData["parts"][$partIndex]["points"] = array(); |
355
|
|
|
} |
356
|
|
|
while (!in_array($readPoints, $this->SHPData["parts"]) && ($readPoints < ($this->SHPData["numpoints"])) && !feof($this->SHPFile)) { |
357
|
|
|
$this->SHPData["parts"][$partIndex]["points"][] = $this->_loadPoint(); |
358
|
|
|
$readPoints++; |
359
|
|
|
} |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
fseek($this->SHPFile, $firstIndex + ($readPoints * 16)); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* @param string $type |
367
|
|
|
*/ |
368
|
|
|
private function _loadPolyLineMZRecord($type) { |
369
|
|
|
|
370
|
|
|
$this->SHPData[$type."min"] = Util::loadData("d", fread($this->SHPFile, 8)); |
371
|
|
|
$this->SHPData[$type."max"] = Util::loadData("d", fread($this->SHPFile, 8)); |
372
|
|
|
|
373
|
|
|
$firstIndex = ftell($this->SHPFile); |
374
|
|
|
$readPoints = 0; |
375
|
|
|
reset($this->SHPData["parts"]); |
376
|
|
|
while (list($partIndex, $partData) = each($this->SHPData["parts"])) { |
|
|
|
|
377
|
|
|
while (!in_array($readPoints, $this->SHPData["parts"]) && ($readPoints < ($this->SHPData["numpoints"])) && !feof($this->SHPFile)) { |
378
|
|
|
$this->SHPData["parts"][$partIndex]["points"][$readPoints][$type] = Util::loadData("d", fread($this->SHPFile, 8)); |
379
|
|
|
$readPoints++; |
380
|
|
|
} |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
fseek($this->SHPFile, $firstIndex + ($readPoints * 24)); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
private function _loadPolyLineMRecord() { |
387
|
|
|
$this->_loadPolyLineRecord(); |
388
|
|
|
|
389
|
|
|
$this->_loadPolyLineMZRecord("m"); |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
private function _loadPolyLineZRecord() { |
393
|
|
|
$this->_loadPolyLineRecord(); |
394
|
|
|
|
395
|
|
|
$this->_loadPolyLineMZRecord("z"); |
396
|
|
|
$this->_loadPolyLineMZRecord("m"); |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
private function _savePolyLineRecord() { |
400
|
|
|
fwrite($this->SHPFile, pack("dddd", $this->SHPData["xmin"], $this->SHPData["ymin"], $this->SHPData["xmax"], $this->SHPData["ymax"])); |
401
|
|
|
|
402
|
|
|
fwrite($this->SHPFile, pack("VV", $this->SHPData["numparts"], $this->SHPData["numpoints"])); |
403
|
|
|
|
404
|
|
|
for ($i = 0; $i < $this->SHPData["numparts"]; $i++) { |
405
|
|
|
fwrite($this->SHPFile, pack("V", count($this->SHPData["parts"][$i]) - 1)); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
foreach ($this->SHPData["parts"] as $partData) { |
409
|
|
|
reset($partData["points"]); |
410
|
|
|
while (list($pointIndex, $pointData) = each($partData["points"])) { |
|
|
|
|
411
|
|
|
$this->_savePoint($pointData); |
412
|
|
|
} |
413
|
|
|
} |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* @param string $type |
418
|
|
|
*/ |
419
|
|
|
private function _savePolyLineMZRecord($type) { |
420
|
|
|
fwrite($this->SHPFile, pack("dd", $this->SHPData[$type."min"], $this->SHPData[$type."max"])); |
421
|
|
|
|
422
|
|
|
foreach ($this->SHPData["parts"] as $partData) { |
423
|
|
|
reset($partData["points"]); |
424
|
|
|
while (list($pointIndex, $pointData) = each($partData["points"])) { |
|
|
|
|
425
|
|
|
fwrite($this->SHPFile, Util::packDouble($pointData[$type])); |
426
|
|
|
} |
427
|
|
|
} |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
private function _savePolyLineMRecord() { |
431
|
|
|
$this->_savePolyLineRecord(); |
432
|
|
|
|
433
|
|
|
$this->_savePolyLineMZRecord("m"); |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
private function _savePolyLineZRecord() { |
437
|
|
|
$this->_savePolyLineRecord(); |
438
|
|
|
|
439
|
|
|
$this->_savePolyLineMZRecord("z"); |
440
|
|
|
$this->_savePolyLineMZRecord("m"); |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
private function _loadPolygonRecord() { |
444
|
|
|
$this->_loadPolyLineRecord(); |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
private function _loadPolygonMRecord() { |
448
|
|
|
$this->_loadPolyLineMRecord(); |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
private function _loadPolygonZRecord() { |
452
|
|
|
$this->_loadPolyLineZRecord(); |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
private function _savePolygonRecord() { |
456
|
|
|
$this->_savePolyLineRecord(); |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
private function _savePolygonMRecord() { |
460
|
|
|
$this->_savePolyLineMRecord(); |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
private function _savePolygonZRecord() { |
464
|
|
|
$this->_savePolyLineZRecord(); |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
private function _adjustBBox($point) { |
468
|
|
|
// Adjusts bounding box based on point |
469
|
|
|
$directions = array('x', 'y', 'z', 'm'); |
470
|
|
|
foreach ($directions as $direction) { |
471
|
|
|
if (!isset($point[$direction])) { |
472
|
|
|
continue; |
473
|
|
|
} |
474
|
|
|
$min = $direction . 'min'; |
475
|
|
|
$max = $direction . 'max'; |
476
|
|
|
if (!isset($this->SHPData[$min]) || ($this->SHPData[$min] > $point[$direction])) { |
|
|
|
|
477
|
|
|
$this->SHPData[$min] = $point[$direction]; |
478
|
|
|
} |
479
|
|
|
if (!isset($this->SHPData[$max]) || ($this->SHPData[$max] < $point[$direction])) { |
|
|
|
|
480
|
|
|
$this->SHPData[$max] = $point[$direction]; |
481
|
|
|
} |
482
|
|
|
} |
483
|
|
|
} |
484
|
|
|
|
485
|
|
|
public function addPoint($point, $partIndex = 0) { |
486
|
|
|
switch ($this->shapeType) { |
487
|
|
|
case 0: |
488
|
|
|
//Don't add anything |
489
|
|
|
break; |
490
|
|
|
case 1: |
491
|
|
|
case 11: |
492
|
|
|
case 21: |
493
|
|
|
if (in_array($this->shapeType, array(11, 21)) && !isset($point["m"])) { |
|
|
|
|
494
|
|
|
$point["m"] = 0.0; // no_value |
495
|
|
|
} |
496
|
|
|
if (in_array($this->shapeType, array(11)) && !isset($point["z"])) { |
|
|
|
|
497
|
|
|
$point["z"] = 0.0; // no_value |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
//Substitutes the value of the current point |
501
|
|
|
$this->SHPData = $point; |
502
|
|
|
$this->_adjustBBox($point); |
503
|
|
|
break; |
504
|
|
|
case 3: |
505
|
|
|
case 5: |
506
|
|
|
case 13: |
507
|
|
|
case 15: |
508
|
|
|
case 23: |
509
|
|
|
case 25: |
510
|
|
|
if (in_array($this->shapeType, array(13, 15, 23, 25)) && !isset($point["m"])) { |
|
|
|
|
511
|
|
|
$point["m"] = 0.0; // no_value |
512
|
|
|
} |
513
|
|
|
if (in_array($this->shapeType, array(13, 15)) && !isset($point["z"])) { |
|
|
|
|
514
|
|
|
$point["z"] = 0.0; // no_value |
515
|
|
|
} |
516
|
|
|
|
517
|
|
|
$this->_adjustBBox($point); |
518
|
|
|
|
519
|
|
|
//Adds a new point to the selected part |
520
|
|
|
$this->SHPData["parts"][$partIndex]["points"][] = $point; |
521
|
|
|
|
522
|
|
|
$this->SHPData["numparts"] = count($this->SHPData["parts"]); |
523
|
|
|
$this->SHPData["numpoints"] = 1 + (isset($this->SHPData["numpoints"]) ? $this->SHPData["numpoints"] : 0); |
524
|
|
|
break; |
525
|
|
|
case 8: |
526
|
|
|
case 18: |
527
|
|
|
case 28: |
528
|
|
|
if (in_array($this->shapeType, array(18, 28)) && !isset($point["m"])) { |
|
|
|
|
529
|
|
|
$point["m"] = 0.0; // no_value |
530
|
|
|
} |
531
|
|
|
if (in_array($this->shapeType, array(18)) && !isset($point["z"])) { |
|
|
|
|
532
|
|
|
$point["z"] = 0.0; // no_value |
533
|
|
|
} |
534
|
|
|
|
535
|
|
|
$this->_adjustBBox($point); |
536
|
|
|
|
537
|
|
|
//Adds a new point |
538
|
|
|
$this->SHPData["points"][] = $point; |
539
|
|
|
$this->SHPData["numpoints"] = 1 + (isset($this->SHPData["numpoints"]) ? $this->SHPData["numpoints"] : 0); |
540
|
|
|
break; |
541
|
|
|
default: |
542
|
|
|
$this->setError(sprintf("The Shape Type '%s' is not supported.", $this->shapeType)); |
543
|
|
|
break; |
544
|
|
|
} |
545
|
|
|
} |
546
|
|
|
|
547
|
|
|
public function deletePoint($pointIndex = 0, $partIndex = 0) { |
548
|
|
|
switch ($this->shapeType) { |
549
|
|
|
case 0: |
550
|
|
|
//Don't delete anything |
551
|
|
|
break; |
552
|
|
|
case 1: |
553
|
|
|
case 11: |
554
|
|
|
case 21: |
555
|
|
|
//Sets the value of the point to zero |
556
|
|
|
$this->SHPData["x"] = 0.0; |
557
|
|
|
$this->SHPData["y"] = 0.0; |
558
|
|
|
if (in_array($this->shapeType, array(11, 21))) { |
559
|
|
|
$this->SHPData["m"] = 0.0; |
560
|
|
|
} |
561
|
|
|
if (in_array($this->shapeType, array(11))) { |
562
|
|
|
$this->SHPData["z"] = 0.0; |
563
|
|
|
} |
564
|
|
|
break; |
565
|
|
|
case 3: |
566
|
|
|
case 5: |
567
|
|
|
case 13: |
568
|
|
|
case 15: |
569
|
|
|
case 23: |
570
|
|
|
case 25: |
571
|
|
|
//Deletes the point from the selected part, if exists |
572
|
|
|
if (isset($this->SHPData["parts"][$partIndex]) && isset($this->SHPData["parts"][$partIndex]["points"][$pointIndex])) { |
573
|
|
|
for ($i = $pointIndex; $i < (count($this->SHPData["parts"][$partIndex]["points"]) - 1); $i++) { |
574
|
|
|
$this->SHPData["parts"][$partIndex]["points"][$i] = $this->SHPData["parts"][$partIndex]["points"][$i + 1]; |
575
|
|
|
} |
576
|
|
|
unset($this->SHPData["parts"][$partIndex]["points"][count($this->SHPData["parts"][$partIndex]["points"]) - 1]); |
577
|
|
|
|
578
|
|
|
$this->SHPData["numparts"] = count($this->SHPData["parts"]); |
579
|
|
|
$this->SHPData["numpoints"]--; |
580
|
|
|
} |
581
|
|
|
break; |
582
|
|
|
case 8: |
583
|
|
|
case 18: |
584
|
|
|
case 28: |
585
|
|
|
//Deletes the point, if exists |
586
|
|
|
if (isset($this->SHPData["points"][$pointIndex])) { |
587
|
|
|
for ($i = $pointIndex; $i < (count($this->SHPData["points"]) - 1); $i++) { |
588
|
|
|
$this->SHPData["points"][$i] = $this->SHPData["points"][$i + 1]; |
589
|
|
|
} |
590
|
|
|
unset($this->SHPData["points"][count($this->SHPData["points"]) - 1]); |
591
|
|
|
|
592
|
|
|
$this->SHPData["numpoints"]--; |
593
|
|
|
} |
594
|
|
|
break; |
595
|
|
|
default: |
596
|
|
|
$this->setError(sprintf("The Shape Type '%s' is not supported.", $this->shapeType)); |
597
|
|
|
break; |
598
|
|
|
} |
599
|
|
|
} |
600
|
|
|
|
601
|
|
|
public function getContentLength() { |
602
|
|
|
// The content length for a record is the length of the record contents section measured in 16-bit words. |
603
|
|
|
// one coordinate makes 4 16-bit words (64 bit double) |
604
|
|
|
switch ($this->shapeType) { |
605
|
|
|
case 0: |
606
|
|
|
$result = 0; |
607
|
|
|
break; |
608
|
|
|
case 1: |
609
|
|
|
$result = 10; |
610
|
|
|
break; |
611
|
|
|
case 21: |
612
|
|
|
$result = 10 + 4; |
613
|
|
|
break; |
614
|
|
|
case 11: |
615
|
|
|
$result = 10 + 8; |
616
|
|
|
break; |
617
|
|
|
case 3: |
618
|
|
|
case 5: |
619
|
|
|
$result = 22 + 2 * count($this->SHPData["parts"]); |
620
|
|
|
for ($i = 0; $i < count($this->SHPData["parts"]); $i++) { |
|
|
|
|
621
|
|
|
$result += 8 * count($this->SHPData["parts"][$i]["points"]); |
622
|
|
|
} |
623
|
|
|
break; |
624
|
|
|
case 23: |
625
|
|
|
case 25: |
|
|
|
|
626
|
|
|
$result = 22 + (2 * 4) + 2 * count($this->SHPData["parts"]); |
627
|
|
|
for ($i = 0; $i < count($this->SHPData["parts"]); $i++) { |
|
|
|
|
628
|
|
|
$result += (8 + 4) * count($this->SHPData["parts"][$i]["points"]); |
629
|
|
|
} |
630
|
|
|
break; |
631
|
|
|
case 13: |
632
|
|
|
case 15: |
|
|
|
|
633
|
|
|
$result = 22 + (4 * 4) + 2 * count($this->SHPData["parts"]); |
634
|
|
|
for ($i = 0; $i < count($this->SHPData["parts"]); $i++) { |
|
|
|
|
635
|
|
|
$result += (8 + 8) * count($this->SHPData["parts"][$i]["points"]); |
636
|
|
|
} |
637
|
|
|
break; |
638
|
|
|
case 8: |
639
|
|
|
$result = 20 + 8 * count($this->SHPData["points"]); |
640
|
|
|
break; |
641
|
|
|
case 28: |
|
|
|
|
642
|
|
|
$result = 20 + (2 * 4) + (8 + 4) * count($this->SHPData["points"]); |
643
|
|
|
break; |
644
|
|
|
case 18: |
|
|
|
|
645
|
|
|
$result = 20 + (4 * 4) + (8 + 8) * count($this->SHPData["points"]); |
646
|
|
|
break; |
647
|
|
|
default: |
648
|
|
|
$result = false; |
649
|
|
|
$this->setError(sprintf("The Shape Type '%s' is not supported.", $this->shapeType)); |
650
|
|
|
break; |
651
|
|
|
} |
652
|
|
|
return $result; |
653
|
|
|
} |
654
|
|
|
|
655
|
|
|
private function _loadDBFData() { |
656
|
|
|
$this->DBFData = @dbase_get_record_with_names($this->DBFFile, $this->recordNumber); |
657
|
|
|
unset($this->DBFData["deleted"]); |
658
|
|
|
} |
659
|
|
|
|
660
|
|
|
private function _saveDBFData() { |
661
|
|
|
unset($this->DBFData["deleted"]); |
662
|
|
|
if ($this->recordNumber <= dbase_numrecords($this->DBFFile)) { |
663
|
|
|
if (!dbase_replace_record($this->DBFFile, array_values($this->DBFData), $this->recordNumber)) { |
664
|
|
|
$this->setError("I wasn't possible to update the information in the DBF file."); |
665
|
|
|
} |
666
|
|
|
} else { |
667
|
|
|
if (!dbase_add_record($this->DBFFile, array_values($this->DBFData))) { |
668
|
|
|
$this->setError("I wasn't possible to add the information to the DBF file."); |
669
|
|
|
} |
670
|
|
|
} |
671
|
|
|
} |
672
|
|
|
|
673
|
|
|
/** |
674
|
|
|
* @param string $error |
675
|
|
|
*/ |
676
|
|
|
public function setError($error) { |
677
|
|
|
$this->lastError = $error; |
678
|
|
|
return false; |
679
|
|
|
} |
680
|
|
|
} |
681
|
|
|
|
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:
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: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: