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
|
|
|
var $SHPFile = NULL; |
|
|
|
|
35
|
|
|
var $DBFFile = NULL; |
|
|
|
|
36
|
|
|
|
37
|
|
|
var $recordNumber = NULL; |
|
|
|
|
38
|
|
|
var $shapeType = NULL; |
|
|
|
|
39
|
|
|
|
40
|
|
|
var $lastError = ""; |
|
|
|
|
41
|
|
|
|
42
|
|
|
var $SHPData = array(); |
|
|
|
|
43
|
|
|
var $DBFData = array(); |
|
|
|
|
44
|
|
|
|
45
|
|
|
public function __construct($shapeType) { |
46
|
|
|
$this->shapeType = $shapeType; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
function loadFromFile(&$SHPFile, &$DBFFile) { |
|
|
|
|
50
|
|
|
$this->SHPFile = $SHPFile; |
51
|
|
|
$this->DBFFile = $DBFFile; |
52
|
|
|
$this->_loadHeaders(); |
53
|
|
|
|
54
|
|
|
switch ($this->shapeType) { |
55
|
|
|
case 0: |
56
|
|
|
$this->_loadNullRecord(); |
57
|
|
|
break; |
58
|
|
|
case 1: |
59
|
|
|
$this->_loadPointRecord(); |
60
|
|
|
break; |
61
|
|
|
case 21: |
62
|
|
|
$this->_loadPointMRecord(); |
63
|
|
|
break; |
64
|
|
|
case 11: |
65
|
|
|
$this->_loadPointZRecord(); |
66
|
|
|
break; |
67
|
|
|
case 3: |
68
|
|
|
$this->_loadPolyLineRecord(); |
69
|
|
|
break; |
70
|
|
|
case 23: |
71
|
|
|
$this->_loadPolyLineMRecord(); |
72
|
|
|
break; |
73
|
|
|
case 13: |
74
|
|
|
$this->_loadPolyLineZRecord(); |
75
|
|
|
break; |
76
|
|
|
case 5: |
77
|
|
|
$this->_loadPolygonRecord(); |
78
|
|
|
break; |
79
|
|
|
case 25: |
80
|
|
|
$this->_loadPolygonMRecord(); |
81
|
|
|
break; |
82
|
|
|
case 15: |
83
|
|
|
$this->_loadPolygonZRecord(); |
84
|
|
|
break; |
85
|
|
|
case 8: |
86
|
|
|
$this->_loadMultiPointRecord(); |
87
|
|
|
break; |
88
|
|
|
case 28: |
89
|
|
|
$this->_loadMultiPointMRecord(); |
90
|
|
|
break; |
91
|
|
|
case 18: |
92
|
|
|
$this->_loadMultiPointZRecord(); |
93
|
|
|
break; |
94
|
|
|
default: |
95
|
|
|
$this->setError(sprintf("The Shape Type '%s' is not supported.", $this->shapeType)); |
96
|
|
|
break; |
97
|
|
|
} |
98
|
|
|
$this->_loadDBFData(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
function saveToFile(&$SHPFile, &$DBFFile, $recordNumber) { |
|
|
|
|
102
|
|
|
$this->SHPFile = $SHPFile; |
103
|
|
|
$this->DBFFile = $DBFFile; |
104
|
|
|
$this->recordNumber = $recordNumber; |
105
|
|
|
$this->_saveHeaders(); |
106
|
|
|
|
107
|
|
|
switch ($this->shapeType) { |
108
|
|
|
case 0: |
109
|
|
|
$this->_saveNullRecord(); |
110
|
|
|
break; |
111
|
|
|
case 1: |
112
|
|
|
$this->_savePointRecord(); |
113
|
|
|
break; |
114
|
|
|
case 21: |
115
|
|
|
$this->_savePointMRecord(); |
116
|
|
|
break; |
117
|
|
|
case 11: |
118
|
|
|
$this->_savePointZRecord(); |
119
|
|
|
break; |
120
|
|
|
case 3: |
121
|
|
|
$this->_savePolyLineRecord(); |
122
|
|
|
break; |
123
|
|
|
case 23: |
124
|
|
|
$this->_savePolyLineMRecord(); |
125
|
|
|
break; |
126
|
|
|
case 13: |
127
|
|
|
$this->_savePolyLineZRecord(); |
128
|
|
|
break; |
129
|
|
|
case 5: |
130
|
|
|
$this->_savePolygonRecord(); |
131
|
|
|
break; |
132
|
|
|
case 25: |
133
|
|
|
$this->_savePolygonMRecord(); |
134
|
|
|
break; |
135
|
|
|
case 15: |
136
|
|
|
$this->_savePolygonZRecord(); |
137
|
|
|
break; |
138
|
|
|
case 8: |
139
|
|
|
$this->_saveMultiPointRecord(); |
140
|
|
|
break; |
141
|
|
|
case 28: |
142
|
|
|
$this->_saveMultiPointMRecord(); |
143
|
|
|
break; |
144
|
|
|
case 18: |
145
|
|
|
$this->_saveMultiPointZRecord(); |
146
|
|
|
break; |
147
|
|
|
default: |
148
|
|
|
$this->setError(sprintf("The Shape Type '%s' is not supported.", $this->shapeType)); |
149
|
|
|
break; |
150
|
|
|
} |
151
|
|
|
$this->_saveDBFData(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
function updateDBFInfo($header) { |
|
|
|
|
155
|
|
|
$tmp = $this->DBFData; |
156
|
|
|
unset($this->DBFData); |
157
|
|
|
$this->DBFData = array(); |
158
|
|
|
reset($header); |
159
|
|
|
while (list($key, $value) = each($header)) { |
|
|
|
|
160
|
|
|
$this->DBFData[$value[0]] = (isset($tmp[$value[0]])) ? $tmp[$value[0]] : ""; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
function _loadHeaders() { |
|
|
|
|
165
|
|
|
$this->recordNumber = Util::loadData("N", fread($this->SHPFile, 4)); |
166
|
|
|
$tmp = Util::loadData("N", fread($this->SHPFile, 4)); //We read the length of the record |
|
|
|
|
167
|
|
|
$this->shapeType = Util::loadData("V", fread($this->SHPFile, 4)); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
function _saveHeaders() { |
|
|
|
|
171
|
|
|
fwrite($this->SHPFile, pack("N", $this->recordNumber)); |
172
|
|
|
fwrite($this->SHPFile, pack("N", $this->getContentLength())); |
173
|
|
|
fwrite($this->SHPFile, pack("V", $this->shapeType)); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
function _loadPoint() { |
|
|
|
|
177
|
|
|
$data = array(); |
178
|
|
|
|
179
|
|
|
$data["x"] = Util::loadData("d", fread($this->SHPFile, 8)); |
180
|
|
|
$data["y"] = Util::loadData("d", fread($this->SHPFile, 8)); |
181
|
|
|
|
182
|
|
|
return $data; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
function _loadPointM() { |
|
|
|
|
186
|
|
|
$data = array(); |
187
|
|
|
|
188
|
|
|
$data["x"] = Util::loadData("d", fread($this->SHPFile, 8)); |
189
|
|
|
$data["y"] = Util::loadData("d", fread($this->SHPFile, 8)); |
190
|
|
|
$data["m"] = Util::loadData("d", fread($this->SHPFile, 8)); |
191
|
|
|
|
192
|
|
|
return $data; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
function _loadPointZ() { |
|
|
|
|
196
|
|
|
$data = array(); |
197
|
|
|
|
198
|
|
|
$data["x"] = Util::loadData("d", fread($this->SHPFile, 8)); |
199
|
|
|
$data["y"] = Util::loadData("d", fread($this->SHPFile, 8)); |
200
|
|
|
$data["z"] = Util::loadData("d", fread($this->SHPFile, 8)); |
201
|
|
|
$data["m"] = Util::loadData("d", fread($this->SHPFile, 8)); |
202
|
|
|
|
203
|
|
|
return $data; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
function _savePoint($data) { |
|
|
|
|
207
|
|
|
fwrite($this->SHPFile, Util::packDouble($data["x"])); |
208
|
|
|
fwrite($this->SHPFile, Util::packDouble($data["y"])); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
function _savePointM($data) { |
|
|
|
|
212
|
|
|
fwrite($this->SHPFile, Util::packDouble($data["x"])); |
213
|
|
|
fwrite($this->SHPFile, Util::packDouble($data["y"])); |
214
|
|
|
fwrite($this->SHPFile, Util::packDouble($data["m"])); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
function _savePointZ($data) { |
|
|
|
|
218
|
|
|
fwrite($this->SHPFile, Util::packDouble($data["x"])); |
219
|
|
|
fwrite($this->SHPFile, Util::packDouble($data["y"])); |
220
|
|
|
fwrite($this->SHPFile, Util::packDouble($data["z"])); |
221
|
|
|
fwrite($this->SHPFile, Util::packDouble($data["m"])); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
function _saveMeasure($data) { |
|
|
|
|
225
|
|
|
fwrite($this->SHPFile, Util::packDouble($data["m"])); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
function _saveZCoordinate($data) { |
|
|
|
|
229
|
|
|
fwrite($this->SHPFile, Util::packDouble($data["z"])); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
function _loadNullRecord() { |
|
|
|
|
233
|
|
|
$this->SHPData = array(); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
function _saveNullRecord() { |
|
|
|
|
237
|
|
|
//Don't save anything |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
function _loadPointRecord() { |
|
|
|
|
241
|
|
|
$this->SHPData = $this->_loadPoint(); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
function _loadPointMRecord() { |
|
|
|
|
245
|
|
|
$this->SHPData = $this->_loadPointM(); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
function _loadPointZRecord() { |
|
|
|
|
249
|
|
|
$this->SHPData = $this->_loadPointZ(); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
function _savePointRecord() { |
|
|
|
|
253
|
|
|
$this->_savePoint($this->SHPData); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
function _savePointMRecord() { |
|
|
|
|
257
|
|
|
$this->_savePointM($this->SHPData); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
function _savePointZRecord() { |
|
|
|
|
261
|
|
|
$this->_savePointZ($this->SHPData); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
function _loadMultiPointRecord() { |
|
|
|
|
265
|
|
|
$this->SHPData = array(); |
266
|
|
|
$this->SHPData["xmin"] = Util::loadData("d", fread($this->SHPFile, 8)); |
267
|
|
|
$this->SHPData["ymin"] = Util::loadData("d", fread($this->SHPFile, 8)); |
268
|
|
|
$this->SHPData["xmax"] = Util::loadData("d", fread($this->SHPFile, 8)); |
269
|
|
|
$this->SHPData["ymax"] = Util::loadData("d", fread($this->SHPFile, 8)); |
270
|
|
|
|
271
|
|
|
$this->SHPData["numpoints"] = Util::loadData("V", fread($this->SHPFile, 4)); |
272
|
|
|
|
273
|
|
View Code Duplication |
for ($i = 0; $i <= $this->SHPData["numpoints"]; $i++) { |
|
|
|
|
274
|
|
|
$this->SHPData["points"][] = $this->_loadPoint(); |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
function _loadMultiPointMZRecord( $type ) { |
|
|
|
|
279
|
|
|
|
280
|
|
|
$this->SHPData[$type."min"] = Util::loadData("d", fread($this->SHPFile, 8)); |
281
|
|
|
$this->SHPData[$type."max"] = Util::loadData("d", fread($this->SHPFile, 8)); |
282
|
|
|
|
283
|
|
View Code Duplication |
for ($i = 0; $i <= $this->SHPData["numpoints"]; $i++) { |
|
|
|
|
284
|
|
|
$this->SHPData["points"][$i][$type] = Util::loadData("d", fread($this->SHPFile, 8)); |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
function _loadMultiPointMRecord() { |
|
|
|
|
289
|
|
|
$this->_loadMultiPointRecord(); |
290
|
|
|
|
291
|
|
|
$this->_loadMultiPointMZRecord("m"); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
function _loadMultiPointZRecord() { |
|
|
|
|
295
|
|
|
$this->_loadMultiPointRecord(); |
296
|
|
|
|
297
|
|
|
$this->_loadMultiPointMZRecord("z"); |
298
|
|
|
$this->_loadMultiPointMZRecord("m"); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
function _saveMultiPointRecord() { |
|
|
|
|
302
|
|
|
fwrite($this->SHPFile, pack("dddd", $this->SHPData["xmin"], $this->SHPData["ymin"], $this->SHPData["xmax"], $this->SHPData["ymax"])); |
303
|
|
|
|
304
|
|
|
fwrite($this->SHPFile, pack("V", $this->SHPData["numpoints"])); |
305
|
|
|
|
306
|
|
View Code Duplication |
for ($i = 0; $i <= $this->SHPData["numpoints"]; $i++) { |
|
|
|
|
307
|
|
|
$this->_savePoint($this->SHPData["points"][$i]); |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
function _saveMultiPointMZRecord( $type ) { |
|
|
|
|
312
|
|
|
|
313
|
|
|
fwrite($this->SHPFile, pack("dd", $this->SHPData[$type."min"], $this->SHPData[$type."max"])); |
314
|
|
|
|
315
|
|
View Code Duplication |
for ($i = 0; $i <= $this->SHPData["numpoints"]; $i++) { |
|
|
|
|
316
|
|
|
fwrite($this->SHPFile, Util::packDouble($this->SHPData["points"][$type])); |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
function _saveMultiPointMRecord() { |
|
|
|
|
321
|
|
|
$this->_saveMultiPointRecord(); |
322
|
|
|
|
323
|
|
|
$this->_saveMultiPointMZRecord("m"); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
function _saveMultiPointZRecord() { |
|
|
|
|
327
|
|
|
$this->_saveMultiPointRecord(); |
328
|
|
|
|
329
|
|
|
$this->_saveMultiPointMZRecord("z"); |
330
|
|
|
$this->_saveMultiPointMZRecord("m"); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
function _loadPolyLineRecord() { |
|
|
|
|
334
|
|
|
$this->SHPData = array(); |
335
|
|
|
$this->SHPData["xmin"] = Util::loadData("d", fread($this->SHPFile, 8)); |
336
|
|
|
$this->SHPData["ymin"] = Util::loadData("d", fread($this->SHPFile, 8)); |
337
|
|
|
$this->SHPData["xmax"] = Util::loadData("d", fread($this->SHPFile, 8)); |
338
|
|
|
$this->SHPData["ymax"] = Util::loadData("d", fread($this->SHPFile, 8)); |
339
|
|
|
|
340
|
|
|
$this->SHPData["numparts"] = Util::loadData("V", fread($this->SHPFile, 4)); |
341
|
|
|
$this->SHPData["numpoints"] = Util::loadData("V", fread($this->SHPFile, 4)); |
342
|
|
|
|
343
|
|
View Code Duplication |
for ($i = 0; $i < $this->SHPData["numparts"]; $i++) { |
|
|
|
|
344
|
|
|
$this->SHPData["parts"][$i] = Util::loadData("V", fread($this->SHPFile, 4)); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
$firstIndex = ftell($this->SHPFile); |
348
|
|
|
$readPoints = 0; |
349
|
|
|
reset($this->SHPData["parts"]); |
350
|
|
|
while (list($partIndex, $partData) = each($this->SHPData["parts"])) { |
|
|
|
|
351
|
|
|
if (!isset($this->SHPData["parts"][$partIndex]["points"]) || !is_array($this->SHPData["parts"][$partIndex]["points"])) { |
352
|
|
|
$this->SHPData["parts"][$partIndex] = array(); |
353
|
|
|
$this->SHPData["parts"][$partIndex]["points"] = array(); |
354
|
|
|
} |
355
|
|
|
while (!in_array($readPoints, $this->SHPData["parts"]) && ($readPoints < ($this->SHPData["numpoints"])) && !feof($this->SHPFile)) { |
356
|
|
|
$this->SHPData["parts"][$partIndex]["points"][] = $this->_loadPoint(); |
357
|
|
|
$readPoints++; |
358
|
|
|
} |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
fseek($this->SHPFile, $firstIndex + ($readPoints*16)); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
function _loadPolyLineMZRecord( $type ) { |
|
|
|
|
365
|
|
|
|
366
|
|
|
$this->SHPData[$type."min"] = Util::loadData("d", fread($this->SHPFile, 8)); |
367
|
|
|
$this->SHPData[$type."max"] = Util::loadData("d", fread($this->SHPFile, 8)); |
368
|
|
|
|
369
|
|
|
$firstIndex = ftell($this->SHPFile); |
370
|
|
|
$readPoints = 0; |
371
|
|
|
reset($this->SHPData["parts"]); |
372
|
|
|
while (list($partIndex, $partData) = each($this->SHPData["parts"])) { |
|
|
|
|
373
|
|
|
while (!in_array($readPoints, $this->SHPData["parts"]) && ($readPoints < ($this->SHPData["numpoints"])) && !feof($this->SHPFile)) { |
374
|
|
|
$this->SHPData["parts"][$partIndex]["points"][$readPoints][$type] = Util::loadData("d", fread($this->SHPFile, 8)); |
375
|
|
|
$readPoints++; |
376
|
|
|
} |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
fseek($this->SHPFile, $firstIndex + ($readPoints*24)); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
function _loadPolyLineMRecord() { |
|
|
|
|
383
|
|
|
$this->_loadPolyLineRecord(); |
384
|
|
|
|
385
|
|
|
$this->_loadPolyLineMZRecord("m"); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
function _loadPolyLineZRecord() { |
|
|
|
|
389
|
|
|
$this->_loadPolyLineRecord(); |
390
|
|
|
|
391
|
|
|
$this->_loadPolyLineMZRecord("z"); |
392
|
|
|
$this->_loadPolyLineMZRecord("m"); |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
function _savePolyLineRecord() { |
|
|
|
|
396
|
|
|
fwrite($this->SHPFile, pack("dddd", $this->SHPData["xmin"], $this->SHPData["ymin"], $this->SHPData["xmax"], $this->SHPData["ymax"])); |
397
|
|
|
|
398
|
|
|
fwrite($this->SHPFile, pack("VV", $this->SHPData["numparts"], $this->SHPData["numpoints"])); |
399
|
|
|
|
400
|
|
|
for ($i = 0; $i < $this->SHPData["numparts"]; $i++) { |
401
|
|
|
fwrite($this->SHPFile, pack("V", count($this->SHPData["parts"][$i])-1)); |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
foreach ($this->SHPData["parts"] as $partData){ |
405
|
|
|
reset($partData["points"]); |
406
|
|
|
while (list($pointIndex, $pointData) = each($partData["points"])) { |
|
|
|
|
407
|
|
|
$this->_savePoint($pointData); |
408
|
|
|
} |
409
|
|
|
} |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
function _savePolyLineMZRecord( $type ) { |
|
|
|
|
413
|
|
|
fwrite($this->SHPFile, pack("dd", $this->SHPData[$type."min"], $this->SHPData[$type."max"])); |
414
|
|
|
|
415
|
|
|
foreach ($this->SHPData["parts"] as $partData){ |
416
|
|
|
reset($partData["points"]); |
417
|
|
|
while (list($pointIndex, $pointData) = each($partData["points"])) { |
|
|
|
|
418
|
|
|
fwrite($this->SHPFile, Util::packDouble($pointData[$type])); |
419
|
|
|
} |
420
|
|
|
} |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
function _savePolyLineMRecord() { |
|
|
|
|
424
|
|
|
$this->_savePolyLineRecord(); |
425
|
|
|
|
426
|
|
|
$this->_savePolyLineMZRecord("m"); |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
function _savePolyLineZRecord() { |
|
|
|
|
430
|
|
|
$this->_savePolyLineRecord(); |
431
|
|
|
|
432
|
|
|
$this->_savePolyLineMZRecord("z"); |
433
|
|
|
$this->_savePolyLineMZRecord("m"); |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
function _loadPolygonRecord() { |
|
|
|
|
437
|
|
|
$this->_loadPolyLineRecord(); |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
function _loadPolygonMRecord() { |
|
|
|
|
441
|
|
|
$this->_loadPolyLineMRecord(); |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
function _loadPolygonZRecord() { |
|
|
|
|
445
|
|
|
$this->_loadPolyLineZRecord(); |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
function _savePolygonRecord() { |
|
|
|
|
449
|
|
|
$this->_savePolyLineRecord(); |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
function _savePolygonMRecord() { |
|
|
|
|
453
|
|
|
$this->_savePolyLineMRecord(); |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
function _savePolygonZRecord() { |
|
|
|
|
457
|
|
|
$this->_savePolyLineZRecord(); |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
function addPoint($point, $partIndex = 0) { |
|
|
|
|
461
|
|
|
switch ($this->shapeType) { |
462
|
|
|
case 0: |
463
|
|
|
//Don't add anything |
464
|
|
|
break; |
465
|
|
|
case 1: |
466
|
|
|
case 11: |
467
|
|
|
case 21: |
468
|
|
View Code Duplication |
if (in_array($this->shapeType,array(11,21)) && !isset($point["m"])) $point["m"] = 0.0; // no_value |
|
|
|
|
469
|
|
View Code Duplication |
if (in_array($this->shapeType,array(11)) && !isset($point["z"])) $point["z"] = 0.0; // no_value |
|
|
|
|
470
|
|
|
//Substitutes the value of the current point |
471
|
|
|
$this->SHPData = $point; |
472
|
|
|
break; |
473
|
|
|
case 3: |
474
|
|
|
case 5: |
475
|
|
|
case 13: |
476
|
|
|
case 15: |
477
|
|
|
case 23: |
478
|
|
|
case 25: |
479
|
|
View Code Duplication |
if (in_array($this->shapeType,array(13,15,23,25)) && !isset($point["m"])) $point["m"] = 0.0; // no_value |
|
|
|
|
480
|
|
View Code Duplication |
if (in_array($this->shapeType,array(13,15)) && !isset($point["z"])) $point["z"] = 0.0; // no_value |
|
|
|
|
481
|
|
|
|
482
|
|
|
//Adds a new point to the selected part |
483
|
|
View Code Duplication |
if (!isset($this->SHPData["xmin"]) || ($this->SHPData["xmin"] > $point["x"])) $this->SHPData["xmin"] = $point["x"]; |
|
|
|
|
484
|
|
View Code Duplication |
if (!isset($this->SHPData["ymin"]) || ($this->SHPData["ymin"] > $point["y"])) $this->SHPData["ymin"] = $point["y"]; |
|
|
|
|
485
|
|
View Code Duplication |
if (isset($point["m"]) && (!isset($this->SHPData["mmin"]) || ($this->SHPData["mmin"] > $point["m"]))) $this->SHPData["mmin"] = $point["m"]; |
|
|
|
|
486
|
|
View Code Duplication |
if (isset($point["z"]) && (!isset($this->SHPData["zmin"]) || ($this->SHPData["zmin"] > $point["z"]))) $this->SHPData["zmin"] = $point["z"]; |
|
|
|
|
487
|
|
View Code Duplication |
if (!isset($this->SHPData["xmax"]) || ($this->SHPData["xmax"] < $point["x"])) $this->SHPData["xmax"] = $point["x"]; |
|
|
|
|
488
|
|
View Code Duplication |
if (!isset($this->SHPData["ymax"]) || ($this->SHPData["ymax"] < $point["y"])) $this->SHPData["ymax"] = $point["y"]; |
|
|
|
|
489
|
|
View Code Duplication |
if (isset($point["m"]) && (!isset($this->SHPData["mmax"]) || ($this->SHPData["mmax"] < $point["m"]))) $this->SHPData["mmax"] = $point["m"]; |
|
|
|
|
490
|
|
View Code Duplication |
if (isset($point["z"]) && (!isset($this->SHPData["zmax"]) || ($this->SHPData["zmax"] < $point["z"]))) $this->SHPData["zmax"] = $point["z"]; |
|
|
|
|
491
|
|
|
|
492
|
|
|
$this->SHPData["parts"][$partIndex]["points"][] = $point; |
493
|
|
|
|
494
|
|
|
$this->SHPData["numparts"] = count($this->SHPData["parts"]); |
495
|
|
|
$this->SHPData["numpoints"] = 1 + (isset($this->SHPData["numpoints"])?$this->SHPData["numpoints"]:0); |
496
|
|
|
break; |
497
|
|
|
case 8: |
498
|
|
|
case 18: |
499
|
|
|
case 28: |
500
|
|
View Code Duplication |
if (in_array($this->shapeType,array(18,28)) && !isset($point["m"])) $point["m"] = 0.0; // no_value |
|
|
|
|
501
|
|
View Code Duplication |
if (in_array($this->shapeType,array(18)) && !isset($point["z"])) $point["z"] = 0.0; // no_value |
|
|
|
|
502
|
|
|
|
503
|
|
|
//Adds a new point |
504
|
|
View Code Duplication |
if (!isset($this->SHPData["xmin"]) || ($this->SHPData["xmin"] > $point["x"])) $this->SHPData["xmin"] = $point["x"]; |
|
|
|
|
505
|
|
View Code Duplication |
if (!isset($this->SHPData["ymin"]) || ($this->SHPData["ymin"] > $point["y"])) $this->SHPData["ymin"] = $point["y"]; |
|
|
|
|
506
|
|
View Code Duplication |
if (isset($point["m"]) && (!isset($this->SHPData["mmin"]) || ($this->SHPData["mmin"] > $point["m"]))) $this->SHPData["mmin"] = $point["m"]; |
|
|
|
|
507
|
|
View Code Duplication |
if (isset($point["z"]) && (!isset($this->SHPData["zmin"]) || ($this->SHPData["zmin"] > $point["z"]))) $this->SHPData["zmin"] = $point["z"]; |
|
|
|
|
508
|
|
View Code Duplication |
if (!isset($this->SHPData["xmax"]) || ($this->SHPData["xmax"] < $point["x"])) $this->SHPData["xmax"] = $point["x"]; |
|
|
|
|
509
|
|
View Code Duplication |
if (!isset($this->SHPData["ymax"]) || ($this->SHPData["ymax"] < $point["y"])) $this->SHPData["ymax"] = $point["y"]; |
|
|
|
|
510
|
|
View Code Duplication |
if (isset($point["m"]) && (!isset($this->SHPData["mmax"]) || ($this->SHPData["mmax"] < $point["m"]))) $this->SHPData["mmax"] = $point["m"]; |
|
|
|
|
511
|
|
View Code Duplication |
if (isset($point["z"]) && (!isset($this->SHPData["zmax"]) || ($this->SHPData["zmax"] < $point["z"]))) $this->SHPData["zmax"] = $point["z"]; |
|
|
|
|
512
|
|
|
|
513
|
|
|
$this->SHPData["points"][] = $point; |
514
|
|
|
$this->SHPData["numpoints"] = 1 + (isset($this->SHPData["numpoints"])?$this->SHPData["numpoints"]:0); |
515
|
|
|
break; |
516
|
|
|
default: |
517
|
|
|
$this->setError(sprintf("The Shape Type '%s' is not supported.", $this->shapeType)); |
518
|
|
|
break; |
519
|
|
|
} |
520
|
|
|
} |
521
|
|
|
|
522
|
|
|
function deletePoint($pointIndex = 0, $partIndex = 0) { |
|
|
|
|
523
|
|
|
switch ($this->shapeType) { |
524
|
|
|
case 0: |
525
|
|
|
//Don't delete anything |
526
|
|
|
break; |
527
|
|
|
case 1: |
528
|
|
|
case 11: |
529
|
|
|
case 21: |
530
|
|
|
//Sets the value of the point to zero |
531
|
|
|
$this->SHPData["x"] = 0.0; |
532
|
|
|
$this->SHPData["y"] = 0.0; |
533
|
|
|
if (in_array($this->shapeType,array(11,21))) $this->SHPData["m"] = 0.0; |
534
|
|
|
if (in_array($this->shapeType,array(11))) $this->SHPData["z"] = 0.0; |
535
|
|
|
break; |
536
|
|
|
case 3: |
537
|
|
|
case 5: |
538
|
|
|
case 13: |
539
|
|
|
case 15: |
540
|
|
|
case 23: |
541
|
|
|
case 25: |
542
|
|
|
//Deletes the point from the selected part, if exists |
543
|
|
|
if (isset($this->SHPData["parts"][$partIndex]) && isset($this->SHPData["parts"][$partIndex]["points"][$pointIndex])) { |
544
|
|
|
for ($i = $pointIndex; $i < (count($this->SHPData["parts"][$partIndex]["points"]) - 1); $i++) { |
545
|
|
|
$this->SHPData["parts"][$partIndex]["points"][$i] = $this->SHPData["parts"][$partIndex]["points"][$i + 1]; |
546
|
|
|
} |
547
|
|
|
unset($this->SHPData["parts"][$partIndex]["points"][count($this->SHPData["parts"][$partIndex]["points"]) - 1]); |
548
|
|
|
|
549
|
|
|
$this->SHPData["numparts"] = count($this->SHPData["parts"]); |
550
|
|
|
$this->SHPData["numpoints"]--; |
551
|
|
|
} |
552
|
|
|
break; |
553
|
|
|
case 8: |
554
|
|
|
case 18: |
555
|
|
|
case 28: |
556
|
|
|
//Deletes the point, if exists |
557
|
|
|
if (isset($this->SHPData["points"][$pointIndex])) { |
558
|
|
|
for ($i = $pointIndex; $i < (count($this->SHPData["points"]) - 1); $i++) { |
559
|
|
|
$this->SHPData["points"][$i] = $this->SHPData["points"][$i + 1]; |
560
|
|
|
} |
561
|
|
|
unset($this->SHPData["points"][count($this->SHPData["points"]) - 1]); |
562
|
|
|
|
563
|
|
|
$this->SHPData["numpoints"]--; |
564
|
|
|
} |
565
|
|
|
break; |
566
|
|
|
default: |
567
|
|
|
$this->setError(sprintf("The Shape Type '%s' is not supported.", $this->shapeType)); |
568
|
|
|
break; |
569
|
|
|
} |
570
|
|
|
} |
571
|
|
|
|
572
|
|
|
function getContentLength() { |
|
|
|
|
573
|
|
|
// The content length for a record is the length of the record contents section measured in 16-bit words. |
574
|
|
|
// one coordinate makes 4 16-bit words (64 bit double) |
575
|
|
|
switch ($this->shapeType) { |
576
|
|
|
case 0: |
577
|
|
|
$result = 0; |
578
|
|
|
break; |
579
|
|
|
case 1: |
580
|
|
|
$result = 10; |
581
|
|
|
break; |
582
|
|
|
case 21: |
583
|
|
|
$result = 10 + 4; |
584
|
|
|
break; |
585
|
|
|
case 11: |
586
|
|
|
$result = 10 + 8; |
587
|
|
|
break; |
588
|
|
|
case 3: |
589
|
|
|
case 5: |
590
|
|
|
$result = 22 + 2*count($this->SHPData["parts"]); |
591
|
|
|
for ($i = 0; $i < count($this->SHPData["parts"]); $i++) { |
|
|
|
|
592
|
|
|
$result += 8*count($this->SHPData["parts"][$i]["points"]); |
593
|
|
|
} |
594
|
|
|
break; |
595
|
|
|
case 23: |
596
|
|
View Code Duplication |
case 25: |
|
|
|
|
597
|
|
|
$result = 22 + (2*4) + 2*count($this->SHPData["parts"]); |
598
|
|
|
for ($i = 0; $i < count($this->SHPData["parts"]); $i++) { |
|
|
|
|
599
|
|
|
$result += (8+4)*count($this->SHPData["parts"][$i]["points"]); |
600
|
|
|
} |
601
|
|
|
break; |
602
|
|
|
case 13: |
603
|
|
View Code Duplication |
case 15: |
|
|
|
|
604
|
|
|
$result = 22 + (4*4) + 2*count($this->SHPData["parts"]); |
605
|
|
|
for ($i = 0; $i < count($this->SHPData["parts"]); $i++) { |
|
|
|
|
606
|
|
|
$result += (8+8)*count($this->SHPData["parts"][$i]["points"]); |
607
|
|
|
} |
608
|
|
|
break; |
609
|
|
|
case 8: |
610
|
|
|
$result = 20 + 8*count($this->SHPData["points"]); |
611
|
|
|
break; |
612
|
|
View Code Duplication |
case 28: |
|
|
|
|
613
|
|
|
$result = 20 + (2*4) + (8+4)*count($this->SHPData["points"]); |
614
|
|
|
break; |
615
|
|
View Code Duplication |
case 18: |
|
|
|
|
616
|
|
|
$result = 20 + (4*4) + (8+8)*count($this->SHPData["points"]); |
617
|
|
|
break; |
618
|
|
|
default: |
619
|
|
|
$result = false; |
620
|
|
|
$this->setError(sprintf("The Shape Type '%s' is not supported.", $this->shapeType)); |
621
|
|
|
break; |
622
|
|
|
} |
623
|
|
|
return $result; |
624
|
|
|
} |
625
|
|
|
|
626
|
|
|
function _loadDBFData() { |
|
|
|
|
627
|
|
|
$this->DBFData = @dbase_get_record_with_names($this->DBFFile, $this->recordNumber); |
628
|
|
|
unset($this->DBFData["deleted"]); |
629
|
|
|
} |
630
|
|
|
|
631
|
|
|
function _saveDBFData() { |
|
|
|
|
632
|
|
|
unset($this->DBFData["deleted"]); |
633
|
|
|
if ($this->recordNumber <= dbase_numrecords($this->DBFFile)) { |
634
|
|
|
if (!dbase_replace_record($this->DBFFile, array_values($this->DBFData), $this->recordNumber)) { |
635
|
|
|
$this->setError("I wasn't possible to update the information in the DBF file."); |
636
|
|
|
} |
637
|
|
|
} else { |
638
|
|
|
if (!dbase_add_record($this->DBFFile, array_values($this->DBFData))) { |
639
|
|
|
$this->setError("I wasn't possible to add the information to the DBF file."); |
640
|
|
|
} |
641
|
|
|
} |
642
|
|
|
} |
643
|
|
|
|
644
|
|
|
function setError($error) { |
|
|
|
|
645
|
|
|
$this->lastError = $error; |
646
|
|
|
return false; |
647
|
|
|
} |
648
|
|
|
} |
649
|
|
|
|
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.