Skip to content

Commit a21b619

Browse files
authored
Adds a toggle to allow shape files with no dbf (#41)
Adds a toggle to allow shape files with no dbf Fixes #36 Closes #38
1 parent 20cad01 commit a21b619

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

Diff for: data/no-dbf.shp

50.7 KB
Binary file not shown.

Diff for: data/no-dbf.shx

356 Bytes
Binary file not shown.

Diff for: src/ShapeFile.php

+22-4
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ class ShapeFile
8787
/** @var array */
8888
public $records = [];
8989

90+
/** @var bool */
91+
private $allowNoDbf = false;
92+
9093
/**
9194
* Checks whether dbase manipulations are supported.
9295
*/
@@ -121,6 +124,11 @@ public function __construct(
121124
$this->fileLength = 50;
122125
}
123126

127+
public function setAllowNoDbf(bool $allowNoDbf): void
128+
{
129+
$this->allowNoDbf = $allowNoDbf;
130+
}
131+
124132
/**
125133
* Loads shapefile and dbase (if supported).
126134
*
@@ -326,8 +334,16 @@ public function getIndexFromDBFData(string $field, $value): int
326334
*/
327335
private function loadDBFHeader(): array
328336
{
329-
$DBFFile = fopen($this->getFilename('.dbf'), 'r');
337+
if (! self::supportsDbase()) {
338+
return [];
339+
}
330340

341+
$dbfName = $this->getFilename('.dbf');
342+
if (! file_exists($dbfName)) {
343+
return [];
344+
}
345+
346+
$DBFFile = fopen($dbfName, 'r');
331347
$result = [];
332348
$i = 1;
333349
$inHeader = true;
@@ -405,9 +421,7 @@ private function loadHeaders(): bool
405421
$this->boundingBox['mmin'] = Util::loadData('d', $this->readSHP(8));
406422
$this->boundingBox['mmax'] = Util::loadData('d', $this->readSHP(8));
407423

408-
if (self::supportsDbase()) {
409-
$this->dbfHeader = $this->loadDBFHeader();
410-
}
424+
$this->dbfHeader = $this->loadDBFHeader();
411425

412426
return true;
413427
}
@@ -620,6 +634,10 @@ private function openDBFFile(): bool
620634

621635
$dbfName = $this->getFilename('.dbf');
622636
if (! is_readable($dbfName)) {
637+
if ($this->allowNoDbf) {
638+
return true;
639+
}
640+
623641
$this->setError(sprintf('It wasn\'t possible to find the DBase file "%s"', $dbfName));
624642

625643
return false;

Diff for: tests/ShapeFileTest.php

+11
Original file line numberDiff line numberDiff line change
@@ -548,4 +548,15 @@ public function testSearch(): void
548548
$shp->getIndexFromDBFData('CNTRY_NAME', 'Czech Republic')
549549
);
550550
}
551+
552+
public function testAllowsNoDbf(): void
553+
{
554+
if (! ShapeFile::supportsDbase()) {
555+
self::markTestSkipped();
556+
}
557+
558+
$shp = new ShapeFile(0);
559+
$shp->setAllowNoDbf(true);
560+
self::assertTrue($shp->loadFromFile('data/no-dbf.*'));
561+
}
551562
}

0 commit comments

Comments
 (0)