GeoJSONResource.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import { each, isString, createHashMap, hasOwn } from 'zrender/lib/core/util.js';
  41. import parseGeoJson from './parseGeoJson.js'; // Built-in GEO fixer.
  42. import fixNanhai from './fix/nanhai.js';
  43. import fixTextCoord from './fix/textCoord.js';
  44. import fixDiaoyuIsland from './fix/diaoyuIsland.js';
  45. import BoundingRect from 'zrender/lib/core/BoundingRect.js';
  46. var DEFAULT_NAME_PROPERTY = 'name';
  47. var GeoJSONResource =
  48. /** @class */
  49. function () {
  50. function GeoJSONResource(mapName, geoJSON, specialAreas) {
  51. this.type = 'geoJSON';
  52. this._parsedMap = createHashMap();
  53. this._mapName = mapName;
  54. this._specialAreas = specialAreas; // PENDING: delay the parse to the first usage to rapid up the FMP?
  55. this._geoJSON = parseInput(geoJSON);
  56. }
  57. /**
  58. * @param nameMap can be null/undefined
  59. * @param nameProperty can be null/undefined
  60. */
  61. GeoJSONResource.prototype.load = function (nameMap, nameProperty) {
  62. nameProperty = nameProperty || DEFAULT_NAME_PROPERTY;
  63. var parsed = this._parsedMap.get(nameProperty);
  64. if (!parsed) {
  65. var rawRegions = this._parseToRegions(nameProperty);
  66. parsed = this._parsedMap.set(nameProperty, {
  67. regions: rawRegions,
  68. boundingRect: calculateBoundingRect(rawRegions)
  69. });
  70. }
  71. var regionsMap = createHashMap();
  72. var finalRegions = [];
  73. each(parsed.regions, function (region) {
  74. var regionName = region.name; // Try use the alias in geoNameMap
  75. if (nameMap && hasOwn(nameMap, regionName)) {
  76. region = region.cloneShallow(regionName = nameMap[regionName]);
  77. }
  78. finalRegions.push(region);
  79. regionsMap.set(regionName, region);
  80. });
  81. return {
  82. regions: finalRegions,
  83. boundingRect: parsed.boundingRect || new BoundingRect(0, 0, 0, 0),
  84. regionsMap: regionsMap
  85. };
  86. };
  87. GeoJSONResource.prototype._parseToRegions = function (nameProperty) {
  88. var mapName = this._mapName;
  89. var geoJSON = this._geoJSON;
  90. var rawRegions; // https://jsperf.com/try-catch-performance-overhead
  91. try {
  92. rawRegions = geoJSON ? parseGeoJson(geoJSON, nameProperty) : [];
  93. } catch (e) {
  94. throw new Error('Invalid geoJson format\n' + e.message);
  95. }
  96. fixNanhai(mapName, rawRegions);
  97. each(rawRegions, function (region) {
  98. var regionName = region.name;
  99. fixTextCoord(mapName, region);
  100. fixDiaoyuIsland(mapName, region); // Some area like Alaska in USA map needs to be tansformed
  101. // to look better
  102. var specialArea = this._specialAreas && this._specialAreas[regionName];
  103. if (specialArea) {
  104. region.transformTo(specialArea.left, specialArea.top, specialArea.width, specialArea.height);
  105. }
  106. }, this);
  107. return rawRegions;
  108. };
  109. /**
  110. * Only for exporting to users.
  111. * **MUST NOT** used internally.
  112. */
  113. GeoJSONResource.prototype.getMapForUser = function () {
  114. return {
  115. // For backward compatibility, use geoJson
  116. // PENDING: it has been returning them without clone.
  117. // do we need to avoid outsite modification?
  118. geoJson: this._geoJSON,
  119. geoJSON: this._geoJSON,
  120. specialAreas: this._specialAreas
  121. };
  122. };
  123. return GeoJSONResource;
  124. }();
  125. export { GeoJSONResource };
  126. function calculateBoundingRect(regions) {
  127. var rect;
  128. for (var i = 0; i < regions.length; i++) {
  129. var regionRect = regions[i].getBoundingRect();
  130. rect = rect || regionRect.clone();
  131. rect.union(regionRect);
  132. }
  133. return rect;
  134. }
  135. function parseInput(source) {
  136. return !isString(source) ? source : typeof JSON !== 'undefined' && JSON.parse ? JSON.parse(source) : new Function('return (' + source + ');')();
  137. }