ShapeFile.lib.php 22 KB

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