Region.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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.js';
  42. import * as vec2 from 'zrender/lib/core/vector.js';
  43. import * as polygonContain from 'zrender/lib/contain/polygon.js';
  44. import * as matrix from 'zrender/lib/core/matrix.js';
  45. import { each } from 'zrender/lib/core/util.js';
  46. var TMP_TRANSFORM = [];
  47. function transformPoints(points, transform) {
  48. for (var p = 0; p < points.length; p++) {
  49. vec2.applyTransform(points[p], points[p], transform);
  50. }
  51. }
  52. function updateBBoxFromPoints(points, min, max, projection) {
  53. for (var i = 0; i < points.length; i++) {
  54. var p = points[i];
  55. if (projection) {
  56. // projection may return null point.
  57. p = projection.project(p);
  58. }
  59. if (p && isFinite(p[0]) && isFinite(p[1])) {
  60. vec2.min(min, min, p);
  61. vec2.max(max, max, p);
  62. }
  63. }
  64. }
  65. function centroid(points) {
  66. var signedArea = 0;
  67. var cx = 0;
  68. var cy = 0;
  69. var len = points.length;
  70. var x0 = points[len - 1][0];
  71. var y0 = points[len - 1][1];
  72. // Polygon should been closed.
  73. for (var i = 0; i < len; i++) {
  74. var x1 = points[i][0];
  75. var y1 = points[i][1];
  76. var a = x0 * y1 - x1 * y0;
  77. signedArea += a;
  78. cx += (x0 + x1) * a;
  79. cy += (y0 + y1) * a;
  80. x0 = x1;
  81. y0 = y1;
  82. }
  83. return signedArea ? [cx / signedArea / 3, cy / signedArea / 3, signedArea] : [points[0][0] || 0, points[0][1] || 0];
  84. }
  85. var Region = /** @class */function () {
  86. function Region(name) {
  87. this.name = name;
  88. }
  89. Region.prototype.setCenter = function (center) {
  90. this._center = center;
  91. };
  92. /**
  93. * Get center point in data unit. That is,
  94. * for GeoJSONRegion, the unit is lat/lng,
  95. * for GeoSVGRegion, the unit is SVG local coord.
  96. */
  97. Region.prototype.getCenter = function () {
  98. var center = this._center;
  99. if (!center) {
  100. // In most cases there are no need to calculate this center.
  101. // So calculate only when called.
  102. center = this._center = this.calcCenter();
  103. }
  104. return center;
  105. };
  106. return Region;
  107. }();
  108. export { Region };
  109. var GeoJSONPolygonGeometry = /** @class */function () {
  110. function GeoJSONPolygonGeometry(exterior, interiors) {
  111. this.type = 'polygon';
  112. this.exterior = exterior;
  113. this.interiors = interiors;
  114. }
  115. return GeoJSONPolygonGeometry;
  116. }();
  117. export { GeoJSONPolygonGeometry };
  118. var GeoJSONLineStringGeometry = /** @class */function () {
  119. function GeoJSONLineStringGeometry(points) {
  120. this.type = 'linestring';
  121. this.points = points;
  122. }
  123. return GeoJSONLineStringGeometry;
  124. }();
  125. export { GeoJSONLineStringGeometry };
  126. var GeoJSONRegion = /** @class */function (_super) {
  127. __extends(GeoJSONRegion, _super);
  128. function GeoJSONRegion(name, geometries, cp) {
  129. var _this = _super.call(this, name) || this;
  130. _this.type = 'geoJSON';
  131. _this.geometries = geometries;
  132. _this._center = cp && [cp[0], cp[1]];
  133. return _this;
  134. }
  135. GeoJSONRegion.prototype.calcCenter = function () {
  136. var geometries = this.geometries;
  137. var largestGeo;
  138. var largestGeoSize = 0;
  139. for (var i = 0; i < geometries.length; i++) {
  140. var geo = geometries[i];
  141. var exterior = geo.exterior;
  142. // Simple trick to use points count instead of polygon area as region size.
  143. // Ignore linestring
  144. var size = exterior && exterior.length;
  145. if (size > largestGeoSize) {
  146. largestGeo = geo;
  147. largestGeoSize = size;
  148. }
  149. }
  150. if (largestGeo) {
  151. return centroid(largestGeo.exterior);
  152. }
  153. // from bounding rect by default.
  154. var rect = this.getBoundingRect();
  155. return [rect.x + rect.width / 2, rect.y + rect.height / 2];
  156. };
  157. GeoJSONRegion.prototype.getBoundingRect = function (projection) {
  158. var rect = this._rect;
  159. // Always recalculate if using projection.
  160. if (rect && !projection) {
  161. return rect;
  162. }
  163. var min = [Infinity, Infinity];
  164. var max = [-Infinity, -Infinity];
  165. var geometries = this.geometries;
  166. each(geometries, function (geo) {
  167. if (geo.type === 'polygon') {
  168. // Doesn't consider hole
  169. updateBBoxFromPoints(geo.exterior, min, max, projection);
  170. } else {
  171. each(geo.points, function (points) {
  172. updateBBoxFromPoints(points, min, max, projection);
  173. });
  174. }
  175. });
  176. // Normalie invalid bounding.
  177. if (!(isFinite(min[0]) && isFinite(min[1]) && isFinite(max[0]) && isFinite(max[1]))) {
  178. min[0] = min[1] = max[0] = max[1] = 0;
  179. }
  180. rect = new BoundingRect(min[0], min[1], max[0] - min[0], max[1] - min[1]);
  181. if (!projection) {
  182. this._rect = rect;
  183. }
  184. return rect;
  185. };
  186. GeoJSONRegion.prototype.contain = function (coord) {
  187. var rect = this.getBoundingRect();
  188. var geometries = this.geometries;
  189. if (!rect.contain(coord[0], coord[1])) {
  190. return false;
  191. }
  192. loopGeo: for (var i = 0, len = geometries.length; i < len; i++) {
  193. var geo = geometries[i];
  194. // Only support polygon.
  195. if (geo.type !== 'polygon') {
  196. continue;
  197. }
  198. var exterior = geo.exterior;
  199. var interiors = geo.interiors;
  200. if (polygonContain.contain(exterior, coord[0], coord[1])) {
  201. // Not in the region if point is in the hole.
  202. for (var k = 0; k < (interiors ? interiors.length : 0); k++) {
  203. if (polygonContain.contain(interiors[k], coord[0], coord[1])) {
  204. continue loopGeo;
  205. }
  206. }
  207. return true;
  208. }
  209. }
  210. return false;
  211. };
  212. /**
  213. * Transform the raw coords to target bounding.
  214. * @param x
  215. * @param y
  216. * @param width
  217. * @param height
  218. */
  219. GeoJSONRegion.prototype.transformTo = function (x, y, width, height) {
  220. var rect = this.getBoundingRect();
  221. var aspect = rect.width / rect.height;
  222. if (!width) {
  223. width = aspect * height;
  224. } else if (!height) {
  225. height = width / aspect;
  226. }
  227. var target = new BoundingRect(x, y, width, height);
  228. var transform = rect.calculateTransform(target);
  229. var geometries = this.geometries;
  230. for (var i = 0; i < geometries.length; i++) {
  231. var geo = geometries[i];
  232. if (geo.type === 'polygon') {
  233. transformPoints(geo.exterior, transform);
  234. each(geo.interiors, function (interior) {
  235. transformPoints(interior, transform);
  236. });
  237. } else {
  238. each(geo.points, function (points) {
  239. transformPoints(points, transform);
  240. });
  241. }
  242. }
  243. rect = this._rect;
  244. rect.copy(target);
  245. // Update center
  246. this._center = [rect.x + rect.width / 2, rect.y + rect.height / 2];
  247. };
  248. GeoJSONRegion.prototype.cloneShallow = function (name) {
  249. name == null && (name = this.name);
  250. var newRegion = new GeoJSONRegion(name, this.geometries, this._center);
  251. newRegion._rect = this._rect;
  252. newRegion.transformTo = null; // Simply avoid to be called.
  253. return newRegion;
  254. };
  255. return GeoJSONRegion;
  256. }(Region);
  257. export { GeoJSONRegion };
  258. var GeoSVGRegion = /** @class */function (_super) {
  259. __extends(GeoSVGRegion, _super);
  260. function GeoSVGRegion(name, elOnlyForCalculate) {
  261. var _this = _super.call(this, name) || this;
  262. _this.type = 'geoSVG';
  263. _this._elOnlyForCalculate = elOnlyForCalculate;
  264. return _this;
  265. }
  266. GeoSVGRegion.prototype.calcCenter = function () {
  267. var el = this._elOnlyForCalculate;
  268. var rect = el.getBoundingRect();
  269. var center = [rect.x + rect.width / 2, rect.y + rect.height / 2];
  270. var mat = matrix.identity(TMP_TRANSFORM);
  271. var target = el;
  272. while (target && !target.isGeoSVGGraphicRoot) {
  273. matrix.mul(mat, target.getLocalTransform(), mat);
  274. target = target.parent;
  275. }
  276. matrix.invert(mat, mat);
  277. vec2.applyTransform(center, center, mat);
  278. return center;
  279. };
  280. return GeoSVGRegion;
  281. }(Region);
  282. export { GeoSVGRegion };