Geo.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 { __extends } from "tslib";
  41. import * as zrUtil from 'zrender/lib/core/util.js';
  42. import BoundingRect from 'zrender/lib/core/BoundingRect.js';
  43. import View from '../View.js';
  44. import geoSourceManager from './geoSourceManager.js';
  45. import { SINGLE_REFERRING } from '../../util/model.js';
  46. import { warn } from '../../util/log.js';
  47. var GEO_DEFAULT_PARAMS = {
  48. 'geoJSON': {
  49. aspectScale: 0.75,
  50. invertLongitute: true
  51. },
  52. 'geoSVG': {
  53. aspectScale: 1,
  54. invertLongitute: false
  55. }
  56. };
  57. export var geo2DDimensions = ['lng', 'lat'];
  58. var Geo = /** @class */function (_super) {
  59. __extends(Geo, _super);
  60. function Geo(name, map, opt) {
  61. var _this = _super.call(this, name) || this;
  62. _this.dimensions = geo2DDimensions;
  63. _this.type = 'geo';
  64. // Only store specified name coord via `addGeoCoord`.
  65. _this._nameCoordMap = zrUtil.createHashMap();
  66. _this.map = map;
  67. var projection = opt.projection;
  68. var source = geoSourceManager.load(map, opt.nameMap, opt.nameProperty);
  69. var resource = geoSourceManager.getGeoResource(map);
  70. var resourceType = _this.resourceType = resource ? resource.type : null;
  71. var regions = _this.regions = source.regions;
  72. var defaultParams = GEO_DEFAULT_PARAMS[resource.type];
  73. _this._regionsMap = source.regionsMap;
  74. _this.regions = source.regions;
  75. if (process.env.NODE_ENV !== 'production' && projection) {
  76. // Do some check
  77. if (resourceType === 'geoSVG') {
  78. if (process.env.NODE_ENV !== 'production') {
  79. warn("Map " + map + " with SVG source can't use projection. Only GeoJSON source supports projection.");
  80. }
  81. projection = null;
  82. }
  83. if (!(projection.project && projection.unproject)) {
  84. if (process.env.NODE_ENV !== 'production') {
  85. warn('project and unproject must be both provided in the projeciton.');
  86. }
  87. projection = null;
  88. }
  89. }
  90. _this.projection = projection;
  91. var boundingRect;
  92. if (projection) {
  93. // Can't reuse the raw bounding rect
  94. for (var i = 0; i < regions.length; i++) {
  95. var regionRect = regions[i].getBoundingRect(projection);
  96. boundingRect = boundingRect || regionRect.clone();
  97. boundingRect.union(regionRect);
  98. }
  99. } else {
  100. boundingRect = source.boundingRect;
  101. }
  102. _this.setBoundingRect(boundingRect.x, boundingRect.y, boundingRect.width, boundingRect.height);
  103. // aspectScale and invertLongitute actually is the parameters default raw projection.
  104. // So we ignore them if projection is given.
  105. // Ignore default aspect scale if projection exits.
  106. _this.aspectScale = projection ? 1 : zrUtil.retrieve2(opt.aspectScale, defaultParams.aspectScale);
  107. // Not invert longitude if projection exits.
  108. _this._invertLongitute = projection ? false : defaultParams.invertLongitute;
  109. return _this;
  110. }
  111. Geo.prototype._transformTo = function (x, y, width, height) {
  112. var rect = this.getBoundingRect();
  113. var invertLongitute = this._invertLongitute;
  114. rect = rect.clone();
  115. if (invertLongitute) {
  116. // Longitude is inverted.
  117. rect.y = -rect.y - rect.height;
  118. }
  119. var rawTransformable = this._rawTransformable;
  120. rawTransformable.transform = rect.calculateTransform(new BoundingRect(x, y, width, height));
  121. var rawParent = rawTransformable.parent;
  122. rawTransformable.parent = null;
  123. rawTransformable.decomposeTransform();
  124. rawTransformable.parent = rawParent;
  125. if (invertLongitute) {
  126. rawTransformable.scaleY = -rawTransformable.scaleY;
  127. }
  128. this._updateTransform();
  129. };
  130. Geo.prototype.getRegion = function (name) {
  131. return this._regionsMap.get(name);
  132. };
  133. Geo.prototype.getRegionByCoord = function (coord) {
  134. var regions = this.regions;
  135. for (var i = 0; i < regions.length; i++) {
  136. var region = regions[i];
  137. if (region.type === 'geoJSON' && region.contain(coord)) {
  138. return regions[i];
  139. }
  140. }
  141. };
  142. /**
  143. * Add geoCoord for indexing by name
  144. */
  145. Geo.prototype.addGeoCoord = function (name, geoCoord) {
  146. this._nameCoordMap.set(name, geoCoord);
  147. };
  148. /**
  149. * Get geoCoord by name
  150. */
  151. Geo.prototype.getGeoCoord = function (name) {
  152. var region = this._regionsMap.get(name);
  153. // Calculate center only on demand.
  154. return this._nameCoordMap.get(name) || region && region.getCenter();
  155. };
  156. Geo.prototype.dataToPoint = function (data, noRoam, out) {
  157. if (zrUtil.isString(data)) {
  158. // Map area name to geoCoord
  159. data = this.getGeoCoord(data);
  160. }
  161. if (data) {
  162. var projection = this.projection;
  163. if (projection) {
  164. // projection may return null point.
  165. data = projection.project(data);
  166. }
  167. return data && this.projectedToPoint(data, noRoam, out);
  168. }
  169. };
  170. Geo.prototype.pointToData = function (point) {
  171. var projection = this.projection;
  172. if (projection) {
  173. // projection may return null point.
  174. point = projection.unproject(point);
  175. }
  176. return point && this.pointToProjected(point);
  177. };
  178. /**
  179. * Point to projected data. Same with pointToData when projection is used.
  180. */
  181. Geo.prototype.pointToProjected = function (point) {
  182. return _super.prototype.pointToData.call(this, point);
  183. };
  184. Geo.prototype.projectedToPoint = function (projected, noRoam, out) {
  185. return _super.prototype.dataToPoint.call(this, projected, noRoam, out);
  186. };
  187. Geo.prototype.convertToPixel = function (ecModel, finder, value) {
  188. var coordSys = getCoordSys(finder);
  189. return coordSys === this ? coordSys.dataToPoint(value) : null;
  190. };
  191. Geo.prototype.convertFromPixel = function (ecModel, finder, pixel) {
  192. var coordSys = getCoordSys(finder);
  193. return coordSys === this ? coordSys.pointToData(pixel) : null;
  194. };
  195. return Geo;
  196. }(View);
  197. ;
  198. zrUtil.mixin(Geo, View);
  199. function getCoordSys(finder) {
  200. var geoModel = finder.geoModel;
  201. var seriesModel = finder.seriesModel;
  202. return geoModel ? geoModel.coordinateSystem : seriesModel ? seriesModel.coordinateSystem // For map series.
  203. || (seriesModel.getReferringComponents('geo', SINGLE_REFERRING).models[0] || {}).coordinateSystem : null;
  204. }
  205. export default Geo;