cione-comp-lib
Version:
`$ yarn add cione-comp-lib` 或者 `$ npm i cione-comp-lib -S`
216 lines (215 loc) • 6.88 kB
JavaScript
import { __extends } from "../../../../tslib/tslib.es6.mjs";
import BoundingRect from "../../../../zrender/lib/core/BoundingRect.mjs";
import { applyTransform, min, max } from "../../../../zrender/lib/core/vector.mjs";
import { contain } from "../../../../zrender/lib/contain/polygon.mjs";
import { identity, mul, invert } from "../../../../zrender/lib/core/matrix.mjs";
import { each } from "../../../../zrender/lib/core/util.mjs";
var TMP_TRANSFORM = [];
function transformPoints(points, transform) {
for (var p = 0; p < points.length; p++) {
applyTransform(points[p], points[p], transform);
}
}
function updateBBoxFromPoints(points, min$1, max$1, projection) {
for (var i = 0; i < points.length; i++) {
var p = points[i];
if (projection) {
p = projection.project(p);
}
if (p && isFinite(p[0]) && isFinite(p[1])) {
min(min$1, min$1, p);
max(max$1, max$1, p);
}
}
}
function centroid(points) {
var signedArea = 0;
var cx = 0;
var cy = 0;
var len = points.length;
var x0 = points[len - 1][0];
var y0 = points[len - 1][1];
for (var i = 0; i < len; i++) {
var x1 = points[i][0];
var y1 = points[i][1];
var a = x0 * y1 - x1 * y0;
signedArea += a;
cx += (x0 + x1) * a;
cy += (y0 + y1) * a;
x0 = x1;
y0 = y1;
}
return signedArea ? [cx / signedArea / 3, cy / signedArea / 3, signedArea] : [points[0][0] || 0, points[0][1] || 0];
}
var Region = function() {
function Region2(name) {
this.name = name;
}
Region2.prototype.setCenter = function(center) {
this._center = center;
};
Region2.prototype.getCenter = function() {
var center = this._center;
if (!center) {
center = this._center = this.calcCenter();
}
return center;
};
return Region2;
}();
var GeoJSONPolygonGeometry = function() {
function GeoJSONPolygonGeometry2(exterior, interiors) {
this.type = "polygon";
this.exterior = exterior;
this.interiors = interiors;
}
return GeoJSONPolygonGeometry2;
}();
var GeoJSONLineStringGeometry = function() {
function GeoJSONLineStringGeometry2(points) {
this.type = "linestring";
this.points = points;
}
return GeoJSONLineStringGeometry2;
}();
var GeoJSONRegion = function(_super) {
__extends(GeoJSONRegion2, _super);
function GeoJSONRegion2(name, geometries, cp) {
var _this = _super.call(this, name) || this;
_this.type = "geoJSON";
_this.geometries = geometries;
_this._center = cp && [cp[0], cp[1]];
return _this;
}
GeoJSONRegion2.prototype.calcCenter = function() {
var geometries = this.geometries;
var largestGeo;
var largestGeoSize = 0;
for (var i = 0; i < geometries.length; i++) {
var geo = geometries[i];
var exterior = geo.exterior;
var size = exterior && exterior.length;
if (size > largestGeoSize) {
largestGeo = geo;
largestGeoSize = size;
}
}
if (largestGeo) {
return centroid(largestGeo.exterior);
}
var rect = this.getBoundingRect();
return [rect.x + rect.width / 2, rect.y + rect.height / 2];
};
GeoJSONRegion2.prototype.getBoundingRect = function(projection) {
var rect = this._rect;
if (rect && !projection) {
return rect;
}
var min2 = [Infinity, Infinity];
var max2 = [-Infinity, -Infinity];
var geometries = this.geometries;
each(geometries, function(geo) {
if (geo.type === "polygon") {
updateBBoxFromPoints(geo.exterior, min2, max2, projection);
} else {
each(geo.points, function(points) {
updateBBoxFromPoints(points, min2, max2, projection);
});
}
});
if (!(isFinite(min2[0]) && isFinite(min2[1]) && isFinite(max2[0]) && isFinite(max2[1]))) {
min2[0] = min2[1] = max2[0] = max2[1] = 0;
}
rect = new BoundingRect(min2[0], min2[1], max2[0] - min2[0], max2[1] - min2[1]);
if (!projection) {
this._rect = rect;
}
return rect;
};
GeoJSONRegion2.prototype.contain = function(coord) {
var rect = this.getBoundingRect();
var geometries = this.geometries;
if (!rect.contain(coord[0], coord[1])) {
return false;
}
loopGeo:
for (var i = 0, len = geometries.length; i < len; i++) {
var geo = geometries[i];
if (geo.type !== "polygon") {
continue;
}
var exterior = geo.exterior;
var interiors = geo.interiors;
if (contain(exterior, coord[0], coord[1])) {
for (var k = 0; k < (interiors ? interiors.length : 0); k++) {
if (contain(interiors[k], coord[0], coord[1])) {
continue loopGeo;
}
}
return true;
}
}
return false;
};
GeoJSONRegion2.prototype.transformTo = function(x, y, width, height) {
var rect = this.getBoundingRect();
var aspect = rect.width / rect.height;
if (!width) {
width = aspect * height;
} else if (!height) {
height = width / aspect;
}
var target = new BoundingRect(x, y, width, height);
var transform = rect.calculateTransform(target);
var geometries = this.geometries;
for (var i = 0; i < geometries.length; i++) {
var geo = geometries[i];
if (geo.type === "polygon") {
transformPoints(geo.exterior, transform);
each(geo.interiors, function(interior) {
transformPoints(interior, transform);
});
} else {
each(geo.points, function(points) {
transformPoints(points, transform);
});
}
}
rect = this._rect;
rect.copy(target);
this._center = [rect.x + rect.width / 2, rect.y + rect.height / 2];
};
GeoJSONRegion2.prototype.cloneShallow = function(name) {
name == null && (name = this.name);
var newRegion = new GeoJSONRegion2(name, this.geometries, this._center);
newRegion._rect = this._rect;
newRegion.transformTo = null;
return newRegion;
};
return GeoJSONRegion2;
}(Region);
var GeoSVGRegion = function(_super) {
__extends(GeoSVGRegion2, _super);
function GeoSVGRegion2(name, elOnlyForCalculate) {
var _this = _super.call(this, name) || this;
_this.type = "geoSVG";
_this._elOnlyForCalculate = elOnlyForCalculate;
return _this;
}
GeoSVGRegion2.prototype.calcCenter = function() {
var el = this._elOnlyForCalculate;
var rect = el.getBoundingRect();
var center = [rect.x + rect.width / 2, rect.y + rect.height / 2];
var mat = identity(TMP_TRANSFORM);
var target = el;
while (target && !target.isGeoSVGGraphicRoot) {
mul(mat, target.getLocalTransform(), mat);
target = target.parent;
}
invert(mat, mat);
applyTransform(center, center, mat);
return center;
};
return GeoSVGRegion2;
}(Region);
export { GeoJSONLineStringGeometry, GeoJSONPolygonGeometry, GeoJSONRegion, GeoSVGRegion, Region };