geoJSONLoader.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 _util = require("zrender/lib/core/util");
  20. var each = _util.each;
  21. var parseGeoJson = require("./parseGeoJson");
  22. var _model = require("../../util/model");
  23. var makeInner = _model.makeInner;
  24. var fixNanhai = require("./fix/nanhai");
  25. var fixTextCoord = require("./fix/textCoord");
  26. var fixGeoCoord = require("./fix/geoCoord");
  27. var fixDiaoyuIsland = require("./fix/diaoyuIsland");
  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. // Built-in GEO fixer.
  47. var inner = makeInner();
  48. var _default = {
  49. /**
  50. * @param {string} mapName
  51. * @param {Object} mapRecord {specialAreas, geoJSON}
  52. * @param {string} nameProperty
  53. * @return {Object} {regions, boundingRect}
  54. */
  55. load: function (mapName, mapRecord, nameProperty) {
  56. var parsed = inner(mapRecord).parsed;
  57. if (parsed) {
  58. return parsed;
  59. }
  60. var specialAreas = mapRecord.specialAreas || {};
  61. var geoJSON = mapRecord.geoJSON;
  62. var regions; // https://jsperf.com/try-catch-performance-overhead
  63. try {
  64. regions = geoJSON ? parseGeoJson(geoJSON, nameProperty) : [];
  65. } catch (e) {
  66. throw new Error('Invalid geoJson format\n' + e.message);
  67. }
  68. fixNanhai(mapName, regions);
  69. each(regions, function (region) {
  70. var regionName = region.name;
  71. fixTextCoord(mapName, region);
  72. fixGeoCoord(mapName, region);
  73. fixDiaoyuIsland(mapName, region); // Some area like Alaska in USA map needs to be tansformed
  74. // to look better
  75. var specialArea = specialAreas[regionName];
  76. if (specialArea) {
  77. region.transformTo(specialArea.left, specialArea.top, specialArea.width, specialArea.height);
  78. }
  79. });
  80. return inner(mapRecord).parsed = {
  81. regions: regions,
  82. boundingRect: getBoundingRect(regions)
  83. };
  84. }
  85. };
  86. function getBoundingRect(regions) {
  87. var rect;
  88. for (var i = 0; i < regions.length; i++) {
  89. var regionRect = regions[i].getBoundingRect();
  90. rect = rect || regionRect.clone();
  91. rect.union(regionRect);
  92. }
  93. return rect;
  94. }
  95. module.exports = _default;