geoJSONLoader.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. * @return {Object} {regions, boundingRect}
  53. */
  54. load: function (mapName, mapRecord) {
  55. var parsed = inner(mapRecord).parsed;
  56. if (parsed) {
  57. return parsed;
  58. }
  59. var specialAreas = mapRecord.specialAreas || {};
  60. var geoJSON = mapRecord.geoJSON;
  61. var regions; // https://jsperf.com/try-catch-performance-overhead
  62. try {
  63. regions = geoJSON ? parseGeoJson(geoJSON) : [];
  64. } catch (e) {
  65. throw new Error('Invalid geoJson format\n' + e.message);
  66. }
  67. fixNanhai(mapName, regions);
  68. each(regions, function (region) {
  69. var regionName = region.name;
  70. fixTextCoord(mapName, region);
  71. fixGeoCoord(mapName, region);
  72. fixDiaoyuIsland(mapName, region); // Some area like Alaska in USA map needs to be tansformed
  73. // to look better
  74. var specialArea = specialAreas[regionName];
  75. if (specialArea) {
  76. region.transformTo(specialArea.left, specialArea.top, specialArea.width, specialArea.height);
  77. }
  78. });
  79. return inner(mapRecord).parsed = {
  80. regions: regions,
  81. boundingRect: getBoundingRect(regions)
  82. };
  83. }
  84. };
  85. function getBoundingRect(regions) {
  86. var rect;
  87. for (var i = 0; i < regions.length; i++) {
  88. var regionRect = regions[i].getBoundingRect();
  89. rect = rect || regionRect.clone();
  90. rect.union(regionRect);
  91. }
  92. return rect;
  93. }
  94. module.exports = _default;