|
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
|
|
|
function loadData($type, $data) { |
|
32
|
|
|
if (!$data) return $data; |
|
33
|
|
|
$tmp = unpack($type, $data); |
|
34
|
|
|
return current($tmp); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
function swap($binValue) { |
|
38
|
|
|
$result = $binValue{strlen($binValue) - 1}; |
|
39
|
|
|
for($i = strlen($binValue) - 2; $i >= 0 ; $i--) { |
|
40
|
|
|
$result .= $binValue{$i}; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $result; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
function packDouble($value, $mode = 'LE') { |
|
47
|
|
|
$value = (double)$value; |
|
48
|
|
|
$bin = pack("d", $value); |
|
49
|
|
|
|
|
50
|
|
|
//We test if the conversion of an integer (1) is done as LE or BE by default |
|
51
|
|
|
switch (pack ('L', 1)) { |
|
52
|
|
|
case pack ('V', 1): //Little Endian |
|
53
|
|
|
$result = ($mode == 'LE') ? $bin : swap($bin); |
|
54
|
|
|
break; |
|
55
|
|
|
case pack ('N', 1): //Big Endian |
|
56
|
|
|
$result = ($mode == 'BE') ? $bin : swap($bin); |
|
57
|
|
|
break; |
|
58
|
|
|
default: //Some other thing, we just return false |
|
59
|
|
|
$result = FALSE; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $result; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* ShapeFile class |
|
67
|
|
|
* |
|
68
|
|
|
* @package bfShapeFiles |
|
69
|
|
|
*/ |
|
70
|
|
|
class ShapeFile { |
|
|
|
|
|
|
71
|
|
|
var $FileName; |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
var $SHPFile; |
|
|
|
|
|
|
74
|
|
|
var $SHXFile; |
|
|
|
|
|
|
75
|
|
|
var $DBFFile; |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
var $DBFHeader; |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
var $lastError = ""; |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
var $boundingBox = array("xmin" => 0.0, "ymin" => 0.0, "xmax" => 0.0, "ymax" => 0.0); |
|
|
|
|
|
|
82
|
|
|
var $fileLength = 0; |
|
|
|
|
|
|
83
|
|
|
var $shapeType = 0; |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
var $records; |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
public function __construct($shapeType, $boundingBox = array("xmin" => 0.0, "ymin" => 0.0, "xmax" => 0.0, "ymax" => 0.0), $FileName = NULL) { |
|
88
|
|
|
$this->shapeType = $shapeType; |
|
89
|
|
|
$this->boundingBox = $boundingBox; |
|
90
|
|
|
$this->FileName = $FileName; |
|
91
|
|
|
$this->fileLength = 50; // The value for file length is the total length of the file in 16-bit words (including the fifty 16-bit words that make up the header). |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
function loadFromFile($FileName) { |
|
|
|
|
|
|
95
|
|
|
$this->FileName = $FileName; |
|
96
|
|
|
|
|
97
|
|
|
if (($this->_openSHPFile()) && ($this->_openDBFFile())) { |
|
98
|
|
|
$this->_loadHeaders(); |
|
99
|
|
|
$this->_loadRecords(); |
|
100
|
|
|
$this->_closeSHPFile(); |
|
101
|
|
|
$this->_closeDBFFile(); |
|
102
|
|
|
} else { |
|
103
|
|
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
function saveToFile($FileName = NULL) { |
|
|
|
|
|
|
108
|
|
|
if ($FileName != NULL) $this->FileName = $FileName; |
|
109
|
|
|
|
|
110
|
|
|
if (($this->_openSHPFile(TRUE)) && ($this->_openSHXFile(TRUE)) && ($this->_openDBFFile(TRUE))) { |
|
111
|
|
|
$this->_saveHeaders(); |
|
112
|
|
|
$this->_saveRecords(); |
|
113
|
|
|
$this->_closeSHPFile(); |
|
114
|
|
|
$this->_closeSHXFile(); |
|
115
|
|
|
$this->_closeDBFFile(); |
|
116
|
|
|
} else { |
|
117
|
|
|
return false; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
function addRecord($record) { |
|
|
|
|
|
|
122
|
|
|
if ((isset($this->DBFHeader)) && (is_array($this->DBFHeader))) { |
|
123
|
|
|
$record->updateDBFInfo($this->DBFHeader); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$this->fileLength += ($record->getContentLength() + 4); |
|
127
|
|
|
$this->records[] = $record; |
|
128
|
|
|
$this->records[count($this->records) - 1]->recordNumber = count($this->records); |
|
129
|
|
|
|
|
130
|
|
|
if ($this->boundingBox["xmin"]==0.0 || ($this->boundingBox["xmin"]>$record->SHPData["xmin"])) $this->boundingBox["xmin"] = $record->SHPData["xmin"]; |
|
131
|
|
|
if ($this->boundingBox["xmax"]==0.0 || ($this->boundingBox["xmax"]<$record->SHPData["xmax"])) $this->boundingBox["xmax"] = $record->SHPData["xmax"]; |
|
132
|
|
|
|
|
133
|
|
|
if ($this->boundingBox["ymin"]==0.0 || ($this->boundingBox["ymin"]>$record->SHPData["ymin"])) $this->boundingBox["ymin"] = $record->SHPData["ymin"]; |
|
134
|
|
|
if ($this->boundingBox["ymax"]==0.0 || ($this->boundingBox["ymax"]<$record->SHPData["ymax"])) $this->boundingBox["ymax"] = $record->SHPData["ymax"]; |
|
135
|
|
|
|
|
136
|
|
|
if (in_array($this->shapeType,array(11,13,15,18,21,23,25,28))) { |
|
137
|
|
View Code Duplication |
if (!isset($this->boundingBox["mmin"]) || $this->boundingBox["mmin"]==0.0 || ($this->boundingBox["mmin"]>$record->SHPData["mmin"])) $this->boundingBox["mmin"] = $record->SHPData["mmin"]; |
|
|
|
|
|
|
138
|
|
View Code Duplication |
if (!isset($this->boundingBox["mmax"]) || $this->boundingBox["mmax"]==0.0 || ($this->boundingBox["mmax"]<$record->SHPData["mmax"])) $this->boundingBox["mmax"] = $record->SHPData["mmax"]; |
|
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
if (in_array($this->shapeType,array(11,13,15,18))) { |
|
142
|
|
View Code Duplication |
if (!isset($this->boundingBox["zmin"]) || $this->boundingBox["zmin"]==0.0 || ($this->boundingBox["zmin"]>$record->SHPData["zmin"])) $this->boundingBox["zmin"] = $record->SHPData["zmin"]; |
|
|
|
|
|
|
143
|
|
View Code Duplication |
if (!isset($this->boundingBox["zmax"]) || $this->boundingBox["zmax"]==0.0 || ($this->boundingBox["zmax"]<$record->SHPData["zmax"])) $this->boundingBox["zmax"] = $record->SHPData["zmax"]; |
|
|
|
|
|
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return (count($this->records) - 1); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
function deleteRecord($index) { |
|
|
|
|
|
|
150
|
|
|
if (isset($this->records[$index])) { |
|
151
|
|
|
$this->fileLength -= ($this->records[$index]->getContentLength() + 4); |
|
152
|
|
|
for ($i = $index; $i < (count($this->records) - 1); $i++) { |
|
153
|
|
|
$this->records[$i] = $this->records[$i + 1]; |
|
154
|
|
|
} |
|
155
|
|
|
unset($this->records[count($this->records) - 1]); |
|
156
|
|
|
$this->_deleteRecordFromDBF($index); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
function getDBFHeader() { |
|
|
|
|
|
|
161
|
|
|
return $this->DBFHeader; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
function setDBFHeader($header) { |
|
|
|
|
|
|
165
|
|
|
$this->DBFHeader = $header; |
|
166
|
|
|
|
|
167
|
|
|
for ($i = 0; $i < count($this->records); $i++) { |
|
|
|
|
|
|
168
|
|
|
$this->records[$i]->updateDBFInfo($header); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
function getIndexFromDBFData($field, $value) { |
|
|
|
|
|
|
173
|
|
|
$result = -1; |
|
174
|
|
|
for ($i = 0; $i < (count($this->records) - 1); $i++) { |
|
175
|
|
|
if (isset($this->records[$i]->DBFData[$field]) && (strtoupper($this->records[$i]->DBFData[$field]) == strtoupper($value))) { |
|
176
|
|
|
$result = $i; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
return $result; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
function _loadDBFHeader() { |
|
|
|
|
|
|
184
|
|
|
$DBFFile = fopen(str_replace('.*', '.dbf', $this->FileName), 'r'); |
|
185
|
|
|
|
|
186
|
|
|
$result = array(); |
|
187
|
|
|
$buff32 = array(); |
|
|
|
|
|
|
188
|
|
|
$i = 1; |
|
189
|
|
|
$inHeader = true; |
|
190
|
|
|
|
|
191
|
|
|
while ($inHeader) { |
|
192
|
|
|
if (!feof($DBFFile)) { |
|
193
|
|
|
$buff32 = fread($DBFFile, 32); |
|
194
|
|
|
if ($i > 1) { |
|
195
|
|
|
if (substr($buff32, 0, 1) == chr(13)) { |
|
196
|
|
|
$inHeader = false; |
|
197
|
|
|
} else { |
|
198
|
|
|
$pos = strpos(substr($buff32, 0, 10), chr(0)); |
|
199
|
|
|
$pos = ($pos == 0 ? 10 : $pos); |
|
200
|
|
|
|
|
201
|
|
|
$fieldName = substr($buff32, 0, $pos); |
|
202
|
|
|
$fieldType = substr($buff32, 11, 1); |
|
203
|
|
|
$fieldLen = ord(substr($buff32, 16, 1)); |
|
204
|
|
|
$fieldDec = ord(substr($buff32, 17, 1)); |
|
205
|
|
|
|
|
206
|
|
|
array_push($result, array($fieldName, $fieldType, $fieldLen, $fieldDec)); |
|
207
|
|
|
} |
|
208
|
|
|
} |
|
209
|
|
|
$i++; |
|
210
|
|
|
} else { |
|
211
|
|
|
$inHeader = false; |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
fclose($DBFFile); |
|
216
|
|
|
return($result); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
function _deleteRecordFromDBF($index) { |
|
|
|
|
|
|
220
|
|
|
if (@dbase_delete_record($this->DBFFile, $index)) { |
|
221
|
|
|
@dbase_pack($this->DBFFile); |
|
|
|
|
|
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
function _loadHeaders() { |
|
|
|
|
|
|
226
|
|
|
fseek($this->SHPFile, 24, SEEK_SET); |
|
227
|
|
|
$this->fileLength = loadData("N", fread($this->SHPFile, 4)); |
|
228
|
|
|
|
|
229
|
|
|
fseek($this->SHPFile, 32, SEEK_SET); |
|
230
|
|
|
$this->shapeType = loadData("V", fread($this->SHPFile, 4)); |
|
231
|
|
|
|
|
232
|
|
|
$this->boundingBox = array(); |
|
233
|
|
|
$this->boundingBox["xmin"] = loadData("d", fread($this->SHPFile, 8)); |
|
234
|
|
|
$this->boundingBox["ymin"] = loadData("d", fread($this->SHPFile, 8)); |
|
235
|
|
|
$this->boundingBox["xmax"] = loadData("d", fread($this->SHPFile, 8)); |
|
236
|
|
|
$this->boundingBox["ymax"] = loadData("d", fread($this->SHPFile, 8)); |
|
237
|
|
|
$this->boundingBox["zmin"] = loadData("d", fread($this->SHPFile, 8)); |
|
238
|
|
|
$this->boundingBox["zmax"] = loadData("d", fread($this->SHPFile, 8)); |
|
239
|
|
|
$this->boundingBox["mmin"] = loadData("d", fread($this->SHPFile, 8)); |
|
240
|
|
|
$this->boundingBox["mmax"] = loadData("d", fread($this->SHPFile, 8)); |
|
241
|
|
|
|
|
242
|
|
|
$this->DBFHeader = $this->_loadDBFHeader(); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
function _saveHeaders() { |
|
|
|
|
|
|
246
|
|
|
fwrite($this->SHPFile, pack("NNNNNN", 9994, 0, 0, 0, 0, 0)); |
|
247
|
|
|
fwrite($this->SHPFile, pack("N", $this->fileLength)); |
|
248
|
|
|
fwrite($this->SHPFile, pack("V", 1000)); |
|
249
|
|
|
fwrite($this->SHPFile, pack("V", $this->shapeType)); |
|
250
|
|
|
fwrite($this->SHPFile, packDouble($this->boundingBox['xmin'])); |
|
251
|
|
|
fwrite($this->SHPFile, packDouble($this->boundingBox['ymin'])); |
|
252
|
|
|
fwrite($this->SHPFile, packDouble($this->boundingBox['xmax'])); |
|
253
|
|
|
fwrite($this->SHPFile, packDouble($this->boundingBox['ymax'])); |
|
254
|
|
|
fwrite($this->SHPFile, packDouble(isset($this->boundingBox['zmin'])?$this->boundingBox['zmin']:0)); |
|
255
|
|
|
fwrite($this->SHPFile, packDouble(isset($this->boundingBox['zmax'])?$this->boundingBox['zmax']:0)); |
|
256
|
|
|
fwrite($this->SHPFile, packDouble(isset($this->boundingBox['mmin'])?$this->boundingBox['mmin']:0)); |
|
257
|
|
|
fwrite($this->SHPFile, packDouble(isset($this->boundingBox['mmax'])?$this->boundingBox['mmax']:0)); |
|
258
|
|
|
|
|
259
|
|
|
fwrite($this->SHXFile, pack("NNNNNN", 9994, 0, 0, 0, 0, 0)); |
|
260
|
|
|
fwrite($this->SHXFile, pack("N", 50 + 4*count($this->records))); |
|
261
|
|
|
fwrite($this->SHXFile, pack("V", 1000)); |
|
262
|
|
|
fwrite($this->SHXFile, pack("V", $this->shapeType)); |
|
263
|
|
|
fwrite($this->SHXFile, packDouble($this->boundingBox['xmin'])); |
|
264
|
|
|
fwrite($this->SHXFile, packDouble($this->boundingBox['ymin'])); |
|
265
|
|
|
fwrite($this->SHXFile, packDouble($this->boundingBox['xmax'])); |
|
266
|
|
|
fwrite($this->SHXFile, packDouble($this->boundingBox['ymax'])); |
|
267
|
|
|
fwrite($this->SHXFile, packDouble(isset($this->boundingBox['zmin'])?$this->boundingBox['zmin']:0)); |
|
268
|
|
|
fwrite($this->SHXFile, packDouble(isset($this->boundingBox['zmax'])?$this->boundingBox['zmax']:0)); |
|
269
|
|
|
fwrite($this->SHXFile, packDouble(isset($this->boundingBox['mmin'])?$this->boundingBox['mmin']:0)); |
|
270
|
|
|
fwrite($this->SHXFile, packDouble(isset($this->boundingBox['mmax'])?$this->boundingBox['mmax']:0)); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
function _loadRecords() { |
|
|
|
|
|
|
274
|
|
|
fseek($this->SHPFile, 100); |
|
275
|
|
|
while (!feof($this->SHPFile)) { |
|
276
|
|
|
$bByte = ftell($this->SHPFile); |
|
277
|
|
|
$record = new ShapeRecord(-1); |
|
278
|
|
|
$record->loadFromFile($this->SHPFile, $this->DBFFile); |
|
279
|
|
|
$eByte = ftell($this->SHPFile); |
|
280
|
|
|
if (($eByte <= $bByte) || ($record->lastError != "")) { |
|
|
|
|
|
|
281
|
|
|
return false; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
$this->records[] = $record; |
|
285
|
|
|
} |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
function _saveRecords() { |
|
|
|
|
|
|
289
|
|
|
if (file_exists(str_replace('.*', '.dbf', $this->FileName))) { |
|
290
|
|
|
@unlink(str_replace('.*', '.dbf', $this->FileName)); |
|
|
|
|
|
|
291
|
|
|
} |
|
292
|
|
View Code Duplication |
if (!($this->DBFFile = @dbase_create(str_replace('.*', '.dbf', $this->FileName), $this->DBFHeader))) { |
|
|
|
|
|
|
293
|
|
|
return $this->setError(sprintf("It wasn't possible to create the DBase file '%s'", str_replace('.*', '.dbf', $this->FileName))); |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
$offset = 50; |
|
297
|
|
|
if (is_array($this->records) && (count($this->records) > 0)) { |
|
298
|
|
|
reset($this->records); |
|
299
|
|
|
while (list($index, $record) = each($this->records)) { |
|
300
|
|
|
//Save the record to the .shp file |
|
301
|
|
|
$record->saveToFile($this->SHPFile, $this->DBFFile, $index + 1); |
|
302
|
|
|
|
|
303
|
|
|
//Save the record to the .shx file |
|
304
|
|
|
fwrite($this->SHXFile, pack("N", $offset)); |
|
305
|
|
|
fwrite($this->SHXFile, pack("N", $record->getContentLength())); |
|
306
|
|
|
$offset += (4 + $record->getContentLength()); |
|
307
|
|
|
} |
|
308
|
|
|
} |
|
309
|
|
|
@dbase_pack($this->DBFFile); |
|
|
|
|
|
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
View Code Duplication |
function _openSHPFile($toWrite = false) { |
|
|
|
|
|
|
313
|
|
|
$this->SHPFile = @fopen(str_replace('.*', '.shp', $this->FileName), ($toWrite ? "wb+" : "rb")); |
|
314
|
|
|
if (!$this->SHPFile) { |
|
315
|
|
|
return $this->setError(sprintf("It wasn't possible to open the Shape file '%s'", str_replace('.*', '.shp', $this->FileName))); |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
return TRUE; |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
function _closeSHPFile() { |
|
|
|
|
|
|
322
|
|
|
if ($this->SHPFile) { |
|
323
|
|
|
fclose($this->SHPFile); |
|
324
|
|
|
$this->SHPFile = NULL; |
|
325
|
|
|
} |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
View Code Duplication |
function _openSHXFile($toWrite = false) { |
|
|
|
|
|
|
329
|
|
|
$this->SHXFile = @fopen(str_replace('.*', '.shx', $this->FileName), ($toWrite ? "wb+" : "rb")); |
|
330
|
|
|
if (!$this->SHXFile) { |
|
331
|
|
|
return $this->setError(sprintf("It wasn't possible to open the Index file '%s'", str_replace('.*', '.shx', $this->FileName))); |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
return TRUE; |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
function _closeSHXFile() { |
|
|
|
|
|
|
338
|
|
|
if ($this->SHXFile) { |
|
339
|
|
|
fclose($this->SHXFile); |
|
340
|
|
|
$this->SHXFile = NULL; |
|
341
|
|
|
} |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
function _openDBFFile($toWrite = false) { |
|
|
|
|
|
|
345
|
|
|
$checkFunction = $toWrite ? "is_writable" : "is_readable"; |
|
346
|
|
|
if (($toWrite) && (!file_exists(str_replace('.*', '.dbf', $this->FileName)))) { |
|
347
|
|
View Code Duplication |
if (!@dbase_create(str_replace('.*', '.dbf', $this->FileName), $this->DBFHeader)) { |
|
|
|
|
|
|
348
|
|
|
return $this->setError(sprintf("It wasn't possible to create the DBase file '%s'", str_replace('.*', '.dbf', $this->FileName))); |
|
349
|
|
|
} |
|
350
|
|
|
} |
|
351
|
|
|
if ($checkFunction(str_replace('.*', '.dbf', $this->FileName))) { |
|
352
|
|
|
$this->DBFFile = dbase_open(str_replace('.*', '.dbf', $this->FileName), ($toWrite ? 2 : 0)); |
|
353
|
|
|
if (!$this->DBFFile) { |
|
354
|
|
|
return $this->setError(sprintf("It wasn't possible to open the DBase file '%s'", str_replace('.*', '.dbf', $this->FileName))); |
|
355
|
|
|
} |
|
356
|
|
|
} else { |
|
357
|
|
|
return $this->setError(sprintf("It wasn't possible to find the DBase file '%s'", str_replace('.*', '.dbf', $this->FileName))); |
|
358
|
|
|
} |
|
359
|
|
|
return TRUE; |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
function _closeDBFFile() { |
|
|
|
|
|
|
363
|
|
|
if ($this->DBFFile) { |
|
364
|
|
|
dbase_close($this->DBFFile); |
|
365
|
|
|
$this->DBFFile = NULL; |
|
366
|
|
|
} |
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
function setError($error) { |
|
|
|
|
|
|
370
|
|
|
$this->lastError = $error; |
|
371
|
|
|
return false; |
|
372
|
|
|
} |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
class ShapeRecord { |
|
|
|
|
|
|
376
|
|
|
var $SHPFile = NULL; |
|
|
|
|
|
|
377
|
|
|
var $DBFFile = NULL; |
|
|
|
|
|
|
378
|
|
|
|
|
379
|
|
|
var $recordNumber = NULL; |
|
|
|
|
|
|
380
|
|
|
var $shapeType = NULL; |
|
|
|
|
|
|
381
|
|
|
|
|
382
|
|
|
var $lastError = ""; |
|
|
|
|
|
|
383
|
|
|
|
|
384
|
|
|
var $SHPData = array(); |
|
|
|
|
|
|
385
|
|
|
var $DBFData = array(); |
|
|
|
|
|
|
386
|
|
|
|
|
387
|
|
|
public function __construct($shapeType) { |
|
388
|
|
|
$this->shapeType = $shapeType; |
|
389
|
|
|
} |
|
390
|
|
|
|
|
391
|
|
|
function loadFromFile(&$SHPFile, &$DBFFile) { |
|
|
|
|
|
|
392
|
|
|
$this->SHPFile = $SHPFile; |
|
393
|
|
|
$this->DBFFile = $DBFFile; |
|
394
|
|
|
$this->_loadHeaders(); |
|
395
|
|
|
|
|
396
|
|
|
switch ($this->shapeType) { |
|
397
|
|
|
case 0: |
|
398
|
|
|
$this->_loadNullRecord(); |
|
399
|
|
|
break; |
|
400
|
|
|
case 1: |
|
401
|
|
|
$this->_loadPointRecord(); |
|
402
|
|
|
break; |
|
403
|
|
|
case 21: |
|
404
|
|
|
$this->_loadPointMRecord(); |
|
405
|
|
|
break; |
|
406
|
|
|
case 11: |
|
407
|
|
|
$this->_loadPointZRecord(); |
|
408
|
|
|
break; |
|
409
|
|
|
case 3: |
|
410
|
|
|
$this->_loadPolyLineRecord(); |
|
411
|
|
|
break; |
|
412
|
|
|
case 23: |
|
413
|
|
|
$this->_loadPolyLineMRecord(); |
|
414
|
|
|
break; |
|
415
|
|
|
case 13: |
|
416
|
|
|
$this->_loadPolyLineZRecord(); |
|
417
|
|
|
break; |
|
418
|
|
|
case 5: |
|
419
|
|
|
$this->_loadPolygonRecord(); |
|
420
|
|
|
break; |
|
421
|
|
|
case 25: |
|
422
|
|
|
$this->_loadPolygonMRecord(); |
|
423
|
|
|
break; |
|
424
|
|
|
case 15: |
|
425
|
|
|
$this->_loadPolygonZRecord(); |
|
426
|
|
|
break; |
|
427
|
|
|
case 8: |
|
428
|
|
|
$this->_loadMultiPointRecord(); |
|
429
|
|
|
break; |
|
430
|
|
|
case 28: |
|
431
|
|
|
$this->_loadMultiPointMRecord(); |
|
432
|
|
|
break; |
|
433
|
|
|
case 18: |
|
434
|
|
|
$this->_loadMultiPointZRecord(); |
|
435
|
|
|
break; |
|
436
|
|
|
default: |
|
437
|
|
|
$this->setError(sprintf("The Shape Type '%s' is not supported.", $this->shapeType)); |
|
438
|
|
|
break; |
|
439
|
|
|
} |
|
440
|
|
|
$this->_loadDBFData(); |
|
441
|
|
|
} |
|
442
|
|
|
|
|
443
|
|
|
function saveToFile(&$SHPFile, &$DBFFile, $recordNumber) { |
|
|
|
|
|
|
444
|
|
|
$this->SHPFile = $SHPFile; |
|
445
|
|
|
$this->DBFFile = $DBFFile; |
|
446
|
|
|
$this->recordNumber = $recordNumber; |
|
447
|
|
|
$this->_saveHeaders(); |
|
448
|
|
|
|
|
449
|
|
|
switch ($this->shapeType) { |
|
450
|
|
|
case 0: |
|
451
|
|
|
$this->_saveNullRecord(); |
|
452
|
|
|
break; |
|
453
|
|
|
case 1: |
|
454
|
|
|
$this->_savePointRecord(); |
|
455
|
|
|
break; |
|
456
|
|
|
case 21: |
|
457
|
|
|
$this->_savePointMRecord(); |
|
458
|
|
|
break; |
|
459
|
|
|
case 11: |
|
460
|
|
|
$this->_savePointZRecord(); |
|
461
|
|
|
break; |
|
462
|
|
|
case 3: |
|
463
|
|
|
$this->_savePolyLineRecord(); |
|
464
|
|
|
break; |
|
465
|
|
|
case 23: |
|
466
|
|
|
$this->_savePolyLineMRecord(); |
|
467
|
|
|
break; |
|
468
|
|
|
case 13: |
|
469
|
|
|
$this->_savePolyLineZRecord(); |
|
470
|
|
|
break; |
|
471
|
|
|
case 5: |
|
472
|
|
|
$this->_savePolygonRecord(); |
|
473
|
|
|
break; |
|
474
|
|
|
case 25: |
|
475
|
|
|
$this->_savePolygonMRecord(); |
|
476
|
|
|
break; |
|
477
|
|
|
case 15: |
|
478
|
|
|
$this->_savePolygonZRecord(); |
|
479
|
|
|
break; |
|
480
|
|
|
case 8: |
|
481
|
|
|
$this->_saveMultiPointRecord(); |
|
482
|
|
|
break; |
|
483
|
|
|
case 28: |
|
484
|
|
|
$this->_saveMultiPointMRecord(); |
|
485
|
|
|
break; |
|
486
|
|
|
case 18: |
|
487
|
|
|
$this->_saveMultiPointZRecord(); |
|
488
|
|
|
break; |
|
489
|
|
|
default: |
|
490
|
|
|
$this->setError(sprintf("The Shape Type '%s' is not supported.", $this->shapeType)); |
|
491
|
|
|
break; |
|
492
|
|
|
} |
|
493
|
|
|
$this->_saveDBFData(); |
|
494
|
|
|
} |
|
495
|
|
|
|
|
496
|
|
|
function updateDBFInfo($header) { |
|
|
|
|
|
|
497
|
|
|
$tmp = $this->DBFData; |
|
498
|
|
|
unset($this->DBFData); |
|
499
|
|
|
$this->DBFData = array(); |
|
500
|
|
|
reset($header); |
|
501
|
|
|
while (list($key, $value) = each($header)) { |
|
|
|
|
|
|
502
|
|
|
$this->DBFData[$value[0]] = (isset($tmp[$value[0]])) ? $tmp[$value[0]] : ""; |
|
503
|
|
|
} |
|
504
|
|
|
} |
|
505
|
|
|
|
|
506
|
|
|
function _loadHeaders() { |
|
|
|
|
|
|
507
|
|
|
$this->recordNumber = loadData("N", fread($this->SHPFile, 4)); |
|
508
|
|
|
$tmp = loadData("N", fread($this->SHPFile, 4)); //We read the length of the record |
|
|
|
|
|
|
509
|
|
|
$this->shapeType = loadData("V", fread($this->SHPFile, 4)); |
|
510
|
|
|
} |
|
511
|
|
|
|
|
512
|
|
|
function _saveHeaders() { |
|
|
|
|
|
|
513
|
|
|
fwrite($this->SHPFile, pack("N", $this->recordNumber)); |
|
514
|
|
|
fwrite($this->SHPFile, pack("N", $this->getContentLength())); |
|
515
|
|
|
fwrite($this->SHPFile, pack("V", $this->shapeType)); |
|
516
|
|
|
} |
|
517
|
|
|
|
|
518
|
|
|
function _loadPoint() { |
|
|
|
|
|
|
519
|
|
|
$data = array(); |
|
520
|
|
|
|
|
521
|
|
|
$data["x"] = loadData("d", fread($this->SHPFile, 8)); |
|
522
|
|
|
$data["y"] = loadData("d", fread($this->SHPFile, 8)); |
|
523
|
|
|
|
|
524
|
|
|
return $data; |
|
525
|
|
|
} |
|
526
|
|
|
|
|
527
|
|
|
function _loadPointM() { |
|
|
|
|
|
|
528
|
|
|
$data = array(); |
|
529
|
|
|
|
|
530
|
|
|
$data["x"] = loadData("d", fread($this->SHPFile, 8)); |
|
531
|
|
|
$data["y"] = loadData("d", fread($this->SHPFile, 8)); |
|
532
|
|
|
$data["m"] = loadData("d", fread($this->SHPFile, 8)); |
|
533
|
|
|
|
|
534
|
|
|
return $data; |
|
535
|
|
|
} |
|
536
|
|
|
|
|
537
|
|
|
function _loadPointZ() { |
|
|
|
|
|
|
538
|
|
|
$data = array(); |
|
539
|
|
|
|
|
540
|
|
|
$data["x"] = loadData("d", fread($this->SHPFile, 8)); |
|
541
|
|
|
$data["y"] = loadData("d", fread($this->SHPFile, 8)); |
|
542
|
|
|
$data["z"] = loadData("d", fread($this->SHPFile, 8)); |
|
543
|
|
|
$data["m"] = loadData("d", fread($this->SHPFile, 8)); |
|
544
|
|
|
|
|
545
|
|
|
return $data; |
|
546
|
|
|
} |
|
547
|
|
|
|
|
548
|
|
|
function _savePoint($data) { |
|
|
|
|
|
|
549
|
|
|
fwrite($this->SHPFile, packDouble($data["x"])); |
|
550
|
|
|
fwrite($this->SHPFile, packDouble($data["y"])); |
|
551
|
|
|
} |
|
552
|
|
|
|
|
553
|
|
|
function _savePointM($data) { |
|
|
|
|
|
|
554
|
|
|
fwrite($this->SHPFile, packDouble($data["x"])); |
|
555
|
|
|
fwrite($this->SHPFile, packDouble($data["y"])); |
|
556
|
|
|
fwrite($this->SHPFile, packDouble($data["m"])); |
|
557
|
|
|
} |
|
558
|
|
|
|
|
559
|
|
|
function _savePointZ($data) { |
|
|
|
|
|
|
560
|
|
|
fwrite($this->SHPFile, packDouble($data["x"])); |
|
561
|
|
|
fwrite($this->SHPFile, packDouble($data["y"])); |
|
562
|
|
|
fwrite($this->SHPFile, packDouble($data["z"])); |
|
563
|
|
|
fwrite($this->SHPFile, packDouble($data["m"])); |
|
564
|
|
|
} |
|
565
|
|
|
|
|
566
|
|
|
function _saveMeasure($data) { |
|
|
|
|
|
|
567
|
|
|
fwrite($this->SHPFile, packDouble($data["m"])); |
|
568
|
|
|
} |
|
569
|
|
|
|
|
570
|
|
|
function _saveZCoordinate($data) { |
|
|
|
|
|
|
571
|
|
|
fwrite($this->SHPFile, packDouble($data["z"])); |
|
572
|
|
|
} |
|
573
|
|
|
|
|
574
|
|
|
function _loadNullRecord() { |
|
|
|
|
|
|
575
|
|
|
$this->SHPData = array(); |
|
576
|
|
|
} |
|
577
|
|
|
|
|
578
|
|
|
function _saveNullRecord() { |
|
|
|
|
|
|
579
|
|
|
//Don't save anything |
|
580
|
|
|
} |
|
581
|
|
|
|
|
582
|
|
|
function _loadPointRecord() { |
|
|
|
|
|
|
583
|
|
|
$this->SHPData = $this->_loadPoint(); |
|
584
|
|
|
} |
|
585
|
|
|
|
|
586
|
|
|
function _loadPointMRecord() { |
|
|
|
|
|
|
587
|
|
|
$this->SHPData = $this->_loadPointM(); |
|
588
|
|
|
} |
|
589
|
|
|
|
|
590
|
|
|
function _loadPointZRecord() { |
|
|
|
|
|
|
591
|
|
|
$this->SHPData = $this->_loadPointZ(); |
|
592
|
|
|
} |
|
593
|
|
|
|
|
594
|
|
|
function _savePointRecord() { |
|
|
|
|
|
|
595
|
|
|
$this->_savePoint($this->SHPData); |
|
596
|
|
|
} |
|
597
|
|
|
|
|
598
|
|
|
function _savePointMRecord() { |
|
|
|
|
|
|
599
|
|
|
$this->_savePointM($this->SHPData); |
|
600
|
|
|
} |
|
601
|
|
|
|
|
602
|
|
|
function _savePointZRecord() { |
|
|
|
|
|
|
603
|
|
|
$this->_savePointZ($this->SHPData); |
|
604
|
|
|
} |
|
605
|
|
|
|
|
606
|
|
|
function _loadMultiPointRecord() { |
|
|
|
|
|
|
607
|
|
|
$this->SHPData = array(); |
|
608
|
|
|
$this->SHPData["xmin"] = loadData("d", fread($this->SHPFile, 8)); |
|
609
|
|
|
$this->SHPData["ymin"] = loadData("d", fread($this->SHPFile, 8)); |
|
610
|
|
|
$this->SHPData["xmax"] = loadData("d", fread($this->SHPFile, 8)); |
|
611
|
|
|
$this->SHPData["ymax"] = loadData("d", fread($this->SHPFile, 8)); |
|
612
|
|
|
|
|
613
|
|
|
$this->SHPData["numpoints"] = loadData("V", fread($this->SHPFile, 4)); |
|
614
|
|
|
|
|
615
|
|
View Code Duplication |
for ($i = 0; $i <= $this->SHPData["numpoints"]; $i++) { |
|
|
|
|
|
|
616
|
|
|
$this->SHPData["points"][] = $this->_loadPoint(); |
|
617
|
|
|
} |
|
618
|
|
|
} |
|
619
|
|
|
|
|
620
|
|
|
function _loadMultiPointMZRecord( $type ) { |
|
|
|
|
|
|
621
|
|
|
|
|
622
|
|
|
$this->SHPData[$type."min"] = loadData("d", fread($this->SHPFile, 8)); |
|
623
|
|
|
$this->SHPData[$type."max"] = loadData("d", fread($this->SHPFile, 8)); |
|
624
|
|
|
|
|
625
|
|
View Code Duplication |
for ($i = 0; $i <= $this->SHPData["numpoints"]; $i++) { |
|
|
|
|
|
|
626
|
|
|
$this->SHPData["points"][$i][$type] = loadData("d", fread($this->SHPFile, 8)); |
|
627
|
|
|
} |
|
628
|
|
|
} |
|
629
|
|
|
|
|
630
|
|
|
function _loadMultiPointMRecord() { |
|
|
|
|
|
|
631
|
|
|
$this->_loadMultiPointRecord(); |
|
632
|
|
|
|
|
633
|
|
|
$this->_loadMultiPointMZRecord("m"); |
|
634
|
|
|
} |
|
635
|
|
|
|
|
636
|
|
|
function _loadMultiPointZRecord() { |
|
|
|
|
|
|
637
|
|
|
$this->_loadMultiPointRecord(); |
|
638
|
|
|
|
|
639
|
|
|
$this->_loadMultiPointMZRecord("z"); |
|
640
|
|
|
$this->_loadMultiPointMZRecord("m"); |
|
641
|
|
|
} |
|
642
|
|
|
|
|
643
|
|
|
function _saveMultiPointRecord() { |
|
|
|
|
|
|
644
|
|
|
fwrite($this->SHPFile, pack("dddd", $this->SHPData["xmin"], $this->SHPData["ymin"], $this->SHPData["xmax"], $this->SHPData["ymax"])); |
|
645
|
|
|
|
|
646
|
|
|
fwrite($this->SHPFile, pack("V", $this->SHPData["numpoints"])); |
|
647
|
|
|
|
|
648
|
|
View Code Duplication |
for ($i = 0; $i <= $this->SHPData["numpoints"]; $i++) { |
|
|
|
|
|
|
649
|
|
|
$this->_savePoint($this->SHPData["points"][$i]); |
|
650
|
|
|
} |
|
651
|
|
|
} |
|
652
|
|
|
|
|
653
|
|
|
function _saveMultiPointMZRecord( $type ) { |
|
|
|
|
|
|
654
|
|
|
|
|
655
|
|
|
fwrite($this->SHPFile, pack("dd", $this->SHPData[$type."min"], $this->SHPData[$type."max"])); |
|
656
|
|
|
|
|
657
|
|
View Code Duplication |
for ($i = 0; $i <= $this->SHPData["numpoints"]; $i++) { |
|
|
|
|
|
|
658
|
|
|
fwrite($this->SHPFile, packDouble($this->SHPData["points"][$type])); |
|
659
|
|
|
} |
|
660
|
|
|
} |
|
661
|
|
|
|
|
662
|
|
|
function _saveMultiPointMRecord() { |
|
|
|
|
|
|
663
|
|
|
$this->_saveMultiPointRecord(); |
|
664
|
|
|
|
|
665
|
|
|
$this->_saveMultiPointMZRecord("m"); |
|
666
|
|
|
} |
|
667
|
|
|
|
|
668
|
|
|
function _saveMultiPointZRecord() { |
|
|
|
|
|
|
669
|
|
|
$this->_saveMultiPointRecord(); |
|
670
|
|
|
|
|
671
|
|
|
$this->_saveMultiPointMZRecord("z"); |
|
672
|
|
|
$this->_saveMultiPointMZRecord("m"); |
|
673
|
|
|
} |
|
674
|
|
|
|
|
675
|
|
|
function _loadPolyLineRecord() { |
|
|
|
|
|
|
676
|
|
|
$this->SHPData = array(); |
|
677
|
|
|
$this->SHPData["xmin"] = loadData("d", fread($this->SHPFile, 8)); |
|
678
|
|
|
$this->SHPData["ymin"] = loadData("d", fread($this->SHPFile, 8)); |
|
679
|
|
|
$this->SHPData["xmax"] = loadData("d", fread($this->SHPFile, 8)); |
|
680
|
|
|
$this->SHPData["ymax"] = loadData("d", fread($this->SHPFile, 8)); |
|
681
|
|
|
|
|
682
|
|
|
$this->SHPData["numparts"] = loadData("V", fread($this->SHPFile, 4)); |
|
683
|
|
|
$this->SHPData["numpoints"] = loadData("V", fread($this->SHPFile, 4)); |
|
684
|
|
|
|
|
685
|
|
View Code Duplication |
for ($i = 0; $i < $this->SHPData["numparts"]; $i++) { |
|
|
|
|
|
|
686
|
|
|
$this->SHPData["parts"][$i] = loadData("V", fread($this->SHPFile, 4)); |
|
687
|
|
|
} |
|
688
|
|
|
|
|
689
|
|
|
$firstIndex = ftell($this->SHPFile); |
|
690
|
|
|
$readPoints = 0; |
|
691
|
|
|
reset($this->SHPData["parts"]); |
|
692
|
|
|
while (list($partIndex, $partData) = each($this->SHPData["parts"])) { |
|
|
|
|
|
|
693
|
|
|
if (!isset($this->SHPData["parts"][$partIndex]["points"]) || !is_array($this->SHPData["parts"][$partIndex]["points"])) { |
|
694
|
|
|
$this->SHPData["parts"][$partIndex] = array(); |
|
695
|
|
|
$this->SHPData["parts"][$partIndex]["points"] = array(); |
|
696
|
|
|
} |
|
697
|
|
|
while (!in_array($readPoints, $this->SHPData["parts"]) && ($readPoints < ($this->SHPData["numpoints"])) && !feof($this->SHPFile)) { |
|
698
|
|
|
$this->SHPData["parts"][$partIndex]["points"][] = $this->_loadPoint(); |
|
699
|
|
|
$readPoints++; |
|
700
|
|
|
} |
|
701
|
|
|
} |
|
702
|
|
|
|
|
703
|
|
|
fseek($this->SHPFile, $firstIndex + ($readPoints*16)); |
|
704
|
|
|
} |
|
705
|
|
|
|
|
706
|
|
|
function _loadPolyLineMZRecord( $type ) { |
|
|
|
|
|
|
707
|
|
|
|
|
708
|
|
|
$this->SHPData[$type."min"] = loadData("d", fread($this->SHPFile, 8)); |
|
709
|
|
|
$this->SHPData[$type."max"] = loadData("d", fread($this->SHPFile, 8)); |
|
710
|
|
|
|
|
711
|
|
|
$firstIndex = ftell($this->SHPFile); |
|
712
|
|
|
$readPoints = 0; |
|
713
|
|
|
reset($this->SHPData["parts"]); |
|
714
|
|
|
while (list($partIndex, $partData) = each($this->SHPData["parts"])) { |
|
|
|
|
|
|
715
|
|
|
while (!in_array($readPoints, $this->SHPData["parts"]) && ($readPoints < ($this->SHPData["numpoints"])) && !feof($this->SHPFile)) { |
|
716
|
|
|
$this->SHPData["parts"][$partIndex]["points"][$readPoints][$type] = loadData("d", fread($this->SHPFile, 8)); |
|
717
|
|
|
$readPoints++; |
|
718
|
|
|
} |
|
719
|
|
|
} |
|
720
|
|
|
|
|
721
|
|
|
fseek($this->SHPFile, $firstIndex + ($readPoints*24)); |
|
722
|
|
|
} |
|
723
|
|
|
|
|
724
|
|
|
function _loadPolyLineMRecord() { |
|
|
|
|
|
|
725
|
|
|
$this->_loadPolyLineRecord(); |
|
726
|
|
|
|
|
727
|
|
|
$this->_loadPolyLineMZRecord("m"); |
|
728
|
|
|
} |
|
729
|
|
|
|
|
730
|
|
|
function _loadPolyLineZRecord() { |
|
|
|
|
|
|
731
|
|
|
$this->_loadPolyLineRecord(); |
|
732
|
|
|
|
|
733
|
|
|
$this->_loadPolyLineMZRecord("z"); |
|
734
|
|
|
$this->_loadPolyLineMZRecord("m"); |
|
735
|
|
|
} |
|
736
|
|
|
|
|
737
|
|
|
function _savePolyLineRecord() { |
|
|
|
|
|
|
738
|
|
|
fwrite($this->SHPFile, pack("dddd", $this->SHPData["xmin"], $this->SHPData["ymin"], $this->SHPData["xmax"], $this->SHPData["ymax"])); |
|
739
|
|
|
|
|
740
|
|
|
fwrite($this->SHPFile, pack("VV", $this->SHPData["numparts"], $this->SHPData["numpoints"])); |
|
741
|
|
|
|
|
742
|
|
View Code Duplication |
for ($i = 0; $i < $this->SHPData["numparts"]; $i++) { |
|
|
|
|
|
|
743
|
|
|
fwrite($this->SHPFile, pack("V", count($this->SHPData["parts"][$i])-1)); |
|
744
|
|
|
} |
|
745
|
|
|
|
|
746
|
|
|
foreach ($this->SHPData["parts"] as $partData){ |
|
747
|
|
|
reset($partData["points"]); |
|
748
|
|
|
while (list($pointIndex, $pointData) = each($partData["points"])) { |
|
|
|
|
|
|
749
|
|
|
$this->_savePoint($pointData); |
|
750
|
|
|
} |
|
751
|
|
|
} |
|
752
|
|
|
} |
|
753
|
|
|
|
|
754
|
|
|
function _savePolyLineMZRecord( $type ) { |
|
|
|
|
|
|
755
|
|
|
fwrite($this->SHPFile, pack("dd", $this->SHPData[$type."min"], $this->SHPData[$type."max"])); |
|
756
|
|
|
|
|
757
|
|
|
foreach ($this->SHPData["parts"] as $partData){ |
|
758
|
|
|
reset($partData["points"]); |
|
759
|
|
|
while (list($pointIndex, $pointData) = each($partData["points"])) { |
|
|
|
|
|
|
760
|
|
|
fwrite($this->SHPFile, packDouble($pointData[$type])); |
|
761
|
|
|
} |
|
762
|
|
|
} |
|
763
|
|
|
} |
|
764
|
|
|
|
|
765
|
|
|
function _savePolyLineMRecord() { |
|
|
|
|
|
|
766
|
|
|
$this->_savePolyLineRecord(); |
|
767
|
|
|
|
|
768
|
|
|
$this->_savePolyLineMZRecord("m"); |
|
769
|
|
|
} |
|
770
|
|
|
|
|
771
|
|
|
function _savePolyLineZRecord() { |
|
|
|
|
|
|
772
|
|
|
$this->_savePolyLineRecord(); |
|
773
|
|
|
|
|
774
|
|
|
$this->_savePolyLineMZRecord("z"); |
|
775
|
|
|
$this->_savePolyLineMZRecord("m"); |
|
776
|
|
|
} |
|
777
|
|
|
|
|
778
|
|
|
function _loadPolygonRecord() { |
|
|
|
|
|
|
779
|
|
|
$this->_loadPolyLineRecord(); |
|
780
|
|
|
} |
|
781
|
|
|
|
|
782
|
|
|
function _loadPolygonMRecord() { |
|
|
|
|
|
|
783
|
|
|
$this->_loadPolyLineMRecord(); |
|
784
|
|
|
} |
|
785
|
|
|
|
|
786
|
|
|
function _loadPolygonZRecord() { |
|
|
|
|
|
|
787
|
|
|
$this->_loadPolyLineZRecord(); |
|
788
|
|
|
} |
|
789
|
|
|
|
|
790
|
|
|
function _savePolygonRecord() { |
|
|
|
|
|
|
791
|
|
|
$this->_savePolyLineRecord(); |
|
792
|
|
|
} |
|
793
|
|
|
|
|
794
|
|
|
function _savePolygonMRecord() { |
|
|
|
|
|
|
795
|
|
|
$this->_savePolyLineMRecord(); |
|
796
|
|
|
} |
|
797
|
|
|
|
|
798
|
|
|
function _savePolygonZRecord() { |
|
|
|
|
|
|
799
|
|
|
$this->_savePolyLineZRecord(); |
|
800
|
|
|
} |
|
801
|
|
|
|
|
802
|
|
|
function addPoint($point, $partIndex = 0) { |
|
|
|
|
|
|
803
|
|
|
switch ($this->shapeType) { |
|
804
|
|
|
case 0: |
|
805
|
|
|
//Don't add anything |
|
806
|
|
|
break; |
|
807
|
|
|
case 1: |
|
808
|
|
|
case 11: |
|
809
|
|
|
case 21: |
|
810
|
|
View Code Duplication |
if (in_array($this->shapeType,array(11,21)) && !isset($point["m"])) $point["m"] = 0.0; // no_value |
|
|
|
|
|
|
811
|
|
View Code Duplication |
if (in_array($this->shapeType,array(11)) && !isset($point["z"])) $point["z"] = 0.0; // no_value |
|
|
|
|
|
|
812
|
|
|
//Substitutes the value of the current point |
|
813
|
|
|
$this->SHPData = $point; |
|
814
|
|
|
break; |
|
815
|
|
|
case 3: |
|
816
|
|
|
case 5: |
|
817
|
|
|
case 13: |
|
818
|
|
|
case 15: |
|
819
|
|
|
case 23: |
|
820
|
|
|
case 25: |
|
821
|
|
View Code Duplication |
if (in_array($this->shapeType,array(13,15,23,25)) && !isset($point["m"])) $point["m"] = 0.0; // no_value |
|
|
|
|
|
|
822
|
|
View Code Duplication |
if (in_array($this->shapeType,array(13,15)) && !isset($point["z"])) $point["z"] = 0.0; // no_value |
|
|
|
|
|
|
823
|
|
|
|
|
824
|
|
|
//Adds a new point to the selected part |
|
825
|
|
View Code Duplication |
if (!isset($this->SHPData["xmin"]) || ($this->SHPData["xmin"] > $point["x"])) $this->SHPData["xmin"] = $point["x"]; |
|
|
|
|
|
|
826
|
|
View Code Duplication |
if (!isset($this->SHPData["ymin"]) || ($this->SHPData["ymin"] > $point["y"])) $this->SHPData["ymin"] = $point["y"]; |
|
|
|
|
|
|
827
|
|
View Code Duplication |
if (isset($point["m"]) && (!isset($this->SHPData["mmin"]) || ($this->SHPData["mmin"] > $point["m"]))) $this->SHPData["mmin"] = $point["m"]; |
|
|
|
|
|
|
828
|
|
View Code Duplication |
if (isset($point["z"]) && (!isset($this->SHPData["zmin"]) || ($this->SHPData["zmin"] > $point["z"]))) $this->SHPData["zmin"] = $point["z"]; |
|
|
|
|
|
|
829
|
|
View Code Duplication |
if (!isset($this->SHPData["xmax"]) || ($this->SHPData["xmax"] < $point["x"])) $this->SHPData["xmax"] = $point["x"]; |
|
|
|
|
|
|
830
|
|
View Code Duplication |
if (!isset($this->SHPData["ymax"]) || ($this->SHPData["ymax"] < $point["y"])) $this->SHPData["ymax"] = $point["y"]; |
|
|
|
|
|
|
831
|
|
View Code Duplication |
if (isset($point["m"]) && (!isset($this->SHPData["mmax"]) || ($this->SHPData["mmax"] < $point["m"]))) $this->SHPData["mmax"] = $point["m"]; |
|
|
|
|
|
|
832
|
|
View Code Duplication |
if (isset($point["z"]) && (!isset($this->SHPData["zmax"]) || ($this->SHPData["zmax"] < $point["z"]))) $this->SHPData["zmax"] = $point["z"]; |
|
|
|
|
|
|
833
|
|
|
|
|
834
|
|
|
$this->SHPData["parts"][$partIndex]["points"][] = $point; |
|
835
|
|
|
|
|
836
|
|
|
$this->SHPData["numparts"] = count($this->SHPData["parts"]); |
|
837
|
|
|
$this->SHPData["numpoints"] = 1 + (isset($this->SHPData["numpoints"])?$this->SHPData["numpoints"]:0); |
|
838
|
|
|
break; |
|
839
|
|
|
case 8: |
|
840
|
|
|
case 18: |
|
841
|
|
|
case 28: |
|
842
|
|
View Code Duplication |
if (in_array($this->shapeType,array(18,28)) && !isset($point["m"])) $point["m"] = 0.0; // no_value |
|
|
|
|
|
|
843
|
|
View Code Duplication |
if (in_array($this->shapeType,array(18)) && !isset($point["z"])) $point["z"] = 0.0; // no_value |
|
|
|
|
|
|
844
|
|
|
|
|
845
|
|
|
//Adds a new point |
|
846
|
|
View Code Duplication |
if (!isset($this->SHPData["xmin"]) || ($this->SHPData["xmin"] > $point["x"])) $this->SHPData["xmin"] = $point["x"]; |
|
|
|
|
|
|
847
|
|
View Code Duplication |
if (!isset($this->SHPData["ymin"]) || ($this->SHPData["ymin"] > $point["y"])) $this->SHPData["ymin"] = $point["y"]; |
|
|
|
|
|
|
848
|
|
View Code Duplication |
if (isset($point["m"]) && (!isset($this->SHPData["mmin"]) || ($this->SHPData["mmin"] > $point["m"]))) $this->SHPData["mmin"] = $point["m"]; |
|
|
|
|
|
|
849
|
|
View Code Duplication |
if (isset($point["z"]) && (!isset($this->SHPData["zmin"]) || ($this->SHPData["zmin"] > $point["z"]))) $this->SHPData["zmin"] = $point["z"]; |
|
|
|
|
|
|
850
|
|
View Code Duplication |
if (!isset($this->SHPData["xmax"]) || ($this->SHPData["xmax"] < $point["x"])) $this->SHPData["xmax"] = $point["x"]; |
|
|
|
|
|
|
851
|
|
View Code Duplication |
if (!isset($this->SHPData["ymax"]) || ($this->SHPData["ymax"] < $point["y"])) $this->SHPData["ymax"] = $point["y"]; |
|
|
|
|
|
|
852
|
|
View Code Duplication |
if (isset($point["m"]) && (!isset($this->SHPData["mmax"]) || ($this->SHPData["mmax"] < $point["m"]))) $this->SHPData["mmax"] = $point["m"]; |
|
|
|
|
|
|
853
|
|
View Code Duplication |
if (isset($point["z"]) && (!isset($this->SHPData["zmax"]) || ($this->SHPData["zmax"] < $point["z"]))) $this->SHPData["zmax"] = $point["z"]; |
|
|
|
|
|
|
854
|
|
|
|
|
855
|
|
|
$this->SHPData["points"][] = $point; |
|
856
|
|
|
$this->SHPData["numpoints"] = 1 + (isset($this->SHPData["numpoints"])?$this->SHPData["numpoints"]:0); |
|
857
|
|
|
break; |
|
858
|
|
|
default: |
|
859
|
|
|
$this->setError(sprintf("The Shape Type '%s' is not supported.", $this->shapeType)); |
|
860
|
|
|
break; |
|
861
|
|
|
} |
|
862
|
|
|
} |
|
863
|
|
|
|
|
864
|
|
|
function deletePoint($pointIndex = 0, $partIndex = 0) { |
|
|
|
|
|
|
865
|
|
|
switch ($this->shapeType) { |
|
866
|
|
|
case 0: |
|
867
|
|
|
//Don't delete anything |
|
868
|
|
|
break; |
|
869
|
|
|
case 1: |
|
870
|
|
|
case 11: |
|
871
|
|
|
case 21: |
|
872
|
|
|
//Sets the value of the point to zero |
|
873
|
|
|
$this->SHPData["x"] = 0.0; |
|
874
|
|
|
$this->SHPData["y"] = 0.0; |
|
875
|
|
|
if (in_array($this->shapeType,array(11,21))) $this->SHPData["m"] = 0.0; |
|
876
|
|
|
if (in_array($this->shapeType,array(11))) $this->SHPData["z"] = 0.0; |
|
877
|
|
|
break; |
|
878
|
|
|
case 3: |
|
879
|
|
|
case 5: |
|
880
|
|
|
case 13: |
|
881
|
|
|
case 15: |
|
882
|
|
|
case 23: |
|
883
|
|
|
case 25: |
|
884
|
|
|
//Deletes the point from the selected part, if exists |
|
885
|
|
|
if (isset($this->SHPData["parts"][$partIndex]) && isset($this->SHPData["parts"][$partIndex]["points"][$pointIndex])) { |
|
886
|
|
|
for ($i = $pointIndex; $i < (count($this->SHPData["parts"][$partIndex]["points"]) - 1); $i++) { |
|
887
|
|
|
$this->SHPData["parts"][$partIndex]["points"][$i] = $this->SHPData["parts"][$partIndex]["points"][$i + 1]; |
|
888
|
|
|
} |
|
889
|
|
|
unset($this->SHPData["parts"][$partIndex]["points"][count($this->SHPData["parts"][$partIndex]["points"]) - 1]); |
|
890
|
|
|
|
|
891
|
|
|
$this->SHPData["numparts"] = count($this->SHPData["parts"]); |
|
892
|
|
|
$this->SHPData["numpoints"]--; |
|
893
|
|
|
} |
|
894
|
|
|
break; |
|
895
|
|
|
case 8: |
|
896
|
|
|
case 18: |
|
897
|
|
|
case 28: |
|
898
|
|
|
//Deletes the point, if exists |
|
899
|
|
|
if (isset($this->SHPData["points"][$pointIndex])) { |
|
900
|
|
|
for ($i = $pointIndex; $i < (count($this->SHPData["points"]) - 1); $i++) { |
|
901
|
|
|
$this->SHPData["points"][$i] = $this->SHPData["points"][$i + 1]; |
|
902
|
|
|
} |
|
903
|
|
|
unset($this->SHPData["points"][count($this->SHPData["points"]) - 1]); |
|
904
|
|
|
|
|
905
|
|
|
$this->SHPData["numpoints"]--; |
|
906
|
|
|
} |
|
907
|
|
|
break; |
|
908
|
|
|
default: |
|
909
|
|
|
$this->setError(sprintf("The Shape Type '%s' is not supported.", $this->shapeType)); |
|
910
|
|
|
break; |
|
911
|
|
|
} |
|
912
|
|
|
} |
|
913
|
|
|
|
|
914
|
|
|
function getContentLength() { |
|
|
|
|
|
|
915
|
|
|
// The content length for a record is the length of the record contents section measured in 16-bit words. |
|
916
|
|
|
// one coordinate makes 4 16-bit words (64 bit double) |
|
917
|
|
|
switch ($this->shapeType) { |
|
918
|
|
|
case 0: |
|
919
|
|
|
$result = 0; |
|
920
|
|
|
break; |
|
921
|
|
|
case 1: |
|
922
|
|
|
$result = 10; |
|
923
|
|
|
break; |
|
924
|
|
|
case 21: |
|
925
|
|
|
$result = 10 + 4; |
|
926
|
|
|
break; |
|
927
|
|
|
case 11: |
|
928
|
|
|
$result = 10 + 8; |
|
929
|
|
|
break; |
|
930
|
|
|
case 3: |
|
931
|
|
|
case 5: |
|
932
|
|
|
$result = 22 + 2*count($this->SHPData["parts"]); |
|
933
|
|
|
for ($i = 0; $i < count($this->SHPData["parts"]); $i++) { |
|
|
|
|
|
|
934
|
|
|
$result += 8*count($this->SHPData["parts"][$i]["points"]); |
|
935
|
|
|
} |
|
936
|
|
|
break; |
|
937
|
|
|
case 23: |
|
938
|
|
View Code Duplication |
case 25: |
|
|
|
|
|
|
939
|
|
|
$result = 22 + (2*4) + 2*count($this->SHPData["parts"]); |
|
940
|
|
|
for ($i = 0; $i < count($this->SHPData["parts"]); $i++) { |
|
|
|
|
|
|
941
|
|
|
$result += (8+4)*count($this->SHPData["parts"][$i]["points"]); |
|
942
|
|
|
} |
|
943
|
|
|
break; |
|
944
|
|
|
case 13: |
|
945
|
|
View Code Duplication |
case 15: |
|
|
|
|
|
|
946
|
|
|
$result = 22 + (4*4) + 2*count($this->SHPData["parts"]); |
|
947
|
|
|
for ($i = 0; $i < count($this->SHPData["parts"]); $i++) { |
|
|
|
|
|
|
948
|
|
|
$result += (8+8)*count($this->SHPData["parts"][$i]["points"]); |
|
949
|
|
|
} |
|
950
|
|
|
break; |
|
951
|
|
|
case 8: |
|
952
|
|
|
$result = 20 + 8*count($this->SHPData["points"]); |
|
953
|
|
|
break; |
|
954
|
|
View Code Duplication |
case 28: |
|
|
|
|
|
|
955
|
|
|
$result = 20 + (2*4) + (8+4)*count($this->SHPData["points"]); |
|
956
|
|
|
break; |
|
957
|
|
View Code Duplication |
case 18: |
|
|
|
|
|
|
958
|
|
|
$result = 20 + (4*4) + (8+8)*count($this->SHPData["points"]); |
|
959
|
|
|
break; |
|
960
|
|
|
default: |
|
961
|
|
|
$result = false; |
|
962
|
|
|
$this->setError(sprintf("The Shape Type '%s' is not supported.", $this->shapeType)); |
|
963
|
|
|
break; |
|
964
|
|
|
} |
|
965
|
|
|
return $result; |
|
966
|
|
|
} |
|
967
|
|
|
|
|
968
|
|
|
function _loadDBFData() { |
|
|
|
|
|
|
969
|
|
|
$this->DBFData = @dbase_get_record_with_names($this->DBFFile, $this->recordNumber); |
|
970
|
|
|
unset($this->DBFData["deleted"]); |
|
971
|
|
|
} |
|
972
|
|
|
|
|
973
|
|
|
function _saveDBFData() { |
|
|
|
|
|
|
974
|
|
|
unset($this->DBFData["deleted"]); |
|
975
|
|
|
if ($this->recordNumber <= dbase_numrecords($this->DBFFile)) { |
|
976
|
|
|
if (!dbase_replace_record($this->DBFFile, array_values($this->DBFData), $this->recordNumber)) { |
|
977
|
|
|
$this->setError("I wasn't possible to update the information in the DBF file."); |
|
978
|
|
|
} |
|
979
|
|
|
} else { |
|
980
|
|
|
if (!dbase_add_record($this->DBFFile, array_values($this->DBFData))) { |
|
981
|
|
|
$this->setError("I wasn't possible to add the information to the DBF file."); |
|
982
|
|
|
} |
|
983
|
|
|
} |
|
984
|
|
|
} |
|
985
|
|
|
|
|
986
|
|
|
function setError($error) { |
|
|
|
|
|
|
987
|
|
|
$this->lastError = $error; |
|
988
|
|
|
return false; |
|
989
|
|
|
} |
|
990
|
|
|
} |
|
991
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.