PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/MapGuide/php/LoadMap.php

https://github.com/alonso/fusion
PHP | 317 lines | 234 code | 38 blank | 45 comment | 32 complexity | e7bdc29c5ebebf16e927ae3e82b63869 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /**
  3. * LoadMap
  4. *
  5. * $Id: LoadMap.php 1521 2008-09-11 17:44:26Z madair $
  6. *
  7. * Copyright (c) 2007, DM Solutions Group Inc.
  8. * Permission is hereby granted, free of charge, to any person obtaining a
  9. * copy of this software and associated documentation files (the "Software"),
  10. * to deal in the Software without restriction, including without limitation
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. * and/or sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included
  16. * in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. * DEALINGS IN THE SOFTWARE.
  25. */
  26. /*****************************************************************************
  27. * Purpose: get map initial information
  28. *****************************************************************************/
  29. include('Common.php');
  30. include('Utilities.php');
  31. try
  32. {
  33. $mappingService = $siteConnection->CreateService(MgServiceType::MappingService);
  34. $featureService = $siteConnection->CreateService(MgServiceType::FeatureService);
  35. // Get a runtime map from a map definition
  36. if (isset($_REQUEST['mapid']))
  37. {
  38. $mapid = $_REQUEST['mapid'];
  39. //echo $mapid;
  40. $resourceID = new MgResourceIdentifier($mapid);
  41. $map = new MgMap();
  42. $mapTitle = $resourceID->GetName();
  43. //echo "<br> maname $mapName <br>";
  44. $map->Create($resourceService, $resourceID, $mapTitle);
  45. $mapName = uniqid($mapTitle);
  46. $mapStateId = new MgResourceIdentifier("Session:" . $sessionID . "//" . $mapName . "." . MgResourceType::Map);
  47. //create an empty selection object and store it in the session repository
  48. $sel = new MgSelection($map);
  49. $sel->Save($resourceService, $mapName);
  50. $map->Save($resourceService, $mapStateId);
  51. } else {
  52. $map = new MgMap();
  53. $map->Open($resourceService, $mapName);
  54. $mapTitle = $map->GetName();
  55. $mapid = $map->GetMapDefinition()->ToString();
  56. }
  57. //$sessionId = $map->GetSessionId();
  58. //$mapName = $map->GetName() ;
  59. $extents = $map->GetMapExtent();
  60. @$oMin = $extents->GetLowerLeftCoordinate();
  61. @$oMax = $extents->GetUpperRightCoordinate();
  62. @$srs = $map->GetMapSRS();
  63. if($srs != "")
  64. {
  65. @$csFactory = new MgCoordinateSystemFactory();
  66. @$cs = $csFactory->Create($srs);
  67. @$metersPerUnit = $cs->ConvertCoordinateSystemUnitsToMeters(1.0);
  68. // $unitsType = $cs->GetUnits();
  69. }
  70. else
  71. {
  72. $metersPerUnit = 1.0;
  73. //$unitsType = "Meters";
  74. }
  75. header('Content-type: text/x-json');
  76. header('X-JSON: true');
  77. echo "{";
  78. echo "sessionId:'$sessionID',";
  79. echo "mapId:'$mapid',";
  80. echo "metersPerUnit:$metersPerUnit,";
  81. //echo "mapTitle:'".addslashes(htmlentities($mapTitle))."',";
  82. //echo "mapName:'".addslashes(htmlentities($mapName))."',";
  83. echo "mapTitle:'".addslashes($mapTitle)."',";
  84. echo "mapName:'".addslashes($mapName)."',";
  85. echo "extent:[";
  86. echo $oMin->GetX().",";
  87. echo $oMin->GetY().",";
  88. echo $oMax->GetX().",";
  89. echo $oMax->GetY()."],";
  90. $layers=$map->GetLayers();
  91. echo "layers:[";
  92. $layerSep = '';
  93. for($i=0;$i<$layers->GetCount();$i++)
  94. {
  95. $layer=$layers->GetItem($i);
  96. //only output layers that are part of the 'Normal Group' and
  97. //not the base map group used for tile maps.
  98. echo $layerSep.'{';
  99. OutputLayerInfo($layer, $resourceService, $featureService);
  100. echo '}';
  101. $layerSep = ',';
  102. }
  103. echo "],";
  104. //Get layer groups as xml
  105. $groups = $map->GetLayerGroups();
  106. echo "groups:[";
  107. $groupSep = '';
  108. for($i=0;$i<$groups->GetCount();$i++)
  109. {
  110. $group=$groups->GetItem($i);
  111. echo $groupSep.'{';
  112. OutputGroupInfo($group);
  113. echo '}';
  114. $groupSep = ',';
  115. }
  116. echo"],";
  117. //FiniteDisplayScales for tiled maps
  118. echo "FiniteDisplayScales:[";
  119. for ($i=0; $i<$map->GetFiniteDisplayScaleCount(); $i++)
  120. {
  121. if ($i>0)
  122. echo ",";
  123. echo $map->GetFiniteDisplayScaleAt($i);
  124. }
  125. echo"]";
  126. echo "}";
  127. }
  128. catch (MgException $e)
  129. {
  130. echo "ERROR: " . $e->GetMessage() . "\n";
  131. echo $e->GetDetails() . "\n";
  132. echo $e->GetStackTrace() . "\n";
  133. }
  134. exit;
  135. function buildScaleRanges($layer) {
  136. global $resourceService;
  137. $resID = $layer->GetLayerDefinition();
  138. $layerContent = $resourceService->GetResourceContent($resID);
  139. $xmldoc = DOMDocument::loadXML(ByteReaderToString($layerContent));
  140. $type = 0;
  141. $scaleRanges = $xmldoc->getElementsByTagName('VectorScaleRange');
  142. if($scaleRanges->length == 0) {
  143. $scaleRanges = $xmldoc->getElementsByTagName('GridScaleRange');
  144. if($scaleRanges->length == 0) {
  145. $scaleRanges = $xmldoc->getElementsByTagName('DrawingLayerDefinition');
  146. if($scaleRanges->length == 0) {
  147. return;
  148. }
  149. $type = 2;
  150. } else {
  151. $type = 1;
  152. }
  153. }
  154. $typeStyles = array("PointTypeStyle", "LineTypeStyle", "AreaTypeStyle", "CompositeTypeStyle");
  155. $ruleNames = array("PointRule", "LineRule", "AreaRule", "CompositeRule");
  156. $output = 'scaleRanges: [';
  157. $scaleSep = '';
  158. for($sc = 0; $sc < $scaleRanges->length; $sc++)
  159. {
  160. $scaleRange = $scaleRanges->item($sc);
  161. $minElt = $scaleRange->getElementsByTagName('MinScale');
  162. $maxElt = $scaleRange->getElementsByTagName('MaxScale');
  163. $minScale = "0";
  164. $maxScale = "'infinity'"; // as MDF's VectorScaleRange::MAX_MAP_SCALE
  165. if($minElt->length > 0)
  166. $minScale = $minElt->item(0)->nodeValue;
  167. if($maxElt->length > 0)
  168. $maxScale = $maxElt->item(0)->nodeValue;
  169. $output .= $scaleSep."{";
  170. $output .= "minScale:".$minScale.",";
  171. $output .= "maxScale:".$maxScale;
  172. if($type != 0) {
  173. $output .= "}";
  174. $scaleSep = ',';
  175. break;
  176. }
  177. $output .= ',styles:[';
  178. $styleIndex = 0;
  179. $styleSep = '';
  180. for($ts=0, $count = count($typeStyles); $ts < $count; $ts++)
  181. {
  182. $typeStyle = $scaleRange->getElementsByTagName($typeStyles[$ts]);
  183. $catIndex = 0;
  184. for($st = 0; $st < $typeStyle->length; $st++) {
  185. // We will check if this typestyle is going to be shown in the legend
  186. $showInLegend = $typeStyle->item($st)->getElementsByTagName("ShowInLegend");
  187. if($showInLegend->length > 0)
  188. if($showInLegend->item(0)->nodeValue == "false")
  189. continue; // This typestyle does not need to be shown in the legend
  190. $rules = $typeStyle->item(0)->getElementsByTagName($ruleNames[$ts]);
  191. for($r = 0; $r < $rules->length; $r++) {
  192. $rule = $rules->item($r);
  193. $label = $rule->getElementsByTagName("LegendLabel");
  194. $filter = $rule->getElementsByTagName("Filter");
  195. $labelText = $label->length==1? $label->item(0)->nodeValue: "";
  196. $filterText = $filter->length==1? $filter->item(0)->nodeValue: "";
  197. $output .= $styleSep."{";
  198. $output .= "legendLabel:'".addslashes(trim($labelText))."',";
  199. $output .= "filter:'".addslashes(trim($filterText))."',";
  200. $output .= "geometryType:".($ts+1).",";
  201. $output .= "categoryIndex:".($catIndex++);
  202. $output .= '}';
  203. $styleSep = ',';
  204. }
  205. }
  206. }
  207. $output .= ']}';
  208. $scaleSep = ',';
  209. }
  210. $output .= ']';
  211. return $output;
  212. }
  213. // Converts a boolean to "yes" or "no"
  214. // --from MG Web Tier API Reference
  215. function BooleanToString($boolean)
  216. {
  217. if (is_bool($boolean))
  218. return ($boolean ? "true" : "false");
  219. else
  220. return "'ERROR in BooleanToString.'";
  221. }
  222. function OutputGroupInfo($group)
  223. {
  224. echo "groupName:'".addslashes($group->GetName())."',";
  225. echo "legendLabel:'".addslashes($group->GetLegendLabel())."',";
  226. echo "uniqueId:'".$group->GetObjectId()."',";
  227. echo "displayInLegend:".BooleanToString($group->GetDisplayInLegend()).",";
  228. echo "expandInLegend:".BooleanToString($group->GetExpandInLegend()).",";
  229. echo "layerGroupType:'".$group->GetLayerGroupType()."',";
  230. $parent = $group->GetGroup();
  231. echo "parentUniqueId:";
  232. echo $parent != null ? "'".$parent->GetObjectId()."'," : "null,";
  233. echo "visible:".BooleanToString($group->GetVisible()).",";
  234. echo "actuallyVisible:".BooleanToString($group->isVisible());
  235. }
  236. function OutputLayerInfo($layer, $resourceService, $featureService)
  237. {
  238. $mappings = GetLayerPropertyMappings($resourceService, $layer);
  239. if (!isset($_SESSION['property_mappings'])) {
  240. $_SESSION['property_mappings'] = array();
  241. }
  242. $_SESSION['property_mappings'][$layer->GetObjectId()] = $mappings;
  243. $layerDefinition = $layer->GetLayerDefinition();
  244. $aLayerTypes = GetLayerTypes($featureService, $layer);
  245. //echo '<pre>'; print_r($aLayerTypes); echo '</pre>'; exit;
  246. echo "propertyMappings:{";
  247. $sep = '';
  248. foreach($mappings as $name => $value) {
  249. echo $sep."'$name':'$value'";
  250. $sep = ',';
  251. }
  252. echo "},";
  253. echo "uniqueId:'".$layer->GetObjectId()."',";
  254. echo "layerName:'".addslashes($layer->GetName())."',";
  255. echo 'layerTypes:[';
  256. $sep = '';
  257. for ( $j=0; $j < count($aLayerTypes); $j++ )
  258. {
  259. echo $sep . $aLayerTypes[$j];
  260. $sep = ',';
  261. }
  262. echo '],';
  263. echo "displayInLegend:".BooleanToString($layer->GetDisplayInLegend()).",";
  264. echo "expandInLegend:".BooleanToString($layer->GetExpandInLegend()).",";
  265. echo "resourceId:'".$layerDefinition->ToString()."',";
  266. echo "parentGroup:";
  267. echo $layer->GetGroup() ? "'".$layer->GetGroup()->GetObjectId()."'," : 'null,';
  268. echo "legendLabel:'".addslashes($layer->GetLegendLabel())."',";
  269. echo "selectable:".BooleanToString($layer->GetSelectable()).",";
  270. echo "visible:".BooleanToString($layer->GetVisible()).",";
  271. echo "actuallyVisible:".BooleanToString($layer->isVisible()).",";
  272. if (IsLayerEditable($resourceService, $layer)) {
  273. echo "editable:true,";
  274. } else {
  275. echo "editable:false,";
  276. }
  277. echo buildScaleRanges($layer);
  278. }
  279. ?>