Geo.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. var zrUtil = require("zrender/lib/core/util");
  20. var BoundingRect = require("zrender/lib/core/BoundingRect");
  21. var View = require("../View");
  22. var geoSourceManager = require("./geoSourceManager");
  23. /*
  24. * Licensed to the Apache Software Foundation (ASF) under one
  25. * or more contributor license agreements. See the NOTICE file
  26. * distributed with this work for additional information
  27. * regarding copyright ownership. The ASF licenses this file
  28. * to you under the Apache License, Version 2.0 (the
  29. * "License"); you may not use this file except in compliance
  30. * with the License. You may obtain a copy of the License at
  31. *
  32. * http://www.apache.org/licenses/LICENSE-2.0
  33. *
  34. * Unless required by applicable law or agreed to in writing,
  35. * software distributed under the License is distributed on an
  36. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  37. * KIND, either express or implied. See the License for the
  38. * specific language governing permissions and limitations
  39. * under the License.
  40. */
  41. /**
  42. * [Geo description]
  43. * For backward compatibility, the orginal interface:
  44. * `name, map, geoJson, specialAreas, nameMap` is kept.
  45. *
  46. * @param {string|Object} name
  47. * @param {string} map Map type
  48. * Specify the positioned areas by left, top, width, height
  49. * @param {Object.<string, string>} [nameMap]
  50. * Specify name alias
  51. * @param {boolean} [invertLongitute=true]
  52. */
  53. function Geo(name, map, nameMap, invertLongitute) {
  54. View.call(this, name);
  55. /**
  56. * Map type
  57. * @type {string}
  58. */
  59. this.map = map;
  60. var source = geoSourceManager.load(map, nameMap);
  61. this._nameCoordMap = source.nameCoordMap;
  62. this._regionsMap = source.regionsMap;
  63. this._invertLongitute = invertLongitute == null ? true : invertLongitute;
  64. /**
  65. * @readOnly
  66. */
  67. this.regions = source.regions;
  68. /**
  69. * @type {module:zrender/src/core/BoundingRect}
  70. */
  71. this._rect = source.boundingRect;
  72. }
  73. Geo.prototype = {
  74. constructor: Geo,
  75. type: 'geo',
  76. /**
  77. * @param {Array.<string>}
  78. * @readOnly
  79. */
  80. dimensions: ['lng', 'lat'],
  81. /**
  82. * If contain given lng,lat coord
  83. * @param {Array.<number>}
  84. * @readOnly
  85. */
  86. containCoord: function (coord) {
  87. var regions = this.regions;
  88. for (var i = 0; i < regions.length; i++) {
  89. if (regions[i].contain(coord)) {
  90. return true;
  91. }
  92. }
  93. return false;
  94. },
  95. /**
  96. * @override
  97. */
  98. transformTo: function (x, y, width, height) {
  99. var rect = this.getBoundingRect();
  100. var invertLongitute = this._invertLongitute;
  101. rect = rect.clone();
  102. if (invertLongitute) {
  103. // Longitute is inverted
  104. rect.y = -rect.y - rect.height;
  105. }
  106. var rawTransformable = this._rawTransformable;
  107. rawTransformable.transform = rect.calculateTransform(new BoundingRect(x, y, width, height));
  108. rawTransformable.decomposeTransform();
  109. if (invertLongitute) {
  110. var scale = rawTransformable.scale;
  111. scale[1] = -scale[1];
  112. }
  113. rawTransformable.updateTransform();
  114. this._updateTransform();
  115. },
  116. /**
  117. * @param {string} name
  118. * @return {module:echarts/coord/geo/Region}
  119. */
  120. getRegion: function (name) {
  121. return this._regionsMap.get(name);
  122. },
  123. getRegionByCoord: function (coord) {
  124. var regions = this.regions;
  125. for (var i = 0; i < regions.length; i++) {
  126. if (regions[i].contain(coord)) {
  127. return regions[i];
  128. }
  129. }
  130. },
  131. /**
  132. * Add geoCoord for indexing by name
  133. * @param {string} name
  134. * @param {Array.<number>} geoCoord
  135. */
  136. addGeoCoord: function (name, geoCoord) {
  137. this._nameCoordMap.set(name, geoCoord);
  138. },
  139. /**
  140. * Get geoCoord by name
  141. * @param {string} name
  142. * @return {Array.<number>}
  143. */
  144. getGeoCoord: function (name) {
  145. return this._nameCoordMap.get(name);
  146. },
  147. /**
  148. * @override
  149. */
  150. getBoundingRect: function () {
  151. return this._rect;
  152. },
  153. /**
  154. * @param {string|Array.<number>} data
  155. * @param {boolean} noRoam
  156. * @param {Array.<number>} [out]
  157. * @return {Array.<number>}
  158. */
  159. dataToPoint: function (data, noRoam, out) {
  160. if (typeof data === 'string') {
  161. // Map area name to geoCoord
  162. data = this.getGeoCoord(data);
  163. }
  164. if (data) {
  165. return View.prototype.dataToPoint.call(this, data, noRoam, out);
  166. }
  167. },
  168. /**
  169. * @override
  170. */
  171. convertToPixel: zrUtil.curry(doConvert, 'dataToPoint'),
  172. /**
  173. * @override
  174. */
  175. convertFromPixel: zrUtil.curry(doConvert, 'pointToData')
  176. };
  177. zrUtil.mixin(Geo, View);
  178. function doConvert(methodName, ecModel, finder, value) {
  179. var geoModel = finder.geoModel;
  180. var seriesModel = finder.seriesModel;
  181. var coordSys = geoModel ? geoModel.coordinateSystem : seriesModel ? seriesModel.coordinateSystem // For map.
  182. || (seriesModel.getReferringComponents('geo')[0] || {}).coordinateSystem : null;
  183. return coordSys === this ? coordSys[methodName](value) : null;
  184. }
  185. var _default = Geo;
  186. module.exports = _default;