geoCreator.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 * as zrUtil from 'zrender/lib/core/util.js';
  41. import Geo, { geo2DDimensions } from './Geo.js';
  42. import * as layout from '../../util/layout.js';
  43. import * as numberUtil from '../../util/number.js';
  44. import geoSourceManager from './geoSourceManager.js';
  45. import * as vector from 'zrender/lib/core/vector.js';
  46. /**
  47. * Resize method bound to the geo
  48. */
  49. function resizeGeo(geoModel, api) {
  50. var boundingCoords = geoModel.get('boundingCoords');
  51. if (boundingCoords != null) {
  52. var leftTop_1 = boundingCoords[0];
  53. var rightBottom_1 = boundingCoords[1];
  54. if (!(isFinite(leftTop_1[0]) && isFinite(leftTop_1[1]) && isFinite(rightBottom_1[0]) && isFinite(rightBottom_1[1]))) {
  55. if (process.env.NODE_ENV !== 'production') {
  56. console.error('Invalid boundingCoords');
  57. }
  58. } else {
  59. // Sample around the lng/lat rect and use projection to calculate actual bounding rect.
  60. var projection_1 = this.projection;
  61. if (projection_1) {
  62. var xMin = leftTop_1[0];
  63. var yMin = leftTop_1[1];
  64. var xMax = rightBottom_1[0];
  65. var yMax = rightBottom_1[1];
  66. leftTop_1 = [Infinity, Infinity];
  67. rightBottom_1 = [-Infinity, -Infinity];
  68. // TODO better way?
  69. var sampleLine = function (x0, y0, x1, y1) {
  70. var dx = x1 - x0;
  71. var dy = y1 - y0;
  72. for (var i = 0; i <= 100; i++) {
  73. var p = i / 100;
  74. var pt = projection_1.project([x0 + dx * p, y0 + dy * p]);
  75. vector.min(leftTop_1, leftTop_1, pt);
  76. vector.max(rightBottom_1, rightBottom_1, pt);
  77. }
  78. };
  79. // Top
  80. sampleLine(xMin, yMin, xMax, yMin);
  81. // Right
  82. sampleLine(xMax, yMin, xMax, yMax);
  83. // Bottom
  84. sampleLine(xMax, yMax, xMin, yMax);
  85. // Left
  86. sampleLine(xMin, yMax, xMax, yMin);
  87. }
  88. this.setBoundingRect(leftTop_1[0], leftTop_1[1], rightBottom_1[0] - leftTop_1[0], rightBottom_1[1] - leftTop_1[1]);
  89. }
  90. }
  91. var rect = this.getBoundingRect();
  92. var centerOption = geoModel.get('layoutCenter');
  93. var sizeOption = geoModel.get('layoutSize');
  94. var viewWidth = api.getWidth();
  95. var viewHeight = api.getHeight();
  96. var aspect = rect.width / rect.height * this.aspectScale;
  97. var useCenterAndSize = false;
  98. var center;
  99. var size;
  100. if (centerOption && sizeOption) {
  101. center = [numberUtil.parsePercent(centerOption[0], viewWidth), numberUtil.parsePercent(centerOption[1], viewHeight)];
  102. size = numberUtil.parsePercent(sizeOption, Math.min(viewWidth, viewHeight));
  103. if (!isNaN(center[0]) && !isNaN(center[1]) && !isNaN(size)) {
  104. useCenterAndSize = true;
  105. } else {
  106. if (process.env.NODE_ENV !== 'production') {
  107. console.warn('Given layoutCenter or layoutSize data are invalid. Use left/top/width/height instead.');
  108. }
  109. }
  110. }
  111. var viewRect;
  112. if (useCenterAndSize) {
  113. viewRect = {};
  114. if (aspect > 1) {
  115. // Width is same with size
  116. viewRect.width = size;
  117. viewRect.height = size / aspect;
  118. } else {
  119. viewRect.height = size;
  120. viewRect.width = size * aspect;
  121. }
  122. viewRect.y = center[1] - viewRect.height / 2;
  123. viewRect.x = center[0] - viewRect.width / 2;
  124. } else {
  125. // Use left/top/width/height
  126. var boxLayoutOption = geoModel.getBoxLayoutParams();
  127. boxLayoutOption.aspect = aspect;
  128. viewRect = layout.getLayoutRect(boxLayoutOption, {
  129. width: viewWidth,
  130. height: viewHeight
  131. });
  132. }
  133. this.setViewRect(viewRect.x, viewRect.y, viewRect.width, viewRect.height);
  134. this.setCenter(geoModel.get('center'), api);
  135. this.setZoom(geoModel.get('zoom'));
  136. }
  137. // Back compat for ECharts2, where the coord map is set on map series:
  138. // {type: 'map', geoCoord: {'cityA': [116.46,39.92], 'cityA': [119.12,24.61]}},
  139. function setGeoCoords(geo, model) {
  140. zrUtil.each(model.get('geoCoord'), function (geoCoord, name) {
  141. geo.addGeoCoord(name, geoCoord);
  142. });
  143. }
  144. var GeoCreator = /** @class */function () {
  145. function GeoCreator() {
  146. // For deciding which dimensions to use when creating list data
  147. this.dimensions = geo2DDimensions;
  148. }
  149. GeoCreator.prototype.create = function (ecModel, api) {
  150. var geoList = [];
  151. function getCommonGeoProperties(model) {
  152. return {
  153. nameProperty: model.get('nameProperty'),
  154. aspectScale: model.get('aspectScale'),
  155. projection: model.get('projection')
  156. };
  157. }
  158. // FIXME Create each time may be slow
  159. ecModel.eachComponent('geo', function (geoModel, idx) {
  160. var mapName = geoModel.get('map');
  161. var geo = new Geo(mapName + idx, mapName, zrUtil.extend({
  162. nameMap: geoModel.get('nameMap')
  163. }, getCommonGeoProperties(geoModel)));
  164. geo.zoomLimit = geoModel.get('scaleLimit');
  165. geoList.push(geo);
  166. // setGeoCoords(geo, geoModel);
  167. geoModel.coordinateSystem = geo;
  168. geo.model = geoModel;
  169. // Inject resize method
  170. geo.resize = resizeGeo;
  171. geo.resize(geoModel, api);
  172. });
  173. ecModel.eachSeries(function (seriesModel) {
  174. var coordSys = seriesModel.get('coordinateSystem');
  175. if (coordSys === 'geo') {
  176. var geoIndex = seriesModel.get('geoIndex') || 0;
  177. seriesModel.coordinateSystem = geoList[geoIndex];
  178. }
  179. });
  180. // If has map series
  181. var mapModelGroupBySeries = {};
  182. ecModel.eachSeriesByType('map', function (seriesModel) {
  183. if (!seriesModel.getHostGeoModel()) {
  184. var mapType = seriesModel.getMapType();
  185. mapModelGroupBySeries[mapType] = mapModelGroupBySeries[mapType] || [];
  186. mapModelGroupBySeries[mapType].push(seriesModel);
  187. }
  188. });
  189. zrUtil.each(mapModelGroupBySeries, function (mapSeries, mapType) {
  190. var nameMapList = zrUtil.map(mapSeries, function (singleMapSeries) {
  191. return singleMapSeries.get('nameMap');
  192. });
  193. var geo = new Geo(mapType, mapType, zrUtil.extend({
  194. nameMap: zrUtil.mergeAll(nameMapList)
  195. }, getCommonGeoProperties(mapSeries[0])));
  196. geo.zoomLimit = zrUtil.retrieve.apply(null, zrUtil.map(mapSeries, function (singleMapSeries) {
  197. return singleMapSeries.get('scaleLimit');
  198. }));
  199. geoList.push(geo);
  200. // Inject resize method
  201. geo.resize = resizeGeo;
  202. geo.resize(mapSeries[0], api);
  203. zrUtil.each(mapSeries, function (singleMapSeries) {
  204. singleMapSeries.coordinateSystem = geo;
  205. setGeoCoords(geo, singleMapSeries);
  206. });
  207. });
  208. return geoList;
  209. };
  210. /**
  211. * Fill given regions array
  212. */
  213. GeoCreator.prototype.getFilledRegions = function (originRegionArr, mapName, nameMap, nameProperty) {
  214. // Not use the original
  215. var regionsArr = (originRegionArr || []).slice();
  216. var dataNameMap = zrUtil.createHashMap();
  217. for (var i = 0; i < regionsArr.length; i++) {
  218. dataNameMap.set(regionsArr[i].name, regionsArr[i]);
  219. }
  220. var source = geoSourceManager.load(mapName, nameMap, nameProperty);
  221. zrUtil.each(source.regions, function (region) {
  222. var name = region.name;
  223. !dataNameMap.get(name) && regionsArr.push({
  224. name: name
  225. });
  226. });
  227. return regionsArr;
  228. };
  229. return GeoCreator;
  230. }();
  231. var geoCreator = new GeoCreator();
  232. export default geoCreator;