ShapeFile.lib.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. <?php
  2. /**
  3. * BytesFall ShapeFiles library
  4. *
  5. * The library implements the 2D variants of the ShapeFile format as defined in
  6. * http://www.esri.com/library/whitepapers/pdfs/shapefile.pdf.
  7. * The library currently supports reading and editing of ShapeFiles and the
  8. * Associated information (DBF file).
  9. *
  10. * @package bfShapeFiles
  11. * @version 0.0.2
  12. * @link http://bfshapefiles.sourceforge.net/
  13. * @license http://www.gnu.org/licenses/gpl-2.0.html GPLv2-or-later
  14. *
  15. * Copyright 2006-2007 Ovidio <ovidio AT users.sourceforge.net>
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, you can download one from
  28. * http://www.gnu.org/copyleft/gpl.html.
  29. *
  30. */
  31. function loadData($type, $data) {
  32. if (!$data) return $data;
  33. $tmp = unpack($type, $data);
  34. return current($tmp);
  35. }
  36. function swap($binValue) {
  37. $result = $binValue{strlen($binValue) - 1};
  38. for($i = strlen($binValue) - 2; $i >= 0 ; $i--) {
  39. $result .= $binValue{$i};
  40. }
  41. return $result;
  42. }
  43. function packDouble($value, $mode = 'LE') {
  44. $value = (double)$value;
  45. $bin = pack("d", $value);
  46. //We test if the conversion of an integer (1) is done as LE or BE by default
  47. switch (pack ('L', 1)) {
  48. case pack ('V', 1): //Little Endian
  49. $result = ($mode == 'LE') ? $bin : swap($bin);
  50. break;
  51. case pack ('N', 1): //Big Endian
  52. $result = ($mode == 'BE') ? $bin : swap($bin);
  53. break;
  54. default: //Some other thing, we just return false
  55. $result = FALSE;
  56. }
  57. return $result;
  58. }
  59. /**
  60. * ShapeFile class
  61. *
  62. * @package bfShapeFiles
  63. */
  64. class ShapeFile {
  65. var $FileName;
  66. var $SHPFile;
  67. var $SHXFile;
  68. var $DBFFile;
  69. var $DBFHeader;
  70. var $lastError = "";
  71. var $boundingBox = array("xmin" => 0.0, "ymin" => 0.0, "xmax" => 0.0, "ymax" => 0.0);
  72. var $fileLength = 0;
  73. var $shapeType = 0;
  74. var $records;
  75. function ShapeFile($shapeType, $boundingBox = array("xmin" => 0.0, "ymin" => 0.0, "xmax" => 0.0, "ymax" => 0.0), $FileName = NULL) {
  76. $this->shapeType = $shapeType;
  77. $this->boundingBox = $boundingBox;
  78. $this->FileName = $FileName;
  79. $this->fileLength = 50;
  80. }
  81. function loadFromFile($FileName) {
  82. $this->FileName = $FileName;
  83. if (($this->_openSHPFile()) && ($this->_openDBFFile())) {
  84. $this->_loadHeaders();
  85. $this->_loadRecords();
  86. $this->_closeSHPFile();
  87. $this->_closeDBFFile();
  88. } else {
  89. return false;
  90. }
  91. }
  92. function saveToFile($FileName = NULL) {
  93. if ($FileName != NULL) $this->FileName = $FileName;
  94. if (($this->_openSHPFile(TRUE)) && ($this->_openSHXFile(TRUE)) && ($this->_openDBFFile(TRUE))) {
  95. $this->_saveHeaders();
  96. $this->_saveRecords();
  97. $this->_closeSHPFile();
  98. $this->_closeSHXFile();
  99. $this->_closeDBFFile();
  100. } else {
  101. return false;
  102. }
  103. }
  104. function addRecord($record) {
  105. if ((isset($this->DBFHeader)) && (is_array($this->DBFHeader))) {
  106. $record->updateDBFInfo($this->DBFHeader);
  107. }
  108. $this->fileLength += ($record->getContentLength() + 4);
  109. $this->records[] = $record;
  110. $this->records[count($this->records) - 1]->recordNumber = count($this->records);
  111. return (count($this->records) - 1);
  112. }
  113. function deleteRecord($index) {
  114. if (isset($this->records[$index])) {
  115. $this->fileLength -= ($this->records[$index]->getContentLength() + 4);
  116. for ($i = $index; $i < (count($this->records) - 1); $i++) {
  117. $this->records[$i] = $this->records[$i + 1];
  118. }
  119. unset($this->records[count($this->records) - 1]);
  120. $this->_deleteRecordFromDBF($index);
  121. }
  122. }
  123. function getDBFHeader() {
  124. return $this->DBFHeader;
  125. }
  126. function setDBFHeader($header) {
  127. $this->DBFHeader = $header;
  128. for ($i = 0; $i < count($this->records); $i++) {
  129. $this->records[$i]->updateDBFInfo($header);
  130. }
  131. }
  132. function getIndexFromDBFData($field, $value) {
  133. $result = -1;
  134. for ($i = 0; $i < (count($this->records) - 1); $i++) {
  135. if (isset($this->records[$i]->DBFData[$field]) && (strtoupper($this->records[$i]->DBFData[$field]) == strtoupper($value))) {
  136. $result = $i;
  137. }
  138. }
  139. return $result;
  140. }
  141. function _loadDBFHeader() {
  142. $DBFFile = fopen(str_replace('.*', '.dbf', $this->FileName), 'r');
  143. $result = array();
  144. $buff32 = array();
  145. $i = 1;
  146. $inHeader = true;
  147. while ($inHeader) {
  148. if (!feof($DBFFile)) {
  149. $buff32 = fread($DBFFile, 32);
  150. if ($i > 1) {
  151. if (substr($buff32, 0, 1) == chr(13)) {
  152. $inHeader = false;
  153. } else {
  154. $pos = strpos(substr($buff32, 0, 10), chr(0));
  155. $pos = ($pos == 0 ? 10 : $pos);
  156. $fieldName = substr($buff32, 0, $pos);
  157. $fieldType = substr($buff32, 11, 1);
  158. $fieldLen = ord(substr($buff32, 16, 1));
  159. $fieldDec = ord(substr($buff32, 17, 1));
  160. array_push($result, array($fieldName, $fieldType, $fieldLen, $fieldDec));
  161. }
  162. }
  163. $i++;
  164. } else {
  165. $inHeader = false;
  166. }
  167. }
  168. fclose($DBFFile);
  169. return($result);
  170. }
  171. function _deleteRecordFromDBF($index) {
  172. if (@dbase_delete_record($this->DBFFile, $index)) {
  173. @dbase_pack($this->DBFFile);
  174. }
  175. }
  176. function _loadHeaders() {
  177. fseek($this->SHPFile, 24, SEEK_SET);
  178. $this->fileLength = loadData("N", fread($this->SHPFile, 4));
  179. fseek($this->SHPFile, 32, SEEK_SET);
  180. $this->shapeType = loadData("V", fread($this->SHPFile, 4));
  181. $this->boundingBox = array();
  182. $this->boundingBox["xmin"] = loadData("d", fread($this->SHPFile, 8));
  183. $this->boundingBox["ymin"] = loadData("d", fread($this->SHPFile, 8));
  184. $this->boundingBox["xmax"] = loadData("d", fread($this->SHPFile, 8));
  185. $this->boundingBox["ymax"] = loadData("d", fread($this->SHPFile, 8));
  186. $this->DBFHeader = $this->_loadDBFHeader();
  187. }
  188. function _saveHeaders() {
  189. fwrite($this->SHPFile, pack("NNNNNN", 9994, 0, 0, 0, 0, 0));
  190. fwrite($this->SHPFile, pack("N", $this->fileLength));
  191. fwrite($this->SHPFile, pack("V", 1000));
  192. fwrite($this->SHPFile, pack("V", $this->shapeType));
  193. fwrite($this->SHPFile, packDouble($this->boundingBox['xmin']));
  194. fwrite($this->SHPFile, packDouble($this->boundingBox['ymin']));
  195. fwrite($this->SHPFile, packDouble($this->boundingBox['xmax']));
  196. fwrite($this->SHPFile, packDouble($this->boundingBox['ymax']));
  197. fwrite($this->SHPFile, pack("dddd", 0, 0, 0, 0));
  198. fwrite($this->SHXFile, pack("NNNNNN", 9994, 0, 0, 0, 0, 0));
  199. fwrite($this->SHXFile, pack("N", 50 + 4*count($this->records)));
  200. fwrite($this->SHXFile, pack("V", 1000));
  201. fwrite($this->SHXFile, pack("V", $this->shapeType));
  202. fwrite($this->SHXFile, packDouble($this->boundingBox['xmin']));
  203. fwrite($this->SHXFile, packDouble($this->boundingBox['ymin']));
  204. fwrite($this->SHXFile, packDouble($this->boundingBox['xmax']));
  205. fwrite($this->SHXFile, packDouble($this->boundingBox['ymax']));
  206. fwrite($this->SHXFile, pack("dddd", 0, 0, 0, 0));
  207. }
  208. function _loadRecords() {
  209. fseek($this->SHPFile, 100);
  210. while (!feof($this->SHPFile)) {
  211. $bByte = ftell($this->SHPFile);
  212. $record = new ShapeRecord(-1);
  213. $record->loadFromFile($this->SHPFile, $this->DBFFile);
  214. $eByte = ftell($this->SHPFile);
  215. if (($eByte <= $bByte) || ($record->lastError != "")) {
  216. return false;
  217. }
  218. $this->records[] = $record;
  219. }
  220. }
  221. function _saveRecords() {
  222. if (file_exists(str_replace('.*', '.dbf', $this->FileName))) {
  223. @unlink(str_replace('.*', '.dbf', $this->FileName));
  224. }
  225. if (!($this->DBFFile = @dbase_create(str_replace('.*', '.dbf', $this->FileName), $this->DBFHeader))) {
  226. return $this->setError(sprintf("It wasn't possible to create the DBase file '%s'", str_replace('.*', '.dbf', $this->FileName)));
  227. }
  228. $offset = 50;
  229. if (is_array($this->records) && (count($this->records) > 0)) {
  230. reset($this->records);
  231. while (list($index, $record) = each($this->records)) {
  232. //Save the record to the .shp file
  233. $record->saveToFile($this->SHPFile, $this->DBFFile, $index + 1);
  234. //Save the record to the .shx file
  235. fwrite($this->SHXFile, pack("N", $offset));
  236. fwrite($this->SHXFile, pack("N", $record->getContentLength()));
  237. $offset += (4 + $record->getContentLength());
  238. }
  239. }
  240. @dbase_pack($this->DBFFile);
  241. }
  242. function _openSHPFile($toWrite = false) {
  243. $this->SHPFile = @fopen(str_replace('.*', '.shp', $this->FileName), ($toWrite ? "wb+" : "rb"));
  244. if (!$this->SHPFile) {
  245. return $this->setError(sprintf("It wasn't possible to open the Shape file '%s'", str_replace('.*', '.shp', $this->FileName)));
  246. }
  247. return TRUE;
  248. }
  249. function _closeSHPFile() {
  250. if ($this->SHPFile) {
  251. fclose($this->SHPFile);
  252. $this->SHPFile = NULL;
  253. }
  254. }
  255. function _openSHXFile($toWrite = false) {
  256. $this->SHXFile = @fopen(str_replace('.*', '.shx', $this->FileName), ($toWrite ? "wb+" : "rb"));
  257. if (!$this->SHXFile) {
  258. return $this->setError(sprintf("It wasn't possible to open the Index file '%s'", str_replace('.*', '.shx', $this->FileName)));
  259. }
  260. return TRUE;
  261. }
  262. function _closeSHXFile() {
  263. if ($this->SHXFile) {
  264. fclose($this->SHXFile);
  265. $this->SHXFile = NULL;
  266. }
  267. }
  268. function _openDBFFile($toWrite = false) {
  269. $checkFunction = $toWrite ? "is_writable" : "is_readable";
  270. if (($toWrite) && (!file_exists(str_replace('.*', '.dbf', $this->FileName)))) {
  271. if (!@dbase_create(str_replace('.*', '.dbf', $this->FileName), $this->DBFHeader)) {
  272. return $this->setError(sprintf("It wasn't possible to create the DBase file '%s'", str_replace('.*', '.dbf', $this->FileName)));
  273. }
  274. }
  275. if ($checkFunction(str_replace('.*', '.dbf', $this->FileName))) {
  276. $this->DBFFile = dbase_open(str_replace('.*', '.dbf', $this->FileName), ($toWrite ? 2 : 0));
  277. if (!$this->DBFFile) {
  278. return $this->setError(sprintf("It wasn't possible to open the DBase file '%s'", str_replace('.*', '.dbf', $this->FileName)));
  279. }
  280. } else {
  281. return $this->setError(sprintf("It wasn't possible to find the DBase file '%s'", str_replace('.*', '.dbf', $this->FileName)));
  282. }
  283. return TRUE;
  284. }
  285. function _closeDBFFile() {
  286. if ($this->DBFFile) {
  287. dbase_close($this->DBFFile);
  288. $this->DBFFile = NULL;
  289. }
  290. }
  291. function setError($error) {
  292. $this->lastError = $error;
  293. return false;
  294. }
  295. }
  296. class ShapeRecord {
  297. var $SHPFile = NULL;
  298. var $DBFFile = NULL;
  299. var $recordNumber = NULL;
  300. var $shapeType = NULL;
  301. var $lastError = "";
  302. var $SHPData = array();
  303. var $DBFData = array();
  304. function ShapeRecord($shapeType) {
  305. $this->shapeType = $shapeType;
  306. }
  307. function loadFromFile(&$SHPFile, &$DBFFile) {
  308. $this->SHPFile = $SHPFile;
  309. $this->DBFFile = $DBFFile;
  310. $this->_loadHeaders();
  311. switch ($this->shapeType) {
  312. case 0:
  313. $this->_loadNullRecord();
  314. break;
  315. case 1:
  316. $this->_loadPointRecord();
  317. break;
  318. case 3:
  319. $this->_loadPolyLineRecord();
  320. break;
  321. case 5:
  322. $this->_loadPolygonRecord();
  323. break;
  324. case 8:
  325. $this->_loadMultiPointRecord();
  326. break;
  327. default:
  328. $this->setError(sprintf("The Shape Type '%s' is not supported.", $this->shapeType));
  329. break;
  330. }
  331. $this->_loadDBFData();
  332. }
  333. function saveToFile(&$SHPFile, &$DBFFile, $recordNumber) {
  334. $this->SHPFile = $SHPFile;
  335. $this->DBFFile = $DBFFile;
  336. $this->recordNumber = $recordNumber;
  337. $this->_saveHeaders();
  338. switch ($this->shapeType) {
  339. case 0:
  340. $this->_saveNullRecord();
  341. break;
  342. case 1:
  343. $this->_savePointRecord();
  344. break;
  345. case 3:
  346. $this->_savePolyLineRecord();
  347. break;
  348. case 5:
  349. $this->_savePolygonRecord();
  350. break;
  351. case 8:
  352. $this->_saveMultiPointRecord();
  353. break;
  354. default:
  355. $this->setError(sprintf("The Shape Type '%s' is not supported.", $this->shapeType));
  356. break;
  357. }
  358. $this->_saveDBFData();
  359. }
  360. function updateDBFInfo($header) {
  361. $tmp = $this->DBFData;
  362. unset($this->DBFData);
  363. $this->DBFData = array();
  364. reset($header);
  365. while (list($key, $value) = each($header)) {
  366. $this->DBFData[$value[0]] = (isset($tmp[$value[0]])) ? $tmp[$value[0]] : "";
  367. }
  368. }
  369. function _loadHeaders() {
  370. $this->recordNumber = loadData("N", fread($this->SHPFile, 4));
  371. $tmp = loadData("N", fread($this->SHPFile, 4)); //We read the length of the record
  372. $this->shapeType = loadData("V", fread($this->SHPFile, 4));
  373. }
  374. function _saveHeaders() {
  375. fwrite($this->SHPFile, pack("N", $this->recordNumber));
  376. fwrite($this->SHPFile, pack("N", $this->getContentLength()));
  377. fwrite($this->SHPFile, pack("V", $this->shapeType));
  378. }
  379. function _loadPoint() {
  380. $data = array();
  381. $data["x"] = loadData("d", fread($this->SHPFile, 8));
  382. $data["y"] = loadData("d", fread($this->SHPFile, 8));
  383. return $data;
  384. }
  385. function _savePoint($data) {
  386. fwrite($this->SHPFile, packDouble($data["x"]));
  387. fwrite($this->SHPFile, packDouble($data["y"]));
  388. }
  389. function _loadNullRecord() {
  390. $this->SHPData = array();
  391. }
  392. function _saveNullRecord() {
  393. //Don't save anything
  394. }
  395. function _loadPointRecord() {
  396. $this->SHPData = $this->_loadPoint();
  397. }
  398. function _savePointRecord() {
  399. $this->_savePoint($this->SHPData);
  400. }
  401. function _loadMultiPointRecord() {
  402. $this->SHPData = array();
  403. $this->SHPData["xmin"] = loadData("d", fread($this->SHPFile, 8));
  404. $this->SHPData["ymin"] = loadData("d", fread($this->SHPFile, 8));
  405. $this->SHPData["xmax"] = loadData("d", fread($this->SHPFile, 8));
  406. $this->SHPData["ymax"] = loadData("d", fread($this->SHPFile, 8));
  407. $this->SHPData["numpoints"] = loadData("V", fread($this->SHPFile, 4));
  408. for ($i = 0; $i <= $this->SHPData["numpoints"]; $i++) {
  409. $this->SHPData["points"][] = $this->_loadPoint();
  410. }
  411. }
  412. function _saveMultiPointRecord() {
  413. fwrite($this->SHPFile, pack("dddd", $this->SHPData["xmin"], $this->SHPData["ymin"], $this->SHPData["xmax"], $this->SHPData["ymax"]));
  414. fwrite($this->SHPFile, pack("V", $this->SHPData["numpoints"]));
  415. for ($i = 0; $i <= $this->SHPData["numpoints"]; $i++) {
  416. $this->_savePoint($this->SHPData["points"][$i]);
  417. }
  418. }
  419. function _loadPolyLineRecord() {
  420. $this->SHPData = array();
  421. $this->SHPData["xmin"] = loadData("d", fread($this->SHPFile, 8));
  422. $this->SHPData["ymin"] = loadData("d", fread($this->SHPFile, 8));
  423. $this->SHPData["xmax"] = loadData("d", fread($this->SHPFile, 8));
  424. $this->SHPData["ymax"] = loadData("d", fread($this->SHPFile, 8));
  425. $this->SHPData["numparts"] = loadData("V", fread($this->SHPFile, 4));
  426. $this->SHPData["numpoints"] = loadData("V", fread($this->SHPFile, 4));
  427. for ($i = 0; $i < $this->SHPData["numparts"]; $i++) {
  428. $this->SHPData["parts"][$i] = loadData("V", fread($this->SHPFile, 4));
  429. }
  430. $firstIndex = ftell($this->SHPFile);
  431. $readPoints = 0;
  432. reset($this->SHPData["parts"]);
  433. while (list($partIndex, $partData) = each($this->SHPData["parts"])) {
  434. if (!isset($this->SHPData["parts"][$partIndex]["points"]) || !is_array($this->SHPData["parts"][$partIndex]["points"])) {
  435. $this->SHPData["parts"][$partIndex] = array();
  436. $this->SHPData["parts"][$partIndex]["points"] = array();
  437. }
  438. while (!in_array($readPoints, $this->SHPData["parts"]) && ($readPoints < ($this->SHPData["numpoints"])) && !feof($this->SHPFile)) {
  439. $this->SHPData["parts"][$partIndex]["points"][] = $this->_loadPoint();
  440. $readPoints++;
  441. }
  442. }
  443. fseek($this->SHPFile, $firstIndex + ($readPoints*16));
  444. }
  445. function _savePolyLineRecord() {
  446. fwrite($this->SHPFile, pack("dddd", $this->SHPData["xmin"], $this->SHPData["ymin"], $this->SHPData["xmax"], $this->SHPData["ymax"]));
  447. fwrite($this->SHPFile, pack("VV", $this->SHPData["numparts"], $this->SHPData["numpoints"]));
  448. for ($i = 0; $i < $this->SHPData["numparts"]; $i++) {
  449. fwrite($this->SHPFile, pack("V", count($this->SHPData["parts"][$i])));
  450. }
  451. reset($this->SHPData["parts"]);
  452. foreach ($this->SHPData["parts"] as $partData){
  453. reset($partData["points"]);
  454. while (list($pointIndex, $pointData) = each($partData["points"])) {
  455. $this->_savePoint($pointData);
  456. }
  457. }
  458. }
  459. function _loadPolygonRecord() {
  460. $this->_loadPolyLineRecord();
  461. }
  462. function _savePolygonRecord() {
  463. $this->_savePolyLineRecord();
  464. }
  465. function addPoint($point, $partIndex = 0) {
  466. switch ($this->shapeType) {
  467. case 0:
  468. //Don't add anything
  469. break;
  470. case 1:
  471. //Substitutes the value of the current point
  472. $this->SHPData = $point;
  473. break;
  474. case 3:
  475. case 5:
  476. //Adds a new point to the selected part
  477. if (!isset($this->SHPData["xmin"]) || ($this->SHPData["xmin"] > $point["x"])) $this->SHPData["xmin"] = $point["x"];
  478. if (!isset($this->SHPData["ymin"]) || ($this->SHPData["ymin"] > $point["y"])) $this->SHPData["ymin"] = $point["y"];
  479. if (!isset($this->SHPData["xmax"]) || ($this->SHPData["xmax"] < $point["x"])) $this->SHPData["xmax"] = $point["x"];
  480. if (!isset($this->SHPData["ymax"]) || ($this->SHPData["ymax"] < $point["y"])) $this->SHPData["ymax"] = $point["y"];
  481. $this->SHPData["parts"][$partIndex]["points"][] = $point;
  482. $this->SHPData["numparts"] = count($this->SHPData["parts"]);
  483. $this->SHPData["numpoints"]++;
  484. break;
  485. case 8:
  486. //Adds a new point
  487. if (!isset($this->SHPData["xmin"]) || ($this->SHPData["xmin"] > $point["x"])) $this->SHPData["xmin"] = $point["x"];
  488. if (!isset($this->SHPData["ymin"]) || ($this->SHPData["ymin"] > $point["y"])) $this->SHPData["ymin"] = $point["y"];
  489. if (!isset($this->SHPData["xmax"]) || ($this->SHPData["xmax"] < $point["x"])) $this->SHPData["xmax"] = $point["x"];
  490. if (!isset($this->SHPData["ymax"]) || ($this->SHPData["ymax"] < $point["y"])) $this->SHPData["ymax"] = $point["y"];
  491. $this->SHPData["points"][] = $point;
  492. $this->SHPData["numpoints"]++;
  493. break;
  494. default:
  495. $this->setError(sprintf("The Shape Type '%s' is not supported.", $this->shapeType));
  496. break;
  497. }
  498. }
  499. function deletePoint($pointIndex = 0, $partIndex = 0) {
  500. switch ($this->shapeType) {
  501. case 0:
  502. //Don't delete anything
  503. break;
  504. case 1:
  505. //Sets the value of the point to zero
  506. $this->SHPData["x"] = 0.0;
  507. $this->SHPData["y"] = 0.0;
  508. break;
  509. case 3:
  510. case 5:
  511. //Deletes the point from the selected part, if exists
  512. if (isset($this->SHPData["parts"][$partIndex]) && isset($this->SHPData["parts"][$partIndex]["points"][$pointIndex])) {
  513. for ($i = $pointIndex; $i < (count($this->SHPData["parts"][$partIndex]["points"]) - 1); $i++) {
  514. $this->SHPData["parts"][$partIndex]["points"][$i] = $this->SHPData["parts"][$partIndex]["points"][$i + 1];
  515. }
  516. unset($this->SHPData["parts"][$partIndex]["points"][count($this->SHPData["parts"][$partIndex]["points"]) - 1]);
  517. $this->SHPData["numparts"] = count($this->SHPData["parts"]);
  518. $this->SHPData["numpoints"]--;
  519. }
  520. break;
  521. case 8:
  522. //Deletes the point, if exists
  523. if (isset($this->SHPData["points"][$pointIndex])) {
  524. for ($i = $pointIndex; $i < (count($this->SHPData["points"]) - 1); $i++) {
  525. $this->SHPData["points"][$i] = $this->SHPData["points"][$i + 1];
  526. }
  527. unset($this->SHPData["points"][count($this->SHPData["points"]) - 1]);
  528. $this->SHPData["numpoints"]--;
  529. }
  530. break;
  531. default:
  532. $this->setError(sprintf("The Shape Type '%s' is not supported.", $this->shapeType));
  533. break;
  534. }
  535. }
  536. function getContentLength() {
  537. switch ($this->shapeType) {
  538. case 0:
  539. $result = 0;
  540. break;
  541. case 1:
  542. $result = 10;
  543. break;
  544. case 3:
  545. case 5:
  546. $result = 22 + 2*count($this->SHPData["parts"]);
  547. for ($i = 0; $i < count($this->SHPData["parts"]); $i++) {
  548. $result += 8*count($this->SHPData["parts"][$i]["points"]);
  549. }
  550. break;
  551. case 8:
  552. $result = 20 + 8*count($this->SHPData["points"]);
  553. break;
  554. default:
  555. $result = false;
  556. $this->setError(sprintf("The Shape Type '%s' is not supported.", $this->shapeType));
  557. break;
  558. }
  559. return $result;
  560. }
  561. function _loadDBFData() {
  562. $this->DBFData = @dbase_get_record_with_names($this->DBFFile, $this->recordNumber);
  563. unset($this->DBFData["deleted"]);
  564. }
  565. function _saveDBFData() {
  566. unset($this->DBFData["deleted"]);
  567. if ($this->recordNumber <= dbase_numrecords($this->DBFFile)) {
  568. if (!dbase_replace_record($this->DBFFile, array_values($this->DBFData), $this->recordNumber)) {
  569. $this->setError("I wasn't possible to update the information in the DBF file.");
  570. }
  571. } else {
  572. if (!dbase_add_record($this->DBFFile, array_values($this->DBFData))) {
  573. $this->setError("I wasn't possible to add the information to the DBF file.");
  574. }
  575. }
  576. }
  577. function setError($error) {
  578. $this->lastError = $error;
  579. return false;
  580. }
  581. }
  582. ?>