ShapeRecord.php 24.3 KB
Newer Older
Hamza Arfaoui's avatar
Hamza Arfaoui committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
<?php
/**
 * phpMyAdmin ShapeFile library
 * <https://github.com/phpmyadmin/shapefile/>.
 *
 * Copyright 2006-2007 Ovidio <ovidio AT users.sourceforge.net>
 * Copyright 2016 - 2017 Michal Čihař <michal@cihar.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you can download one from
 * https://www.gnu.org/copyleft/gpl.html.
 */

namespace PhpMyAdmin\ShapeFile;

/**
 * ShapeFile record class.
 */
class ShapeRecord
{
    private $SHPFile = null;
    private $DBFFile = null;
    private $ShapeFile = null;

    private $size = 0;
    private $read = 0;

    public $recordNumber = null;
    public $shapeType = null;

    public $lastError = '';

    public $SHPData = array();
    public $DBFData = array();

    /**
     * @param int $shapeType
     */
    public function __construct($shapeType)
    {
        $this->shapeType = $shapeType;
    }

    /**
     * Loads record from files.
     *
     * @param ShapeFile $ShapeFile
     * @param file      &$SHPFile  Opened SHP file
     * @param file      &$DBFFile  Opened DBF file
     */
    public function loadFromFile(&$ShapeFile, &$SHPFile, &$DBFFile)
    {
        $this->ShapeFile = $ShapeFile;
        $this->SHPFile = $SHPFile;
        $this->DBFFile = $DBFFile;
        $this->_loadHeaders();

        /* No header read */
        if ($this->read == 0) {
            return;
        }

        switch ($this->shapeType) {
            case 0:
                $this->_loadNullRecord();
                break;
            case 1:
                $this->_loadPointRecord();
                break;
            case 21:
                $this->_loadPointMRecord();
                break;
            case 11:
                $this->_loadPointZRecord();
                break;
            case 3:
                $this->_loadPolyLineRecord();
                break;
            case 23:
                $this->_loadPolyLineMRecord();
                break;
            case 13:
                $this->_loadPolyLineZRecord();
                break;
            case 5:
                $this->_loadPolygonRecord();
                break;
            case 25:
                $this->_loadPolygonMRecord();
                break;
            case 15:
                $this->_loadPolygonZRecord();
                break;
            case 8:
                $this->_loadMultiPointRecord();
                break;
            case 28:
                $this->_loadMultiPointMRecord();
                break;
            case 18:
                $this->_loadMultiPointZRecord();
                break;
            default:
                $this->setError(sprintf('The Shape Type "%s" is not supported.', $this->shapeType));
                break;
        }

        /* We need to skip rest of the record */
        while ($this->read < $this->size) {
            $this->_loadData('V', 4);
        }

        /* Check if we didn't read too much */
        if ($this->read != $this->size) {
            $this->setError(sprintf('Failed to parse record, read=%d, size=%d', $this->read, $this->size));
        }

        if (ShapeFile::supports_dbase() && isset($this->DBFFile)) {
            $this->_loadDBFData();
        }
    }

    /**
     * Saves record to files.
     *
     * @param file &$SHPFile     Opened SHP file
     * @param file &$DBFFile     Opened DBF file
     * @param int  $recordNumber Record number
     */
    public function saveToFile(&$SHPFile, &$DBFFile, $recordNumber)
    {
        $this->SHPFile = $SHPFile;
        $this->DBFFile = $DBFFile;
        $this->recordNumber = $recordNumber;
        $this->_saveHeaders();

        switch ($this->shapeType) {
            case 0:
                // Nothing to save
                break;
            case 1:
                $this->_savePointRecord();
                break;
            case 21:
                $this->_savePointMRecord();
                break;
            case 11:
                $this->_savePointZRecord();
                break;
            case 3:
                $this->_savePolyLineRecord();
                break;
            case 23:
                $this->_savePolyLineMRecord();
                break;
            case 13:
                $this->_savePolyLineZRecord();
                break;
            case 5:
                $this->_savePolygonRecord();
                break;
            case 25:
                $this->_savePolygonMRecord();
                break;
            case 15:
                $this->_savePolygonZRecord();
                break;
            case 8:
                $this->_saveMultiPointRecord();
                break;
            case 28:
                $this->_saveMultiPointMRecord();
                break;
            case 18:
                $this->_saveMultiPointZRecord();
                break;
            default:
                $this->setError(sprintf('The Shape Type "%s" is not supported.', $this->shapeType));
                break;
        }
        if (ShapeFile::supports_dbase() && !is_null($this->DBFFile)) {
            $this->_saveDBFData();
        }
    }

