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