Skip to content
Merged
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
6 changes: 3 additions & 3 deletions examples/read_point.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
$shp->loadFromFile('../data/capitals.*');

$i = 1;
foreach ($shp->records as $record){
foreach ($shp->records as $record) {
echo "<pre>";
echo "Record No. $i:\n\n\n";
echo "SHP Data = ";
print_r($record->SHPData); //All the data related to the point
print_r($record->SHPData); //All the data related to the point
print_r("\n\n\n");
echo "DBF Data = ";
print_r($record->DBFData); //All the information related to each point
print_r($record->DBFData); //All the information related to each point
print_r("\n\n\n");
echo "</pre>";
$i++;
Expand Down
6 changes: 3 additions & 3 deletions examples/read_polygon.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
$shp->loadFromFile('../data/mexico.*');

$i = 1;
foreach ($shp->records as $record){
foreach ($shp->records as $record) {
echo "<pre>";
echo "Record No. $i:\n\n\n";
echo "SHP Data = ";
print_r($record->SHPData); //All the data related to the poligon
print_r($record->SHPData); //All the data related to the poligon
print_r("\n\n\n");
echo "DBF Data = ";
print_r($record->DBFData); //All the information related to each poligon
print_r($record->DBFData); //All the information related to each poligon
print_r("\n\n\n");
echo "</pre>";
$i++;
Expand Down
38 changes: 19 additions & 19 deletions src/ShapeFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,34 +97,34 @@ public function addRecord($record) {
$this->records[] = $record;
$this->records[count($this->records) - 1]->recordNumber = count($this->records);

if ($this->boundingBox["xmin"]==0.0 || ($this->boundingBox["xmin"]>$record->SHPData["xmin"])) {
if ($this->boundingBox["xmin"] == 0.0 || ($this->boundingBox["xmin"] > $record->SHPData["xmin"])) {
$this->boundingBox["xmin"] = $record->SHPData["xmin"];
}
if ($this->boundingBox["xmax"]==0.0 || ($this->boundingBox["xmax"]<$record->SHPData["xmax"])) {
if ($this->boundingBox["xmax"] == 0.0 || ($this->boundingBox["xmax"] < $record->SHPData["xmax"])) {
$this->boundingBox["xmax"] = $record->SHPData["xmax"];
}

if ($this->boundingBox["ymin"]==0.0 || ($this->boundingBox["ymin"]>$record->SHPData["ymin"])) {
if ($this->boundingBox["ymin"] == 0.0 || ($this->boundingBox["ymin"] > $record->SHPData["ymin"])) {
$this->boundingBox["ymin"] = $record->SHPData["ymin"];
}
if ($this->boundingBox["ymax"]==0.0 || ($this->boundingBox["ymax"]<$record->SHPData["ymax"])) {
if ($this->boundingBox["ymax"] == 0.0 || ($this->boundingBox["ymax"] < $record->SHPData["ymax"])) {
$this->boundingBox["ymax"] = $record->SHPData["ymax"];
}

if (in_array($this->shapeType,array(11,13,15,18,21,23,25,28))) {
if (!isset($this->boundingBox["mmin"]) || $this->boundingBox["mmin"]==0.0 || ($this->boundingBox["mmin"]>$record->SHPData["mmin"])) {
if (in_array($this->shapeType, array(11, 13, 15, 18, 21, 23, 25, 28))) {
if (!isset($this->boundingBox["mmin"]) || $this->boundingBox["mmin"] == 0.0 || ($this->boundingBox["mmin"] > $record->SHPData["mmin"])) {
$this->boundingBox["mmin"] = $record->SHPData["mmin"];
}
if (!isset($this->boundingBox["mmax"]) || $this->boundingBox["mmax"]==0.0 || ($this->boundingBox["mmax"]<$record->SHPData["mmax"])) {
if (!isset($this->boundingBox["mmax"]) || $this->boundingBox["mmax"] == 0.0 || ($this->boundingBox["mmax"] < $record->SHPData["mmax"])) {
$this->boundingBox["mmax"] = $record->SHPData["mmax"];
}
}

if (in_array($this->shapeType,array(11,13,15,18))) {
if (!isset($this->boundingBox["zmin"]) || $this->boundingBox["zmin"]==0.0 || ($this->boundingBox["zmin"]>$record->SHPData["zmin"])) {
if (in_array($this->shapeType, array(11, 13, 15, 18))) {
if (!isset($this->boundingBox["zmin"]) || $this->boundingBox["zmin"] == 0.0 || ($this->boundingBox["zmin"] > $record->SHPData["zmin"])) {
$this->boundingBox["zmin"] = $record->SHPData["zmin"];
}
if (!isset($this->boundingBox["zmax"]) || $this->boundingBox["zmax"]==0.0 || ($this->boundingBox["zmax"]<$record->SHPData["zmax"])) {
if (!isset($this->boundingBox["zmax"]) || $this->boundingBox["zmax"] == 0.0 || ($this->boundingBox["zmax"] < $record->SHPData["zmax"])) {
$this->boundingBox["zmax"] = $record->SHPData["zmax"];
}
}
Expand Down Expand Up @@ -237,23 +237,23 @@ private function _saveHeaders() {
fwrite($this->SHPFile, Util::packDouble($this->boundingBox['ymin']));
fwrite($this->SHPFile, Util::packDouble($this->boundingBox['xmax']));
fwrite($this->SHPFile, Util::packDouble($this->boundingBox['ymax']));
fwrite($this->SHPFile, Util::packDouble(isset($this->boundingBox['zmin'])?$this->boundingBox['zmin']:0));
fwrite($this->SHPFile, Util::packDouble(isset($this->boundingBox['zmax'])?$this->boundingBox['zmax']:0));
fwrite($this->SHPFile, Util::packDouble(isset($this->boundingBox['mmin'])?$this->boundingBox['mmin']:0));
fwrite($this->SHPFile, Util::packDouble(isset($this->boundingBox['mmax'])?$this->boundingBox['mmax']:0));
fwrite($this->SHPFile, Util::packDouble(isset($this->boundingBox['zmin']) ? $this->boundingBox['zmin'] : 0));
fwrite($this->SHPFile, Util::packDouble(isset($this->boundingBox['zmax']) ? $this->boundingBox['zmax'] : 0));
fwrite($this->SHPFile, Util::packDouble(isset($this->boundingBox['mmin']) ? $this->boundingBox['mmin'] : 0));
fwrite($this->SHPFile, Util::packDouble(isset($this->boundingBox['mmax']) ? $this->boundingBox['mmax'] : 0));

fwrite($this->SHXFile, pack("NNNNNN", 9994, 0, 0, 0, 0, 0));
fwrite($this->SHXFile, pack("N", 50 + 4*count($this->records)));
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, Util::packDouble($this->boundingBox['xmin']));
fwrite($this->SHXFile, Util::packDouble($this->boundingBox['ymin']));
fwrite($this->SHXFile, Util::packDouble($this->boundingBox['xmax']));
fwrite($this->SHXFile, Util::packDouble($this->boundingBox['ymax']));
fwrite($this->SHXFile, Util::packDouble(isset($this->boundingBox['zmin'])?$this->boundingBox['zmin']:0));
fwrite($this->SHXFile, Util::packDouble(isset($this->boundingBox['zmax'])?$this->boundingBox['zmax']:0));
fwrite($this->SHXFile, Util::packDouble(isset($this->boundingBox['mmin'])?$this->boundingBox['mmin']:0));
fwrite($this->SHXFile, Util::packDouble(isset($this->boundingBox['mmax'])?$this->boundingBox['mmax']:0));
fwrite($this->SHXFile, Util::packDouble(isset($this->boundingBox['zmin']) ? $this->boundingBox['zmin'] : 0));
fwrite($this->SHXFile, Util::packDouble(isset($this->boundingBox['zmax']) ? $this->boundingBox['zmax'] : 0));
fwrite($this->SHXFile, Util::packDouble(isset($this->boundingBox['mmin']) ? $this->boundingBox['mmin'] : 0));
fwrite($this->SHXFile, Util::packDouble(isset($this->boundingBox['mmax']) ? $this->boundingBox['mmax'] : 0));
}

private function _loadRecords() {
Expand Down
59 changes: 31 additions & 28 deletions src/ShapeRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class ShapeRecord {
public $SHPData = array();
public $DBFData = array();

/**
* @param integer $shapeType
*/
public function __construct($shapeType) {
$this->shapeType = $shapeType;
}
Expand Down Expand Up @@ -275,7 +278,7 @@ private function _loadMultiPointRecord() {
}
}

private function _loadMultiPointMZRecord( $type ) {
private function _loadMultiPointMZRecord($type) {

$this->SHPData[$type."min"] = Util::loadData("d", fread($this->SHPFile, 8));
$this->SHPData[$type."max"] = Util::loadData("d", fread($this->SHPFile, 8));
Expand Down Expand Up @@ -308,7 +311,7 @@ private function _saveMultiPointRecord() {
}
}

private function _saveMultiPointMZRecord( $type ) {
private function _saveMultiPointMZRecord($type) {

fwrite($this->SHPFile, pack("dd", $this->SHPData[$type."min"], $this->SHPData[$type."max"]));

Expand Down Expand Up @@ -358,10 +361,10 @@ private function _loadPolyLineRecord() {
}
}

fseek($this->SHPFile, $firstIndex + ($readPoints*16));
fseek($this->SHPFile, $firstIndex + ($readPoints * 16));
}

private function _loadPolyLineMZRecord( $type ) {
private function _loadPolyLineMZRecord($type) {

$this->SHPData[$type."min"] = Util::loadData("d", fread($this->SHPFile, 8));
$this->SHPData[$type."max"] = Util::loadData("d", fread($this->SHPFile, 8));
Expand All @@ -376,7 +379,7 @@ private function _loadPolyLineMZRecord( $type ) {
}
}

fseek($this->SHPFile, $firstIndex + ($readPoints*24));
fseek($this->SHPFile, $firstIndex + ($readPoints * 24));
}

private function _loadPolyLineMRecord() {
Expand All @@ -398,21 +401,21 @@ private function _savePolyLineRecord() {
fwrite($this->SHPFile, pack("VV", $this->SHPData["numparts"], $this->SHPData["numpoints"]));

for ($i = 0; $i < $this->SHPData["numparts"]; $i++) {
fwrite($this->SHPFile, pack("V", count($this->SHPData["parts"][$i])-1));
fwrite($this->SHPFile, pack("V", count($this->SHPData["parts"][$i]) - 1));
}

foreach ($this->SHPData["parts"] as $partData){
foreach ($this->SHPData["parts"] as $partData) {
reset($partData["points"]);
while (list($pointIndex, $pointData) = each($partData["points"])) {
$this->_savePoint($pointData);
}
}
}

private function _savePolyLineMZRecord( $type ) {
private function _savePolyLineMZRecord($type) {
fwrite($this->SHPFile, pack("dd", $this->SHPData[$type."min"], $this->SHPData[$type."max"]));

foreach ($this->SHPData["parts"] as $partData){
foreach ($this->SHPData["parts"] as $partData) {
reset($partData["points"]);
while (list($pointIndex, $pointData) = each($partData["points"])) {
fwrite($this->SHPFile, Util::packDouble($pointData[$type]));
Expand Down Expand Up @@ -465,10 +468,10 @@ public function addPoint($point, $partIndex = 0) {
case 1:
case 11:
case 21:
if (in_array($this->shapeType,array(11,21)) && !isset($point["m"])) {
if (in_array($this->shapeType, array(11, 21)) && !isset($point["m"])) {
$point["m"] = 0.0; // no_value
}
if (in_array($this->shapeType,array(11)) && !isset($point["z"])) {
if (in_array($this->shapeType, array(11)) && !isset($point["z"])) {
$point["z"] = 0.0; // no_value
}
//Substitutes the value of the current point
Expand All @@ -480,10 +483,10 @@ public function addPoint($point, $partIndex = 0) {
case 15:
case 23:
case 25:
if (in_array($this->shapeType,array(13,15,23,25)) && !isset($point["m"])) {
if (in_array($this->shapeType, array(13, 15, 23, 25)) && !isset($point["m"])) {
$point["m"] = 0.0; // no_value
}
if (in_array($this->shapeType,array(13,15)) && !isset($point["z"])) {
if (in_array($this->shapeType, array(13, 15)) && !isset($point["z"])) {
$point["z"] = 0.0; // no_value
}

Expand Down Expand Up @@ -516,15 +519,15 @@ public function addPoint($point, $partIndex = 0) {
$this->SHPData["parts"][$partIndex]["points"][] = $point;

$this->SHPData["numparts"] = count($this->SHPData["parts"]);
$this->SHPData["numpoints"] = 1 + (isset($this->SHPData["numpoints"])?$this->SHPData["numpoints"]:0);
$this->SHPData["numpoints"] = 1 + (isset($this->SHPData["numpoints"]) ? $this->SHPData["numpoints"] : 0);
break;
case 8:
case 18:
case 28:
if (in_array($this->shapeType,array(18,28)) && !isset($point["m"])) {
if (in_array($this->shapeType, array(18, 28)) && !isset($point["m"])) {
$point["m"] = 0.0; // no_value
}
if (in_array($this->shapeType,array(18)) && !isset($point["z"])) {
if (in_array($this->shapeType, array(18)) && !isset($point["z"])) {
$point["z"] = 0.0; // no_value
}

Expand Down Expand Up @@ -555,7 +558,7 @@ public function addPoint($point, $partIndex = 0) {
}

$this->SHPData["points"][] = $point;
$this->SHPData["numpoints"] = 1 + (isset($this->SHPData["numpoints"])?$this->SHPData["numpoints"]:0);
$this->SHPData["numpoints"] = 1 + (isset($this->SHPData["numpoints"]) ? $this->SHPData["numpoints"] : 0);
break;
default:
$this->setError(sprintf("The Shape Type '%s' is not supported.", $this->shapeType));
Expand All @@ -574,10 +577,10 @@ public function deletePoint($pointIndex = 0, $partIndex = 0) {
//Sets the value of the point to zero
$this->SHPData["x"] = 0.0;
$this->SHPData["y"] = 0.0;
if (in_array($this->shapeType,array(11,21))) {
if (in_array($this->shapeType, array(11, 21))) {
$this->SHPData["m"] = 0.0;
}
if (in_array($this->shapeType,array(11))) {
if (in_array($this->shapeType, array(11))) {
$this->SHPData["z"] = 0.0;
}
break;
Expand Down Expand Up @@ -635,33 +638,33 @@ public function getContentLength() {
break;
case 3:
case 5:
$result = 22 + 2*count($this->SHPData["parts"]);
$result = 22 + 2 * count($this->SHPData["parts"]);
for ($i = 0; $i < count($this->SHPData["parts"]); $i++) {
$result += 8*count($this->SHPData["parts"][$i]["points"]);
$result += 8 * count($this->SHPData["parts"][$i]["points"]);
}
break;
case 23:
case 25:
$result = 22 + (2*4) + 2*count($this->SHPData["parts"]);
$result = 22 + (2 * 4) + 2 * count($this->SHPData["parts"]);
for ($i = 0; $i < count($this->SHPData["parts"]); $i++) {
$result += (8+4)*count($this->SHPData["parts"][$i]["points"]);
$result += (8 + 4) * count($this->SHPData["parts"][$i]["points"]);
}
break;
case 13:
case 15:
$result = 22 + (4*4) + 2*count($this->SHPData["parts"]);
$result = 22 + (4 * 4) + 2 * count($this->SHPData["parts"]);
for ($i = 0; $i < count($this->SHPData["parts"]); $i++) {
$result += (8+8)*count($this->SHPData["parts"][$i]["points"]);
$result += (8 + 8) * count($this->SHPData["parts"][$i]["points"]);
}
break;
case 8:
$result = 20 + 8*count($this->SHPData["points"]);
$result = 20 + 8 * count($this->SHPData["points"]);
break;
case 28:
$result = 20 + (2*4) + (8+4)*count($this->SHPData["points"]);
$result = 20 + (2 * 4) + (8 + 4) * count($this->SHPData["points"]);
break;
case 18:
$result = 20 + (4*4) + (8+8)*count($this->SHPData["points"]);
$result = 20 + (4 * 4) + (8 + 8) * count($this->SHPData["points"]);
break;
default:
$result = false;
Expand Down
14 changes: 8 additions & 6 deletions src/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,32 @@

class Util {
public static function loadData($type, $data) {
if (!$data) return $data;
if (!$data) {
return $data;
}
$tmp = unpack($type, $data);
return current($tmp);
}

public static function swap($binValue) {
$result = $binValue{strlen($binValue) - 1};
for($i = strlen($binValue) - 2; $i >= 0 ; $i--) {
for ($i = strlen($binValue) - 2; $i >= 0; $i--) {
$result .= $binValue{$i};
}

return $result;
}

public static function packDouble($value, $mode = 'LE') {
$value = (double)$value;
$value = (double) $value;
$bin = pack("d", $value);

//We test if the conversion of an integer (1) is done as LE or BE by default
switch (pack ('L', 1)) {
case pack ('V', 1): //Little Endian
switch (pack('L', 1)) {
case pack('V', 1): //Little Endian
$result = ($mode == 'LE') ? $bin : self::swap($bin);
break;
case pack ('N', 1): //Big Endian
case pack('N', 1): //Big Endian
$result = ($mode == 'BE') ? $bin : self::swap($bin);
break;
default: //Some other thing, we just return false
Expand Down