    /**
     * Updates DBF data to match header.
     *
     * @param array $header DBF structure header
     */
    public function updateDBFInfo($header)
    {
        $tmp = $this->DBFData;
        unset($this->DBFData);
        $this->DBFData = array();
        foreach ($header as $value) {
            $this->DBFData[$value[0]] = (isset($tmp[$value[0]])) ? $tmp[$value[0]] : '';
        }
    }

    /**
     * Reads data.
     *
     * @param string $type  type for unpack()
     * @param int    $count number of bytes
     *
     * @return mixed
     */
    private function _loadData($type, $count)
    {
        $data = $this->ShapeFile->readSHP($count);
        if ($data === false) {
            return false;
        }
        $this->read += strlen($data);

        return Util::loadData($type, $data);
    }

    /**
     * Loads metadata header from a file.
     */
    private function _loadHeaders()
    {
        $this->shapeType = false;
        $this->recordNumber = $this->_loadData('N', 4);
        if ($this->recordNumber === false) {
            return;
        }
        // We read the length of the record
        $this->size = $this->_loadData('N', 4);
        if ($this->size === false) {
            return;
        }
        $this->size = $this->size * 2 + 8;
        $this->shapeType = $this->_loadData('V', 4);
    }

    /**
     * Saves metadata header to a file.
     */
    private function _saveHeaders()
    {
        fwrite($this->SHPFile, pack('N', $this->recordNumber));
        fwrite($this->SHPFile, pack('N', $this->getContentLength()));
        fwrite($this->SHPFile, pack('V', $this->shapeType));
    }

    private function _loadPoint()
    {
        $data = array();

        $data['x'] = $this->_loadData('d', 8);
        $data['y'] = $this->_loadData('d', 8);

        return $data;
    }

    private function _loadPointM()
    {
        $data = $this->_loadPoint();

        $data['m'] = $this->_loadData('d', 8);

        return $data;
    }

    private function _loadPointZ()
    {
        $data = $this->_loadPoint();

        $data['z'] = $this->_loadData('d', 8);
        $data['m'] = $this->_loadData('d', 8);

        return $data;
    }

    private function _savePoint($data)
    {
        fwrite($this->SHPFile, Util::packDouble($data['x']));
        fwrite($this->SHPFile, Util::packDouble($data['y']));
    }

    private function _savePointM($data)
    {
        fwrite($this->SHPFile, Util::packDouble($data['x']));
        fwrite($this->SHPFile, Util::packDouble($data['y']));
        fwrite($this->SHPFile, Util::packDouble($data['m']));
    }

    private function _savePointZ($data)
    {
        fwrite($this->SHPFile, Util::packDouble($data['x']));
        fwrite($this->SHPFile, Util::packDouble($data['y']));
        fwrite($this->SHPFile, Util::packDouble($data['z']));
        fwrite($this->SHPFile, Util::packDouble($data['m']));
    }

    private function _loadNullRecord()
    {
        $this->SHPData = array();
    }

    private function _loadPointRecord()
    {
        $this->SHPData = $this->_loadPoint();
    }

    private function _loadPointMRecord()
    {
        $this->SHPData = $this->_loadPointM();
    }

