Skip to content

Commit

Permalink
Check whether we have read complete record
Browse files Browse the repository at this point in the history
Signed-off-by: Michal Čihař <michal@cihar.com>
  • Loading branch information
nijel committed Jun 24, 2016
1 parent 54765c5 commit 2bbc3a0
Showing 1 changed file with 56 additions and 26 deletions.
82 changes: 56 additions & 26 deletions src/ShapeRecord.php
Expand Up @@ -26,6 +26,9 @@ class ShapeRecord {
private $DBFFile = null;
private $ShapeFile = null;

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

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

Expand All @@ -47,6 +50,11 @@ public function loadFromFile(&$ShapeFile, &$SHPFile, &$DBFFile) {
$this->DBFFile = $DBFFile;
$this->_loadHeaders();

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

switch ($this->shapeType) {
case 0:
$this->_loadNullRecord();
Expand Down Expand Up @@ -91,6 +99,11 @@ public function loadFromFile(&$ShapeFile, &$SHPFile, &$DBFFile) {
$this->setError(sprintf('The Shape Type "%s" is not supported.', $this->shapeType));
break;
}

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();
}
Expand Down Expand Up @@ -158,11 +171,28 @@ public function updateDBFInfo($header) {
}
}

/**
* 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) {
$this->read += strlen($data);
}
return Util::loadData($type, $data);
}

private function _loadHeaders() {
$this->recordNumber = Util::loadData('N', $this->ShapeFile->readSHP(4));
$this->recordNumber = $this->_loadData('N', 4);
// We read the length of the record
Util::loadData('N', $this->ShapeFile->readSHP(4));
$this->shapeType = Util::loadData('V', $this->ShapeFile->readSHP(4));
$this->size = $this->_loadData('N', 4) * 2 + 8;
$this->shapeType = $this->_loadData('V', 4);
}

private function _saveHeaders() {
Expand All @@ -174,29 +204,29 @@ private function _saveHeaders() {
private function _loadPoint() {
$data = array();

$data['x'] = Util::loadData('d', $this->ShapeFile->readSHP(8));
$data['y'] = Util::loadData('d', $this->ShapeFile->readSHP(8));
$data['x'] = $this->_loadData('d', 8);
$data['y'] = $this->_loadData('d', 8);

return $data;
}

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

$data['x'] = Util::loadData('d', $this->ShapeFile->readSHP(8));
$data['y'] = Util::loadData('d', $this->ShapeFile->readSHP(8));
$data['m'] = Util::loadData('d', $this->ShapeFile->readSHP(8));
$data['x'] = $this->_loadData('d', 8);
$data['y'] = $this->_loadData('d', 8);
$data['m'] = $this->_loadData('d', 8);

return $data;
}

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

$data['x'] = Util::loadData('d', $this->ShapeFile->readSHP(8));
$data['y'] = Util::loadData('d', $this->ShapeFile->readSHP(8));
$data['z'] = Util::loadData('d', $this->ShapeFile->readSHP(8));
$data['m'] = Util::loadData('d', $this->ShapeFile->readSHP(8));
$data['x'] = $this->_loadData('d', 8);
$data['y'] = $this->_loadData('d', 8);
$data['z'] = $this->_loadData('d', 8);
$data['m'] = $this->_loadData('d', 8);

return $data;
}
Expand Down Expand Up @@ -249,17 +279,17 @@ private function _savePointZRecord() {

private function _loadBBox()
{
$this->SHPData['xmin'] = Util::loadData('d', $this->ShapeFile->readSHP(8));
$this->SHPData['ymin'] = Util::loadData('d', $this->ShapeFile->readSHP(8));
$this->SHPData['xmax'] = Util::loadData('d', $this->ShapeFile->readSHP(8));
$this->SHPData['ymax'] = Util::loadData('d', $this->ShapeFile->readSHP(8));
$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'] = Util::loadData('V', $this->ShapeFile->readSHP(4));
$this->SHPData['numpoints'] = $this->_loadData('V', 4);

for ($i = 0; $i <= $this->SHPData['numpoints']; $i++) {
$this->SHPData['points'][] = $this->_loadPoint();
Expand All @@ -276,11 +306,11 @@ private function _loadMultiPointMZRecord($type)
return;
}

$this->SHPData[$type.'min'] = Util::loadData('d', $this->ShapeFile->readSHP(8));
$this->SHPData[$type.'max'] = Util::loadData('d', $this->ShapeFile->readSHP(8));
$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] = Util::loadData('d', $this->ShapeFile->readSHP(8));
$this->SHPData['points'][$i][$type] = $this->_loadData('d', 8);
}
}

Expand Down Expand Up @@ -336,14 +366,14 @@ private function _loadPolyLineRecord() {
$this->SHPData = array();
$this->_loadBBox();

$this->SHPData['numparts'] = Util::loadData('V', $this->ShapeFile->readSHP(4));
$this->SHPData['numpoints'] = Util::loadData('V', $this->ShapeFile->readSHP(4));
$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] = Util::loadData('V', $this->ShapeFile->readSHP(4));
$this->SHPData['parts'][$i] = $this->_loadData('V', 4);
}

$part = 0;
Expand All @@ -367,8 +397,8 @@ private function _loadPolyLineMZRecord($type) {
return;
}

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

$numparts = $this->SHPData['numparts'];
$numpoints = $this->SHPData['numpoints'];
Expand All @@ -378,7 +408,7 @@ private function _loadPolyLineMZRecord($type) {
if ($part + 1 < $numparts && $this->SHPData['parts'][$part + 1] == $i) {
$part++;
}
$this->SHPData['parts'][$part]['points'][$i][$type] = Util::loadData('d', $this->ShapeFile->readSHP(8));
$this->SHPData['parts'][$part]['points'][$i][$type] = $this->_loadData('d', 8);
}
}

Expand Down

0 comments on commit 2bbc3a0

Please sign in to comment.