Region.js 9.2 KB

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