    private function _loadPointZRecord()
    {
        $this->SHPData = $this->_loadPointZ();
    }

    private function _savePointRecord()
    {
        $this->_savePoint($this->SHPData);
    }

    private function _savePointMRecord()
    {
        $this->_savePointM($this->SHPData);
    }

    private function _savePointZRecord()
    {
        $this->_savePointZ($this->SHPData);
    }

    private function _loadBBox()
    {
        $this->SHPData['xmin'] = $this->_loadData('d', 8);
        $this->SHPData['ymin'] = $this->_loadData('d', 8);
        $this->SHPData['xmax'] = $this->_loadData('d', 8);
        $this->SHPData['ymax'] = $this->_loadData('d', 8);
    }

    private function _loadMultiPointRecord()
    {
        $this->SHPData = array();
        $this->_loadBBox();

        $this->SHPData['numpoints'] = $this->_loadData('V', 4);

        for ($i = 0; $i < $this->SHPData['numpoints']; ++$i) {
            $this->SHPData['points'][] = $this->_loadPoint();
        }
    }

    /**
     * @param string $type
     */
    private function _loadMultiPointMZRecord($type)
    {
        /* The m dimension is optional, depends on bounding box data */
        if ($type == 'm' && !$this->ShapeFile->hasMeasure()) {
            return;
        }

        $this->SHPData[$type . 'min'] = $this->_loadData('d', 8);
        $this->SHPData[$type . 'max'] = $this->_loadData('d', 8);

        for ($i = 0; $i < $this->SHPData['numpoints']; ++$i) {
            $this->SHPData['points'][$i][$type] = $this->_loadData('d', 8);
        }
    }

    private function _loadMultiPointMRecord()
    {
        $this->_loadMultiPointRecord();

        $this->_loadMultiPointMZRecord('m');
    }

    private function _loadMultiPointZRecord()
    {
        $this->_loadMultiPointRecord();

        $this->_loadMultiPointMZRecord('z');
        $this->_loadMultiPointMZRecord('m');
    }

    private function _saveMultiPointRecord()
    {
        fwrite($this->SHPFile, pack('dddd', $this->SHPData['xmin'], $this->SHPData['ymin'], $this->SHPData['xmax'], $this->SHPData['ymax']));

        fwrite($this->SHPFile, pack('V', $this->SHPData['numpoints']));

        for ($i = 0; $i < $this->SHPData['numpoints']; ++$i) {
            $this->_savePoint($this->SHPData['points'][$i]);
        }
    }

    /**
     * @param string $type
     */
    private function _saveMultiPointMZRecord($type)
    {
        fwrite($this->SHPFile, pack('dd', $this->SHPData[$type . 'min'], $this->SHPData[$type . 'max']));

        for ($i = 0; $i < $this->SHPData['numpoints']; ++$i) {
            fwrite($this->SHPFile, Util::packDouble($this->SHPData['points'][$i][$type]));
        }
    }

    private function _saveMultiPointMRecord()
    {
        $this->_saveMultiPointRecord();

        $this->_saveMultiPointMZRecord('m');
    }

    private function _saveMultiPointZRecord()
    {
        $this->_saveMultiPointRecord();

        $this->_saveMultiPointMZRecord('z');
        $this->_saveMultiPointMZRecord('m');
    }

    private function _loadPolyLineRecord()
    {
        $this->SHPData = array();
        $this->_loadBBox();

        $this->SHPData['numparts'] = $this->_loadData('V', 4);
        $this->SHPData['numpoints'] = $this->_loadData('V', 4);

        $numparts = $this->SHPData['numparts'];
        $numpoints = $this->SHPData['numpoints'];

        for ($i = 0; $i < $numparts; ++$i) {
            $this->SHPData['parts'][$i] = $this->_loadData('V', 4);
        }

        $part = 0;
        for ($i = 0; $i < $numpoints; ++$i) {
            if ($part + 1 < $numparts && $this->SHPData['parts'][$part + 1] == $i) {
                ++$part;
            }
            if (!isset($this->SHPData['parts'][$part]['points']) || !is_array($this->SHPData['parts'][$part]['points'])) {
                $this->SHPData['parts'][$part] = array('points' => array());
            }
            $this->SHPData['parts'][$part]['points'][] = $this->_loadPoint();
        }
    }

