geoCreator.js 9.6 KB

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