maplair
Version:
JavaScript library for mobile-friendly interactive maps
70 lines (69 loc) • 2.34 kB
JavaScript
import { Point } from "../../geometry/Point.js";
import { Bounds } from "../../geometry/Bounds.js";
import { LatLng } from "../../geo/LatLng.js";
import { clipPolygon, polygonCenter } from "../../geometry/PolyUtil.js";
import { isFlat } from "../../geometry/LineUtil.js";
import { Polyline } from "./Polyline.js";
//#region src/layer/vector/Polygon.js
var Polygon = class Polygon extends Polyline {
static {
this.setDefaultOptions({ fill: true });
}
constructor(...args) {
super(...args);
this.callInitHooks(Polygon);
}
isEmpty() {
return !this._latlngs.length || !this._latlngs[0].length;
}
getCenter() {
if (!this._map) throw new Error("Must add layer to map before using getCenter()");
return polygonCenter(this._defaultShape(), this._map.options.crs);
}
_convertLatLngs(latlngs) {
const result = super._convertLatLngs(latlngs), len = result.length;
if (len >= 2 && result[0] instanceof LatLng && result[0].equals(result[len - 1])) result.pop();
return result;
}
_setLatLngs(latlngs) {
super._setLatLngs(latlngs);
if (isFlat(this._latlngs)) this._latlngs = [this._latlngs];
}
_defaultShape() {
return isFlat(this._latlngs[0]) ? this._latlngs[0] : this._latlngs[0][0];
}
_clipPoints() {
let bounds = this._renderer._bounds;
const w = this.options.weight, p = new Point(w, w);
bounds = new Bounds(bounds.min.subtract(p), bounds.max.add(p));
this._parts = [];
if (!this._pxBounds || !this._pxBounds.intersects(bounds)) return;
if (this.options.noClip) {
this._parts = this._rings;
return;
}
for (const ring of this._rings) {
const clipped = clipPolygon(ring, bounds, true);
if (clipped.length) this._parts.push(clipped);
}
}
_updatePath() {
this._renderer._updatePoly(this, true);
}
_containsPoint(p) {
let inside = false, part, p1, p2, i, j, k, len, len2;
if (!this._pxBounds || !this._pxBounds.contains(p)) return false;
for (i = 0, len = this._parts.length; i < len; i++) {
part = this._parts[i];
for (j = 0, len2 = part.length, k = len2 - 1; j < len2; k = j++) {
p1 = part[j];
p2 = part[k];
if (p1.y > p.y !== p2.y > p.y && p.x < (p2.x - p1.x) * (p.y - p1.y) / (p2.y - p1.y) + p1.x) inside = !inside;
}
}
return inside || super._containsPoint(p, true);
}
};
//#endregion
export { Polygon };
//# sourceMappingURL=Polygon.js.map