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