    /**
     * @param string $type
     */
    private function _loadPolyLineMZRecord($type)
    {
        /* The m dimension is optional, depends on bounding box data */
        if ($type == 'm' && !$this->ShapeFile->hasMeasure()) {
            return;
        }

        $this->SHPData[$type . 'min'] = $this->_loadData('d', 8);
        $this->SHPData[$type . 'max'] = $this->_loadData('d', 8);

        $numparts = $this->SHPData['numparts'];
        $numpoints = $this->SHPData['numpoints'];

        $part = 0;
        for ($i = 0; $i < $numpoints; ++$i) {
            if ($part + 1 < $numparts && $this->SHPData['parts'][$part + 1] == $i) {
                ++$part;
            }
            $this->SHPData['parts'][$part]['points'][$i][$type] = $this->_loadData('d', 8);
        }
    }

    private function _loadPolyLineMRecord()
    {
        $this->_loadPolyLineRecord();

        $this->_loadPolyLineMZRecord('m');
    }

    private function _loadPolyLineZRecord()
    {
        $this->_loadPolyLineRecord();

        $this->_loadPolyLineMZRecord('z');
        $this->_loadPolyLineMZRecord('m');
    }

    private function _savePolyLineRecord()
    {
        fwrite($this->SHPFile, pack('dddd', $this->SHPData['xmin'], $this->SHPData['ymin'], $this->SHPData['xmax'], $this->SHPData['ymax']));

        fwrite($this->SHPFile, pack('VV', $this->SHPData['numparts'], $this->SHPData['numpoints']));

        $part_index = 0;
        for ($i = 0; $i < $this->SHPData['numparts']; ++$i) {
            fwrite($this->SHPFile, pack('V', $part_index));
            $part_index += count($this->SHPData['parts'][$i]['points']);
        }

        foreach ($this->SHPData['parts'] as $partData) {
            foreach ($partData['points'] as $pointData) {
                $this->_savePoint($pointData);
            }
        }
    }

    /**
     * @param string $type
     */
    private function _savePolyLineMZRecord($type)
    {
        fwrite($this->SHPFile, pack('dd', $this->SHPData[$type . 'min'], $this->SHPData[$type . 'max']));

        foreach ($this->SHPData['parts'] as $partData) {
            foreach ($partData['points'] as $pointData) {
                fwrite($this->SHPFile, Util::packDouble($pointData[$type]));
            }
        }
    }

    private function _savePolyLineMRecord()
    {
        $this->_savePolyLineRecord();

        $this->_savePolyLineMZRecord('m');
    }

    private function _savePolyLineZRecord()
    {
        $this->_savePolyLineRecord();

        $this->_savePolyLineMZRecord('z');
        $this->_savePolyLineMZRecord('m');
    }

    private function _loadPolygonRecord()
    {
        $this->_loadPolyLineRecord();
    }

    private function _loadPolygonMRecord()
    {
        $this->_loadPolyLineMRecord();
    }

    private function _loadPolygonZRecord()
    {
        $this->_loadPolyLineZRecord();
    }

    private function _savePolygonRecord()
    {
        $this->_savePolyLineRecord();
    }

    private function _savePolygonMRecord()
    {
        $this->_savePolyLineMRecord();
    }

    private function _savePolygonZRecord()
    {
        $this->_savePolyLineZRecord();
    }

