geoSourceManager.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. * @param {string} nameProperty
  55. * @return {Object} source {regions, regionsMap, nameCoordMap, boundingRect}
  56. */
  57. load: function (mapName, nameMap, nameProperty) {
  58. var regions = [];
  59. var regionsMap = createHashMap();
  60. var nameCoordMap = createHashMap();
  61. var boundingRect;
  62. var mapRecords = retrieveMap(mapName);
  63. each(mapRecords, function (record) {
  64. var singleSource = loaders[record.type].load(mapName, record, nameProperty);
  65. each(singleSource.regions, function (region) {
  66. var regionName = region.name; // Try use the alias in geoNameMap
  67. if (nameMap && nameMap.hasOwnProperty(regionName)) {
  68. region = region.cloneShallow(regionName = nameMap[regionName]);
  69. }
  70. regions.push(region);
  71. regionsMap.set(regionName, region);
  72. nameCoordMap.set(regionName, region.center);
  73. });
  74. var rect = singleSource.boundingRect;
  75. if (rect) {
  76. boundingRect ? boundingRect.union(rect) : boundingRect = rect.clone();
  77. }
  78. });
  79. return {
  80. regions: regions,
  81. regionsMap: regionsMap,
  82. nameCoordMap: nameCoordMap,
  83. // FIXME Always return new ?
  84. boundingRect: boundingRect || new BoundingRect(0, 0, 0, 0)
  85. };
  86. },
  87. /**
  88. * @param {string} mapName
  89. * @param {string} hostKey For cache.
  90. * @return {Array.<module:zrender/Element>} Roots.
  91. */
  92. makeGraphic: makeInvoker('makeGraphic'),
  93. /**
  94. * @param {string} mapName
  95. * @param {string} hostKey For cache.
  96. */
  97. removeGraphic: makeInvoker('removeGraphic')
  98. };
  99. function makeInvoker(methodName) {
  100. return function (mapName, hostKey) {
  101. var mapRecords = retrieveMap(mapName);
  102. var results = [];
  103. each(mapRecords, function (record) {
  104. var method = loaders[record.type][methodName];
  105. method && results.push(method(mapName, record, hostKey));
  106. });
  107. return results;
  108. };
  109. }
  110. function mapNotExistsError(mapName) {}
  111. function retrieveMap(mapName) {
  112. var mapRecords = mapDataStorage.retrieveMap(mapName) || [];
  113. return mapRecords;
  114. }
  115. module.exports = _default;