Region.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 BoundingRect from 'zrender/lib/core/BoundingRect';
  42. import * as bbox from 'zrender/lib/core/bbox';
  43. import * as vec2 from 'zrender/lib/core/vector';
  44. import * as polygonContain from 'zrender/lib/contain/polygon';
  45. import * as matrix from 'zrender/lib/core/matrix';
  46. var TMP_TRANSFORM = [];
  47. var Region =
  48. /** @class */
  49. function () {
  50. function Region(name) {
  51. this.name = name;
  52. }
  53. /**
  54. * Get center point in data unit. That is,
  55. * for GeoJSONRegion, the unit is lat/lng,
  56. * for GeoSVGRegion, the unit is SVG local coord.
  57. */
  58. Region.prototype.getCenter = function () {
  59. return;
  60. };
  61. return Region;
  62. }();
  63. export { Region };
  64. var GeoJSONRegion =
  65. /** @class */
  66. function (_super) {
  67. __extends(GeoJSONRegion, _super);
  68. function GeoJSONRegion(name, geometries, cp) {
  69. var _this = _super.call(this, name) || this;
  70. _this.type = 'geoJSON';
  71. _this.geometries = geometries;
  72. if (!cp) {
  73. var rect = _this.getBoundingRect();
  74. cp = [rect.x + rect.width / 2, rect.y + rect.height / 2];
  75. } else {
  76. cp = [cp[0], cp[1]];
  77. }
  78. _this._center = cp;
  79. return _this;
  80. }
  81. GeoJSONRegion.prototype.getBoundingRect = function () {
  82. var rect = this._rect;
  83. if (rect) {
  84. return rect;
  85. }
  86. var MAX_NUMBER = Number.MAX_VALUE;
  87. var min = [MAX_NUMBER, MAX_NUMBER];
  88. var max = [-MAX_NUMBER, -MAX_NUMBER];
  89. var min2 = [];
  90. var max2 = [];
  91. var geometries = this.geometries;
  92. var i = 0;
  93. for (; i < geometries.length; i++) {
  94. // Only support polygon
  95. if (geometries[i].type !== 'polygon') {
  96. continue;
  97. } // Doesn't consider hole
  98. var exterior = geometries[i].exterior;
  99. bbox.fromPoints(exterior, min2, max2);
  100. vec2.min(min, min, min2);
  101. vec2.max(max, max, max2);
  102. } // No data
  103. if (i === 0) {
  104. min[0] = min[1] = max[0] = max[1] = 0;
  105. }
  106. return this._rect = new BoundingRect(min[0], min[1], max[0] - min[0], max[1] - min[1]);
  107. };
  108. GeoJSONRegion.prototype.contain = function (coord) {
  109. var rect = this.getBoundingRect();
  110. var geometries = this.geometries;
  111. if (!rect.contain(coord[0], coord[1])) {
  112. return false;
  113. }
  114. loopGeo: for (var i = 0, len = geometries.length; i < len; i++) {
  115. // Only support polygon.
  116. if (geometries[i].type !== 'polygon') {
  117. continue;
  118. }
  119. var exterior = geometries[i].exterior;
  120. var interiors = geometries[i].interiors;
  121. if (polygonContain.contain(exterior, coord[0], coord[1])) {
  122. // Not in the region if point is in the hole.
  123. for (var k = 0; k < (interiors ? interiors.length : 0); k++) {
  124. if (polygonContain.contain(interiors[k], coord[0], coord[1])) {
  125. continue loopGeo;
  126. }
  127. }
  128. return true;
  129. }
  130. }
  131. return false;
  132. };
  133. GeoJSONRegion.prototype.transformTo = function (x, y, width, height) {
  134. var rect = this.getBoundingRect();
  135. var aspect = rect.width / rect.height;
  136. if (!width) {
  137. width = aspect * height;
  138. } else if (!height) {
  139. height = width / aspect;
  140. }
  141. var target = new BoundingRect(x, y, width, height);
  142. var transform = rect.calculateTransform(target);
  143. var geometries = this.geometries;
  144. for (var i = 0; i < geometries.length; i++) {
  145. // Only support polygon.
  146. if (geometries[i].type !== 'polygon') {
  147. continue;
  148. }
  149. var exterior = geometries[i].exterior;
  150. var interiors = geometries[i].interiors;
  151. for (var p = 0; p < exterior.length; p++) {
  152. vec2.applyTransform(exterior[p], exterior[p], transform);
  153. }
  154. for (var h = 0; h < (interiors ? interiors.length : 0); h++) {
  155. for (var p = 0; p < interiors[h].length; p++) {
  156. vec2.applyTransform(interiors[h][p], interiors[h][p], transform);
  157. }
  158. }
  159. }
  160. rect = this._rect;
  161. rect.copy(target); // Update center
  162. this._center = [rect.x + rect.width / 2, rect.y + rect.height / 2];
  163. };
  164. GeoJSONRegion.prototype.cloneShallow = function (name) {
  165. name == null && (name = this.name);
  166. var newRegion = new GeoJSONRegion(name, this.geometries, this._center);
  167. newRegion._rect = this._rect;
  168. newRegion.transformTo = null; // Simply avoid to be called.
  169. return newRegion;
  170. };
  171. GeoJSONRegion.prototype.getCenter = function () {
  172. return this._center;
  173. };
  174. GeoJSONRegion.prototype.setCenter = function (center) {
  175. this._center = center;
  176. };
  177. return GeoJSONRegion;
  178. }(Region);
  179. export { GeoJSONRegion };
  180. var GeoSVGRegion =
  181. /** @class */
  182. function (_super) {
  183. __extends(GeoSVGRegion, _super);
  184. function GeoSVGRegion(name, elOnlyForCalculate) {
  185. var _this = _super.call(this, name) || this;
  186. _this.type = 'geoSVG';
  187. _this._elOnlyForCalculate = elOnlyForCalculate;
  188. return _this;
  189. }
  190. GeoSVGRegion.prototype.getCenter = function () {
  191. var center = this._center;
  192. if (!center) {
  193. // In most cases there are no need to calculate this center.
  194. // So calculate only when called.
  195. center = this._center = this._calculateCenter();
  196. }
  197. return center;
  198. };
  199. GeoSVGRegion.prototype._calculateCenter = function () {
  200. var el = this._elOnlyForCalculate;
  201. var rect = el.getBoundingRect();
  202. var center = [rect.x + rect.width / 2, rect.y + rect.height / 2];
  203. var mat = matrix.identity(TMP_TRANSFORM);
  204. var target = el;
  205. while (target && !target.isGeoSVGGraphicRoot) {
  206. matrix.mul(mat, target.getLocalTransform(), mat);
  207. target = target.parent;
  208. }
  209. matrix.invert(mat, mat);
  210. vec2.applyTransform(center, center, mat);
  211. return center;
  212. };
  213. return GeoSVGRegion;
  214. }(Region);
  215. export { GeoSVGRegion };