    private function _adjustBBox($point)
    {
        // Adjusts bounding box based on point
        $directions = array('x', 'y', 'z', 'm');
        foreach ($directions as $direction) {
            if (!isset($point[$direction])) {
                continue;
            }
            $min = $direction . 'min';
            $max = $direction . 'max';
            if (!isset($this->SHPData[$min]) || ($this->SHPData[$min] > $point[$direction])) {
                $this->SHPData[$min] = $point[$direction];
            }
            if (!isset($this->SHPData[$max]) || ($this->SHPData[$max] < $point[$direction])) {
                $this->SHPData[$max] = $point[$direction];
            }
        }
    }

    /**
     * Sets dimension to 0 if not set.
     *
     * @param array  $point     Point to check
     * @param string $dimension Dimension to check
     *
     * @return array
     */
    private function _fixPoint($point, $dimension)
    {
        if (!isset($point[$dimension])) {
            $point[$dimension] = 0.0; // no_value
        }

        return $point;
    }

    /**
     * Adjust point and bounding box when adding point.
     *
     * @param array $point Point data
     *
     * @return array Fixed point data
     */
    private function _adjustPoint($point)
    {
        $type = $this->shapeType / 10;
        if ($type >= 2) {
            $point = $this->_fixPoint($point, 'm');
        } elseif ($type >= 1) {
            $point = $this->_fixPoint($point, 'z');
            $point = $this->_fixPoint($point, 'm');
        }

        return $point;
    }

    /**
     * Adds point to a record.
     *
     * @param array $point     Point data
     * @param int   $partIndex Part index
     */
    public function addPoint($point, $partIndex = 0)
    {
        $point = $this->_adjustPoint($point);
        switch ($this->shapeType) {
            case 0:
                //Don't add anything
                return;
            case 1:
            case 11:
            case 21:
                //Substitutes the value of the current point
                $this->SHPData = $point;
                break;
            case 3:
            case 5:
            case 13:
            case 15:
            case 23:
            case 25:
                //Adds a new point to the selected part
                $this->SHPData['parts'][$partIndex]['points'][] = $point;
                $this->SHPData['numparts'] = count($this->SHPData['parts']);
                $this->SHPData['numpoints'] = 1 + (isset($this->SHPData['numpoints']) ? $this->SHPData['numpoints'] : 0);
                break;
            case 8:
            case 18:
            case 28:
                //Adds a new point
                $this->SHPData['points'][] = $point;
                $this->SHPData['numpoints'] = 1 + (isset($this->SHPData['numpoints']) ? $this->SHPData['numpoints'] : 0);
                break;
            default:
                $this->setError(sprintf('The Shape Type "%s" is not supported.', $this->shapeType));

                return;
        }
        $this->_adjustBBox($point);
    }

    /**
     * Deletes point from a record.
     *
     * @param int $pointIndex Point index
     * @param int $partIndex  Part index
     */
    public function deletePoint($pointIndex = 0, $partIndex = 0)
    {
        switch ($this->shapeType) {
            case 0:
                //Don't delete anything
                break;
            case 1:
            case 11:
            case 21:
                //Sets the value of the point to zero
                $this->SHPData['x'] = 0.0;
                $this->SHPData['y'] = 0.0;
                if (in_array($this->shapeType, array(11, 21))) {
                    $this->SHPData['m'] = 0.0;
                }
                if (in_array($this->shapeType, array(11))) {
                    $this->SHPData['z'] = 0.0;
                }
                break;
            case 3:
            case 5:
            case 13:
            case 15:
            case 23:
            case 25:
                //Deletes the point from the selected part, if exists
                if (isset($this->SHPData['parts'][$partIndex]) && isset($this->SHPData['parts'][$partIndex]['points'][$pointIndex])) {
                    $count = count($this->SHPData['parts'][$partIndex]['points']) - 1;
                    for ($i = $pointIndex; $i < $count; ++$i) {
                        $this->SHPData['parts'][$partIndex]['points'][$i] = $this->SHPData['parts'][$partIndex]['points'][$i + 1];
                    }
                    unset($this->SHPData['parts'][$partIndex]['points'][count($this->SHPData['parts'][$partIndex]['points']) - 1]);

                    $this->SHPData['numparts'] = count($this->SHPData['parts']);
                    --$this->SHPData['numpoints'];
                }
                break;
            case 8:
            case 18:
            case 28:
                //Deletes the point, if exists
                if (isset($this->SHPData['points'][$pointIndex])) {
                    $count = count($this->SHPData['points']) - 1;
                    for ($i = $pointIndex; $i < $count; ++$i) {
                        $this->SHPData['points'][$i] = $this->SHPData['points'][$i + 1];
                    }
                    unset($this->SHPData['points'][count($this->SHPData['points']) - 1]);

                    --$this->SHPData['numpoints'];
                }
                break;
            default:
                $this->setError(sprintf('The Shape Type "%s" is not supported.', $this->shapeType));
                break;
        }
    }

