GeoJSONResource.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 } from 'zrender/lib/core/util';
  41. import parseGeoJson from './parseGeoJson'; // Built-in GEO fixer.
  42. import fixNanhai from './fix/nanhai';
  43. import fixTextCoord from './fix/textCoord';
  44. import fixGeoCoord from './fix/geoCoord';
  45. import fixDiaoyuIsland from './fix/diaoyuIsland';
  46. import BoundingRect from 'zrender/lib/core/BoundingRect';
  47. var DEFAULT_NAME_PROPERTY = 'name';
  48. var GeoJSONResource =
  49. /** @class */
  50. function () {
  51. function GeoJSONResource(mapName, geoJSON, specialAreas) {
  52. this.type = 'geoJSON';
  53. this._parsedMap = createHashMap();
  54. this._mapName = mapName;
  55. this._specialAreas = specialAreas; // PENDING: delay the parse to the first usage to rapid up the FMP?
  56. this._geoJSON = parseInput(geoJSON);
  57. }
  58. /**
  59. * @param nameMap can be null/undefined
  60. * @param nameProperty can be null/undefined
  61. */
  62. GeoJSONResource.prototype.load = function (nameMap, nameProperty) {
  63. nameProperty = nameProperty || DEFAULT_NAME_PROPERTY;
  64. var parsed = this._parsedMap.get(nameProperty);
  65. if (!parsed) {
  66. var rawRegions = this._parseToRegions(nameProperty);
  67. parsed = this._parsedMap.set(nameProperty, {
  68. regions: rawRegions,
  69. boundingRect: calculateBoundingRect(rawRegions)
  70. });
  71. }
  72. var regionsMap = createHashMap();
  73. var finalRegions = [];
  74. each(parsed.regions, function (region) {
  75. var regionName = region.name; // Try use the alias in geoNameMap
  76. if (nameMap && nameMap.hasOwnProperty(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; // https://jsperf.com/try-catch-performance-overhead
  92. try {
  93. rawRegions = geoJSON ? parseGeoJson(geoJSON, nameProperty) : [];
  94. } catch (e) {
  95. throw new Error('Invalid geoJson format\n' + e.message);
  96. }
  97. fixNanhai(mapName, rawRegions);
  98. each(rawRegions, function (region) {
  99. var regionName = region.name;
  100. fixTextCoord(mapName, region);
  101. fixGeoCoord(mapName, region);
  102. fixDiaoyuIsland(mapName, region); // Some area like Alaska in USA map needs to be tansformed
  103. // to look better
  104. var specialArea = this._specialAreas && this._specialAreas[regionName];
  105. if (specialArea) {
  106. region.transformTo(specialArea.left, specialArea.top, specialArea.width, specialArea.height);
  107. }
  108. }, this);
  109. return rawRegions;
  110. };
  111. /**
  112. * Only for exporting to users.
  113. * **MUST NOT** used internally.
  114. */
  115. GeoJSONResource.prototype.getMapForUser = function () {
  116. return {
  117. // For backward compatibility, use geoJson
  118. // PENDING: it has been returning them without clone.
  119. // do we need to avoid outsite modification?
  120. geoJson: this._geoJSON,
  121. geoJSON: this._geoJSON,
  122. specialAreas: this._specialAreas
  123. };
  124. };
  125. return GeoJSONResource;
  126. }();
  127. export { GeoJSONResource };
  128. function calculateBoundingRect(regions) {
  129. var rect;
  130. for (var i = 0; i < regions.length; i++) {
  131. var regionRect = regions[i].getBoundingRect();
  132. rect = rect || regionRect.clone();
  133. rect.union(regionRect);
  134. }
  135. return rect;
  136. }
  137. function parseInput(source) {
  138. return !isString(source) ? source : typeof JSON !== 'undefined' && JSON.parse ? JSON.parse(source) : new Function('return (' + source + ');')();
  139. }