maplair
Version:
JavaScript library for mobile-friendly interactive maps
151 lines (150 loc) • 5.11 kB
JavaScript
import { Point } from "../../geometry/Point.js";
import { Bounds } from "../../geometry/Bounds.js";
import { LatLng } from "../../geo/LatLng.js";
import { isFlat } from "../../geometry/LineUtil.js";
import { getFeature, latLngsToCoords } from "../GeoJSONUtil.js";
import { Polyline } from "./Polyline.js";
import { clipPolygon, polygonCenter } from "../../geometry/PolyUtil.js";
// src/layer/vector/Polygon.js
/** @import {Feature} from 'geojson' */
/** @import {LatLngExpression} from '../../geo/LatLng.js' */
/** @import {PolylineOptions} from './Polyline.js' */
/**
* Options of `Polygon`.
* @typedef {object} PolygonOptions
* @property {boolean} [fill=true] Whether to fill the polygon with color.
* @extends PolylineOptions
*/
/**
* A class for drawing polygon overlays on a map. Extends `Polyline`.
*
* Note that points you pass when creating a polygon shouldn't have an additional last
* point equal to the first one — it's better to filter out such points.
*
* @example
* ```js
* // create a red polygon from an array of LatLng points
* const latlngs = [[37, -109.05],[41, -109.03],[41, -102.05],[37, -102.04]];
*
* const polygon = new Polygon(latlngs, {color: 'red'}).addTo(map);
*
* // zoom the map to the polygon
* map.fitBounds(polygon.getBounds());
* ```
*
* You can also pass an array of arrays of latlngs, with the first array representing
* the outer shape and the other arrays representing holes in the outer shape:
*
* ```js
* const latlngs = [
* [[37, -109.05],[41, -109.03],[41, -102.05],[37, -102.04]], // outer ring
* [[37.29, -108.58],[40.71, -108.58],[40.71, -102.50],[37.29, -102.50]] // hole
* ];
* ```
*
* Additionally, you can pass a multi-dimensional array to represent a MultiPolygon shape.
*
* ```js
* const latlngs = [
* [ // first polygon
* [[37, -109.05],[41, -109.03],[41, -102.05],[37, -102.04]], // outer ring
* [[37.29, -108.58],[40.71, -108.58],[40.71, -102.50],[37.29, -102.50]] // hole
* ],
* [ // second polygon
* [[41, -111.03],[45, -111.04],[45, -104.05],[41, -104.05]]
* ]
* ];
* ```
*/
var Polygon = class Polygon extends Polyline {
static defaultOptions = { fill: true };
/**
* Instantiates a polygon object given an array of geographical points (or arrays
* of them for holes and multi-polygons) and optionally an options object.
* @overload
* @param {LatLngExpression[]|LatLngExpression[][]|LatLngExpression[][][]} latlngs
* @param {PolygonOptions} [options]
*/
/**
* @param {...any} args
*/
constructor(...args) {
super(...args);
this.callInitHooks(Polygon);
}
/**
* Returns `true` if the Polygon has no LatLngs.
* @returns {boolean}
*/
isEmpty() {
return !this._latlngs.length || !this._latlngs[0].length;
}
/**
* Returns the center ([centroid](http://en.wikipedia.org/wiki/Centroid)) of the Polygon.
* @returns {LatLng}
*/
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 || 0, p = new Point(w, w);
bounds = new Bounds(bounds.min.subtract(p), bounds.max.add(p));
this._parts = [];
if (!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);
if (clipped.length) this._parts.push(clipped);
}
}
_updatePath() {
this._renderer._updatePoly(this, true);
}
_isClosed() {
return true;
}
_containsPoint(p) {
let inside = false;
if (!this._pxBounds?.contains(p)) return false;
for (const part of this._parts) for (let j = 0, len = part.length, k = len - 1; j < len; k = j++) {
const 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);
}
/**
* Coordinates values are rounded with [`formatNum`](#util-formatnum) to the
* given `precision`. Returns a [`GeoJSON`](https://en.wikipedia.org/wiki/GeoJSON)
* representation of the polygon (as a GeoJSON `Polygon` or `MultiPolygon` Feature).
* @param {number|false} [precision]
* @returns {Feature}
*/
toGeoJSON(precision) {
const holes = !isFlat(this._latlngs), multi = holes && !isFlat(this._latlngs[0]);
let coords = latLngsToCoords(this._latlngs, multi ? 2 : holes ? 1 : 0, true, precision);
if (!holes) coords = [coords];
return getFeature(this, {
type: `${multi ? "Multi" : ""}Polygon`,
coordinates: coords
});
}
};
export { Polygon };
//# sourceMappingURL=Polygon.js.map