Initial commit
This commit is contained in:
647
pma/vendor/phpmyadmin/shapefile/src/ShapeFile.php
vendored
Normal file
647
pma/vendor/phpmyadmin/shapefile/src/ShapeFile.php
vendored
Normal file
@ -0,0 +1,647 @@
|
||||
<?php
|
||||
/**
|
||||
* phpMyAdmin ShapeFile library
|
||||
* <https://github.com/phpmyadmin/shapefile/>.
|
||||
*
|
||||
* Copyright 2006-2007 Ovidio <ovidio AT users.sourceforge.net>
|
||||
* Copyright 2016 - 2017 Michal Čihař <michal@cihar.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, you can download one from
|
||||
* https://www.gnu.org/copyleft/gpl.html.
|
||||
*/
|
||||
|
||||
namespace PhpMyAdmin\ShapeFile;
|
||||
|
||||
/**
|
||||
* ShapeFile class.
|
||||
*/
|
||||
class ShapeFile
|
||||
{
|
||||
const MAGIC = 0x270a;
|
||||
|
||||
public $FileName;
|
||||
|
||||
private $SHPFile = null;
|
||||
private $SHXFile = null;
|
||||
private $DBFFile = null;
|
||||
|
||||
private $DBFHeader;
|
||||
|
||||
public $lastError = '';
|
||||
|
||||
public $boundingBox = array('xmin' => 0.0, 'ymin' => 0.0, 'xmax' => 0.0, 'ymax' => 0.0);
|
||||
private $fileLength = 0;
|
||||
public $shapeType = 0;
|
||||
|
||||
public $records = array();
|
||||
|
||||
/**
|
||||
* Checks whether dbase manipuations are supported.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function supports_dbase()
|
||||
{
|
||||
return extension_loaded('dbase');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $shapeType File shape type, should be same as all records
|
||||
* @param array $boundingBox File bounding box
|
||||
* @param null|mixed $FileName File name
|
||||
*/
|
||||
public function __construct($shapeType, $boundingBox = array('xmin' => 0.0, 'ymin' => 0.0, 'xmax' => 0.0, 'ymax' => 0.0), $FileName = null)
|
||||
{
|
||||
$this->shapeType = $shapeType;
|
||||
$this->boundingBox = $boundingBox;
|
||||
$this->FileName = $FileName;
|
||||
$this->fileLength = 50; // The value for file length is the total length of the file in 16-bit words (including the fifty 16-bit words that make up the header).
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads shapefile and dbase (if supported).
|
||||
*
|
||||
* @param string $FileName File mask to load (eg. example.*)
|
||||
*/
|
||||
public function loadFromFile($FileName)
|
||||
{
|
||||
if (!empty($FileName)) {
|
||||
$this->FileName = $FileName;
|
||||
$result = $this->_openSHPFile();
|
||||
} else {
|
||||
/* We operate on buffer emulated by readSHP / eofSHP */
|
||||
$result = true;
|
||||
}
|
||||
|
||||
if ($result && ($this->_openDBFFile())) {
|
||||
if (!$this->_loadHeaders()) {
|
||||
$this->_closeSHPFile();
|
||||
$this->_closeDBFFile();
|
||||
|
||||
return false;
|
||||
}
|
||||
if (!$this->_loadRecords()) {
|
||||
$this->_closeSHPFile();
|
||||
$this->_closeDBFFile();
|
||||
|
||||
return false;
|
||||
}
|
||||
$this->_closeSHPFile();
|
||||
$this->_closeDBFFile();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves shapefile.
|
||||
*
|
||||
* @param string|null $FileName Name of file, otherwise existing is used
|
||||
*/
|
||||
public function saveToFile($FileName = null)
|
||||
{
|
||||
if (!is_null($FileName)) {
|
||||
$this->FileName = $FileName;
|
||||
}
|
||||
|
||||
if (($this->_openSHPFile(true)) && ($this->_openSHXFile(true)) && ($this->_createDBFFile())) {
|
||||
$this->_saveHeaders();
|
||||
$this->_saveRecords();
|
||||
$this->_closeSHPFile();
|
||||
$this->_closeSHXFile();
|
||||
$this->_closeDBFFile();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates filename with given extension.
|
||||
*
|
||||
* @param string $extension Extension to use (including dot)
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function _getFilename($extension)
|
||||
{
|
||||
return str_replace('.*', $extension, $this->FileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates bounding box based on SHPData.
|
||||
*
|
||||
* @param string $type Type of box
|
||||
* @param array $data ShapeRecord SHPData
|
||||
*/
|
||||
private function updateBBox($type, $data)
|
||||
{
|
||||
$min = $type . 'min';
|
||||
$max = $type . 'max';
|
||||
|
||||
if (!isset($this->boundingBox[$min]) || $this->boundingBox[$min] == 0.0 || ($this->boundingBox[$min] > $data[$min])) {
|
||||
$this->boundingBox[$min] = $data[$min];
|
||||
}
|
||||
if (!isset($this->boundingBox[$max]) || $this->boundingBox[$max] == 0.0 || ($this->boundingBox[$max] < $data[$max])) {
|
||||
$this->boundingBox[$max] = $data[$max];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds record to shape file.
|
||||
*
|
||||
* @param ShapeRecord $record
|
||||
*
|
||||
* @return int Number of added record
|
||||
*/
|
||||
public function addRecord($record)
|
||||
{
|
||||
if ((isset($this->DBFHeader)) && (is_array($this->DBFHeader))) {
|
||||
$record->updateDBFInfo($this->DBFHeader);
|
||||
}
|
||||
|
||||
$this->fileLength += ($record->getContentLength() + 4);
|
||||
$this->records[] = $record;
|
||||
$this->records[count($this->records) - 1]->recordNumber = count($this->records);
|
||||
|
||||
$this->updateBBox('x', $record->SHPData);
|
||||
$this->updateBBox('y', $record->SHPData);
|
||||
|
||||
if (in_array($this->shapeType, array(11, 13, 15, 18, 21, 23, 25, 28))) {
|
||||
$this->updateBBox('m', $record->SHPData);
|
||||
}
|
||||
|
||||
if (in_array($this->shapeType, array(11, 13, 15, 18))) {
|
||||
$this->updateBBox('z', $record->SHPData);
|
||||
}
|
||||
|
||||
return count($this->records) - 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes record from shapefile.
|
||||
*
|
||||
* @param int $index
|
||||
*/
|
||||
public function deleteRecord($index)
|
||||
{
|
||||
if (isset($this->records[$index])) {
|
||||
$this->fileLength -= ($this->records[$index]->getContentLength() + 4);
|
||||
$count = count($this->records) - 1;
|
||||
for ($i = $index; $i < $count; ++$i) {
|
||||
$this->records[$i] = $this->records[$i + 1];
|
||||
}
|
||||
unset($this->records[count($this->records) - 1]);
|
||||
$this->_deleteRecordFromDBF($index);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns array defining fields in DBF file.
|
||||
*
|
||||
* @return array see setDBFHeader for more information
|
||||
*/
|
||||
public function getDBFHeader()
|
||||
{
|
||||
return $this->DBFHeader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes array defining fields in DBF file, used in dbase_create call.
|
||||
*
|
||||
* @param array $header An array of arrays, each array describing the
|
||||
* format of one field of the database. Each
|
||||
* field consists of a name, a character indicating
|
||||
* the field type, and optionally, a length,
|
||||
* a precision and a nullable flag.
|
||||
*/
|
||||
public function setDBFHeader($header)
|
||||
{
|
||||
$this->DBFHeader = $header;
|
||||
|
||||
$count = count($this->records);
|
||||
for ($i = 0; $i < $count; ++$i) {
|
||||
$this->records[$i]->updateDBFInfo($header);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Lookups value in the DBF file and returs index.
|
||||
*
|
||||
* @param string $field Field to match
|
||||
* @param mixed $value Value to match
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getIndexFromDBFData($field, $value)
|
||||
{
|
||||
foreach ($this->records as $index => $record) {
|
||||
if (isset($record->DBFData[$field]) &&
|
||||
(trim(strtoupper($record->DBFData[$field])) == strtoupper($value))
|
||||
) {
|
||||
return $index;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads DBF metadata.
|
||||
*/
|
||||
private function _loadDBFHeader()
|
||||
{
|
||||
$DBFFile = fopen($this->_getFilename('.dbf'), 'r');
|
||||
|
||||
$result = array();
|
||||
$i = 1;
|
||||
$inHeader = true;
|
||||
|
||||
while ($inHeader) {
|
||||
if (!feof($DBFFile)) {
|
||||
$buff32 = fread($DBFFile, 32);
|
||||
if ($i > 1) {
|
||||
if (substr($buff32, 0, 1) == chr(13)) {
|
||||
$inHeader = false;
|
||||
} else {
|
||||
$pos = strpos(substr($buff32, 0, 10), chr(0));
|
||||
$pos = ($pos == 0 ? 10 : $pos);
|
||||
|
||||
$fieldName = substr($buff32, 0, $pos);
|
||||
$fieldType = substr($buff32, 11, 1);
|
||||
$fieldLen = ord(substr($buff32, 16, 1));
|
||||
$fieldDec = ord(substr($buff32, 17, 1));
|
||||
|
||||
array_push($result, array($fieldName, $fieldType, $fieldLen, $fieldDec));
|
||||
}
|
||||
}
|
||||
++$i;
|
||||
} else {
|
||||
$inHeader = false;
|
||||
}
|
||||
}
|
||||
|
||||
fclose($DBFFile);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes record from the DBF file.
|
||||
*
|
||||
* @param int $index
|
||||
*/
|
||||
private function _deleteRecordFromDBF($index)
|
||||
{
|
||||
if (@dbase_delete_record($this->DBFFile, $index)) {
|
||||
dbase_pack($this->DBFFile);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads SHP file metadata.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function _loadHeaders()
|
||||
{
|
||||
if (Util::loadData('N', $this->readSHP(4)) != self::MAGIC) {
|
||||
$this->setError('Not a SHP file (file code mismatch)');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Skip 20 unused bytes */
|
||||
$this->readSHP(20);
|
||||
|
||||
$this->fileLength = Util::loadData('N', $this->readSHP(4));
|
||||
|
||||
/* We currently ignore version */
|
||||
$this->readSHP(4);
|
||||
|
||||
$this->shapeType = Util::loadData('V', $this->readSHP(4));
|
||||
|
||||
$this->boundingBox = array();
|
||||
$this->boundingBox['xmin'] = Util::loadData('d', $this->readSHP(8));
|
||||
$this->boundingBox['ymin'] = Util::loadData('d', $this->readSHP(8));
|
||||
$this->boundingBox['xmax'] = Util::loadData('d', $this->readSHP(8));
|
||||
$this->boundingBox['ymax'] = Util::loadData('d', $this->readSHP(8));
|
||||
$this->boundingBox['zmin'] = Util::loadData('d', $this->readSHP(8));
|
||||
$this->boundingBox['zmax'] = Util::loadData('d', $this->readSHP(8));
|
||||
$this->boundingBox['mmin'] = Util::loadData('d', $this->readSHP(8));
|
||||
$this->boundingBox['mmax'] = Util::loadData('d', $this->readSHP(8));
|
||||
|
||||
if (self::supports_dbase()) {
|
||||
$this->DBFHeader = $this->_loadDBFHeader();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves bounding box record, possibly using 0 instead of not set values.
|
||||
*
|
||||
* @param file $file File object
|
||||
* @param string $type Bounding box dimension (eg. xmax, mmin...)
|
||||
*/
|
||||
private function _saveBBoxRecord($file, $type)
|
||||
{
|
||||
fwrite($file, Util::packDouble(
|
||||
isset($this->boundingBox[$type]) ? $this->boundingBox[$type] : 0)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves bounding box to a file.
|
||||
*
|
||||
* @param file $file File object
|
||||
*/
|
||||
private function _saveBBox($file)
|
||||
{
|
||||
$this->_saveBBoxRecord($file, 'xmin');
|
||||
$this->_saveBBoxRecord($file, 'ymin');
|
||||
$this->_saveBBoxRecord($file, 'xmax');
|
||||
$this->_saveBBoxRecord($file, 'ymax');
|
||||
$this->_saveBBoxRecord($file, 'zmin');
|
||||
$this->_saveBBoxRecord($file, 'zmax');
|
||||
$this->_saveBBoxRecord($file, 'mmin');
|
||||
$this->_saveBBoxRecord($file, 'mmax');
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves SHP and SHX file metadata.
|
||||
*/
|
||||
private function _saveHeaders()
|
||||
{
|
||||
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));
|
||||
$this->_saveBBox($this->SHPFile);
|
||||
|
||||
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));
|
||||
$this->_saveBBox($this->SHXFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads records from SHP file (and DBF).
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function _loadRecords()
|
||||
{
|
||||
/* Need to start at offset 100 */
|
||||
while (!$this->eofSHP()) {
|
||||
$record = new ShapeRecord(-1);
|
||||
$record->loadFromFile($this, $this->SHPFile, $this->DBFFile);
|
||||
if ($record->lastError != '') {
|
||||
$this->setError($record->lastError);
|
||||
|
||||
return false;
|
||||
}
|
||||
if (($record->shapeType === false || $record->shapeType === '') && $this->eofSHP()) {
|
||||
break;
|
||||
}
|
||||
|
||||
$this->records[] = $record;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves records to SHP and SHX files.
|
||||
*/
|
||||
private function _saveRecords()
|
||||
{
|
||||
$offset = 50;
|
||||
if (is_array($this->records) && (count($this->records) > 0)) {
|
||||
foreach ($this->records as $index => $record) {
|
||||
//Save the record to the .shp file
|
||||
$record->saveToFile($this->SHPFile, $this->DBFFile, $index + 1);
|
||||
|
||||
//Save the record to the .shx file
|
||||
fwrite($this->SHXFile, pack('N', $offset));
|
||||
fwrite($this->SHXFile, pack('N', $record->getContentLength()));
|
||||
$offset += (4 + $record->getContentLength());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic interface to open files.
|
||||
*
|
||||
* @param bool $toWrite Whether file should be opened for writing
|
||||
* @param string $extension File extension
|
||||
* @param string $name Verbose file name to report errors
|
||||
*
|
||||
* @return file|false File handle
|
||||
*/
|
||||
private function _openFile($toWrite, $extension, $name)
|
||||
{
|
||||
$shp_name = $this->_getFilename($extension);
|
||||
$result = @fopen($shp_name, ($toWrite ? 'wb+' : 'rb'));
|
||||
if (!$result) {
|
||||
$this->setError(sprintf('It wasn\'t possible to open the %s file "%s"', $name, $shp_name));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens SHP file.
|
||||
*
|
||||
* @param bool $toWrite Whether file should be opened for writing
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function _openSHPFile($toWrite = false)
|
||||
{
|
||||
$this->SHPFile = $this->_openFile($toWrite, '.shp', 'Shape');
|
||||
if (!$this->SHPFile) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes SHP file.
|
||||
*/
|
||||
private function _closeSHPFile()
|
||||
{
|
||||
if ($this->SHPFile) {
|
||||
fclose($this->SHPFile);
|
||||
$this->SHPFile = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens SHX file.
|
||||
*
|
||||
* @param bool $toWrite Whether file should be opened for writing
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function _openSHXFile($toWrite = false)
|
||||
{
|
||||
$this->SHXFile = $this->_openFile($toWrite, '.shx', 'Index');
|
||||
if (!$this->SHXFile) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes SHX file.
|
||||
*/
|
||||
private function _closeSHXFile()
|
||||
{
|
||||
if ($this->SHXFile) {
|
||||
fclose($this->SHXFile);
|
||||
$this->SHXFile = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates DBF file.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function _createDBFFile()
|
||||
{
|
||||
if (!self::supports_dbase() || !is_array($this->DBFHeader) || count($this->DBFHeader) == 0) {
|
||||
$this->DBFFile = null;
|
||||
|
||||
return true;
|
||||
}
|
||||
$dbf_name = $this->_getFilename('.dbf');
|
||||
|
||||
/* Unlink existing file */
|
||||
if (file_exists($dbf_name)) {
|
||||
unlink($dbf_name);
|
||||
}
|
||||
|
||||
/* Create new file */
|
||||
$this->DBFFile = @dbase_create($dbf_name, $this->DBFHeader);
|
||||
if ($this->DBFFile === false) {
|
||||
$this->setError(sprintf('It wasn\'t possible to create the DBase file "%s"', $dbf_name));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads DBF file if supported.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function _openDBFFile()
|
||||
{
|
||||
if (!self::supports_dbase()) {
|
||||
$this->DBFFile = null;
|
||||
|
||||
return true;
|
||||
}
|
||||
$dbf_name = $this->_getFilename('.dbf');
|
||||
if (is_readable($dbf_name)) {
|
||||
$this->DBFFile = @dbase_open($dbf_name, 0);
|
||||
if (!$this->DBFFile) {
|
||||
$this->setError(sprintf('It wasn\'t possible to open the DBase file "%s"', $dbf_name));
|
||||
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
$this->setError(sprintf('It wasn\'t possible to find the DBase file "%s"', $dbf_name));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes DBF file.
|
||||
*/
|
||||
private function _closeDBFFile()
|
||||
{
|
||||
if ($this->DBFFile) {
|
||||
dbase_close($this->DBFFile);
|
||||
$this->DBFFile = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets error message.
|
||||
*
|
||||
* @param string $error
|
||||
*/
|
||||
public function setError($error)
|
||||
{
|
||||
$this->lastError = $error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads given number of bytes from SHP file.
|
||||
*
|
||||
* @param int $bytes
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function readSHP($bytes)
|
||||
{
|
||||
return fread($this->SHPFile, $bytes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether file is at EOF.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function eofSHP()
|
||||
{
|
||||
return feof($this->SHPFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns shape name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getShapeName()
|
||||
{
|
||||
return Util::nameShape($this->shapeType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether file contains measure data.
|
||||
*
|
||||
* For some reason this is distinguished by zero bounding box in the
|
||||
* specification.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasMeasure()
|
||||
{
|
||||
return $this->boundingBox['mmin'] != 0 || $this->boundingBox['mmax'] != 0;
|
||||
}
|
||||
}
|
849
pma/vendor/phpmyadmin/shapefile/src/ShapeRecord.php
vendored
Normal file
849
pma/vendor/phpmyadmin/shapefile/src/ShapeRecord.php
vendored
Normal file
@ -0,0 +1,849 @@
|
||||
<?php
|
||||
/**
|
||||
* phpMyAdmin ShapeFile library
|
||||
* <https://github.com/phpmyadmin/shapefile/>.
|
||||
*
|
||||
* Copyright 2006-2007 Ovidio <ovidio AT users.sourceforge.net>
|
||||
* Copyright 2016 - 2017 Michal Čihař <michal@cihar.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, you can download one from
|
||||
* https://www.gnu.org/copyleft/gpl.html.
|
||||
*/
|
||||
|
||||
namespace PhpMyAdmin\ShapeFile;
|
||||
|
||||
/**
|
||||
* ShapeFile record class.
|
||||
*/
|
||||
class ShapeRecord
|
||||
{
|
||||
private $SHPFile = null;
|
||||
private $DBFFile = null;
|
||||
private $ShapeFile = null;
|
||||
|
||||
private $size = 0;
|
||||
private $read = 0;
|
||||
|
||||
public $recordNumber = null;
|
||||
public $shapeType = null;
|
||||
|
||||
public $lastError = '';
|
||||
|
||||
public $SHPData = array();
|
||||
public $DBFData = array();
|
||||
|
||||
/**
|
||||
* @param int $shapeType
|
||||
*/
|
||||
public function __construct($shapeType)
|
||||
{
|
||||
$this->shapeType = $shapeType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads record from files.
|
||||
*
|
||||
* @param ShapeFile $ShapeFile
|
||||
* @param file &$SHPFile Opened SHP file
|
||||
* @param file &$DBFFile Opened DBF file
|
||||
*/
|
||||
public function loadFromFile(&$ShapeFile, &$SHPFile, &$DBFFile)
|
||||
{
|
||||
$this->ShapeFile = $ShapeFile;
|
||||
$this->SHPFile = $SHPFile;
|
||||
$this->DBFFile = $DBFFile;
|
||||
$this->_loadHeaders();
|
||||
|
||||
/* No header read */
|
||||
if ($this->read == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch ($this->shapeType) {
|
||||
case 0:
|
||||
$this->_loadNullRecord();
|
||||
break;
|
||||
case 1:
|
||||
$this->_loadPointRecord();
|
||||
break;
|
||||
case 21:
|
||||
$this->_loadPointMRecord();
|
||||
break;
|
||||
case 11:
|
||||
$this->_loadPointZRecord();
|
||||
break;
|
||||
case 3:
|
||||
$this->_loadPolyLineRecord();
|
||||
break;
|
||||
case 23:
|
||||
$this->_loadPolyLineMRecord();
|
||||
break;
|
||||
case 13:
|
||||
$this->_loadPolyLineZRecord();
|
||||
break;
|
||||
case 5:
|
||||
$this->_loadPolygonRecord();
|
||||
break;
|
||||
case 25:
|
||||
$this->_loadPolygonMRecord();
|
||||
break;
|
||||
case 15:
|
||||
$this->_loadPolygonZRecord();
|
||||
break;
|
||||
case 8:
|
||||
$this->_loadMultiPointRecord();
|
||||
break;
|
||||
case 28:
|
||||
$this->_loadMultiPointMRecord();
|
||||
break;
|
||||
case 18:
|
||||
$this->_loadMultiPointZRecord();
|
||||
break;
|
||||
default:
|
||||
$this->setError(sprintf('The Shape Type "%s" is not supported.', $this->shapeType));
|
||||
break;
|
||||
}
|
||||
|
||||
/* We need to skip rest of the record */
|
||||
while ($this->read < $this->size) {
|
||||
$this->_loadData('V', 4);
|
||||
}
|
||||
|
||||
/* Check if we didn't read too much */
|
||||
if ($this->read != $this->size) {
|
||||
$this->setError(sprintf('Failed to parse record, read=%d, size=%d', $this->read, $this->size));
|
||||
}
|
||||
|
||||
if (ShapeFile::supports_dbase() && isset($this->DBFFile)) {
|
||||
$this->_loadDBFData();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves record to files.
|
||||
*
|
||||
* @param file &$SHPFile Opened SHP file
|
||||
* @param file &$DBFFile Opened DBF file
|
||||
* @param int $recordNumber Record number
|
||||
*/
|
||||
public function saveToFile(&$SHPFile, &$DBFFile, $recordNumber)
|
||||
{
|
||||
$this->SHPFile = $SHPFile;
|
||||
$this->DBFFile = $DBFFile;
|
||||
$this->recordNumber = $recordNumber;
|
||||
$this->_saveHeaders();
|
||||
|
||||
switch ($this->shapeType) {
|
||||
case 0:
|
||||
// Nothing to save
|
||||
break;
|
||||
case 1:
|
||||
$this->_savePointRecord();
|
||||
break;
|
||||
case 21:
|
||||
$this->_savePointMRecord();
|
||||
break;
|
||||
case 11:
|
||||
$this->_savePointZRecord();
|
||||
break;
|
||||
case 3:
|
||||
$this->_savePolyLineRecord();
|
||||
break;
|
||||
case 23:
|
||||
$this->_savePolyLineMRecord();
|
||||
break;
|
||||
case 13:
|
||||
$this->_savePolyLineZRecord();
|
||||
break;
|
||||
case 5:
|
||||
$this->_savePolygonRecord();
|
||||
break;
|
||||
case 25:
|
||||
$this->_savePolygonMRecord();
|
||||
break;
|
||||
case 15:
|
||||
$this->_savePolygonZRecord();
|
||||
break;
|
||||
case 8:
|
||||
$this->_saveMultiPointRecord();
|
||||
break;
|
||||
case 28:
|
||||
$this->_saveMultiPointMRecord();
|
||||
break;
|
||||
case 18:
|
||||
$this->_saveMultiPointZRecord();
|
||||
break;
|
||||
default:
|
||||
$this->setError(sprintf('The Shape Type "%s" is not supported.', $this->shapeType));
|
||||
break;
|
||||
}
|
||||
if (ShapeFile::supports_dbase() && !is_null($this->DBFFile)) {
|
||||
$this->_saveDBFData();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates DBF data to match header.
|
||||
*
|
||||
* @param array $header DBF structure header
|
||||
*/
|
||||
public function updateDBFInfo($header)
|
||||
{
|
||||
$tmp = $this->DBFData;
|
||||
unset($this->DBFData);
|
||||
$this->DBFData = array();
|
||||
foreach ($header as $value) {
|
||||
$this->DBFData[$value[0]] = (isset($tmp[$value[0]])) ? $tmp[$value[0]] : '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads data.
|
||||
*
|
||||
* @param string $type type for unpack()
|
||||
* @param int $count number of bytes
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
private function _loadData($type, $count)
|
||||
{
|
||||
$data = $this->ShapeFile->readSHP($count);
|
||||
if ($data === false) {
|
||||
return false;
|
||||
}
|
||||
$this->read += strlen($data);
|
||||
|
||||
return Util::loadData($type, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads metadata header from a file.
|
||||
*/
|
||||
private function _loadHeaders()
|
||||
{
|
||||
$this->shapeType = false;
|
||||
$this->recordNumber = $this->_loadData('N', 4);
|
||||
if ($this->recordNumber === false) {
|
||||
return;
|
||||
}
|
||||
// We read the length of the record
|
||||
$this->size = $this->_loadData('N', 4);
|
||||
if ($this->size === false) {
|
||||
return;
|
||||
}
|
||||
$this->size = $this->size * 2 + 8;
|
||||
$this->shapeType = $this->_loadData('V', 4);
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves metadata header to a file.
|
||||
*/
|
||||
private function _saveHeaders()
|
||||
{
|
||||
fwrite($this->SHPFile, pack('N', $this->recordNumber));
|
||||
fwrite($this->SHPFile, pack('N', $this->getContentLength()));
|
||||
fwrite($this->SHPFile, pack('V', $this->shapeType));
|
||||
}
|
||||
|
||||
private function _loadPoint()
|
||||
{
|
||||
$data = array();
|
||||
|
||||
$data['x'] = $this->_loadData('d', 8);
|
||||
$data['y'] = $this->_loadData('d', 8);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function _loadPointM()
|
||||
{
|
||||
$data = $this->_loadPoint();
|
||||
|
||||
$data['m'] = $this->_loadData('d', 8);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function _loadPointZ()
|
||||
{
|
||||
$data = $this->_loadPoint();
|
||||
|
||||
$data['z'] = $this->_loadData('d', 8);
|
||||
$data['m'] = $this->_loadData('d', 8);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function _savePoint($data)
|
||||
{
|
||||
fwrite($this->SHPFile, Util::packDouble($data['x']));
|
||||
fwrite($this->SHPFile, Util::packDouble($data['y']));
|
||||
}
|
||||
|
||||
private function _savePointM($data)
|
||||
{
|
||||
fwrite($this->SHPFile, Util::packDouble($data['x']));
|
||||
fwrite($this->SHPFile, Util::packDouble($data['y']));
|
||||
fwrite($this->SHPFile, Util::packDouble($data['m']));
|
||||
}
|
||||
|
||||
private function _savePointZ($data)
|
||||
{
|
||||
fwrite($this->SHPFile, Util::packDouble($data['x']));
|
||||
fwrite($this->SHPFile, Util::packDouble($data['y']));
|
||||
fwrite($this->SHPFile, Util::packDouble($data['z']));
|
||||
fwrite($this->SHPFile, Util::packDouble($data['m']));
|
||||
}
|
||||
|
||||
private function _loadNullRecord()
|
||||
{
|
||||
$this->SHPData = array();
|
||||
}
|
||||
|
||||
private function _loadPointRecord()
|
||||
{
|
||||
$this->SHPData = $this->_loadPoint();
|
||||
}
|
||||
|
||||
private function _loadPointMRecord()
|
||||
{
|
||||
$this->SHPData = $this->_loadPointM();
|
||||
}
|
||||
|
||||
private function _loadPointZRecord()
|
||||
{
|
||||
$this->SHPData = $this->_loadPointZ();
|
||||
}
|
||||
|
||||
private function _savePointRecord()
|
||||
{
|
||||
$this->_savePoint($this->SHPData);
|
||||
}
|
||||
|
||||
private function _savePointMRecord()
|
||||
{
|
||||
$this->_savePointM($this->SHPData);
|
||||
}
|
||||
|
||||
private function _savePointZRecord()
|
||||
{
|
||||
$this->_savePointZ($this->SHPData);
|
||||
}
|
||||
|
||||
private function _loadBBox()
|
||||
{
|
||||
$this->SHPData['xmin'] = $this->_loadData('d', 8);
|
||||
$this->SHPData['ymin'] = $this->_loadData('d', 8);
|
||||
$this->SHPData['xmax'] = $this->_loadData('d', 8);
|
||||
$this->SHPData['ymax'] = $this->_loadData('d', 8);
|
||||
}
|
||||
|
||||
private function _loadMultiPointRecord()
|
||||
{
|
||||
$this->SHPData = array();
|
||||
$this->_loadBBox();
|
||||
|
||||
$this->SHPData['numpoints'] = $this->_loadData('V', 4);
|
||||
|
||||
for ($i = 0; $i < $this->SHPData['numpoints']; ++$i) {
|
||||
$this->SHPData['points'][] = $this->_loadPoint();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
*/
|
||||
private function _loadMultiPointMZRecord($type)
|
||||
{
|
||||
/* The m dimension is optional, depends on bounding box data */
|
||||
if ($type == 'm' && !$this->ShapeFile->hasMeasure()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->SHPData[$type . 'min'] = $this->_loadData('d', 8);
|
||||
$this->SHPData[$type . 'max'] = $this->_loadData('d', 8);
|
||||
|
||||
for ($i = 0; $i < $this->SHPData['numpoints']; ++$i) {
|
||||
$this->SHPData['points'][$i][$type] = $this->_loadData('d', 8);
|
||||
}
|
||||
}
|
||||
|
||||
private function _loadMultiPointMRecord()
|
||||
{
|
||||
$this->_loadMultiPointRecord();
|
||||
|
||||
$this->_loadMultiPointMZRecord('m');
|
||||
}
|
||||
|
||||
private function _loadMultiPointZRecord()
|
||||
{
|
||||
$this->_loadMultiPointRecord();
|
||||
|
||||
$this->_loadMultiPointMZRecord('z');
|
||||
$this->_loadMultiPointMZRecord('m');
|
||||
}
|
||||
|
||||
private function _saveMultiPointRecord()
|
||||
{
|
||||
fwrite($this->SHPFile, pack('dddd', $this->SHPData['xmin'], $this->SHPData['ymin'], $this->SHPData['xmax'], $this->SHPData['ymax']));
|
||||
|
||||
fwrite($this->SHPFile, pack('V', $this->SHPData['numpoints']));
|
||||
|
||||
for ($i = 0; $i < $this->SHPData['numpoints']; ++$i) {
|
||||
$this->_savePoint($this->SHPData['points'][$i]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
*/
|
||||
private function _saveMultiPointMZRecord($type)
|
||||
{
|
||||
fwrite($this->SHPFile, pack('dd', $this->SHPData[$type . 'min'], $this->SHPData[$type . 'max']));
|
||||
|
||||
for ($i = 0; $i < $this->SHPData['numpoints']; ++$i) {
|
||||
fwrite($this->SHPFile, Util::packDouble($this->SHPData['points'][$i][$type]));
|
||||
}
|
||||
}
|
||||
|
||||
private function _saveMultiPointMRecord()
|
||||
{
|
||||
$this->_saveMultiPointRecord();
|
||||
|
||||
$this->_saveMultiPointMZRecord('m');
|
||||
}
|
||||
|
||||
private function _saveMultiPointZRecord()
|
||||
{
|
||||
$this->_saveMultiPointRecord();
|
||||
|
||||
$this->_saveMultiPointMZRecord('z');
|
||||
$this->_saveMultiPointMZRecord('m');
|
||||
}
|
||||
|
||||
private function _loadPolyLineRecord()
|
||||
{
|
||||
$this->SHPData = array();
|
||||
$this->_loadBBox();
|
||||
|
||||
$this->SHPData['numparts'] = $this->_loadData('V', 4);
|
||||
$this->SHPData['numpoints'] = $this->_loadData('V', 4);
|
||||
|
||||
$numparts = $this->SHPData['numparts'];
|
||||
$numpoints = $this->SHPData['numpoints'];
|
||||
|
||||
for ($i = 0; $i < $numparts; ++$i) {
|
||||
$this->SHPData['parts'][$i] = $this->_loadData('V', 4);
|
||||
}
|
||||
|
||||
$part = 0;
|
||||
for ($i = 0; $i < $numpoints; ++$i) {
|
||||
if ($part + 1 < $numparts && $this->SHPData['parts'][$part + 1] == $i) {
|
||||
++$part;
|
||||
}
|
||||
if (!isset($this->SHPData['parts'][$part]['points']) || !is_array($this->SHPData['parts'][$part]['points'])) {
|
||||
$this->SHPData['parts'][$part] = array('points' => array());
|
||||
}
|
||||
$this->SHPData['parts'][$part]['points'][] = $this->_loadPoint();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
*/
|
||||
private function _loadPolyLineMZRecord($type)
|
||||
{
|
||||
/* The m dimension is optional, depends on bounding box data */
|
||||
if ($type == 'm' && !$this->ShapeFile->hasMeasure()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->SHPData[$type . 'min'] = $this->_loadData('d', 8);
|
||||
$this->SHPData[$type . 'max'] = $this->_loadData('d', 8);
|
||||
|
||||
$numparts = $this->SHPData['numparts'];
|
||||
$numpoints = $this->SHPData['numpoints'];
|
||||
|
||||
$part = 0;
|
||||
for ($i = 0; $i < $numpoints; ++$i) {
|
||||
if ($part + 1 < $numparts && $this->SHPData['parts'][$part + 1] == $i) {
|
||||
++$part;
|
||||
}
|
||||
$this->SHPData['parts'][$part]['points'][$i][$type] = $this->_loadData('d', 8);
|
||||
}
|
||||
}
|
||||
|
||||
private function _loadPolyLineMRecord()
|
||||
{
|
||||
$this->_loadPolyLineRecord();
|
||||
|
||||
$this->_loadPolyLineMZRecord('m');
|
||||
}
|
||||
|
||||
private function _loadPolyLineZRecord()
|
||||
{
|
||||
$this->_loadPolyLineRecord();
|
||||
|
||||
$this->_loadPolyLineMZRecord('z');
|
||||
$this->_loadPolyLineMZRecord('m');
|
||||
}
|
||||
|
||||
private function _savePolyLineRecord()
|
||||
{
|
||||
fwrite($this->SHPFile, pack('dddd', $this->SHPData['xmin'], $this->SHPData['ymin'], $this->SHPData['xmax'], $this->SHPData['ymax']));
|
||||
|
||||
fwrite($this->SHPFile, pack('VV', $this->SHPData['numparts'], $this->SHPData['numpoints']));
|
||||
|
||||
$part_index = 0;
|
||||
for ($i = 0; $i < $this->SHPData['numparts']; ++$i) {
|
||||
fwrite($this->SHPFile, pack('V', $part_index));
|
||||
$part_index += count($this->SHPData['parts'][$i]['points']);
|
||||
}
|
||||
|
||||
foreach ($this->SHPData['parts'] as $partData) {
|
||||
foreach ($partData['points'] as $pointData) {
|
||||
$this->_savePoint($pointData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $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 ($partData['points'] as $pointData) {
|
||||
fwrite($this->SHPFile, Util::packDouble($pointData[$type]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function _savePolyLineMRecord()
|
||||
{
|
||||
$this->_savePolyLineRecord();
|
||||
|
||||
$this->_savePolyLineMZRecord('m');
|
||||
}
|
||||
|
||||
private function _savePolyLineZRecord()
|
||||
{
|
||||
$this->_savePolyLineRecord();
|
||||
|
||||
$this->_savePolyLineMZRecord('z');
|
||||
$this->_savePolyLineMZRecord('m');
|
||||
}
|
||||
|
||||
private function _loadPolygonRecord()
|
||||
{
|
||||
$this->_loadPolyLineRecord();
|
||||
}
|
||||
|
||||
private function _loadPolygonMRecord()
|
||||
{
|
||||
$this->_loadPolyLineMRecord();
|
||||
}
|
||||
|
||||
private function _loadPolygonZRecord()
|
||||
{
|
||||
$this->_loadPolyLineZRecord();
|
||||
}
|
||||
|
||||
private function _savePolygonRecord()
|
||||
{
|
||||
$this->_savePolyLineRecord();
|
||||
}
|
||||
|
||||
private function _savePolygonMRecord()
|
||||
{
|
||||
$this->_savePolyLineMRecord();
|
||||
}
|
||||
|
||||
private function _savePolygonZRecord()
|
||||
{
|
||||
$this->_savePolyLineZRecord();
|
||||
}
|
||||
|
||||
private function _adjustBBox($point)
|
||||
{
|
||||
// Adjusts bounding box based on point
|
||||
$directions = array('x', 'y', 'z', 'm');
|
||||
foreach ($directions as $direction) {
|
||||
if (!isset($point[$direction])) {
|
||||
continue;
|
||||
}
|
||||
$min = $direction . 'min';
|
||||
$max = $direction . 'max';
|
||||
if (!isset($this->SHPData[$min]) || ($this->SHPData[$min] > $point[$direction])) {
|
||||
$this->SHPData[$min] = $point[$direction];
|
||||
}
|
||||
if (!isset($this->SHPData[$max]) || ($this->SHPData[$max] < $point[$direction])) {
|
||||
$this->SHPData[$max] = $point[$direction];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets dimension to 0 if not set.
|
||||
*
|
||||
* @param array $point Point to check
|
||||
* @param string $dimension Dimension to check
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function _fixPoint($point, $dimension)
|
||||
{
|
||||
if (!isset($point[$dimension])) {
|
||||
$point[$dimension] = 0.0; // no_value
|
||||
}
|
||||
|
||||
return $point;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adjust point and bounding box when adding point.
|
||||
*
|
||||
* @param array $point Point data
|
||||
*
|
||||
* @return array Fixed point data
|
||||
*/
|
||||
private function _adjustPoint($point)
|
||||
{
|
||||
$type = $this->shapeType / 10;
|
||||
if ($type >= 2) {
|
||||
$point = $this->_fixPoint($point, 'm');
|
||||
} elseif ($type >= 1) {
|
||||
$point = $this->_fixPoint($point, 'z');
|
||||
$point = $this->_fixPoint($point, 'm');
|
||||
}
|
||||
|
||||
return $point;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds point to a record.
|
||||
*
|
||||
* @param array $point Point data
|
||||
* @param int $partIndex Part index
|
||||
*/
|
||||
public function addPoint($point, $partIndex = 0)
|
||||
{
|
||||
$point = $this->_adjustPoint($point);
|
||||
switch ($this->shapeType) {
|
||||
case 0:
|
||||
//Don't add anything
|
||||
return;
|
||||
case 1:
|
||||
case 11:
|
||||
case 21:
|
||||
//Substitutes the value of the current point
|
||||
$this->SHPData = $point;
|
||||
break;
|
||||
case 3:
|
||||
case 5:
|
||||
case 13:
|
||||
case 15:
|
||||
case 23:
|
||||
case 25:
|
||||
//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 + (isset($this->SHPData['numpoints']) ? $this->SHPData['numpoints'] : 0);
|
||||
break;
|
||||
case 8:
|
||||
case 18:
|
||||
case 28:
|
||||
//Adds a new point
|
||||
$this->SHPData['points'][] = $point;
|
||||
$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));
|
||||
|
||||
return;
|
||||
}
|
||||
$this->_adjustBBox($point);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes point from a record.
|
||||
*
|
||||
* @param int $pointIndex Point index
|
||||
* @param int $partIndex Part index
|
||||
*/
|
||||
public function deletePoint($pointIndex = 0, $partIndex = 0)
|
||||
{
|
||||
switch ($this->shapeType) {
|
||||
case 0:
|
||||
//Don't delete anything
|
||||
break;
|
||||
case 1:
|
||||
case 11:
|
||||
case 21:
|
||||
//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))) {
|
||||
$this->SHPData['m'] = 0.0;
|
||||
}
|
||||
if (in_array($this->shapeType, array(11))) {
|
||||
$this->SHPData['z'] = 0.0;
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
case 5:
|
||||
case 13:
|
||||
case 15:
|
||||
case 23:
|
||||
case 25:
|
||||
//Deletes the point from the selected part, if exists
|
||||
if (isset($this->SHPData['parts'][$partIndex]) && isset($this->SHPData['parts'][$partIndex]['points'][$pointIndex])) {
|
||||
$count = count($this->SHPData['parts'][$partIndex]['points']) - 1;
|
||||
for ($i = $pointIndex; $i < $count; ++$i) {
|
||||
$this->SHPData['parts'][$partIndex]['points'][$i] = $this->SHPData['parts'][$partIndex]['points'][$i + 1];
|
||||
}
|
||||
unset($this->SHPData['parts'][$partIndex]['points'][count($this->SHPData['parts'][$partIndex]['points']) - 1]);
|
||||
|
||||
$this->SHPData['numparts'] = count($this->SHPData['parts']);
|
||||
--$this->SHPData['numpoints'];
|
||||
}
|
||||
break;
|
||||
case 8:
|
||||
case 18:
|
||||
case 28:
|
||||
//Deletes the point, if exists
|
||||
if (isset($this->SHPData['points'][$pointIndex])) {
|
||||
$count = count($this->SHPData['points']) - 1;
|
||||
for ($i = $pointIndex; $i < $count; ++$i) {
|
||||
$this->SHPData['points'][$i] = $this->SHPData['points'][$i + 1];
|
||||
}
|
||||
unset($this->SHPData['points'][count($this->SHPData['points']) - 1]);
|
||||
|
||||
--$this->SHPData['numpoints'];
|
||||
}
|
||||
break;
|
||||
default:
|
||||
$this->setError(sprintf('The Shape Type "%s" is not supported.', $this->shapeType));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns length of content.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function getContentLength()
|
||||
{
|
||||
// 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:
|
||||
$result = 0;
|
||||
break;
|
||||
case 1:
|
||||
$result = 10;
|
||||
break;
|
||||
case 21:
|
||||
$result = 10 + 4;
|
||||
break;
|
||||
case 11:
|
||||
$result = 10 + 8;
|
||||
break;
|
||||
case 3:
|
||||
case 5:
|
||||
$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:
|
||||
$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:
|
||||
$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:
|
||||
$result = 20 + 8 * count($this->SHPData['points']);
|
||||
break;
|
||||
case 28:
|
||||
$result = 20 + (2 * 4) + (8 + 4) * count($this->SHPData['points']);
|
||||
break;
|
||||
case 18:
|
||||
$result = 20 + (4 * 4) + (8 + 8) * count($this->SHPData['points']);
|
||||
break;
|
||||
default:
|
||||
$result = false;
|
||||
$this->setError(sprintf('The Shape Type "%s" is not supported.', $this->shapeType));
|
||||
break;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function _loadDBFData()
|
||||
{
|
||||
$this->DBFData = @dbase_get_record_with_names($this->DBFFile, $this->recordNumber);
|
||||
unset($this->DBFData['deleted']);
|
||||
}
|
||||
|
||||
private function _saveDBFData()
|
||||
{
|
||||
if (count($this->DBFData) == 0) {
|
||||
return;
|
||||
}
|
||||
unset($this->DBFData['deleted']);
|
||||
if ($this->recordNumber <= dbase_numrecords($this->DBFFile)) {
|
||||
if (!dbase_replace_record($this->DBFFile, array_values($this->DBFData), $this->recordNumber)) {
|
||||
$this->setError('I wasn\'t possible to update the information in the DBF file.');
|
||||
}
|
||||
} else {
|
||||
if (!dbase_add_record($this->DBFFile, array_values($this->DBFData))) {
|
||||
$this->setError('I wasn\'t possible to add the information to the DBF file.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets error message.
|
||||
*
|
||||
* @param string $error
|
||||
*/
|
||||
public function setError($error)
|
||||
{
|
||||
$this->lastError = $error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns shape name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getShapeName()
|
||||
{
|
||||
return Util::nameShape($this->shapeType);
|
||||
}
|
||||
}
|
118
pma/vendor/phpmyadmin/shapefile/src/Util.php
vendored
Normal file
118
pma/vendor/phpmyadmin/shapefile/src/Util.php
vendored
Normal file
@ -0,0 +1,118 @@
|
||||
<?php
|
||||
/**
|
||||
* phpMyAdmin ShapeFile library
|
||||
* <https://github.com/phpmyadmin/shapefile/>.
|
||||
*
|
||||
* Copyright 2006-2007 Ovidio <ovidio AT users.sourceforge.net>
|
||||
* Copyright 2016 - 2017 Michal Čihař <michal@cihar.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, you can download one from
|
||||
* https://www.gnu.org/copyleft/gpl.html.
|
||||
*/
|
||||
|
||||
namespace PhpMyAdmin\ShapeFile;
|
||||
|
||||
class Util
|
||||
{
|
||||
private static $little_endian = null;
|
||||
|
||||
private static $shape_names = array(
|
||||
0 => 'Null Shape',
|
||||
1 => 'Point',
|
||||
3 => 'PolyLine',
|
||||
5 => 'Polygon',
|
||||
8 => 'MultiPoint',
|
||||
11 => 'PointZ',
|
||||
13 => 'PolyLineZ',
|
||||
15 => 'PolygonZ',
|
||||
18 => 'MultiPointZ',
|
||||
21 => 'PointM',
|
||||
23 => 'PolyLineM',
|
||||
25 => 'PolygonM',
|
||||
28 => 'MultiPointM',
|
||||
31 => 'MultiPatch',
|
||||
);
|
||||
|
||||
/**
|
||||
* Reads data.
|
||||
*
|
||||
* @param string $type type for unpack()
|
||||
* @param string $data Data to process
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function loadData($type, $data)
|
||||
{
|
||||
if ($data === false || strlen($data) == 0) {
|
||||
return false;
|
||||
}
|
||||
$tmp = unpack($type, $data);
|
||||
|
||||
return current($tmp);
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes endianity.
|
||||
*
|
||||
* @param string $binValue Binary value
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function swap($binValue)
|
||||
{
|
||||
$result = $binValue[strlen($binValue) - 1];
|
||||
for ($i = strlen($binValue) - 2; $i >= 0; --$i) {
|
||||
$result .= $binValue[$i];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes double value to correct endianity.
|
||||
*
|
||||
* @param float $value Value to pack
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function packDouble($value)
|
||||
{
|
||||
$bin = pack('d', (float) $value);
|
||||
|
||||
if (is_null(self::$little_endian)) {
|
||||
self::$little_endian = (pack('L', 1) == pack('V', 1));
|
||||
}
|
||||
|
||||
if (self::$little_endian) {
|
||||
return $bin;
|
||||
}
|
||||
|
||||
return self::swap($bin);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns shape name.
|
||||
*
|
||||
* @param int $type
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function nameShape($type)
|
||||
{
|
||||
if (isset(self::$shape_names[$type])) {
|
||||
return self::$shape_names[$type];
|
||||
}
|
||||
|
||||
return sprintf('Shape %d', $type);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user