/lib/Maps/ArcGISDataController.php

https://github.com/Alex8452/Kurogo-Mobile-Web-Doc-Translation · PHP · 175 lines · 133 code · 30 blank · 12 comment · 12 complexity · 68fe706c47c22c444497dcbfceeed3b2 MD5 · raw file

  1. <?php
  2. class ArcGISDataController extends MapDataController
  3. {
  4. protected $DEFAULT_PARSER_CLASS = 'ArcGISParser';
  5. protected $filters = array('f' => 'json');
  6. protected function cacheFolder()
  7. {
  8. return Kurogo::getSiteVar('ARCGIS_CACHE');
  9. }
  10. public function getProjection() {
  11. $this->initializeParser();
  12. return $this->parser->getProjection();
  13. }
  14. public function getListItems($categoryPath=array()) {
  15. // TODO: this only works for servers that have simple layers
  16. // i.e. no layer contains additional sublayers
  17. if (count($categoryPath) > 0) {
  18. $category = array_shift($categoryPath);
  19. $this->initializeParser();
  20. $this->initializeLayers();
  21. $this->parser->selectSubLayer($category);
  22. }
  23. $items = $this->items();
  24. $results = array();
  25. // eliminate empty categories
  26. foreach ($items as $item) {
  27. if (!($item instanceof MapFolder) || count($item->getListItems())) {
  28. $results[] = $item;
  29. }
  30. }
  31. // fast forward for categories that only have one item
  32. while (count($results) == 1) {
  33. $container = current($results);
  34. if (!$container instanceof MapFolder) {
  35. break;
  36. }
  37. $results = $container->getListItems();
  38. }
  39. return $results;
  40. }
  41. public function getTitle() {
  42. if ($this->title !== null) {
  43. return $this->title;
  44. }
  45. $this->initializeParser();
  46. return $this->parser->getTitle();
  47. }
  48. public function items($start=0, $limit=null) {
  49. $this->initializeParser();
  50. $this->initializeLayers();
  51. $this->initializeFeatures();
  52. return $this->parser->getListItems();
  53. }
  54. protected function initializeParser() {
  55. if (!$this->parser->isPopulated()) {
  56. $data = $this->getData();
  57. $this->parseData($data);
  58. }
  59. }
  60. protected function initializeFeatures() {
  61. if (!$this->parser->selectedLayerIsPopulated()) {
  62. $oldBaseURL = $this->baseURL;
  63. $this->parser->setBaseURL($oldBaseURL);
  64. $this->baseURL = $this->parser->getURLForLayerFeatures();
  65. $oldFilters = $this->filters;
  66. $this->filters = $this->parser->getFiltersForLayer();
  67. $data = $this->getData();
  68. $this->parseData($data);
  69. $this->filters = $oldFilters;
  70. $this->baseURL = $oldBaseURL;
  71. }
  72. }
  73. protected function initializeLayers() {
  74. if (!$this->parser->selectedLayerIsInitialized()) {
  75. // set this directly so we don't interfere with cache
  76. $oldBaseURL = $this->baseURL;
  77. $this->parser->setBaseURL($oldBaseURL);
  78. $this->baseURL = $this->parser->getURLForSelectedLayer();
  79. $data = $this->getData();
  80. $this->parseData($data);
  81. $this->baseURL = $oldBaseURL;
  82. }
  83. }
  84. protected function init($args) {
  85. parent::init($args);
  86. if (isset($args['ARCGIS_LAYER_ID'])) {
  87. $this->parser->setDefaultLayer($args['ARCGIS_LAYER_ID']);
  88. }
  89. if (isset($args['ID_FIELD'])) {
  90. $this->parser->setIdFIeld($args['ID_FIELD']);
  91. }
  92. $this->addFilter('f', 'json');
  93. }
  94. public function search($searchText) {
  95. $this->initializeParser();
  96. $this->initializeLayers();
  97. $oldBaseURL = $this->baseURL;
  98. $this->parser->clearCache();
  99. $this->parser->setBaseURL($oldBaseURL);
  100. $this->baseURL = $this->parser->getURLForLayerFeatures();
  101. $this->filters = $this->parser->getFiltersForLayer();
  102. $this->addFilter('text', $searchText);
  103. $data = $this->getData();
  104. $this->parseData($data);
  105. // restore previous state
  106. $this->baseURL = $oldBaseURL;
  107. return $this->getAllLeafNodes();
  108. }
  109. public function searchByProximity($center, $tolerance, $maxItems) {
  110. // TODO: these units are completely wrong (but work for harvard b/c
  111. // their units are in feet); we should use MapProjector to get
  112. // a decent range
  113. $dLatDegrees = $tolerance;
  114. $dLonDegrees = $tolerance;
  115. $maxLat = $center['lat'] + $dLatDegrees;
  116. $minLat = $center['lat'] - $dLatDegrees;
  117. $maxLon = $center['lon'] + $dLonDegrees;
  118. $minLon = $center['lon'] - $dLonDegrees;
  119. $this->initializeParser();
  120. $this->initializeLayers();
  121. $oldBaseURL = $this->baseURL;
  122. $this->parser->setBaseURL($oldBaseURL);
  123. $this->baseURL = $this->parser->getURLForLayerFeatures();
  124. $this->addFilter('geometry', "$minLon,$minLat,$maxLon,$maxLat");
  125. $this->addFilter('geometryType', 'esriGeometryEnvelope');
  126. $this->addFilter('spatialRel', 'esriSpatialRelIntersects');
  127. $this->addFilter('returnGeometry', 'false');
  128. $data = $this->getData();
  129. $this->parseData($data);
  130. // restore previous state
  131. $this->baseURL = $oldBaseURL;
  132. $this->removeAllFilters();
  133. $this->addFilter('f', 'json');
  134. return $this->getAllLeafNodes();
  135. }
  136. // TODO make a standalone method in ArcGISParser that
  137. // that doesn't require us to create a throwaway controller
  138. public static function parserFactory($baseURL) {
  139. $throwawayController = new ArcGISDataController();
  140. $throwawayController->init(array('BASE_URL' => $baseURL));
  141. $data = $throwawayController->getData();
  142. $throwawayController->parseData($data);
  143. return $throwawayController->parser;
  144. }
  145. }