Geo.js 7.8 KB

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