UNPKG

@opencage/ol-opencage-geosearch

Version:

An OpenLayers geosearch control that uses OpenCage's geosearch API.

445 lines (444 loc) 12.8 kB
import gt from "ol/control/Control"; import { autocomplete as dt } from "@algolia/autocomplete-js"; import { OpenCageGeoSearchPlugin as Et } from "@opencage/geosearch-core"; function K(n) { return n * 180 / Math.PI; } function U(n) { return n * Math.PI / 180; } function q(n, t, e) { if (n >= t && n < e) return n; const o = e - t; return ((n - t) % o + o) % o + t; } const Pt = { // use the radius of the Normal sphere radians: 6370997 / (2 * Math.PI), degrees: 2 * Math.PI * 6370997 / 360, ft: 0.3048, m: 1, "us-ft": 1200 / 3937 }; class z { /** * @param {Options} options Projection options. */ constructor(t) { this.code_ = t.code, this.units_ = /** @type {import("./Units.js").Units} */ t.units, this.extent_ = t.extent !== void 0 ? t.extent : null, this.worldExtent_ = t.worldExtent !== void 0 ? t.worldExtent : null, this.axisOrientation_ = t.axisOrientation !== void 0 ? t.axisOrientation : "enu", this.global_ = t.global !== void 0 ? t.global : !1, this.canWrapX_ = !!(this.global_ && this.extent_), this.getPointResolutionFunc_ = t.getPointResolution, this.defaultTileGrid_ = null, this.metersPerUnit_ = t.metersPerUnit; } /** * @return {boolean} The projection is suitable for wrapping the x-axis */ canWrapX() { return this.canWrapX_; } /** * Get the code for this projection, e.g. 'EPSG:4326'. * @return {string} Code. * @api */ getCode() { return this.code_; } /** * Get the validity extent for this projection. * @return {import("../extent.js").Extent} Extent. * @api */ getExtent() { return this.extent_; } /** * Get the units of this projection. * @return {import("./Units.js").Units} Units. * @api */ getUnits() { return this.units_; } /** * Get the amount of meters per unit of this projection. If the projection is * not configured with `metersPerUnit` or a units identifier, the return is * `undefined`. * @return {number|undefined} Meters. * @api */ getMetersPerUnit() { return this.metersPerUnit_ || Pt[this.units_]; } /** * Get the world extent for this projection. * @return {import("../extent.js").Extent} Extent. * @api */ getWorldExtent() { return this.worldExtent_; } /** * Get the axis orientation of this projection. * Example values are: * enu - the default easting, northing, elevation. * neu - northing, easting, up - useful for "lat/long" geographic coordinates, * or south orientated transverse mercator. * wnu - westing, northing, up - some planetary coordinate systems have * "west positive" coordinate systems * @return {string} Axis orientation. * @api */ getAxisOrientation() { return this.axisOrientation_; } /** * Is this projection a global projection which spans the whole world? * @return {boolean} Whether the projection is global. * @api */ isGlobal() { return this.global_; } /** * Set if the projection is a global projection which spans the whole world * @param {boolean} global Whether the projection is global. * @api */ setGlobal(t) { this.global_ = t, this.canWrapX_ = !!(t && this.extent_); } /** * @return {import("../tilegrid/TileGrid.js").default} The default tile grid. */ getDefaultTileGrid() { return this.defaultTileGrid_; } /** * @param {import("../tilegrid/TileGrid.js").default} tileGrid The default tile grid. */ setDefaultTileGrid(t) { this.defaultTileGrid_ = t; } /** * Set the validity extent for this projection. * @param {import("../extent.js").Extent} extent Extent. * @api */ setExtent(t) { this.extent_ = t, this.canWrapX_ = !!(this.global_ && t); } /** * Set the world extent for this projection. * @param {import("../extent.js").Extent} worldExtent World extent * [minlon, minlat, maxlon, maxlat]. * @api */ setWorldExtent(t) { this.worldExtent_ = t; } /** * Set the getPointResolution function (see {@link module:ol/proj.getPointResolution} * for this projection. * @param {function(number, import("../coordinate.js").Coordinate):number} func Function * @api */ setGetPointResolution(t) { this.getPointResolutionFunc_ = t; } /** * Get the custom point resolution function for this projection (if set). * @return {GetPointResolution|undefined} The custom point * resolution function (if set). */ getPointResolutionFunc() { return this.getPointResolutionFunc_; } } const R = 6378137, _ = Math.PI * R, mt = [-_, -_, _, _], wt = [-180, -85, 180, 85], F = R * Math.log(Math.tan(Math.PI / 2)); class M extends z { /** * @param {string} code Code. */ constructor(t) { super({ code: t, units: "m", extent: mt, global: !0, worldExtent: wt, getPointResolution: function(e, o) { return e / Math.cosh(o[1] / R); } }); } } const Q = [ new M("EPSG:3857"), new M("EPSG:102100"), new M("EPSG:102113"), new M("EPSG:900913"), new M("http://www.opengis.net/def/crs/EPSG/0/3857"), new M("http://www.opengis.net/gml/srs/epsg.xml#3857") ]; function Mt(n, t, e, o) { const r = n.length; e = e > 1 ? e : 2, o = o ?? e, t === void 0 && (e > 2 ? t = n.slice() : t = new Array(r)); for (let s = 0; s < r; s += o) { t[s] = _ * n[s] / 180; let c = R * Math.log(Math.tan(Math.PI * (+n[s + 1] + 90) / 360)); c > F ? c = F : c < -F && (c = -F), t[s + 1] = c; } return t; } function _t(n, t, e, o) { const r = n.length; e = e > 1 ? e : 2, o = o ?? e, t === void 0 && (e > 2 ? t = n.slice() : t = new Array(r)); for (let s = 0; s < r; s += o) t[s] = 180 * n[s] / _, t[s + 1] = 360 * Math.atan(Math.exp(n[s + 1] / R)) / Math.PI - 90; return t; } const St = 6378137, Y = [-180, -90, 180, 90], Tt = Math.PI * St / 180; class P extends z { /** * @param {string} code Code. * @param {string} [axisOrientation] Axis orientation. */ constructor(t, e) { super({ code: t, units: "degrees", extent: Y, axisOrientation: e, global: !0, metersPerUnit: Tt, worldExtent: Y }); } } const B = [ new P("CRS:84"), new P("EPSG:4326", "neu"), new P("urn:ogc:def:crs:OGC:1.3:CRS84"), new P("urn:ogc:def:crs:OGC:2:84"), new P("http://www.opengis.net/def/crs/OGC/1.3/CRS84"), new P("http://www.opengis.net/gml/srs/epsg.xml#4326", "neu"), new P("http://www.opengis.net/def/crs/EPSG/0/4326", "neu") ]; let j = {}; function xt(n) { return j[n] || j[n.replace(/urn:(x-)?ogc:def:crs:EPSG:(.*:)?(\w+)$/, "EPSG:$3")] || null; } function Ct(n, t) { j[n] = t; } let S = {}; function I(n, t, e) { const o = n.getCode(), r = t.getCode(); o in S || (S[o] = {}), S[o][r] = e; } function N(n, t) { return n in S && t in S[n] ? S[n][t] : null; } const $ = 0.9996, l = 669438e-8, L = l * l, p = L * l, m = l / (1 - l), tt = Math.sqrt(1 - l), T = (1 - tt) / (1 + tt), rt = T * T, V = rt * T, J = V * T, ct = J * T, it = 1 - l / 4 - 3 * L / 64 - 5 * p / 256, Gt = 3 * l / 8 + 3 * L / 32 + 45 * p / 1024, It = 15 * L / 256 + 45 * p / 1024, Rt = 35 * p / 3072, bt = 3 / 2 * T - 27 / 32 * V + 269 / 512 * ct, Ot = 21 / 16 * rt - 55 / 32 * J, yt = 151 / 96 * V - 417 / 128 * ct, At = 1097 / 512 * J, D = 6378137; function Ft(n, t, e) { const o = n - 5e5, c = (e.north ? t : t - 1e7) / $ / (D * it), i = c + bt * Math.sin(2 * c) + Ot * Math.sin(4 * c) + yt * Math.sin(6 * c) + At * Math.sin(8 * c), a = Math.sin(i), w = a * a, d = Math.cos(i), x = a / d, f = x * x, E = f * f, h = 1 - l * w, b = Math.sqrt(1 - l * w), O = D / b, y = (1 - l) / h, u = m * d ** 2, C = u * u, g = o / (O * $), A = g * g, G = A * g, Z = G * g, H = Z * g, ht = H * g, ut = i - x / y * (A / 2 - Z / 24 * (5 + 3 * f + 10 * u - 4 * C - 9 * m)) + ht / 720 * (61 + 90 * f + 298 * u + 45 * E - 252 * m - 3 * C); let v = (g - G / 6 * (1 + 2 * f + u) + H / 120 * (5 - 2 * u + 28 * f - 3 * C + 8 * m + 24 * E)) / d; return v = q( v + U(at(e.number)), -Math.PI, Math.PI ), [K(v), K(ut)]; } const nt = -80, et = 84, Ut = -180, $t = 180; function Dt(n, t, e) { n = q(n, Ut, $t), t < nt ? t = nt : t > et && (t = et); const o = U(t), r = Math.sin(o), s = Math.cos(o), c = r / s, i = c * c, a = i * i, w = U(n), d = at(e.number), x = U(d), f = D / Math.sqrt(1 - l * r ** 2), E = m * s ** 2, h = s * q(w - x, -Math.PI, Math.PI), b = h * h, O = b * h, y = O * h, u = y * h, C = u * h, g = D * (it * o - Gt * Math.sin(2 * o) + It * Math.sin(4 * o) - Rt * Math.sin(6 * o)), A = $ * f * (h + O / 6 * (1 - i + E) + u / 120 * (5 - 18 * i + a + 72 * E - 58 * m)) + 5e5; let G = $ * (g + f * c * (b / 2 + y / 24 * (5 - i + 9 * E + 4 * E ** 2) + C / 720 * (61 - 58 * i + a + 600 * E - 330 * m))); return e.north || (G += 1e7), [A, G]; } function at(n) { return (n - 1) * 6 - 180 + 3; } const Xt = [ /^EPSG:(\d+)$/, /^urn:ogc:def:crs:EPSG::(\d+)$/, /^http:\/\/www\.opengis\.net\/def\/crs\/EPSG\/0\/(\d+)$/ ]; function lt(n) { let t = 0; for (const r of Xt) { const s = n.match(r); if (s) { t = parseInt(s[1]); break; } } if (!t) return null; let e = 0, o = !1; return t > 32700 && t < 32761 ? e = t - 32700 : t > 32600 && t < 32661 && (o = !0, e = t - 32600), e ? { number: e, north: o } : null; } function ot(n, t) { return function(e, o, r, s) { const c = e.length; r = r > 1 ? r : 2, s = s ?? r, o || (r > 2 ? o = e.slice() : o = new Array(c)); for (let i = 0; i < c; i += s) { const a = e[i], w = e[i + 1], d = n(a, w, t); o[i] = d[0], o[i + 1] = d[1]; } return o; }; } function Lt(n) { return lt(n) ? new z({ code: n, units: "m" }) : null; } function pt(n) { const t = lt(n.getCode()); return t ? { forward: ot(Dt, t), inverse: ot(Ft, t) } : null; } const vt = [pt], Nt = [Lt]; function ft(n, t) { if (t !== void 0) { for (let e = 0, o = n.length; e < o; ++e) t[e] = n[e]; t = t; } else t = n.slice(); return t; } function k(n) { Ct(n.getCode(), n), I(n, n, ft); } function Wt(n) { n.forEach(k); } function X(n) { if (typeof n != "string") return n; const t = xt(n); if (t) return t; for (const e of Nt) { const o = e(n); if (o) return o; } return null; } function st(n) { Wt(n), n.forEach(function(t) { n.forEach(function(e) { t !== e && I(t, e, ft); }); }); } function qt(n, t, e, o) { n.forEach(function(r) { t.forEach(function(s) { I(r, s, e), I(s, r, o); }); }); } function jt(n, t) { const e = n.getCode(), o = t.getCode(); let r = N(e, o); if (r) return r; let s = null, c = null; for (const a of vt) s || (s = a(n)), c || (c = a(t)); if (!s && !c) return null; const i = "EPSG:4326"; if (c) if (s) r = W( s.inverse, c.forward ); else { const a = N(e, i); a && (r = W( a, c.forward )); } else { const a = N(i, o); a && (r = W( s.inverse, a )); } return r && (k(n), k(t), I(n, t, r)), r; } function W(n, t) { return function(e, o, r, s) { return o = n(e, o, r, s), t(o, o, r, s); }; } function kt(n, t) { const e = X(n), o = X(t); return jt(e, o); } function zt(n, t, e) { const o = kt(t, e); if (!o) { const r = X(t).getCode(), s = X(e).getCode(); throw new Error( `No transform available between ${r} and ${s}` ); } return o(n, void 0, n.length); } function Vt() { st(Q), st(B), qt( B, Q, Mt, _t ); } Vt(); class Kt extends gt { constructor(t) { const e = t || {}, o = document.createElement("div"); o.className = "ol-opencage-geosearch ol-control ol-unselectable", super({ element: o, target: t.target }), this.options = e, this.element = o, this.setCssPosition(this.options.position), dt({ container: this.element, plugins: [ Et(this.options, { onSelect: this.handleSelect.bind(this), onActive: this.options.onActive, onSubmit: this.options.onSubmit }) ] }); } setMap(t) { super.setMap(t), this.map = t; } setCssPosition(t) { t && (t.match("top") && (this.element.style.top = 0), t.match("bottom") && (this.element.style.bottom = 0), t.match("left") && (this.element.style.left = 0), t.match("right") && (this.element.style.right = 0)); } handleSelect(t) { if (typeof this.options.onSelect == "function") { this.options.onSelect(t); return; } const { item: e } = t, o = zt( [e.geometry.lng, e.geometry.lat], "EPSG:4326", this.getMap().getView().getProjection() ); this.getMap().getView().setCenter(o), this.getMap().getView().setZoom(13); } } export { Kt as default };