Geo.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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';
  42. import BoundingRect from 'zrender/lib/core/BoundingRect';
  43. import View from '../View';
  44. import geoSourceManager from './geoSourceManager';
  45. import { SINGLE_REFERRING } from '../../util/model';
  46. var GEO_DEFAULT_PARAMS = {
  47. 'geoJSON': {
  48. aspectScale: 0.75,
  49. invertLongitute: true
  50. },
  51. 'geoSVG': {
  52. aspectScale: 1,
  53. invertLongitute: false
  54. }
  55. };
  56. export var geo2DDimensions = ['lng', 'lat'];
  57. var Geo =
  58. /** @class */
  59. function (_super) {
  60. __extends(Geo, _super);
  61. function Geo(name, map, opt) {
  62. var _this = _super.call(this, name) || this;
  63. _this.dimensions = geo2DDimensions;
  64. _this.type = 'geo'; // Only store specified name coord via `addGeoCoord`.
  65. _this._nameCoordMap = zrUtil.createHashMap();
  66. _this.map = map;
  67. var source = geoSourceManager.load(map, opt.nameMap, opt.nameProperty);
  68. var resource = geoSourceManager.getGeoResource(map);
  69. _this.resourceType = resource ? resource.type : null;
  70. var defaultParmas = GEO_DEFAULT_PARAMS[resource.type];
  71. _this._regionsMap = source.regionsMap;
  72. _this._invertLongitute = defaultParmas.invertLongitute;
  73. _this.regions = source.regions;
  74. _this.aspectScale = zrUtil.retrieve2(opt.aspectScale, defaultParmas.aspectScale);
  75. var boundingRect = source.boundingRect;
  76. _this.setBoundingRect(boundingRect.x, boundingRect.y, boundingRect.width, boundingRect.height);
  77. return _this;
  78. }
  79. /**
  80. * Whether contain the given [lng, lat] coord.
  81. */
  82. // Never used yet.
  83. // containCoord(coord: number[]) {
  84. // const regions = this.regions;
  85. // for (let i = 0; i < regions.length; i++) {
  86. // const region = regions[i];
  87. // if (region.type === 'geoJSON' && (region as GeoJSONRegion).contain(coord)) {
  88. // return true;
  89. // }
  90. // }
  91. // return false;
  92. // }
  93. Geo.prototype._transformTo = function (x, y, width, height) {
  94. var rect = this.getBoundingRect();
  95. var invertLongitute = this._invertLongitute;
  96. rect = rect.clone();
  97. if (invertLongitute) {
  98. // Longitute is inverted
  99. rect.y = -rect.y - rect.height;
  100. }
  101. var rawTransformable = this._rawTransformable;
  102. rawTransformable.transform = rect.calculateTransform(new BoundingRect(x, y, width, height));
  103. var rawParent = rawTransformable.parent;
  104. rawTransformable.parent = null;
  105. rawTransformable.decomposeTransform();
  106. rawTransformable.parent = rawParent;
  107. if (invertLongitute) {
  108. rawTransformable.scaleY = -rawTransformable.scaleY;
  109. }
  110. this._updateTransform();
  111. };
  112. Geo.prototype.getRegion = function (name) {
  113. return this._regionsMap.get(name);
  114. };
  115. Geo.prototype.getRegionByCoord = function (coord) {
  116. var regions = this.regions;
  117. for (var i = 0; i < regions.length; i++) {
  118. var region = regions[i];
  119. if (region.type === 'geoJSON' && region.contain(coord)) {
  120. return regions[i];
  121. }
  122. }
  123. };
  124. /**
  125. * Add geoCoord for indexing by name
  126. */
  127. Geo.prototype.addGeoCoord = function (name, geoCoord) {
  128. this._nameCoordMap.set(name, geoCoord);
  129. };
  130. /**
  131. * Get geoCoord by name
  132. */
  133. Geo.prototype.getGeoCoord = function (name) {
  134. var region = this._regionsMap.get(name); // calcualte center only on demand.
  135. return this._nameCoordMap.get(name) || region && region.getCenter();
  136. };
  137. Geo.prototype.dataToPoint = function (data, noRoam, out) {
  138. if (typeof data === 'string') {
  139. // Map area name to geoCoord
  140. data = this.getGeoCoord(data);
  141. }
  142. if (data) {
  143. return View.prototype.dataToPoint.call(this, data, noRoam, out);
  144. }
  145. };
  146. Geo.prototype.convertToPixel = function (ecModel, finder, value) {
  147. var coordSys = getCoordSys(finder);
  148. return coordSys === this ? coordSys.dataToPoint(value) : null;
  149. };
  150. Geo.prototype.convertFromPixel = function (ecModel, finder, pixel) {
  151. var coordSys = getCoordSys(finder);
  152. return coordSys === this ? coordSys.pointToData(pixel) : null;
  153. };
  154. return Geo;
  155. }(View);
  156. ;
  157. zrUtil.mixin(Geo, View);
  158. function getCoordSys(finder) {
  159. var geoModel = finder.geoModel;
  160. var seriesModel = finder.seriesModel;
  161. return geoModel ? geoModel.coordinateSystem : seriesModel ? seriesModel.coordinateSystem // For map series.
  162. || (seriesModel.getReferringComponents('geo', SINGLE_REFERRING).models[0] || {}).coordinateSystem : null;
  163. }
  164. export default Geo;