    /**
     * Returns length of content.
     *
     * @return int
     */
    public function getContentLength()
    {
        // The content length for a record is the length of the record contents section measured in 16-bit words.
        // one coordinate makes 4 16-bit words (64 bit double)
        switch ($this->shapeType) {
            case 0:
                $result = 0;
                break;
            case 1:
                $result = 10;
                break;
            case 21:
                $result = 10 + 4;
                break;
            case 11:
                $result = 10 + 8;
                break;
            case 3:
            case 5:
                $count = count($this->SHPData['parts']);
                $result = 22 + 2 * $count;
                for ($i = 0; $i < $count; ++$i) {
                    $result += 8 * count($this->SHPData['parts'][$i]['points']);
                }
                break;
            case 23:
            case 25:
                $count = count($this->SHPData['parts']);
                $result = 22 + (2 * 4) + 2 * $count;
                for ($i = 0; $i < $count; ++$i) {
                    $result += (8 + 4) * count($this->SHPData['parts'][$i]['points']);
                }
                break;
            case 13:
            case 15:
                $count = count($this->SHPData['parts']);
                $result = 22 + (4 * 4) + 2 * $count;
                for ($i = 0; $i < $count; ++$i) {
                    $result += (8 + 8) * count($this->SHPData['parts'][$i]['points']);
                }
                break;
            case 8:
                $result = 20 + 8 * count($this->SHPData['points']);
                break;
            case 28:
                $result = 20 + (2 * 4) + (8 + 4) * count($this->SHPData['points']);
                break;
            case 18:
                $result = 20 + (4 * 4) + (8 + 8) * count($this->SHPData['points']);
                break;
            default:
                $result = false;
                $this->setError(sprintf('The Shape Type "%s" is not supported.', $this->shapeType));
                break;
        }

        return $result;
    }

    private function _loadDBFData()
    {
        $this->DBFData = @dbase_get_record_with_names($this->DBFFile, $this->recordNumber);
        unset($this->DBFData['deleted']);
    }

    private function _saveDBFData()
    {
        if (count($this->DBFData) == 0) {
            return;
        }
        unset($this->DBFData['deleted']);
        if ($this->recordNumber <= dbase_numrecords($this->DBFFile)) {
            if (!dbase_replace_record($this->DBFFile, array_values($this->DBFData), $this->recordNumber)) {
                $this->setError('I wasn\'t possible to update the information in the DBF file.');
            }
        } else {
            if (!dbase_add_record($this->DBFFile, array_values($this->DBFData))) {
                $this->setError('I wasn\'t possible to add the information to the DBF file.');
            }
        }
    }

    /**
     * Sets error message.
     *
     * @param string $error
     */
    public function setError($error)
    {
        $this->lastError = $error;
    }

    /**
     * Returns shape name.
     *
     * @return string
     */
    public function getShapeName()
    {
        return Util::nameShape($this->shapeType);
    }
}