geoSourceManager.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. var _config = require("../../config");
  20. var __DEV__ = _config.__DEV__;
  21. var _util = require("zrender/lib/core/util");
  22. var each = _util.each;
  23. var createHashMap = _util.createHashMap;
  24. var mapDataStorage = require("./mapDataStorage");
  25. var geoJSONLoader = require("./geoJSONLoader");
  26. var geoSVGLoader = require("./geoSVGLoader");
  27. var BoundingRect = require("zrender/lib/core/BoundingRect");
  28. /*
  29. * Licensed to the Apache Software Foundation (ASF) under one
  30. * or more contributor license agreements. See the NOTICE file
  31. * distributed with this work for additional information
  32. * regarding copyright ownership. The ASF licenses this file
  33. * to you under the Apache License, Version 2.0 (the
  34. * "License"); you may not use this file except in compliance
  35. * with the License. You may obtain a copy of the License at
  36. *
  37. * http://www.apache.org/licenses/LICENSE-2.0
  38. *
  39. * Unless required by applicable law or agreed to in writing,
  40. * software distributed under the License is distributed on an
  41. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  42. * KIND, either express or implied. See the License for the
  43. * specific language governing permissions and limitations
  44. * under the License.
  45. */
  46. var loaders = {
  47. geoJSON: geoJSONLoader,
  48. svg: geoSVGLoader
  49. };
  50. var _default = {
  51. /**
  52. * @param {string} mapName
  53. * @param {Object} nameMap
  54. * @return {Object} source {regions, regionsMap, nameCoordMap, boundingRect}
  55. */
  56. load: function (mapName, nameMap) {
  57. var regions = [];
  58. var regionsMap = createHashMap();
  59. var nameCoordMap = createHashMap();
  60. var boundingRect;
  61. var mapRecords = retrieveMap(mapName);
  62. each(mapRecords, function (record) {
  63. var singleSource = loaders[record.type].load(mapName, record);
  64. each(singleSource.regions, function (region) {
  65. var regionName = region.name; // Try use the alias in geoNameMap
  66. if (nameMap && nameMap.hasOwnProperty(regionName)) {
  67. region = region.cloneShallow(regionName = nameMap[regionName]);
  68. }
  69. regions.push(region);
  70. regionsMap.set(regionName, region);
  71. nameCoordMap.set(regionName, region.center);
  72. });
  73. var rect = singleSource.boundingRect;
  74. if (rect) {
  75. boundingRect ? boundingRect.union(rect) : boundingRect = rect.clone();
  76. }
  77. });
  78. return {
  79. regions: regions,
  80. regionsMap: regionsMap,
  81. nameCoordMap: nameCoordMap,
  82. // FIXME Always return new ?
  83. boundingRect: boundingRect || new BoundingRect(0, 0, 0, 0)
  84. };
  85. },
  86. /**
  87. * @param {string} mapName
  88. * @param {string} hostKey For cache.
  89. * @return {Array.<module:zrender/Element>} Roots.
  90. */
  91. makeGraphic: makeInvoker('makeGraphic'),
  92. /**
  93. * @param {string} mapName
  94. * @param {string} hostKey For cache.
  95. */
  96. removeGraphic: makeInvoker('removeGraphic')
  97. };
  98. function makeInvoker(methodName) {
  99. return function (mapName, hostKey) {
  100. var mapRecords = retrieveMap(mapName);
  101. var results = [];
  102. each(mapRecords, function (record) {
  103. var method = loaders[record.type][methodName];
  104. method && results.push(method(mapName, record, hostKey));
  105. });
  106. return results;
  107. };
  108. }
  109. function mapNotExistsError(mapName) {}
  110. function retrieveMap(mapName) {
  111. var mapRecords = mapDataStorage.retrieveMap(mapName) || [];
  112. return mapRecords;
  113. }
  114. module.exports = _default;