2 function loadData($type, $data) {
3 if (!$data) return $data;
4 $tmp = unpack($type, $data);
8 function swap($binValue) {
9 $result = $binValue{strlen($binValue) - 1};
10 for($i = strlen($binValue) - 2; $i >= 0 ; $i--) {
11 $result .= $binValue{$i};
17 function packDouble($value, $mode = 'LE') {
18 $value = (double)$value;
19 $bin = pack("d", $value);
21 //We test if the conversion of an integer (1) is done as LE or BE by default
22 switch (pack ('L', 1)) {
23 case pack ('V', 1): //Little Endian
24 $result = ($mode == 'LE') ?
$bin : swap($bin);
26 case pack ('N', 1): //Big Endian
27 $result = ($mode == 'BE') ?
$bin : swap($bin);
29 default: //Some other thing, we just return false
47 var $boundingBox = array("xmin" => 0.0, "ymin" => 0.0, "xmax" => 0.0, "ymax" => 0.0);
53 function ShapeFile($shapeType, $boundingBox = array("xmin" => 0.0, "ymin" => 0.0, "xmax" => 0.0, "ymax" => 0.0), $FileName = NULL) {
54 $this->shapeType
= $shapeType;
55 $this->boundingBox
= $boundingBox;
56 $this->FileName
= $FileName;
57 $this->fileLength
= 50;
60 function loadFromFile($FileName) {
61 $this->FileName
= $FileName;
63 if (($this->_openSHPFile()) && ($this->_openDBFFile())) {
64 $this->_loadHeaders();
65 $this->_loadRecords();
66 $this->_closeSHPFile();
67 $this->_closeDBFFile();
73 function saveToFile($FileName = NULL) {
74 if ($FileName != NULL) $this->FileName
= $FileName;
76 if (($this->_openSHPFile(TRUE)) && ($this->_openSHXFile(TRUE)) && ($this->_openDBFFile(TRUE))) {
77 $this->_saveHeaders();
78 $this->_saveRecords();
79 $this->_closeSHPFile();
80 $this->_closeSHXFile();
81 $this->_closeDBFFile();
87 function addRecord($record) {
88 if ((isset($this->DBFHeader
)) && (is_array($this->DBFHeader
))) {
89 $record->updateDBFInfo($this->DBFHeader
);
92 $this->fileLength +
= ($record->getContentLength() +
4);
93 $this->records
[] = $record;
94 $this->records
[count($this->records
) - 1]->recordNumber
= count($this->records
);
96 return (count($this->records
) - 1);
99 function deleteRecord($index) {
100 if (isset($this->records
[$index])) {
101 $this->fileLength
-= ($this->records
[$index]->getContentLength() +
4);
102 for ($i = $index; $i < (count($this->records
) - 1); $i++
) {
103 $this->records
[$i] = $this->records
[$i +
1];
105 unset($this->records
[count($this->records
) - 1]);
106 $this->_deleteRecordFromDBF($index);
110 function getDBFHeader() {
111 return $this->DBFHeader
;
114 function setDBFHeader($header) {
115 $this->DBFHeader
= $header;
117 for ($i = 0; $i < count($this->records
); $i++
) {
118 $this->records
[$i]->updateDBFInfo($header);
122 function getIndexFromDBFData($field, $value) {
124 for ($i = 0; $i < (count($this->records
) - 1); $i++
) {
125 if (isset($this->records
[$i]->DBFData
[$field]) && (strtoupper($this->records
[$i]->DBFData
[$field]) == strtoupper($value))) {
133 function _loadDBFHeader() {
134 $DBFFile = fopen(str_replace('.*', '.dbf', $this->FileName
), 'r');
142 if (!feof($DBFFile)) {
143 $buff32 = fread($DBFFile, 32);
145 if (substr($buff32, 0, 1) == chr(13)) {
148 $pos = strpos(substr($buff32, 0, 10), chr(0));
149 $pos = ($pos == 0 ?
10 : $pos);
151 $fieldName = substr($buff32, 0, $pos);
152 $fieldType = substr($buff32, 11, 1);
153 $fieldLen = ord(substr($buff32, 16, 1));
154 $fieldDec = ord(substr($buff32, 17, 1));
156 array_push($result, array($fieldName, $fieldType, $fieldLen, $fieldDec));
169 function _deleteRecordFromDBF($index) {
170 if (@dbase_delete_record
($this->DBFFile
, $index)) {
171 @dbase_pack
($this->DBFFile
);
175 function _loadHeaders() {
176 fseek($this->SHPFile
, 24, SEEK_SET
);
177 $this->fileLength
= loadData("N", fread($this->SHPFile
, 4));
179 fseek($this->SHPFile
, 32, SEEK_SET
);
180 $this->shapeType
= loadData("V", fread($this->SHPFile
, 4));
182 $this->boundingBox
= array();
183 $this->boundingBox
["xmin"] = loadData("d", fread($this->SHPFile
, 8));
184 $this->boundingBox
["ymin"] = loadData("d", fread($this->SHPFile
, 8));
185 $this->boundingBox
["xmax"] = loadData("d", fread($this->SHPFile
, 8));
186 $this->boundingBox
["ymax"] = loadData("d", fread($this->SHPFile
, 8));
188 $this->DBFHeader
= $this->_loadDBFHeader();
191 function _saveHeaders() {
192 fwrite($this->SHPFile
, pack("NNNNNN", 9994, 0, 0, 0, 0, 0));
193 fwrite($this->SHPFile
, pack("N", $this->fileLength
));
194 fwrite($this->SHPFile
, pack("V", 1000));
195 fwrite($this->SHPFile
, pack("V", $this->shapeType
));
196 fwrite($this->SHPFile
, packDouble($this->boundingBox
['xmin']));
197 fwrite($this->SHPFile
, packDouble($this->boundingBox
['ymin']));
198 fwrite($this->SHPFile
, packDouble($this->boundingBox
['xmax']));
199 fwrite($this->SHPFile
, packDouble($this->boundingBox
['ymax']));
200 fwrite($this->SHPFile
, pack("dddd", 0, 0, 0, 0));
202 fwrite($this->SHXFile
, pack("NNNNNN", 9994, 0, 0, 0, 0, 0));
203 fwrite($this->SHXFile
, pack("N", 50 +
4*count($this->records
)));
204 fwrite($this->SHXFile
, pack("V", 1000));
205 fwrite($this->SHXFile
, pack("V", $this->shapeType
));
206 fwrite($this->SHXFile
, packDouble($this->boundingBox
['xmin']));
207 fwrite($this->SHXFile
, packDouble($this->boundingBox
['ymin']));
208 fwrite($this->SHXFile
, packDouble($this->boundingBox
['xmax']));
209 fwrite($this->SHXFile
, packDouble($this->boundingBox
['ymax']));
210 fwrite($this->SHXFile
, pack("dddd", 0, 0, 0, 0));
213 function _loadRecords() {
214 fseek($this->SHPFile
, 100);
215 while (!feof($this->SHPFile
)) {
216 $bByte = ftell($this->SHPFile
);
217 $record = new ShapeRecord(-1);
218 $record->loadFromFile($this->SHPFile
, $this->DBFFile
);
219 $eByte = ftell($this->SHPFile
);
220 if (($eByte <= $bByte) ||
($record->lastError
!= "")) {
224 $this->records
[] = $record;
228 function _saveRecords() {
229 if (file_exists(str_replace('.*', '.dbf', $this->FileName
))) {
230 @unlink
(str_replace('.*', '.dbf', $this->FileName
));
232 if (!($this->DBFFile
= @dbase_create
(str_replace('.*', '.dbf', $this->FileName
), $this->DBFHeader
))) {
233 return $this->setError(sprintf("It wasn't possible to create the DBase file '%s'", str_replace('.*', '.dbf', $this->FileName
)));
237 if (is_array($this->records
) && (count($this->records
) > 0)) {
238 reset($this->records
);
239 while (list($index, $record) = each($this->records
)) {
240 //Save the record to the .shp file
241 $record->saveToFile($this->SHPFile
, $this->DBFFile
, $index +
1);
243 //Save the record to the .shx file
244 fwrite($this->SHXFile
, pack("N", $offset));
245 fwrite($this->SHXFile
, pack("N", $record->getContentLength()));
246 $offset +
= (4 +
$record->getContentLength());
249 @dbase_pack
($this->DBFFile
);
252 function _openSHPFile($toWrite = false) {
253 $this->SHPFile
= @fopen
(str_replace('.*', '.shp', $this->FileName
), ($toWrite ?
"wb+" : "rb"));
254 if (!$this->SHPFile
) {
255 return $this->setError(sprintf("It wasn't possible to open the Shape file '%s'", str_replace('.*', '.shp', $this->FileName
)));
261 function _closeSHPFile() {
262 if ($this->SHPFile
) {
263 fclose($this->SHPFile
);
264 $this->SHPFile
= NULL;
268 function _openSHXFile($toWrite = false) {
269 $this->SHXFile
= @fopen
(str_replace('.*', '.shx', $this->FileName
), ($toWrite ?
"wb+" : "rb"));
270 if (!$this->SHXFile
) {
271 return $this->setError(sprintf("It wasn't possible to open the Index file '%s'", str_replace('.*', '.shx', $this->FileName
)));
277 function _closeSHXFile() {
278 if ($this->SHXFile
) {
279 fclose($this->SHXFile
);
280 $this->SHXFile
= NULL;
284 function _openDBFFile($toWrite = false) {
285 $checkFunction = $toWrite ?
"is_writable" : "is_readable";
286 if (($toWrite) && (!file_exists(str_replace('.*', '.dbf', $this->FileName
)))) {
287 if (!@dbase_create
(str_replace('.*', '.dbf', $this->FileName
), $this->DBFHeader
)) {
288 return $this->setError(sprintf("It wasn't possible to create the DBase file '%s'", str_replace('.*', '.dbf', $this->FileName
)));
291 if ($checkFunction(str_replace('.*', '.dbf', $this->FileName
))) {
292 $this->DBFFile
= dbase_open(str_replace('.*', '.dbf', $this->FileName
), ($toWrite ?
2 : 0));
293 if (!$this->DBFFile
) {
294 return $this->setError(sprintf("It wasn't possible to open the DBase file '%s'", str_replace('.*', '.dbf', $this->FileName
)));
297 return $this->setError(sprintf("It wasn't possible to find the DBase file '%s'", str_replace('.*', '.dbf', $this->FileName
)));
302 function _closeDBFFile() {
303 if ($this->DBFFile
) {
304 dbase_close($this->DBFFile
);
305 $this->DBFFile
= NULL;
309 function setError($error) {
310 $this->lastError
= $error;
319 var $recordNumber = NULL;
320 var $shapeType = NULL;
324 var $SHPData = array();
325 var $DBFData = array();
327 function ShapeRecord($shapeType) {
328 $this->shapeType
= $shapeType;
331 function loadFromFile(&$SHPFile, &$DBFFile) {
332 $this->SHPFile
= $SHPFile;
333 $this->DBFFile
= $DBFFile;
334 $this->_loadHeaders();
336 switch ($this->shapeType
) {
338 $this->_loadNullRecord();
341 $this->_loadPointRecord();
344 $this->_loadPolyLineRecord();
347 $this->_loadPolygonRecord();
350 $this->_loadMultiPointRecord();
353 $this->setError(sprintf("The Shape Type '%s' is not supported.", $this->shapeType
));
356 $this->_loadDBFData();
359 function saveToFile(&$SHPFile, &$DBFFile, $recordNumber) {
360 $this->SHPFile
= $SHPFile;
361 $this->DBFFile
= $DBFFile;
362 $this->recordNumber
= $recordNumber;
363 $this->_saveHeaders();
365 switch ($this->shapeType
) {
367 $this->_saveNullRecord();
370 $this->_savePointRecord();
373 $this->_savePolyLineRecord();
376 $this->_savePolygonRecord();
379 $this->_saveMultiPointRecord();
382 $this->setError(sprintf("The Shape Type '%s' is not supported.", $this->shapeType
));
385 $this->_saveDBFData();
388 function updateDBFInfo($header) {
389 $tmp = $this->DBFData
;
390 unset($this->DBFData
);
391 $this->DBFData
= array();
393 while (list($key, $value) = each($header)) {
394 $this->DBFData
[$value[0]] = (isset($tmp[$value[0]])) ?
$tmp[$value[0]] : "";
398 function _loadHeaders() {
399 $this->recordNumber
= loadData("N", fread($this->SHPFile
, 4));
400 $tmp = loadData("N", fread($this->SHPFile
, 4)); //We read the length of the record
401 $this->shapeType
= loadData("V", fread($this->SHPFile
, 4));
404 function _saveHeaders() {
405 fwrite($this->SHPFile
, pack("N", $this->recordNumber
));
406 fwrite($this->SHPFile
, pack("N", $this->getContentLength()));
407 fwrite($this->SHPFile
, pack("V", $this->shapeType
));
410 function _loadPoint() {
413 $data["x"] = loadData("d", fread($this->SHPFile
, 8));
414 $data["y"] = loadData("d", fread($this->SHPFile
, 8));
419 function _savePoint($data) {
420 fwrite($this->SHPFile
, packDouble($data["x"]));
421 fwrite($this->SHPFile
, packDouble($data["y"]));
424 function _loadNullRecord() {
425 $this->SHPData
= array();
428 function _saveNullRecord() {
429 //Don't save anything
432 function _loadPointRecord() {
433 $this->SHPData
= $this->_loadPoint();
436 function _savePointRecord() {
437 $this->_savePoint($this->SHPData
);
440 function _loadMultiPointRecord() {
441 $this->SHPData
= array();
442 $this->SHPData
["xmin"] = loadData("d", fread($this->SHPFile
, 8));
443 $this->SHPData
["ymin"] = loadData("d", fread($this->SHPFile
, 8));
444 $this->SHPData
["xmax"] = loadData("d", fread($this->SHPFile
, 8));
445 $this->SHPData
["ymax"] = loadData("d", fread($this->SHPFile
, 8));
447 $this->SHPData
["numpoints"] = loadData("V", fread($this->SHPFile
, 4));
449 for ($i = 0; $i <= $this->SHPData
["numpoints"]; $i++
) {
450 $this->SHPData
["points"][] = $this->_loadPoint();
454 function _saveMultiPointRecord() {
455 fwrite($this->SHPFile
, pack("dddd", $this->SHPData
["xmin"], $this->SHPData
["ymin"], $this->SHPData
["xmax"], $this->SHPData
["ymax"]));
457 fwrite($this->SHPFile
, pack("V", $this->SHPData
["numpoints"]));
459 for ($i = 0; $i <= $this->SHPData
["numpoints"]; $i++
) {
460 $this->_savePoint($this->SHPData
["points"][$i]);
464 function _loadPolyLineRecord() {
465 $this->SHPData
= array();
466 $this->SHPData
["xmin"] = loadData("d", fread($this->SHPFile
, 8));
467 $this->SHPData
["ymin"] = loadData("d", fread($this->SHPFile
, 8));
468 $this->SHPData
["xmax"] = loadData("d", fread($this->SHPFile
, 8));
469 $this->SHPData
["ymax"] = loadData("d", fread($this->SHPFile
, 8));
471 $this->SHPData
["numparts"] = loadData("V", fread($this->SHPFile
, 4));
472 $this->SHPData
["numpoints"] = loadData("V", fread($this->SHPFile
, 4));
474 for ($i = 0; $i < $this->SHPData
["numparts"]; $i++
) {
475 $this->SHPData
["parts"][$i] = loadData("V", fread($this->SHPFile
, 4));
478 $firstIndex = ftell($this->SHPFile
);
480 reset($this->SHPData
["parts"]);
481 while (list($partIndex, $partData) = each($this->SHPData
["parts"])) {
482 if (!isset($this->SHPData
["parts"][$partIndex]["points"]) ||
!is_array($this->SHPData
["parts"][$partIndex]["points"])) {
483 $this->SHPData
["parts"][$partIndex] = array();
484 $this->SHPData
["parts"][$partIndex]["points"] = array();
486 while (!in_array($readPoints, $this->SHPData
["parts"]) && ($readPoints < ($this->SHPData
["numpoints"])) && !feof($this->SHPFile
)) {
487 $this->SHPData
["parts"][$partIndex]["points"][] = $this->_loadPoint();
492 fseek($this->SHPFile
, $firstIndex +
($readPoints*16));
495 function _savePolyLineRecord() {
496 fwrite($this->SHPFile
, pack("dddd", $this->SHPData
["xmin"], $this->SHPData
["ymin"], $this->SHPData
["xmax"], $this->SHPData
["ymax"]));
498 fwrite($this->SHPFile
, pack("VV", $this->SHPData
["numparts"], $this->SHPData
["numpoints"]));
500 for ($i = 0; $i < $this->SHPData
["numparts"]; $i++
) {
501 fwrite($this->SHPFile
, pack("V", count($this->SHPData
["parts"][$i])));
504 reset($this->SHPData
["parts"]);
505 foreach ($this->SHPData
["parts"] as $partData){
506 reset($partData["points"]);
507 while (list($pointIndex, $pointData) = each($partData["points"])) {
508 $this->_savePoint($pointData);
513 function _loadPolygonRecord() {
514 $this->_loadPolyLineRecord();
517 function _savePolygonRecord() {
518 $this->_savePolyLineRecord();
521 function addPoint($point, $partIndex = 0) {
522 switch ($this->shapeType
) {
527 //Substitutes the value of the current point
528 $this->SHPData
= $point;
532 //Adds a new point to the selected part
533 if (!isset($this->SHPData
["xmin"]) ||
($this->SHPData
["xmin"] > $point["x"])) $this->SHPData
["xmin"] = $point["x"];
534 if (!isset($this->SHPData
["ymin"]) ||
($this->SHPData
["ymin"] > $point["y"])) $this->SHPData
["ymin"] = $point["y"];
535 if (!isset($this->SHPData
["xmax"]) ||
($this->SHPData
["xmax"] < $point["x"])) $this->SHPData
["xmax"] = $point["x"];
536 if (!isset($this->SHPData
["ymax"]) ||
($this->SHPData
["ymax"] < $point["y"])) $this->SHPData
["ymax"] = $point["y"];
538 $this->SHPData
["parts"][$partIndex]["points"][] = $point;
540 $this->SHPData
["numparts"] = count($this->SHPData
["parts"]);
541 $this->SHPData
["numpoints"]++
;
545 if (!isset($this->SHPData
["xmin"]) ||
($this->SHPData
["xmin"] > $point["x"])) $this->SHPData
["xmin"] = $point["x"];
546 if (!isset($this->SHPData
["ymin"]) ||
($this->SHPData
["ymin"] > $point["y"])) $this->SHPData
["ymin"] = $point["y"];
547 if (!isset($this->SHPData
["xmax"]) ||
($this->SHPData
["xmax"] < $point["x"])) $this->SHPData
["xmax"] = $point["x"];
548 if (!isset($this->SHPData
["ymax"]) ||
($this->SHPData
["ymax"] < $point["y"])) $this->SHPData
["ymax"] = $point["y"];
550 $this->SHPData
["points"][] = $point;
551 $this->SHPData
["numpoints"]++
;
554 $this->setError(sprintf("The Shape Type '%s' is not supported.", $this->shapeType
));
559 function deletePoint($pointIndex = 0, $partIndex = 0) {
560 switch ($this->shapeType
) {
562 //Don't delete anything
565 //Sets the value of the point to zero
566 $this->SHPData
["x"] = 0.0;
567 $this->SHPData
["y"] = 0.0;
571 //Deletes the point from the selected part, if exists
572 if (isset($this->SHPData
["parts"][$partIndex]) && isset($this->SHPData
["parts"][$partIndex]["points"][$pointIndex])) {
573 for ($i = $pointIndex; $i < (count($this->SHPData
["parts"][$partIndex]["points"]) - 1); $i++
) {
574 $this->SHPData
["parts"][$partIndex]["points"][$i] = $this->SHPData
["parts"][$partIndex]["points"][$i +
1];
576 unset($this->SHPData
["parts"][$partIndex]["points"][count($this->SHPData
["parts"][$partIndex]["points"]) - 1]);
578 $this->SHPData
["numparts"] = count($this->SHPData
["parts"]);
579 $this->SHPData
["numpoints"]--;
583 //Deletes the point, if exists
584 if (isset($this->SHPData
["points"][$pointIndex])) {
585 for ($i = $pointIndex; $i < (count($this->SHPData
["points"]) - 1); $i++
) {
586 $this->SHPData
["points"][$i] = $this->SHPData
["points"][$i +
1];
588 unset($this->SHPData
["points"][count($this->SHPData
["points"]) - 1]);
590 $this->SHPData
["numpoints"]--;
594 $this->setError(sprintf("The Shape Type '%s' is not supported.", $this->shapeType
));
599 function getContentLength() {
600 switch ($this->shapeType
) {
609 $result = 22 +
2*count($this->SHPData
["parts"]);
610 for ($i = 0; $i < count($this->SHPData
["parts"]); $i++
) {
611 $result +
= 8*count($this->SHPData
["parts"][$i]["points"]);
615 $result = 20 +
8*count($this->SHPData
["points"]);
619 $this->setError(sprintf("The Shape Type '%s' is not supported.", $this->shapeType
));
625 function _loadDBFData() {
626 $this->DBFData
= @dbase_get_record_with_names
($this->DBFFile
, $this->recordNumber
);
627 unset($this->DBFData
["deleted"]);
630 function _saveDBFData() {
631 unset($this->DBFData
["deleted"]);
632 if ($this->recordNumber
<= dbase_numrecords($this->DBFFile
)) {
633 if (!dbase_replace_record($this->DBFFile
, array_values($this->DBFData
), $this->recordNumber
)) {
634 $this->setError("I wasn't possible to update the information in the DBF file.");
637 if (!dbase_add_record($this->DBFFile
, array_values($this->DBFData
))) {
638 $this->setError("I wasn't possible to add the information to the DBF file.");
643 function setError($error) {
644 $this->lastError
= $error;