Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a toggle to allow shape files with no dbf #41

Open
wants to merge 1 commit into
base: 3.1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified data/no-dbf.shp
Binary file not shown.
26 changes: 22 additions & 4 deletions src/ShapeFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ class ShapeFile
/** @var array */
public $records = [];

/** @var bool */
private $allowNoDbf = false;

/**
* Checks whether dbase manipulations are supported.
*/
Expand Down Expand Up @@ -121,6 +124,11 @@ public function __construct(
$this->fileLength = 50;
}

public function setAllowNoDbf(bool $allowNoDbf): void
{
$this->allowNoDbf = $allowNoDbf;
}

/**
* Loads shapefile and dbase (if supported).
*
Expand Down Expand Up @@ -326,8 +334,16 @@ public function getIndexFromDBFData(string $field, $value): int
*/
private function loadDBFHeader(): array
{
$DBFFile = fopen($this->getFilename('.dbf'), 'r');
if (! self::supportsDbase()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved this block from loadHeaders so no matter where this method is called then we'll always check for support for dbase.

return [];
}

$dbfName = $this->getFilename('.dbf');
if (! file_exists($dbfName)) {
return [];
}

$DBFFile = fopen($dbfName, 'r');
$result = [];
$i = 1;
$inHeader = true;
Expand Down Expand Up @@ -405,9 +421,7 @@ private function loadHeaders(): bool
$this->boundingBox['mmin'] = Util::loadData('d', $this->readSHP(8));
$this->boundingBox['mmax'] = Util::loadData('d', $this->readSHP(8));

if (self::supportsDbase()) {
$this->dbfHeader = $this->loadDBFHeader();
}
$this->dbfHeader = $this->loadDBFHeader();

return true;
}
Expand Down Expand Up @@ -620,6 +634,10 @@ private function openDBFFile(): bool

$dbfName = $this->getFilename('.dbf');
if (! is_readable($dbfName)) {
if ($this->allowNoDbf) {
return true;
}

$this->setError(sprintf('It wasn\'t possible to find the DBase file "%s"', $dbfName));

return false;
Expand Down
11 changes: 11 additions & 0 deletions tests/ShapeFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,4 +548,15 @@ public function testSearch(): void
$shp->getIndexFromDBFData('CNTRY_NAME', 'Czech Republic')
);
}

public function testAllowsNoDbf(): void
{
if (! ShapeFile::supportsDbase()) {
self::markTestSkipped();
}

$shp = new ShapeFile(0);
$shp->setAllowNoDbf(true);
self::assertTrue($shp->loadFromFile('data/no-dbf.*'));
}
}
Loading