Skip to content

Commit

Permalink
Merge pull request #34 from JoolsMcFly/chore/ShapeType-to-Enum
Browse files Browse the repository at this point in the history
Fix #29 - Turn ShapeType into an enum
  • Loading branch information
MauricioFauth authored Oct 10, 2024
2 parents f825d8f + d302fee commit d8550ba
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 168 deletions.
8 changes: 4 additions & 4 deletions examples/create_shapefile.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,26 @@

require_once __DIR__ . '/../vendor/autoload.php';

$shp = new ShapeFile(ShapeType::POINT, [
$shp = new ShapeFile(ShapeType::Point, [
'xmin' => 464079.002268,
'ymin' => 2120153.74792,
'xmax' => 505213.52849,
'ymax' => 2163205.70036,
]);

$record0 = new ShapeRecord(ShapeType::POINT);
$record0 = new ShapeRecord(ShapeType::Point);
$record0->addPoint([
'x' => 482131.764567,
'y' => 2143634.39608,
]);

$record1 = new ShapeRecord(ShapeType::POINT);
$record1 = new ShapeRecord(ShapeType::Point);
$record1->addPoint([
'x' => 472131.764567,
'y' => 2143634.39608,
]);

$record2 = new ShapeRecord(ShapeType::POINT);
$record2 = new ShapeRecord(ShapeType::Point);
$record2->addPoint([
'x' => 492131.764567,
'y' => 2143634.39608,
Expand Down
2 changes: 1 addition & 1 deletion examples/read.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
// phpcs:ignore Squiz.Functions.GlobalFunction.Found
function display_file(string $filename): void
{
$shp = new ShapeFile(ShapeType::POINT);
$shp = new ShapeFile(ShapeType::Point);
$shp->loadFromFile($filename);

$i = 1;
Expand Down
3 changes: 1 addition & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ parameters:

-
message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertEquals\\(\\)\\.$#"
count: 17
count: 18
path: tests/ShapeFileTest.php

-
Expand Down Expand Up @@ -159,4 +159,3 @@ parameters:
message: "#^Dynamic call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertEquals\\(\\)\\.$#"
count: 1
path: tests/UtilTest.php

18 changes: 11 additions & 7 deletions src/ShapeFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ public static function supportsDbase(): bool
}

/**
* @param int $shapeType File shape type, should be same as all records
* @param ShapeType $shapeType File shape type, should be same as all records
* @param mixed[] $boundingBox File bounding box
* @param string|null $fileName File name
*/
public function __construct(
public int $shapeType,
public ShapeType $shapeType,
public array $boundingBox = [
'xmin' => 0.0,
'ymin' => 0.0,
Expand Down Expand Up @@ -374,7 +374,11 @@ private function loadHeaders(): bool
$this->readSHP(4);

$shapeType = Util::loadData('V', $this->readSHP(4));
$this->shapeType = $shapeType === false ? -1 : (int) $shapeType;
if ($shapeType === false) {
$this->shapeType = ShapeType::Unknown;
} else {
$this->shapeType = ShapeType::tryFrom((int) $shapeType) ?? ShapeType::Unknown;
}

$this->boundingBox = [];
$this->boundingBox['xmin'] = Util::loadData('d', $this->readSHP(8));
Expand Down Expand Up @@ -435,7 +439,7 @@ private function saveHeaders(): void
fwrite($this->shpFile, pack('NNNNNN', self::MAGIC, 0, 0, 0, 0, 0));
fwrite($this->shpFile, pack('N', $this->fileLength));
fwrite($this->shpFile, pack('V', 1000));
fwrite($this->shpFile, pack('V', $this->shapeType));
fwrite($this->shpFile, pack('V', $this->shapeType->value));
$this->saveBBox($this->shpFile);

if ($this->shxFile === false) {
Expand All @@ -445,7 +449,7 @@ private function saveHeaders(): void
fwrite($this->shxFile, pack('NNNNNN', self::MAGIC, 0, 0, 0, 0, 0));
fwrite($this->shxFile, pack('N', 50 + 4 * count($this->records)));
fwrite($this->shxFile, pack('V', 1000));
fwrite($this->shxFile, pack('V', $this->shapeType));
fwrite($this->shxFile, pack('V', $this->shapeType->value));
$this->saveBBox($this->shxFile);
}

Expand All @@ -456,15 +460,15 @@ private function loadRecords(): bool
{
/* Need to start at offset 100 */
while (! $this->eofSHP()) {
$record = new ShapeRecord(-1);
$record = new ShapeRecord(ShapeType::Unknown);
$record->loadFromFile($this, $this->dbfFile);
if ($record->lastError !== '') {
$this->setError($record->lastError);

return false;
}

if (($record->shapeType === -1) && $this->eofSHP()) {
if (($record->shapeType === ShapeType::Unknown) && $this->eofSHP()) {
break;
}

Expand Down
Loading

0 comments on commit d8550ba

Please sign in to comment.