Skip to content

Commit

Permalink
Merge pull request #26 from MauricioFauth/shape-type
Browse files Browse the repository at this point in the history
Create the `PhpMyAdmin\ShapeFile\ShapeType` class
  • Loading branch information
MauricioFauth committed Sep 14, 2023
2 parents ab4a803 + 55cc5e4 commit f7d512e
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 148 deletions.
9 changes: 5 additions & 4 deletions examples/create_shapefile.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,30 @@

use PhpMyAdmin\ShapeFile\ShapeFile;
use PhpMyAdmin\ShapeFile\ShapeRecord;
use PhpMyAdmin\ShapeFile\ShapeType;

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

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

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

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

$record2 = new ShapeRecord(1);
$record2 = new ShapeRecord(ShapeType::POINT);
$record2->addPoint([
'x' => 492131.764567,
'y' => 2143634.39608,
Expand Down
3 changes: 2 additions & 1 deletion examples/read.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/

use PhpMyAdmin\ShapeFile\ShapeFile;
use PhpMyAdmin\ShapeFile\ShapeType;

/**
* Displays content of given file.
Expand All @@ -33,7 +34,7 @@
// phpcs:ignore Squiz.Functions.GlobalFunction.Found
function display_file(string $filename): void
{
$shp = new ShapeFile(1);
$shp = new ShapeFile(ShapeType::POINT);
$shp->loadFromFile($filename);

$i = 1;
Expand Down
10 changes: 0 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
parameters:
ignoreErrors:
-
message: "#^Call to function in_array\\(\\) requires parameter \\#3 to be set\\.$#"
count: 2
path: src/ShapeFile.php

-
message: "#^Cannot cast mixed to int\\.$#"
count: 1
Expand Down Expand Up @@ -55,11 +50,6 @@ parameters:
count: 4
path: src/ShapeRecord.php

-
message: "#^Call to function in_array\\(\\) requires parameter \\#3 to be set\\.$#"
count: 1
path: src/ShapeRecord.php

-
message: "#^Cannot access an offset on mixed\\.$#"
count: 4
Expand Down
1 change: 0 additions & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@
<code>$pointData</code>
<code>$pointData</code>
<code><![CDATA[$point['m']]]></code>
<code><![CDATA[$point['m']]]></code>
<code><![CDATA[$point['z']]]></code>
<code>$recordNumber</code>
<code>$shapeType</code>
Expand Down
8 changes: 5 additions & 3 deletions src/ShapeFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,11 @@ public function addRecord(ShapeRecord $record): int
$this->updateBBox('x', $record->shpData);
$this->updateBBox('y', $record->shpData);

if (in_array($this->shapeType, [11, 13, 15, 18, 21, 23, 25, 28])) {
if (in_array($this->shapeType, ShapeType::MEASURED_TYPES, true)) {
$this->updateBBox('m', $record->shpData);
}

if (in_array($this->shapeType, [11, 13, 15, 18])) {
if (in_array($this->shapeType, ShapeType::TYPES_WITH_Z, true)) {
$this->updateBBox('z', $record->shpData);
}

Expand Down Expand Up @@ -669,10 +669,12 @@ public function eofSHP(): bool

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

/**
Expand Down
146 changes: 74 additions & 72 deletions src/ShapeRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,19 @@ public function loadFromFile(ShapeFile $shapeFile, $dbfFile): void
}

match ($this->shapeType) {
0 => $this->loadNullRecord(),
1 => $this->loadPointRecord(),
21 => $this->loadPointMRecord(),
11 => $this->loadPointZRecord(),
3 => $this->loadPolyLineRecord(),
23 => $this->loadPolyLineMRecord(),
13 => $this->loadPolyLineZRecord(),
5 => $this->loadPolygonRecord(),
25 => $this->loadPolygonMRecord(),
15 => $this->loadPolygonZRecord(),
8 => $this->loadMultiPointRecord(),
28 => $this->loadMultiPointMRecord(),
18 => $this->loadMultiPointZRecord(),
ShapeType::NULL => $this->loadNullRecord(),
ShapeType::POINT => $this->loadPointRecord(),
ShapeType::POINT_M => $this->loadPointMRecord(),
ShapeType::POINT_Z => $this->loadPointZRecord(),
ShapeType::POLY_LINE => $this->loadPolyLineRecord(),
ShapeType::POLY_LINE_M => $this->loadPolyLineMRecord(),
ShapeType::POLY_LINE_Z => $this->loadPolyLineZRecord(),
ShapeType::POLYGON => $this->loadPolygonRecord(),
ShapeType::POLYGON_M => $this->loadPolygonMRecord(),
ShapeType::POLYGON_Z => $this->loadPolygonZRecord(),
ShapeType::MULTI_POINT => $this->loadMultiPointRecord(),
ShapeType::MULTI_POINT_M => $this->loadMultiPointMRecord(),
ShapeType::MULTI_POINT_Z => $this->loadMultiPointZRecord(),
default => $this->setError(sprintf('The Shape Type "%s" is not supported.', $this->shapeType)),
};

Expand Down Expand Up @@ -126,19 +126,19 @@ public function saveToFile($shpFile, $dbfFile, int $recordNumber): void
$this->saveHeaders();

match ($this->shapeType) {
0 => null, // Nothing to save
1 => $this->savePointRecord(),
21 => $this->savePointMRecord(),
11 => $this->savePointZRecord(),
3 => $this->savePolyLineRecord(),
23 => $this->savePolyLineMRecord(),
13 => $this->savePolyLineZRecord(),
5 => $this->savePolygonRecord(),
25 => $this->savePolygonMRecord(),
15 => $this->savePolygonZRecord(),
8 => $this->saveMultiPointRecord(),
28 => $this->saveMultiPointMRecord(),
18 => $this->saveMultiPointZRecord(),
ShapeType::NULL => null, // Nothing to save
ShapeType::POINT => $this->savePointRecord(),
ShapeType::POINT_M => $this->savePointMRecord(),
ShapeType::POINT_Z => $this->savePointZRecord(),
ShapeType::POLY_LINE => $this->savePolyLineRecord(),
ShapeType::POLY_LINE_M => $this->savePolyLineMRecord(),
ShapeType::POLY_LINE_Z => $this->savePolyLineZRecord(),
ShapeType::POLYGON => $this->savePolygonRecord(),
ShapeType::POLYGON_M => $this->savePolygonMRecord(),
ShapeType::POLYGON_Z => $this->savePolygonZRecord(),
ShapeType::MULTI_POINT => $this->saveMultiPointRecord(),
ShapeType::MULTI_POINT_M => $this->saveMultiPointMRecord(),
ShapeType::MULTI_POINT_Z => $this->saveMultiPointZRecord(),
default => $this->setError(sprintf('The Shape Type "%s" is not supported.', $this->shapeType)),
};

Expand Down Expand Up @@ -584,12 +584,12 @@ private function adjustBBox(array $point): void
*/
private function adjustPoint(array $point): array
{
$type = $this->shapeType / 10;
if ($type >= 2) {
if (in_array($this->shapeType, ShapeType::MEASURED_TYPES, true)) {
$point['m'] ??= 0.0;
} elseif ($type >= 1) {
}

if (in_array($this->shapeType, ShapeType::TYPES_WITH_Z, true)) {
$point['z'] ??= 0.0;
$point['m'] ??= 0.0;
}

return $point;
Expand All @@ -605,30 +605,30 @@ public function addPoint(array $point, int $partIndex = 0): void
{
$point = $this->adjustPoint($point);
switch ($this->shapeType) {
case 0:
case ShapeType::NULL:
//Don't add anything
return;

case 1:
case 11:
case 21:
case ShapeType::POINT:
case ShapeType::POINT_Z:
case ShapeType::POINT_M:
//Substitutes the value of the current point
$this->shpData = $point;
break;
case 3:
case 5:
case 13:
case 15:
case 23:
case 25:
case ShapeType::POLY_LINE:
case ShapeType::POLYGON:
case ShapeType::POLY_LINE_Z:
case ShapeType::POLYGON_Z:
case ShapeType::POLY_LINE_M:
case ShapeType::POLYGON_M:
//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 + ($this->shpData['numpoints'] ?? 0);
break;
case 8:
case 18:
case 28:
case ShapeType::MULTI_POINT:
case ShapeType::MULTI_POINT_Z:
case ShapeType::MULTI_POINT_M:
//Adds a new point
$this->shpData['points'][] = $point;
$this->shpData['numpoints'] = 1 + ($this->shpData['numpoints'] ?? 0);
Expand All @@ -651,30 +651,30 @@ public function addPoint(array $point, int $partIndex = 0): void
public function deletePoint(int $pointIndex = 0, int $partIndex = 0): void
{
switch ($this->shapeType) {
case 0:
case ShapeType::NULL:
//Don't delete anything
break;
case 1:
case 11:
case 21:
case ShapeType::POINT:
case ShapeType::POINT_Z:
case ShapeType::POINT_M:
//Sets the value of the point to zero
$this->shpData['x'] = 0.0;
$this->shpData['y'] = 0.0;
if (in_array($this->shapeType, [11, 21])) {
if (in_array($this->shapeType, [ShapeType::POINT_Z, ShapeType::POINT_M], true)) {
$this->shpData['m'] = 0.0;
}

if ($this->shapeType === 11) {
if ($this->shapeType === ShapeType::POINT_Z) {
$this->shpData['z'] = 0.0;
}

break;
case 3:
case 5:
case 13:
case 15:
case 23:
case 25:
case ShapeType::POLY_LINE:
case ShapeType::POLYGON:
case ShapeType::POLY_LINE_Z:
case ShapeType::POLYGON_Z:
case ShapeType::POLY_LINE_M:
case ShapeType::POLYGON_M:
//Deletes the point from the selected part, if exists
if (
isset($this->shpData['parts'][$partIndex])
Expand All @@ -694,9 +694,9 @@ public function deletePoint(int $pointIndex = 0, int $partIndex = 0): void
}

break;
case 8:
case 18:
case 28:
case ShapeType::MULTI_POINT:
case ShapeType::MULTI_POINT_Z:
case ShapeType::MULTI_POINT_M:
//Deletes the point, if exists
if (isset($this->shpData['points'][$pointIndex])) {
$count = count($this->shpData['points']) - 1;
Expand Down Expand Up @@ -724,52 +724,52 @@ public function getContentLength(): int|null
// 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:
case ShapeType::NULL:
$result = 0;
break;
case 1:
case ShapeType::POINT:
$result = 10;
break;
case 21:
case ShapeType::POINT_M:
$result = 10 + 4;
break;
case 11:
case ShapeType::POINT_Z:
$result = 10 + 8;
break;
case 3:
case 5:
case ShapeType::POLY_LINE:
case ShapeType::POLYGON:
$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:
case ShapeType::POLY_LINE_M:
case ShapeType::POLYGON_M:
$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:
case ShapeType::POLY_LINE_Z:
case ShapeType::POLYGON_Z:
$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:
case ShapeType::MULTI_POINT:
$result = 20 + 8 * count($this->shpData['points']);
break;
case 28:
case ShapeType::MULTI_POINT_M:
$result = 20 + (2 * 4) + (8 + 4) * count($this->shpData['points']);
break;
case 18:
case ShapeType::MULTI_POINT_Z:
$result = 20 + (4 * 4) + (8 + 8) * count($this->shpData['points']);
break;
default:
Expand Down Expand Up @@ -815,9 +815,11 @@ public function setError(string $error): void

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

0 comments on commit f7d512e

Please sign in to comment.