GeoJSONResource.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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';
  42. // Built-in GEO fixer.
  43. import fixNanhai from './fix/nanhai.js';
  44. import fixTextCoord from './fix/textCoord.js';
  45. import fixDiaoyuIsland from './fix/diaoyuIsland.js';
  46. import BoundingRect from 'zrender/lib/core/BoundingRect.js';
  47. var DEFAULT_NAME_PROPERTY = 'name';
  48. var GeoJSONResource = /** @class */function () {
  49. function GeoJSONResource(mapName, geoJSON, specialAreas) {
  50. this.type = 'geoJSON';
  51. this._parsedMap = createHashMap();
  52. this._mapName = mapName;
  53. this._specialAreas = specialAreas;
  54. // 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;
  75. // Try use the alias in geoNameMap
  76. if (nameMap && hasOwn(nameMap, regionName)) {
  77. region = region.cloneShallow(regionName = nameMap[regionName]);
  78. }
  79. finalRegions.push(region);
  80. regionsMap.set(regionName, region);
  81. });
  82. return {
  83. regions: finalRegions,
  84. boundingRect: parsed.boundingRect || new BoundingRect(0, 0, 0, 0),
  85. regionsMap: regionsMap
  86. };
  87. };
  88. GeoJSONResource.prototype._parseToRegions = function (nameProperty) {
  89. var mapName = this._mapName;
  90. var geoJSON = this._geoJSON;
  91. var rawRegions;
  92. // https://jsperf.com/try-catch-performance-overhead
  93. try {
  94. rawRegions = geoJSON ? parseGeoJson(geoJSON, nameProperty) : [];
  95. } catch (e) {
  96. throw new Error('Invalid geoJson format\n' + e.message);
  97. }
  98. fixNanhai(mapName, rawRegions);
  99. each(rawRegions, function (region) {
  100. var regionName = region.name;
  101. fixTextCoord(mapName, region);
  102. fixDiaoyuIsland(mapName, region);
  103. // Some area like Alaska in USA map needs to be tansformed
  104. // to look better
  105. var specialArea = this._specialAreas && this._specialAreas[regionName];
  106. if (specialArea) {
  107. region.transformTo(specialArea.left, specialArea.top, specialArea.width, specialArea.height);
  108. }
  109. }, this);
  110. return rawRegions;
  111. };
  112. /**
  113. * Only for exporting to users.
  114. * **MUST NOT** used internally.
  115. */
  116. GeoJSONResource.prototype.getMapForUser = function () {
  117. return {
  118. // For backward compatibility, use geoJson
  119. // PENDING: it has been returning them without clone.
  120. // do we need to avoid outsite modification?
  121. geoJson: this._geoJSON,
  122. geoJSON: this._geoJSON,
  123. specialAreas: this._specialAreas
  124. };
  125. };
  126. return GeoJSONResource;
  127. }();
  128. export { GeoJSONResource };
  129. function calculateBoundingRect(regions) {
  130. var rect;
  131. for (var i = 0; i < regions.length; i++) {
  132. var regionRect = regions[i].getBoundingRect();
  133. rect = rect || regionRect.clone();
  134. rect.union(regionRect);
  135. }
  136. return rect;
  137. }
  138. function parseInput(source) {
  139. return !isString(source) ? source : typeof JSON !== 'undefined' && JSON.parse ? JSON.parse(source) : new Function('return (' + source + ');')();
  140. }