GisMultiPolygon.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Handles actions related to GIS MULTIPOLYGON objects
  5. *
  6. * @package PhpMyAdmin-GIS
  7. */
  8. namespace PhpMyAdmin\Gis;
  9. use TCPDF;
  10. /**
  11. * Handles actions related to GIS MULTIPOLYGON objects
  12. *
  13. * @package PhpMyAdmin-GIS
  14. */
  15. class GisMultiPolygon extends GisGeometry
  16. {
  17. // Hold the singleton instance of the class
  18. private static $_instance;
  19. /**
  20. * A private constructor; prevents direct creation of object.
  21. *
  22. * @access private
  23. */
  24. private function __construct()
  25. {
  26. }
  27. /**
  28. * Returns the singleton.
  29. *
  30. * @return GisMultiPolygon the singleton
  31. * @access public
  32. */
  33. public static function singleton()
  34. {
  35. if (!isset(self::$_instance)) {
  36. $class = __CLASS__;
  37. self::$_instance = new $class;
  38. }
  39. return self::$_instance;
  40. }
  41. /**
  42. * Scales each row.
  43. *
  44. * @param string $spatial spatial data of a row
  45. *
  46. * @return array an array containing the min, max values for x and y coordinates
  47. * @access public
  48. */
  49. public function scaleRow($spatial)
  50. {
  51. $min_max = array();
  52. // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
  53. $multipolygon
  54. = mb_substr(
  55. $spatial,
  56. 15,
  57. mb_strlen($spatial) - 18
  58. );
  59. // Separate each polygon
  60. $polygons = explode(")),((", $multipolygon);
  61. foreach ($polygons as $polygon) {
  62. // If the polygon doesn't have an inner ring, use polygon itself
  63. if (mb_strpos($polygon, "),(") === false) {
  64. $ring = $polygon;
  65. } else {
  66. // Separate outer ring and use it to determine min-max
  67. $parts = explode("),(", $polygon);
  68. $ring = $parts[0];
  69. }
  70. $min_max = $this->setMinMax($ring, $min_max);
  71. }
  72. return $min_max;
  73. }
  74. /**
  75. * Adds to the PNG image object, the data related to a row in the GIS dataset.
  76. *
  77. * @param string $spatial GIS MULTIPOLYGON object
  78. * @param string $label Label for the GIS MULTIPOLYGON object
  79. * @param string $fill_color Color for the GIS MULTIPOLYGON object
  80. * @param array $scale_data Array containing data related to scaling
  81. * @param object $image Image object
  82. *
  83. * @return object the modified image object
  84. * @access public
  85. */
  86. public function prepareRowAsPng(
  87. $spatial,
  88. $label,
  89. $fill_color,
  90. array $scale_data,
  91. $image
  92. ) {
  93. // allocate colors
  94. $black = imagecolorallocate($image, 0, 0, 0);
  95. $red = hexdec(mb_substr($fill_color, 1, 2));
  96. $green = hexdec(mb_substr($fill_color, 3, 2));
  97. $blue = hexdec(mb_substr($fill_color, 4, 2));
  98. $color = imagecolorallocate($image, $red, $green, $blue);
  99. // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
  100. $multipolygon
  101. = mb_substr(
  102. $spatial,
  103. 15,
  104. mb_strlen($spatial) - 18
  105. );
  106. // Separate each polygon
  107. $polygons = explode(")),((", $multipolygon);
  108. $first_poly = true;
  109. foreach ($polygons as $polygon) {
  110. // If the polygon doesn't have an inner polygon
  111. if (mb_strpos($polygon, "),(") === false) {
  112. $points_arr = $this->extractPoints($polygon, $scale_data, true);
  113. } else {
  114. // Separate outer and inner polygons
  115. $parts = explode("),(", $polygon);
  116. $outer = $parts[0];
  117. $inner = array_slice($parts, 1);
  118. $points_arr = $this->extractPoints($outer, $scale_data, true);
  119. foreach ($inner as $inner_poly) {
  120. $points_arr = array_merge(
  121. $points_arr,
  122. $this->extractPoints($inner_poly, $scale_data, true)
  123. );
  124. }
  125. }
  126. // draw polygon
  127. imagefilledpolygon($image, $points_arr, sizeof($points_arr) / 2, $color);
  128. // mark label point if applicable
  129. if (isset($label) && trim($label) != '' && $first_poly) {
  130. $label_point = array($points_arr[2], $points_arr[3]);
  131. }
  132. $first_poly = false;
  133. }
  134. // print label if applicable
  135. if (isset($label_point)) {
  136. imagestring(
  137. $image,
  138. 1,
  139. $points_arr[2],
  140. $points_arr[3],
  141. trim($label),
  142. $black
  143. );
  144. }
  145. return $image;
  146. }
  147. /**
  148. * Adds to the TCPDF instance, the data related to a row in the GIS dataset.
  149. *
  150. * @param string $spatial GIS MULTIPOLYGON object
  151. * @param string $label Label for the GIS MULTIPOLYGON object
  152. * @param string $fill_color Color for the GIS MULTIPOLYGON object
  153. * @param array $scale_data Array containing data related to scaling
  154. * @param TCPDF $pdf TCPDF instance
  155. *
  156. * @return TCPDF the modified TCPDF instance
  157. * @access public
  158. */
  159. public function prepareRowAsPdf($spatial, $label, $fill_color, array $scale_data, $pdf)
  160. {
  161. // allocate colors
  162. $red = hexdec(mb_substr($fill_color, 1, 2));
  163. $green = hexdec(mb_substr($fill_color, 3, 2));
  164. $blue = hexdec(mb_substr($fill_color, 4, 2));
  165. $color = array($red, $green, $blue);
  166. // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
  167. $multipolygon
  168. = mb_substr(
  169. $spatial,
  170. 15,
  171. mb_strlen($spatial) - 18
  172. );
  173. // Separate each polygon
  174. $polygons = explode(")),((", $multipolygon);
  175. $first_poly = true;
  176. foreach ($polygons as $polygon) {
  177. // If the polygon doesn't have an inner polygon
  178. if (mb_strpos($polygon, "),(") === false) {
  179. $points_arr = $this->extractPoints($polygon, $scale_data, true);
  180. } else {
  181. // Separate outer and inner polygons
  182. $parts = explode("),(", $polygon);
  183. $outer = $parts[0];
  184. $inner = array_slice($parts, 1);
  185. $points_arr = $this->extractPoints($outer, $scale_data, true);
  186. foreach ($inner as $inner_poly) {
  187. $points_arr = array_merge(
  188. $points_arr,
  189. $this->extractPoints($inner_poly, $scale_data, true)
  190. );
  191. }
  192. }
  193. // draw polygon
  194. $pdf->Polygon($points_arr, 'F*', array(), $color, true);
  195. // mark label point if applicable
  196. if (isset($label) && trim($label) != '' && $first_poly) {
  197. $label_point = array($points_arr[2], $points_arr[3]);
  198. }
  199. $first_poly = false;
  200. }
  201. // print label if applicable
  202. if (isset($label_point)) {
  203. $pdf->SetXY($label_point[0], $label_point[1]);
  204. $pdf->SetFontSize(5);
  205. $pdf->Cell(0, 0, trim($label));
  206. }
  207. return $pdf;
  208. }
  209. /**
  210. * Prepares and returns the code related to a row in the GIS dataset as SVG.
  211. *
  212. * @param string $spatial GIS MULTIPOLYGON object
  213. * @param string $label Label for the GIS MULTIPOLYGON object
  214. * @param string $fill_color Color for the GIS MULTIPOLYGON object
  215. * @param array $scale_data Array containing data related to scaling
  216. *
  217. * @return string the code related to a row in the GIS dataset
  218. * @access public
  219. */
  220. public function prepareRowAsSvg($spatial, $label, $fill_color, array $scale_data)
  221. {
  222. $polygon_options = array(
  223. 'name' => $label,
  224. 'class' => 'multipolygon vector',
  225. 'stroke' => 'black',
  226. 'stroke-width' => 0.5,
  227. 'fill' => $fill_color,
  228. 'fill-rule' => 'evenodd',
  229. 'fill-opacity' => 0.8,
  230. );
  231. $row = '';
  232. // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
  233. $multipolygon
  234. = mb_substr(
  235. $spatial,
  236. 15,
  237. mb_strlen($spatial) - 18
  238. );
  239. // Separate each polygon
  240. $polygons = explode(")),((", $multipolygon);
  241. foreach ($polygons as $polygon) {
  242. $row .= '<path d="';
  243. // If the polygon doesn't have an inner polygon
  244. if (mb_strpos($polygon, "),(") === false) {
  245. $row .= $this->_drawPath($polygon, $scale_data);
  246. } else {
  247. // Separate outer and inner polygons
  248. $parts = explode("),(", $polygon);
  249. $outer = $parts[0];
  250. $inner = array_slice($parts, 1);
  251. $row .= $this->_drawPath($outer, $scale_data);
  252. foreach ($inner as $inner_poly) {
  253. $row .= $this->_drawPath($inner_poly, $scale_data);
  254. }
  255. }
  256. $polygon_options['id'] = $label . rand();
  257. $row .= '"';
  258. foreach ($polygon_options as $option => $val) {
  259. $row .= ' ' . $option . '="' . trim($val) . '"';
  260. }
  261. $row .= '/>';
  262. }
  263. return $row;
  264. }
  265. /**
  266. * Prepares JavaScript related to a row in the GIS dataset
  267. * to visualize it with OpenLayers.
  268. *
  269. * @param string $spatial GIS MULTIPOLYGON object
  270. * @param int $srid Spatial reference ID
  271. * @param string $label Label for the GIS MULTIPOLYGON object
  272. * @param string $fill_color Color for the GIS MULTIPOLYGON object
  273. * @param array $scale_data Array containing data related to scaling
  274. *
  275. * @return string JavaScript related to a row in the GIS dataset
  276. * @access public
  277. */
  278. public function prepareRowAsOl($spatial, $srid, $label, $fill_color, array $scale_data)
  279. {
  280. $style_options = array(
  281. 'strokeColor' => '#000000',
  282. 'strokeWidth' => 0.5,
  283. 'fillColor' => $fill_color,
  284. 'fillOpacity' => 0.8,
  285. 'label' => $label,
  286. 'fontSize' => 10,
  287. );
  288. if ($srid == 0) {
  289. $srid = 4326;
  290. }
  291. $row = $this->getBoundsForOl($srid, $scale_data);
  292. // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
  293. $multipolygon
  294. = mb_substr(
  295. $spatial,
  296. 15,
  297. mb_strlen($spatial) - 18
  298. );
  299. // Separate each polygon
  300. $polygons = explode(")),((", $multipolygon);
  301. $row .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector('
  302. . 'new OpenLayers.Geometry.MultiPolygon('
  303. . $this->getPolygonArrayForOpenLayers($polygons, $srid)
  304. . '), null, ' . json_encode($style_options) . '));';
  305. return $row;
  306. }
  307. /**
  308. * Draws a ring of the polygon using SVG path element.
  309. *
  310. * @param string $polygon The ring
  311. * @param array $scale_data Array containing data related to scaling
  312. *
  313. * @return string the code to draw the ring
  314. * @access private
  315. */
  316. private function _drawPath($polygon, array $scale_data)
  317. {
  318. $points_arr = $this->extractPoints($polygon, $scale_data);
  319. $row = ' M ' . $points_arr[0][0] . ', ' . $points_arr[0][1];
  320. $other_points = array_slice($points_arr, 1, count($points_arr) - 2);
  321. foreach ($other_points as $point) {
  322. $row .= ' L ' . $point[0] . ', ' . $point[1];
  323. }
  324. $row .= ' Z ';
  325. return $row;
  326. }
  327. /**
  328. * Generate the WKT with the set of parameters passed by the GIS editor.
  329. *
  330. * @param array $gis_data GIS data
  331. * @param int $index Index into the parameter object
  332. * @param string $empty Value for empty points
  333. *
  334. * @return string WKT with the set of parameters passed by the GIS editor
  335. * @access public
  336. */
  337. public function generateWkt(array $gis_data, $index, $empty = '')
  338. {
  339. $data_row = $gis_data[$index]['MULTIPOLYGON'];
  340. $no_of_polygons = isset($data_row['no_of_polygons'])
  341. ? $data_row['no_of_polygons'] : 1;
  342. if ($no_of_polygons < 1) {
  343. $no_of_polygons = 1;
  344. }
  345. $wkt = 'MULTIPOLYGON(';
  346. for ($k = 0; $k < $no_of_polygons; $k++) {
  347. $no_of_lines = isset($data_row[$k]['no_of_lines'])
  348. ? $data_row[$k]['no_of_lines'] : 1;
  349. if ($no_of_lines < 1) {
  350. $no_of_lines = 1;
  351. }
  352. $wkt .= '(';
  353. for ($i = 0; $i < $no_of_lines; $i++) {
  354. $no_of_points = isset($data_row[$k][$i]['no_of_points'])
  355. ? $data_row[$k][$i]['no_of_points'] : 4;
  356. if ($no_of_points < 4) {
  357. $no_of_points = 4;
  358. }
  359. $wkt .= '(';
  360. for ($j = 0; $j < $no_of_points; $j++) {
  361. $wkt .= ((isset($data_row[$k][$i][$j]['x'])
  362. && trim($data_row[$k][$i][$j]['x']) != '')
  363. ? $data_row[$k][$i][$j]['x'] : $empty)
  364. . ' ' . ((isset($data_row[$k][$i][$j]['y'])
  365. && trim($data_row[$k][$i][$j]['y']) != '')
  366. ? $data_row[$k][$i][$j]['y'] : $empty) . ',';
  367. }
  368. $wkt
  369. = mb_substr(
  370. $wkt,
  371. 0,
  372. mb_strlen($wkt) - 1
  373. );
  374. $wkt .= '),';
  375. }
  376. $wkt
  377. = mb_substr(
  378. $wkt,
  379. 0,
  380. mb_strlen($wkt) - 1
  381. );
  382. $wkt .= '),';
  383. }
  384. $wkt
  385. = mb_substr(
  386. $wkt,
  387. 0,
  388. mb_strlen($wkt) - 1
  389. );
  390. $wkt .= ')';
  391. return $wkt;
  392. }
  393. /**
  394. * Generate the WKT for the data from ESRI shape files.
  395. *
  396. * @param array $row_data GIS data
  397. *
  398. * @return string the WKT for the data from ESRI shape files
  399. * @access public
  400. */
  401. public function getShape(array $row_data)
  402. {
  403. // Determines whether each line ring is an inner ring or an outer ring.
  404. // If it's an inner ring get a point on the surface which can be used to
  405. // correctly classify inner rings to their respective outer rings.
  406. foreach ($row_data['parts'] as $i => $ring) {
  407. $row_data['parts'][$i]['isOuter']
  408. = GisPolygon::isOuterRing($ring['points']);
  409. }
  410. // Find points on surface for inner rings
  411. foreach ($row_data['parts'] as $i => $ring) {
  412. if (!$ring['isOuter']) {
  413. $row_data['parts'][$i]['pointOnSurface']
  414. = GisPolygon::getPointOnSurface($ring['points']);
  415. }
  416. }
  417. // Classify inner rings to their respective outer rings.
  418. foreach ($row_data['parts'] as $j => $ring1) {
  419. if ($ring1['isOuter']) {
  420. continue;
  421. }
  422. foreach ($row_data['parts'] as $k => $ring2) {
  423. if (!$ring2['isOuter']) {
  424. continue;
  425. }
  426. // If the pointOnSurface of the inner ring
  427. // is also inside the outer ring
  428. if (GisPolygon::isPointInsidePolygon(
  429. $ring1['pointOnSurface'],
  430. $ring2['points']
  431. )
  432. ) {
  433. if (!isset($ring2['inner'])) {
  434. $row_data['parts'][$k]['inner'] = array();
  435. }
  436. $row_data['parts'][$k]['inner'][] = $j;
  437. }
  438. }
  439. }
  440. $wkt = 'MULTIPOLYGON(';
  441. // for each polygon
  442. foreach ($row_data['parts'] as $ring) {
  443. if (!$ring['isOuter']) {
  444. continue;
  445. }
  446. $wkt .= '('; // start of polygon
  447. $wkt .= '('; // start of outer ring
  448. foreach ($ring['points'] as $point) {
  449. $wkt .= $point['x'] . ' ' . $point['y'] . ',';
  450. }
  451. $wkt
  452. = mb_substr(
  453. $wkt,
  454. 0,
  455. mb_strlen($wkt) - 1
  456. );
  457. $wkt .= ')'; // end of outer ring
  458. // inner rings if any
  459. if (isset($ring['inner'])) {
  460. foreach ($ring['inner'] as $j) {
  461. $wkt .= ',('; // start of inner ring
  462. foreach ($row_data['parts'][$j]['points'] as $innerPoint) {
  463. $wkt .= $innerPoint['x'] . ' ' . $innerPoint['y'] . ',';
  464. }
  465. $wkt
  466. = mb_substr(
  467. $wkt,
  468. 0,
  469. mb_strlen($wkt) - 1
  470. );
  471. $wkt .= ')'; // end of inner ring
  472. }
  473. }
  474. $wkt .= '),'; // end of polygon
  475. }
  476. $wkt
  477. = mb_substr(
  478. $wkt,
  479. 0,
  480. mb_strlen($wkt) - 1
  481. );
  482. $wkt .= ')'; // end of multipolygon
  483. return $wkt;
  484. }
  485. /**
  486. * Generate parameters for the GIS data editor from the value of the GIS column.
  487. *
  488. * @param string $value Value of the GIS column
  489. * @param int $index Index of the geometry
  490. *
  491. * @return array params for the GIS data editor from the value of the GIS column
  492. * @access public
  493. */
  494. public function generateParams($value, $index = -1)
  495. {
  496. $params = array();
  497. if ($index == -1) {
  498. $index = 0;
  499. $data = GisGeometry::generateParams($value);
  500. $params['srid'] = $data['srid'];
  501. $wkt = $data['wkt'];
  502. } else {
  503. $params[$index]['gis_type'] = 'MULTIPOLYGON';
  504. $wkt = $value;
  505. }
  506. // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
  507. $multipolygon
  508. = mb_substr(
  509. $wkt,
  510. 15,
  511. mb_strlen($wkt) - 18
  512. );
  513. // Separate each polygon
  514. $polygons = explode(")),((", $multipolygon);
  515. $param_row =& $params[$index]['MULTIPOLYGON'];
  516. $param_row['no_of_polygons'] = count($polygons);
  517. $k = 0;
  518. foreach ($polygons as $polygon) {
  519. // If the polygon doesn't have an inner polygon
  520. if (mb_strpos($polygon, "),(") === false) {
  521. $param_row[$k]['no_of_lines'] = 1;
  522. $points_arr = $this->extractPoints($polygon, null);
  523. $no_of_points = count($points_arr);
  524. $param_row[$k][0]['no_of_points'] = $no_of_points;
  525. for ($i = 0; $i < $no_of_points; $i++) {
  526. $param_row[$k][0][$i]['x'] = $points_arr[$i][0];
  527. $param_row[$k][0][$i]['y'] = $points_arr[$i][1];
  528. }
  529. } else {
  530. // Separate outer and inner polygons
  531. $parts = explode("),(", $polygon);
  532. $param_row[$k]['no_of_lines'] = count($parts);
  533. $j = 0;
  534. foreach ($parts as $ring) {
  535. $points_arr = $this->extractPoints($ring, null);
  536. $no_of_points = count($points_arr);
  537. $param_row[$k][$j]['no_of_points'] = $no_of_points;
  538. for ($i = 0; $i < $no_of_points; $i++) {
  539. $param_row[$k][$j][$i]['x'] = $points_arr[$i][0];
  540. $param_row[$k][$j][$i]['y'] = $points_arr[$i][1];
  541. }
  542. $j++;
  543. }
  544. }
  545. $k++;
  546. }
  547. return $params;
  548. }
  549. }