ShapeRecord.class.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * This class extends ShapeRecord class to cater the following phpMyAdmin
  5. * specific requirements.
  6. *
  7. * @package PhpMyAdmin-Import
  8. * @subpackage ESRI_Shape
  9. */
  10. if (! defined('PHPMYADMIN')) {
  11. exit;
  12. }
  13. /**
  14. * 1) To load data from .dbf file only when the dBase extension is available.
  15. * 2) To use PMA_importGetNextChunk() functionality to read data, rather than
  16. * reading directly from a file. Using ImportShp::readFromBuffer() in place
  17. * of fread(). This makes it possible to use compressions.
  18. *
  19. * @package PhpMyAdmin-Import
  20. * @subpackage ESRI_Shape
  21. */
  22. class PMA_ShapeRecord extends ShapeRecord
  23. {
  24. /**
  25. * Loads a geometry data record from the file
  26. *
  27. * @param object &$SHPFile .shp file
  28. * @param object &$DBFFile .dbf file
  29. *
  30. * @return void
  31. * @see ShapeRecord::loadFromFile()
  32. */
  33. function loadFromFile(&$SHPFile, &$DBFFile)
  34. {
  35. $this->DBFFile = $DBFFile;
  36. $this->_loadHeaders();
  37. switch ($this->shapeType) {
  38. case 0:
  39. $this->_loadNullRecord();
  40. break;
  41. case 1:
  42. $this->_loadPointRecord();
  43. break;
  44. case 3:
  45. $this->_loadPolyLineRecord();
  46. break;
  47. case 5:
  48. $this->_loadPolygonRecord();
  49. break;
  50. case 8:
  51. $this->_loadMultiPointRecord();
  52. break;
  53. default:
  54. $this->setError(
  55. sprintf(
  56. __("Geometry type '%s' is not supported by MySQL."),
  57. $this->shapeType
  58. )
  59. );
  60. break;
  61. }
  62. if (extension_loaded('dbase') && isset($this->DBFFile)) {
  63. $this->_loadDBFData();
  64. }
  65. }
  66. /**
  67. * Loads metadata from the ESRI shape record header
  68. *
  69. * @return void
  70. * @see ShapeRecord::_loadHeaders()
  71. */
  72. function _loadHeaders()
  73. {
  74. $this->recordNumber = loadData("N", ImportShp::readFromBuffer(4));
  75. ImportShp::readFromBuffer(4);
  76. $this->shapeType = loadData("V", ImportShp::readFromBuffer(4));
  77. }
  78. /**
  79. * Loads data from a point record
  80. *
  81. * @return void
  82. * @see ShapeRecord::_loadPoint()
  83. */
  84. function _loadPoint()
  85. {
  86. $data = array();
  87. $data["x"] = loadData("d", ImportShp::readFromBuffer(8));
  88. $data["y"] = loadData("d", ImportShp::readFromBuffer(8));
  89. return $data;
  90. }
  91. /**
  92. * Loads data from a multipoint record
  93. *
  94. * @return void
  95. * @see ShapeRecord::_loadMultiPointRecord()
  96. */
  97. function _loadMultiPointRecord()
  98. {
  99. $this->SHPData = array();
  100. $this->SHPData["xmin"] = loadData("d", ImportShp::readFromBuffer(8));
  101. $this->SHPData["ymin"] = loadData("d", ImportShp::readFromBuffer(8));
  102. $this->SHPData["xmax"] = loadData("d", ImportShp::readFromBuffer(8));
  103. $this->SHPData["ymax"] = loadData("d", ImportShp::readFromBuffer(8));
  104. $this->SHPData["numpoints"] = loadData("V", ImportShp::readFromBuffer(4));
  105. for ($i = 0; $i <= $this->SHPData["numpoints"]; $i++) {
  106. $this->SHPData["points"][] = $this->_loadPoint();
  107. }
  108. }
  109. /**
  110. * Loads data from a polyline record
  111. *
  112. * @return void
  113. * @see ShapeRecord::_loadPolyLineRecord()
  114. */
  115. function _loadPolyLineRecord()
  116. {
  117. $this->SHPData = array();
  118. $this->SHPData["xmin"] = loadData("d", ImportShp::readFromBuffer(8));
  119. $this->SHPData["ymin"] = loadData("d", ImportShp::readFromBuffer(8));
  120. $this->SHPData["xmax"] = loadData("d", ImportShp::readFromBuffer(8));
  121. $this->SHPData["ymax"] = loadData("d", ImportShp::readFromBuffer(8));
  122. $this->SHPData["numparts"] = loadData("V", ImportShp::readFromBuffer(4));
  123. $this->SHPData["numpoints"] = loadData("V", ImportShp::readFromBuffer(4));
  124. for ($i = 0; $i < $this->SHPData["numparts"]; $i++) {
  125. $this->SHPData["parts"][$i] = loadData(
  126. "V", ImportShp::readFromBuffer(4)
  127. );
  128. }
  129. $readPoints = 0;
  130. reset($this->SHPData["parts"]);
  131. while (list($partIndex, $partData) = each($this->SHPData["parts"])) {
  132. if (! isset($this->SHPData["parts"][$partIndex]["points"])
  133. || !is_array($this->SHPData["parts"][$partIndex]["points"])
  134. ) {
  135. $this->SHPData["parts"][$partIndex] = array();
  136. $this->SHPData["parts"][$partIndex]["points"] = array();
  137. }
  138. while (! in_array($readPoints, $this->SHPData["parts"])
  139. && ($readPoints < ($this->SHPData["numpoints"]))
  140. ) {
  141. $this->SHPData["parts"][$partIndex]["points"][]
  142. = $this->_loadPoint();
  143. $readPoints++;
  144. }
  145. }
  146. }
  147. }
  148. ?>