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
|
|
|
/** |
34
|
|
|
* ShapeFile class |
35
|
|
|
* |
36
|
|
|
* @package bfShapeFiles |
37
|
|
|
*/ |
38
|
|
|
class ShapeFile { |
39
|
|
|
var $FileName; |
|
|
|
|
40
|
|
|
|
41
|
|
|
var $SHPFile; |
|
|
|
|
42
|
|
|
var $SHXFile; |
|
|
|
|
43
|
|
|
var $DBFFile; |
|
|
|
|
44
|
|
|
|
45
|
|
|
var $DBFHeader; |
|
|
|
|
46
|
|
|
|
47
|
|
|
var $lastError = ""; |
|
|
|
|
48
|
|
|
|
49
|
|
|
var $boundingBox = array("xmin" => 0.0, "ymin" => 0.0, "xmax" => 0.0, "ymax" => 0.0); |
|
|
|
|
50
|
|
|
var $fileLength = 0; |
|
|
|
|
51
|
|
|
var $shapeType = 0; |
|
|
|
|
52
|
|
|
|
53
|
|
|
var $records; |
|
|
|
|
54
|
|
|
|
55
|
|
|
public function __construct($shapeType, $boundingBox = array("xmin" => 0.0, "ymin" => 0.0, "xmax" => 0.0, "ymax" => 0.0), $FileName = NULL) { |
56
|
|
|
$this->shapeType = $shapeType; |
57
|
|
|
$this->boundingBox = $boundingBox; |
58
|
|
|
$this->FileName = $FileName; |
59
|
|
|
$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). |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
function loadFromFile($FileName) { |
|
|
|
|
63
|
|
|
$this->FileName = $FileName; |
64
|
|
|
|
65
|
|
|
if (($this->_openSHPFile()) && ($this->_openDBFFile())) { |
66
|
|
|
$this->_loadHeaders(); |
67
|
|
|
$this->_loadRecords(); |
68
|
|
|
$this->_closeSHPFile(); |
69
|
|
|
$this->_closeDBFFile(); |
70
|
|
|
} else { |
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
function saveToFile($FileName = NULL) { |
|
|
|
|
76
|
|
|
if ($FileName != NULL) $this->FileName = $FileName; |
77
|
|
|
|
78
|
|
|
if (($this->_openSHPFile(TRUE)) && ($this->_openSHXFile(TRUE)) && ($this->_openDBFFile(TRUE))) { |
79
|
|
|
$this->_saveHeaders(); |
80
|
|
|
$this->_saveRecords(); |
81
|
|
|
$this->_closeSHPFile(); |
82
|
|
|
$this->_closeSHXFile(); |
83
|
|
|
$this->_closeDBFFile(); |
84
|
|
|
} else { |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
function addRecord($record) { |
|
|
|
|
90
|
|
|
if ((isset($this->DBFHeader)) && (is_array($this->DBFHeader))) { |
91
|
|
|
$record->updateDBFInfo($this->DBFHeader); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$this->fileLength += ($record->getContentLength() + 4); |
95
|
|
|
$this->records[] = $record; |
96
|
|
|
$this->records[count($this->records) - 1]->recordNumber = count($this->records); |
97
|
|
|
|
98
|
|
|
if ($this->boundingBox["xmin"]==0.0 || ($this->boundingBox["xmin"]>$record->SHPData["xmin"])) $this->boundingBox["xmin"] = $record->SHPData["xmin"]; |
99
|
|
|
if ($this->boundingBox["xmax"]==0.0 || ($this->boundingBox["xmax"]<$record->SHPData["xmax"])) $this->boundingBox["xmax"] = $record->SHPData["xmax"]; |
100
|
|
|
|
101
|
|
|
if ($this->boundingBox["ymin"]==0.0 || ($this->boundingBox["ymin"]>$record->SHPData["ymin"])) $this->boundingBox["ymin"] = $record->SHPData["ymin"]; |
102
|
|
|
if ($this->boundingBox["ymax"]==0.0 || ($this->boundingBox["ymax"]<$record->SHPData["ymax"])) $this->boundingBox["ymax"] = $record->SHPData["ymax"]; |
103
|
|
|
|
104
|
|
|
if (in_array($this->shapeType,array(11,13,15,18,21,23,25,28))) { |
105
|
|
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"]; |
|
|
|
|
106
|
|
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"]; |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (in_array($this->shapeType,array(11,13,15,18))) { |
110
|
|
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"]; |
|
|
|
|
111
|
|
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"]; |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return (count($this->records) - 1); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
function deleteRecord($index) { |
|
|
|
|
118
|
|
|
if (isset($this->records[$index])) { |
119
|
|
|
$this->fileLength -= ($this->records[$index]->getContentLength() + 4); |
120
|
|
|
for ($i = $index; $i < (count($this->records) - 1); $i++) { |
121
|
|
|
$this->records[$i] = $this->records[$i + 1]; |
122
|
|
|
} |
123
|
|
|
unset($this->records[count($this->records) - 1]); |
124
|
|
|
$this->_deleteRecordFromDBF($index); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
function getDBFHeader() { |
|
|
|
|
129
|
|
|
return $this->DBFHeader; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
function setDBFHeader($header) { |
|
|
|
|
133
|
|
|
$this->DBFHeader = $header; |
134
|
|
|
|
135
|
|
|
for ($i = 0; $i < count($this->records); $i++) { |
|
|
|
|
136
|
|
|
$this->records[$i]->updateDBFInfo($header); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
function getIndexFromDBFData($field, $value) { |
|
|
|
|
141
|
|
|
$result = -1; |
142
|
|
|
for ($i = 0; $i < (count($this->records) - 1); $i++) { |
143
|
|
|
if (isset($this->records[$i]->DBFData[$field]) && (strtoupper($this->records[$i]->DBFData[$field]) == strtoupper($value))) { |
144
|
|
|
$result = $i; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $result; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
function _loadDBFHeader() { |
|
|
|
|
152
|
|
|
$DBFFile = fopen(str_replace('.*', '.dbf', $this->FileName), 'r'); |
153
|
|
|
|
154
|
|
|
$result = array(); |
155
|
|
|
$buff32 = array(); |
|
|
|
|
156
|
|
|
$i = 1; |
157
|
|
|
$inHeader = true; |
158
|
|
|
|
159
|
|
|
while ($inHeader) { |
160
|
|
|
if (!feof($DBFFile)) { |
161
|
|
|
$buff32 = fread($DBFFile, 32); |
162
|
|
|
if ($i > 1) { |
163
|
|
|
if (substr($buff32, 0, 1) == chr(13)) { |
164
|
|
|
$inHeader = false; |
165
|
|
|
} else { |
166
|
|
|
$pos = strpos(substr($buff32, 0, 10), chr(0)); |
167
|
|
|
$pos = ($pos == 0 ? 10 : $pos); |
168
|
|
|
|
169
|
|
|
$fieldName = substr($buff32, 0, $pos); |
170
|
|
|
$fieldType = substr($buff32, 11, 1); |
171
|
|
|
$fieldLen = ord(substr($buff32, 16, 1)); |
172
|
|
|
$fieldDec = ord(substr($buff32, 17, 1)); |
173
|
|
|
|
174
|
|
|
array_push($result, array($fieldName, $fieldType, $fieldLen, $fieldDec)); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
$i++; |
178
|
|
|
} else { |
179
|
|
|
$inHeader = false; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
fclose($DBFFile); |
184
|
|
|
return($result); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
function _deleteRecordFromDBF($index) { |
|
|
|
|
188
|
|
|
if (@dbase_delete_record($this->DBFFile, $index)) { |
189
|
|
|
@dbase_pack($this->DBFFile); |
|
|
|
|
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
function _loadHeaders() { |
|
|
|
|
194
|
|
|
fseek($this->SHPFile, 24, SEEK_SET); |
195
|
|
|
$this->fileLength = Util::loadData("N", fread($this->SHPFile, 4)); |
196
|
|
|
|
197
|
|
|
fseek($this->SHPFile, 32, SEEK_SET); |
198
|
|
|
$this->shapeType = Util::loadData("V", fread($this->SHPFile, 4)); |
199
|
|
|
|
200
|
|
|
$this->boundingBox = array(); |
201
|
|
|
$this->boundingBox["xmin"] = Util::loadData("d", fread($this->SHPFile, 8)); |
202
|
|
|
$this->boundingBox["ymin"] = Util::loadData("d", fread($this->SHPFile, 8)); |
203
|
|
|
$this->boundingBox["xmax"] = Util::loadData("d", fread($this->SHPFile, 8)); |
204
|
|
|
$this->boundingBox["ymax"] = Util::loadData("d", fread($this->SHPFile, 8)); |
205
|
|
|
$this->boundingBox["zmin"] = Util::loadData("d", fread($this->SHPFile, 8)); |
206
|
|
|
$this->boundingBox["zmax"] = Util::loadData("d", fread($this->SHPFile, 8)); |
207
|
|
|
$this->boundingBox["mmin"] = Util::loadData("d", fread($this->SHPFile, 8)); |
208
|
|
|
$this->boundingBox["mmax"] = Util::loadData("d", fread($this->SHPFile, 8)); |
209
|
|
|
|
210
|
|
|
$this->DBFHeader = $this->_loadDBFHeader(); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
function _saveHeaders() { |
|
|
|
|
214
|
|
|
fwrite($this->SHPFile, pack("NNNNNN", 9994, 0, 0, 0, 0, 0)); |
215
|
|
|
fwrite($this->SHPFile, pack("N", $this->fileLength)); |
216
|
|
|
fwrite($this->SHPFile, pack("V", 1000)); |
217
|
|
|
fwrite($this->SHPFile, pack("V", $this->shapeType)); |
218
|
|
|
fwrite($this->SHPFile, Util::packDouble($this->boundingBox['xmin'])); |
219
|
|
|
fwrite($this->SHPFile, Util::packDouble($this->boundingBox['ymin'])); |
220
|
|
|
fwrite($this->SHPFile, Util::packDouble($this->boundingBox['xmax'])); |
221
|
|
|
fwrite($this->SHPFile, Util::packDouble($this->boundingBox['ymax'])); |
222
|
|
|
fwrite($this->SHPFile, Util::packDouble(isset($this->boundingBox['zmin'])?$this->boundingBox['zmin']:0)); |
223
|
|
|
fwrite($this->SHPFile, Util::packDouble(isset($this->boundingBox['zmax'])?$this->boundingBox['zmax']:0)); |
224
|
|
|
fwrite($this->SHPFile, Util::packDouble(isset($this->boundingBox['mmin'])?$this->boundingBox['mmin']:0)); |
225
|
|
|
fwrite($this->SHPFile, Util::packDouble(isset($this->boundingBox['mmax'])?$this->boundingBox['mmax']:0)); |
226
|
|
|
|
227
|
|
|
fwrite($this->SHXFile, pack("NNNNNN", 9994, 0, 0, 0, 0, 0)); |
228
|
|
|
fwrite($this->SHXFile, pack("N", 50 + 4*count($this->records))); |
229
|
|
|
fwrite($this->SHXFile, pack("V", 1000)); |
230
|
|
|
fwrite($this->SHXFile, pack("V", $this->shapeType)); |
231
|
|
|
fwrite($this->SHXFile, Util::packDouble($this->boundingBox['xmin'])); |
232
|
|
|
fwrite($this->SHXFile, Util::packDouble($this->boundingBox['ymin'])); |
233
|
|
|
fwrite($this->SHXFile, Util::packDouble($this->boundingBox['xmax'])); |
234
|
|
|
fwrite($this->SHXFile, Util::packDouble($this->boundingBox['ymax'])); |
235
|
|
|
fwrite($this->SHXFile, Util::packDouble(isset($this->boundingBox['zmin'])?$this->boundingBox['zmin']:0)); |
236
|
|
|
fwrite($this->SHXFile, Util::packDouble(isset($this->boundingBox['zmax'])?$this->boundingBox['zmax']:0)); |
237
|
|
|
fwrite($this->SHXFile, Util::packDouble(isset($this->boundingBox['mmin'])?$this->boundingBox['mmin']:0)); |
238
|
|
|
fwrite($this->SHXFile, Util::packDouble(isset($this->boundingBox['mmax'])?$this->boundingBox['mmax']:0)); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
function _loadRecords() { |
|
|
|
|
242
|
|
|
fseek($this->SHPFile, 100); |
243
|
|
|
while (!feof($this->SHPFile)) { |
244
|
|
|
$bByte = ftell($this->SHPFile); |
245
|
|
|
$record = new ShapeRecord(-1); |
246
|
|
|
$record->loadFromFile($this->SHPFile, $this->DBFFile); |
247
|
|
|
$eByte = ftell($this->SHPFile); |
248
|
|
|
if (($eByte <= $bByte) || ($record->lastError != "")) { |
|
|
|
|
249
|
|
|
return false; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
$this->records[] = $record; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
function _saveRecords() { |
|
|
|
|
257
|
|
|
if (file_exists(str_replace('.*', '.dbf', $this->FileName))) { |
258
|
|
|
@unlink(str_replace('.*', '.dbf', $this->FileName)); |
|
|
|
|
259
|
|
|
} |
260
|
|
View Code Duplication |
if (!($this->DBFFile = @dbase_create(str_replace('.*', '.dbf', $this->FileName), $this->DBFHeader))) { |
|
|
|
|
261
|
|
|
return $this->setError(sprintf("It wasn't possible to create the DBase file '%s'", str_replace('.*', '.dbf', $this->FileName))); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
$offset = 50; |
265
|
|
|
if (is_array($this->records) && (count($this->records) > 0)) { |
266
|
|
|
reset($this->records); |
267
|
|
|
while (list($index, $record) = each($this->records)) { |
268
|
|
|
//Save the record to the .shp file |
269
|
|
|
$record->saveToFile($this->SHPFile, $this->DBFFile, $index + 1); |
270
|
|
|
|
271
|
|
|
//Save the record to the .shx file |
272
|
|
|
fwrite($this->SHXFile, pack("N", $offset)); |
273
|
|
|
fwrite($this->SHXFile, pack("N", $record->getContentLength())); |
274
|
|
|
$offset += (4 + $record->getContentLength()); |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
@dbase_pack($this->DBFFile); |
|
|
|
|
278
|
|
|
} |
279
|
|
|
|
280
|
|
View Code Duplication |
function _openSHPFile($toWrite = false) { |
|
|
|
|
281
|
|
|
$this->SHPFile = @fopen(str_replace('.*', '.shp', $this->FileName), ($toWrite ? "wb+" : "rb")); |
282
|
|
|
if (!$this->SHPFile) { |
283
|
|
|
return $this->setError(sprintf("It wasn't possible to open the Shape file '%s'", str_replace('.*', '.shp', $this->FileName))); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
return TRUE; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
function _closeSHPFile() { |
|
|
|
|
290
|
|
|
if ($this->SHPFile) { |
291
|
|
|
fclose($this->SHPFile); |
292
|
|
|
$this->SHPFile = NULL; |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|
296
|
|
View Code Duplication |
function _openSHXFile($toWrite = false) { |
|
|
|
|
297
|
|
|
$this->SHXFile = @fopen(str_replace('.*', '.shx', $this->FileName), ($toWrite ? "wb+" : "rb")); |
298
|
|
|
if (!$this->SHXFile) { |
299
|
|
|
return $this->setError(sprintf("It wasn't possible to open the Index file '%s'", str_replace('.*', '.shx', $this->FileName))); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
return TRUE; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
function _closeSHXFile() { |
|
|
|
|
306
|
|
|
if ($this->SHXFile) { |
307
|
|
|
fclose($this->SHXFile); |
308
|
|
|
$this->SHXFile = NULL; |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
function _openDBFFile($toWrite = false) { |
|
|
|
|
313
|
|
|
$checkFunction = $toWrite ? "is_writable" : "is_readable"; |
314
|
|
|
if (($toWrite) && (!file_exists(str_replace('.*', '.dbf', $this->FileName)))) { |
315
|
|
View Code Duplication |
if (!@dbase_create(str_replace('.*', '.dbf', $this->FileName), $this->DBFHeader)) { |
|
|
|
|
316
|
|
|
return $this->setError(sprintf("It wasn't possible to create the DBase file '%s'", str_replace('.*', '.dbf', $this->FileName))); |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
if ($checkFunction(str_replace('.*', '.dbf', $this->FileName))) { |
320
|
|
|
$this->DBFFile = dbase_open(str_replace('.*', '.dbf', $this->FileName), ($toWrite ? 2 : 0)); |
321
|
|
|
if (!$this->DBFFile) { |
322
|
|
|
return $this->setError(sprintf("It wasn't possible to open the DBase file '%s'", str_replace('.*', '.dbf', $this->FileName))); |
323
|
|
|
} |
324
|
|
|
} else { |
325
|
|
|
return $this->setError(sprintf("It wasn't possible to find the DBase file '%s'", str_replace('.*', '.dbf', $this->FileName))); |
326
|
|
|
} |
327
|
|
|
return TRUE; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
function _closeDBFFile() { |
|
|
|
|
331
|
|
|
if ($this->DBFFile) { |
332
|
|
|
dbase_close($this->DBFFile); |
333
|
|
|
$this->DBFFile = NULL; |
334
|
|
|
} |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
function setError($error) { |
|
|
|
|
338
|
|
|
$this->lastError = $error; |
339
|
|
|
return false; |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
|
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.